Exemple #1
0
    def insert_with_block_check(self, node):
        """Modifies a with statement node in-place to add an initial check
        for whether or not the block should be executed. If the block is
        not executed it will raise a XonshBlockError containing the required
        information.
        """
        nwith = self._nwith  # the nesting level of the current with-statement
        lineno = get_lineno(node)
        col = get_col(node, 0)
        # Add or discover target names
        targets = set()
        i = 0  # index of unassigned items

        def make_next_target():
            nonlocal i
            targ = '__xonsh_with_target_{}_{}__'.format(nwith, i)
            n = Name(id=targ, ctx=Store(), lineno=lineno, col_offset=col)
            targets.add(targ)
            i += 1
            return n
        for item in node.items:
            if item.optional_vars is None:
                if has_elts(item.context_expr):
                    targs = [make_next_target() for _ in item.context_expr.elts]
                    optvars = Tuple(elts=targs, ctx=Store(), lineno=lineno,
                                    col_offset=col)
                else:
                    optvars = make_next_target()
                item.optional_vars = optvars
            else:
                targets.update(gather_names(item.optional_vars))
        # Ok, now that targets have been found / created, make the actual check
        # to see if we are in a non-executing block. This is equivalent to
        # writing the following condition:
        #
        #     if getattr(targ0, '__xonsh_block__', False) or \
        #        getattr(targ1, '__xonsh_block__', False) or ...:
        #         raise XonshBlockError(lines, globals(), locals())
        tests = [_getblockattr(t, lineno, col) for t in sorted(targets)]
        if len(tests) == 1:
            test = tests[0]
        else:
            test = BoolOp(op=Or(), values=tests, lineno=lineno, col_offset=col)
        ldx, udx = self._find_with_block_line_idx(node)
        lines = [Str(s=s, lineno=lineno, col_offset=col)
                 for s in self.lines[ldx:udx]]
        check = If(test=test, body=[
            Raise(exc=xonsh_call('XonshBlockError',
                                 args=[List(elts=lines, ctx=Load(),
                                            lineno=lineno, col_offset=col),
                                       xonsh_call('globals', args=[],
                                                  lineno=lineno, col=col),
                                       xonsh_call('locals', args=[],
                                                  lineno=lineno, col=col)],
                                 lineno=lineno, col=col),
                  cause=None, lineno=lineno, col_offset=col)],
                orelse=[], lineno=lineno, col_offset=col)
        node.body.insert(0, check)
Exemple #2
0
 def visit_BinOp(self, node):
     self.generic_visit(node)
     if isinstance(node.op, BitAnd):
         return BoolOp(And(), (node.left, node.right))
     elif isinstance(node.op, BitOr):
         return BoolOp(Or(), (node.left, node.right))
     else:
         raise TypeError("unsupported operation '%s'" %
                         node.op.__class__.__name__)
Exemple #3
0
def parsePredicate(parser):
    predicate = None
    if parser.matchLexeme('!'):
        parser.next()  #skip !.
        return Not(parsePredicate(parser))

    predicate = parseExpression(parser)

    if parser.matchLexeme("."):  # var.type?
        parser.next("list?, record?, string?", tokensort=KEYWORD)
        if parser.matchLexeme(keywords['LIST']):
            ptype = "LIST"
        if parser.matchLexeme(keywords['RECORD']):
            ptype = "RECORD"
        if parser.matchLexeme(keywords['STRING']):
            ptype = "STRING"
        parser.next()
        parser.check('type?', lexeme='?')
        predicate = Is_a(predicate, ptype, lineo=parser.currentToken[2])
        parser.next()
    if parser.matchLexeme('&') and parser.peek(lexeme='&'):
        parser.next()  #skip &&
        parser.next()
        right = parsePredicate(parser)
        #and
        predicate = And(predicate, right, lineo=parser.currentToken[2])
    if parser.matchLexeme('|') and parser.peek(lexeme='|'):
        parser.next()  #skip ||
        parser.next()
        right = parsePredicate(parser)
        predicate = Or(predicate, right, lineo=parser.currentToken[2])
        #parser.next()

    if not predicate:
        raise SyntaxError(currentToken,
                          "pasing predicate failed && || type? variable")

    return predicate
Exemple #4
0
 def p_or_test(tokens):
     return Or(tokens[0], tokens[2])
Exemple #5
0
def _(ra: ras.Or, ctx: Context):
    return ast.BoolOp(op=Or(),
                      values=[
                          convert(ra[0], ctx),
                          convert(ra[1], ctx),
                      ])