Ejemplo n.º 1
0
 def to_node(self):
     if len(self.operator) == 0:
         return self.left.to_node()
     else:
         right_nodes = []
         comp_operator = []
         for i in range(len(self.right)):
             right_nodes.append(self.right[i].to_node())
             if self.operator[i] in ['<', 'is lower than']:
                 comp_operator.append(ast.Lt())
             elif self.operator[i] in ['<=', 'is lower or equal to']:
                 comp_operator.append(ast.LtE())
             elif self.operator[i] in ['>', 'is greater than']:
                 comp_operator.append(ast.Gt())
             elif self.operator[i] in ['>=', 'is greater or equal to']:
                 comp_operator.append(ast.GtE())
             elif self.operator[i] in ['==', 'is equal to']:
                 comp_operator.append(ast.Eq())
             elif self.operator[i] in ['!=', 'is different from',
                                       'is not equal to']:
                 comp_operator.append(ast.NotEq())
             elif self.operator[i] == 'in':
                 comp_operator.append(ast.In())
             elif self.operator[i] == 'not in':
                 comp_operator.append(ast.NotIn())
             elif self.operator[i] == 'is':
                 comp_operator.append(ast.Is())
             elif self.operator[i] == 'is not':
                 comp_operator.append(ast.IsNot())
             else:
                 raise Exception("Unrecognized argument in Comparison")
         return ast.Compare(left=self.left.to_node(), ops=comp_operator,
                            comparators=right_nodes)
Ejemplo n.º 2
0
def compare_is(left, right, invert=False):
    return ast.Compare(
        left=ast.Name(id=left, ctx=ast.Load(), lineno=1, col_offset=0),
        ops=[ast.Is() if not invert else ast.IsNot()],
        comparators=[ast.Name(id=right, ctx=ast.Load(), lineno=1, col_offset=0)],
        lineno=1,
        col_offset=0)
Ejemplo n.º 3
0
    def __call__(self, cls, obj: Opt, grammar: typing.Optional["Grammar"],
                 ctx):
        body = []
        objName = "parsed"
        o = ast.Name(id=objName, ctx=ast.Load())

        returnTypes = []

        objIsAlt = isinstance(obj, Alt)

        if isinstance(obj.child, Ref):
            processerFunc = getProcessorFuncNameForARef(obj.child.name, ctx)
            retType = getReturnTypeForARef(obj.child.name, ctx)
            fe = ast.Name(id=objName, ctx=ast.Load())
            body.append(
                ast.If(
                    test=ast.Compare(left=fe,
                                     ops=[ast.IsNot()],
                                     comparators=[ASTNone]),
                    body=[
                        ast.Return(value=ast.Call(
                            func=processerFunc,
                            args=[fe],
                            keywords=[],
                        ))
                    ],
                    orelse=[],
                ))

            returnTypes.append(retType)
        else:
            raise NotImplementedError(
                "For compatibility each suff that is `opt`ed must be a `ref`. Otherwise we are unable to process these grammars uniformly for all the supported backends. Please put content of "
                + ("`" + obj.name if hasattr(obj, "name") else
                   "the `opt` struct in `" + ctx.currentProdName) +
                "` into a separate rule")

        ctx.members.append(
            ast.FunctionDef(
                name="process_" + ctx.currentProdName,
                args=ast.arguments(
                    posonlyargs=[],
                    args=[
                        astSelfArg,
                        ast.arg(arg=objName,
                                annotation=None,
                                type_comment=None)
                    ],
                    vararg=None,
                    kwonlyargs=[],
                    kw_defaults=[],
                    kwarg=None,
                    defaults=[],
                ),
                body=body,
                decorator_list=[],
                returns=genTypingOptional(retType),
                type_comment=None,
            ))
Ejemplo n.º 4
0
    def _makeIfNotNone(name, lineno=1, indent=""):
        left = ast.Name(id=name, lineno=lineno, col_offset=len(f"{indent}if "), ctx=ast.Load())
        right = ast.NameConstant(value=None, lineno=lineno, col_offset=len(f"{indent}if {name} is not "))
        isnot = ast.IsNot(lineno=lineno, col_offset=len(f"{indent}if {name}"))
        test = ast.Compare(left, [isnot], [right], lineno=lineno, col_offset=len(f"{indent}if {name} "))

        ifstmt = ast.If(test=test, body=[], orelse=[], lineno=lineno, col_offset=len(indent))
        return ifstmt
Ejemplo n.º 5
0
 def visit_Name(self, name):
     # Check if the name is local or not.
     locs = ast.Call(self.builtin("locals"), [], [], None, None)
     globs = ast.Call(self.builtin("globals"), [], [], None, None)
     ops = [ast.In(), ast.IsNot()]
     test = ast.Compare(ast.Str(name.id), ops, [locs, globs])
     expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
     return name, self.explanation_param(expr)
Ejemplo n.º 6
0
 def ast(self):
     pattern = self.tokens[2].ast()
     string = self.tokens[0].ast()
     return ast.Compare(
         left=ast.Call(func=ast.Attribute(value=RE_MODULE, attr='match',
                                          ctx=ast.Load()),
                       args=[pattern, string], keywords=[], starargs=None,
                       kwargs=None),
         ops=[ast.IsNot()], comparators=[ast.Name(id='None', ctx=ast.Load())]
     )
Ejemplo n.º 7
0
def missingCheck(check: ast.Compare, el: ast.Subscript,
                 default: bool) -> ast.BoolOp:
    """Creates AST nodes corresponding to check whether the `el` is None, if it is returns `default`"""
    return ast.BoolOp(op=(ast.Or() if default else ast.And()),
                      values=[
                          ast.Compare(
                              left=el,
                              ops=[(ast.Is() if default else ast.IsNot())],
                              comparators=[noneCnst]), check
                      ])
Ejemplo n.º 8
0
    def visit_Compare(self, node):
        # cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn
        print("  in MyTransformer.visit_Compare()")
        print("   node     =", node)
        print("   op       =", node.ops[0])

        curr_op = node.ops[0]
        comp_negate = curr_op

        rand_num = random.randint(1, 10)
        if rand_num >= 7:
            print("   negating...")
            if isinstance(curr_op, ast.Eq): comp_negate = ast.NotEq()
            elif isinstance(curr_op, ast.NotEq): comp_negate = ast.Eq()

            elif isinstance(curr_op, ast.Lt): comp_negate = ast.GtE()
            elif isinstance(curr_op, ast.LtE): comp_negate = ast.Gt()

            elif isinstance(curr_op, ast.Gt): comp_negate = ast.LtE()
            elif isinstance(curr_op, ast.GtE): comp_negate = ast.Lt()

            elif isinstance(curr_op, ast.Is): comp_negate = ast.IsNot()
            elif isinstance(curr_op, ast.IsNot): comp_negate = ast.Is()

            elif isinstance(curr_op, ast.In): comp_negate = ast.NotIn()
            elif isinstance(curr_op, ast.NotIn): comp_negate = ast.In()
            else: comp_negate = ast.Eq()
        else:
            print("   mixing up...")
            if isinstance(curr_op, ast.Lt): comp_negate = ast.LtE()
            elif isinstance(curr_op, ast.LtE): comp_negate = ast.And()

            elif isinstance(curr_op, ast.Gt): comp_negate = ast.Or()
            elif isinstance(curr_op, ast.GtE): comp_negate = ast.Gt()

            elif isinstance(curr_op, ast.Is): comp_negate = ast.Gt()
            elif isinstance(curr_op, ast.IsNot): comp_negate = ast.Lt()

            elif isinstance(curr_op, ast.In):
                comp_negate = ast.In(
                )  #leave the same for for loops (for x in this)
            elif isinstance(curr_op, ast.NotIn):
                comp_negate = ast.Lt()
            else:
                comp_negate = ast.Eq()

        print("   new comparator =", comp_negate)

        # create negated node | Compare(expr left, cmpop* ops, expr* comparators)
        new_node = node
        new_node.ops = [comp_negate]
        ast.copy_location(new_node, node)
        ast.fix_missing_locations(new_node)
        return new_node
Ejemplo n.º 9
0
    def visit_none_assertion(self, assertion: na.NoneAssertion) -> None:
        """
        Creates an assertion of form "assert var0 is None" or "assert var0 is not None".

        Args:
            assertion: the assertion that is visited.
        """
        self._nodes.append(
            self._create_constant_assert(
                assertion.source,
                ast.Is() if assertion.value else ast.IsNot(), None))
Ejemplo n.º 10
0
 def 比较(片段):
     对照表 = {
         '>': ast.Gt(),
         '>=': ast.GtE(),
         '<': ast.Lt(),
         '<=': ast.LtE(),
         '==': ast.Eq(),
         '!=': ast.NotEq(),
         '===': ast.Is(),
         '!==': ast.IsNot()
     }
     return 语法树.比较(前项=片段[0], 操作符=对照表[片段[1].getstr()], 后项=片段[2], 片段=片段)
Ejemplo n.º 11
0
    def visit_Compare(self, node):
        self.generic_visit(node)
        self.binop_count += 1

        if (self.binop_count == self.count_of_node_to_mutate):
            new_node = copy.deepcopy(node)
            print('IN COMPARE')
            print('THIS IS THE PREVIOUS OP', node.ops)
            for (i, op) in enumerate(node.ops):
                if (isinstance(op, ast.Gt)):
                    num = random.randint(0, 2)
                    if num == 0:
                        new_node.ops[i] = ast.GtE()
                    if num == 1:
                        new_node.ops[i] = ast.LtE()
                    if num == 2:
                        new_node.ops[i] = ast.Lt()
                if (isinstance(op, ast.GtE)):
                    num = random.randint(0, 2)
                    if num == 0:
                        new_node.ops[i] = ast.Gt()
                    if num == 1:
                        new_node.ops[i] = ast.Lt()
                    if num == 2:
                        new_node.ops[i] = ast.LtE()
                if (isinstance(op, ast.Lt)):
                    num = random.randint(0, 2)
                    if num == 0:
                        new_node.ops[i] = ast.LtE()
                    if num == 1:
                        new_node.ops[i] = ast.GtE()
                    if num == 2:
                        new_node.ops[i] = ast.Gt()
                if (isinstance(op, ast.LtE)):
                    num = random.randint(0, 2)
                    if num == 0:
                        new_node.ops[i] = ast.Lt()
                    if num == 1:
                        new_node.ops[i] = ast.GtE()
                    if num == 2:
                        new_node.ops[i] = ast.Gt()
                if (isinstance(op, ast.Eq)):
                    new_node.ops[i] = ast.NotEq()
                if (isinstance(op, ast.NotEq)):
                    new_node.ops[i] = ast.Eq()
                if (isinstance(op, ast.Is)):
                    new_node.ops[i] = ast.IsNot()
                if (isinstance(op, ast.IsNot)):
                    new_node.ops[i] = ast.Is()
                print('THIS IS THE NEW OP', new_node.ops)
                print('I AM CREATING A NEW NODE HERE', self.binop_count)
                return new_node
        return node
Ejemplo n.º 12
0
def p_comp_op(p):
    '''comp_op : ">"
			| "<"
			| OP_EQ
			| OP_GE
			| OP_LE
			| OP_NE
			| OP_NNE
			| TAG_IN
			| TAG_NOT TAG_IN
			| TAG_IS
			| TAG_IS TAG_NOT'''

    if len(p) == 2:
        if p.get_item(1).type == 'OP_EQ':
            p[0] = ast.Eq()

        elif p.get_item(1).type == '>':
            p[0] = ast.Gt()

        elif p.get_item(1).type == '<':
            p[0] = ast.Lt()

        elif p.get_item(1).type == 'OP_GE':
            p[0] = ast.GtE()

        elif p.get_item(1).type == 'OP_LE':
            p[0] = ast.LtE()

        elif p.get_item(1).type == 'OP_NE':
            p[0] = ast.NotEq()

        elif p.get_item(1).type == 'OP_NNE':
            p[0] = ast.NotEq()

        elif p[1] == 'is':
            p[0] = ast.Is()

        elif p[1] == 'in':
            p[0] = ast.In()

    elif len(p) == 3:
        if p[1] == 'is':
            p[0] = ast.IsNot()

        elif p[1] == 'not':
            p[0] = ast.NotIn()

    return
 def to_ast(self):
     call_func = ast_mod.Attribute(value=ast_mod.Name(id=self.module,
                                                      ctx=ast_mod.Load()),
                                   attr=self.funcdef.name,
                                   ctx=ast_mod.Load())
     call = ast_mod.Call(func=call_func, args=self.args, keywords=[])
     if self.return_result is None:
         ops = [ast_mod.IsNot()]
         comparators = [ast_mod.Constant(value=None)]
     elif isinstance(self.return_result, nodes.NameConstant):
         ops = [ast_mod.Is()]
         comparators = [ast_mod.Constant(value=self.return_result.value)]
     else:
         ops = [ast_mod.Eq()]
         comparators = [self.return_result.to_ast()]
     return ast_mod.Assert(test=ast_mod.Compare(left=call,
                                                ops=ops,
                                                comparators=comparators),
                           msg=None)
Ejemplo n.º 14
0
    def test_visit_Compare(self, get_logger_mock):
        node = MagicMock()
        node.left = "left"
        node.comparators = ["middle", "right"]
        node.ops = [ast.LtE(), ast.IsNot()]
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Compare(node))
        self.assertEqual(
            analyzer.parts,
            ["left", " ", "<=", " ", "middle", " ", "is not", " ", "right"],
        )

        node.ops = [ast.LtE(), ast.Import]
        analyzer = ExpressionAnalyzer()
        self.assertIsNone(analyzer.visit_Compare(node))
        self.assertEqual(
            analyzer.parts,
            ["left", " ", "<=", " ", "middle", " ", "...", " ", "right"])
        get_logger_mock().warning.assert_called_once_with(ANY)
Ejemplo n.º 15
0
def genAlternative(
    o: ast.Name, fieldName: str, processerFunc: ast.Attribute
) -> typing.Iterator[typing.Union[ast.Assign, ast.If]]:
    fe = ast.Name(id=fieldName, ctx=ast.Load())
    yield ast.Assign(
        targets=[ast.Name(id=fieldName, ctx=ast.Store())],
        value=unifiedGetAttr(o, fieldName),
        type_comment=None,
    )
    yield ast.If(
        test=ast.Compare(left=fe, ops=[ast.IsNot()], comparators=[ASTNone]),
        body=[
            ast.Return(value=ast.Call(
                func=processerFunc,
                args=[fe],
                keywords=[],
            ))
        ],
        orelse=[],
    )
Ejemplo n.º 16
0
        def _revert_compare(node):
            """
            Helper function to revert a compare node to its negation.
            """
            rev_node = copy.deepcopy(node)
            op = rev_node.ops[0]

            if isinstance(op, ast.Is):
                rev_node.ops = [ast.IsNot()]
            elif isinstance(op, ast.Gt):
                rev_node.ops = [ast.LtE()]
            elif isinstance(op, ast.Lt):
                rev_node.ops = [ast.GtE()]
            elif isinstance(op, ast.Eq):
                rev_node.ops = [ast.NotEq()]
            elif isinstance(op, ast.In):
                rev_node.ops = [ast.NotIn()]
            else:
                raise ConstraintError('Unknown operator: %s' % op)
            return rev_node
 def visit_CmpOp(self, node: CmpOp, *args, **kwargs) -> C.cmpop:
     if node == CmpOp.Eq:
         return C.Eq()
     elif node == CmpOp.NotEq:
         return C.NotEq()
     elif node == CmpOp.Lt:
         return C.Lt()
     elif node == CmpOp.Lte:
         return C.Lte()
     elif node == CmpOp.Gt:
         return C.Gt()
     elif node == CmpOp.Gte:
         return C.Gte()
     elif node == CmpOp.Is:
         return C.Is()
     elif node == CmpOp.IsNot:
         return C.IsNot()
     elif node == CmpOp.In:
         return C.In()
     elif node == CmpOp.NotIn:
         return C.NotIn()
     else:
         raise Exception(f'unknown CmpOp {node!r}')
Ejemplo n.º 18
0
 def comparison(self) -> ast.expr:
     left: ast.expr = self.bit_or()
     operators: list[ast.cmpop] = []
     extra: list[ast.expr] = []
     while self.match_(*TokenGroup.SINGLE_COMPARISON, TokenType.NOT):
         if self.previous().type == TokenType.IS:
             if self.match_(TokenType.NOT):
                 operator = ast.IsNot()
             else:
                 operator = ast.Is()
         elif self.previous().type == TokenType.NOT:
             self.consume(TokenType.IN,
                          "'in' must follow 'not' in comparison.")
             operator = ast.NotIn()
         else:
             operator = COMPARISON_OPERATORS[self.previous().type]()
         right = self.bit_or()
         operators.append(operator)
         extra.append(right)
     if operators:
         return ast.Compare(left, operators, extra,
                            **self.get_loc(left, extra[-1]))
     else:
         return left
Ejemplo n.º 19
0
    def __init__(self, str, lineno=0):
        self.value = str
        self.lineno = lineno


op_ast_map = {
    '+': ast.Add(),
    '-': ast.Sub(),
    '*': ast.Mult(),
    '/': ast.Div(),
    '%': ast.Mod(),
    '**': ast.Pow(),
    '<<': ast.LShift(),
    '>>': ast.RShift(),
    '|': ast.BitOr(),
    '^^': ast.BitXor(),
    '&&': ast.BitAnd(),
    '//': ast.FloorDiv(),
    '==': ast.Eq(),
    '!=': ast.NotEq(),
    '<': ast.Lt(),
    '<=': ast.LtE(),
    '>': ast.Gt(),
    '>=': ast.GtE(),
    'is': ast.Is(),
    'is_not': ast.IsNot(),
    'in': ast.In(),
    'not_in': ast.NotIn(),
    'and': ast.And(),
    'or': ast.Or()
}
Ejemplo n.º 20
0
    def visit_ClassDef(self, node):
        if len(node.bases) > 1:
            raise Exception('Multiple inheritance not supported')

        self.visit(FunctionDef(
            node.name,
            arguments([], None, None, []),
            [
                ast.If(
                    ast.UnaryOp(
                        ast.Not(),
                        ast_call(
                            ast_load('isinstance'),
                            ast_load('this'),
                            ast_load('arguments.callee'),
                        )
                    ),
                    [
                        ast.Return(
                            ast_call(
                                ast_load('new'),
                                ast_load('arguments.callee'),
                                ast_load('arguments'),
                            )
                        )
                    ],
                    []
                ),
                ast.Assign(
                    [ast_store('this.__class__')],
                    ast_load('arguments.callee')
                ),
                ast.Expr(
                    ast_call(
                        ast_load('this.__bind__'),
                        ast_load('this'),
                    ),
                ),
                ast.If(
                    ast.Compare(
                        ast_call(
                            ast_load('type'),
                            ast_load('this.__init__'),
                        ),
                        [ast.IsNot()],
                        [ast.Str('undefined')],
                    ),
                    [
                        ast.Expr(
                            ast_call(
                                ast_load('this.__init__.apply'),
                                ast_load('this'),
                                ast.Subscript(
                                    ast_load('arguments'),
                                    ast.Index(ast.Num(0)),
                                    ast.Load()
                                )
                            )
                        ),
                    ],
                    []
                )
            ],
            []
        ))

        self.push(ClassContext(self.stack[-1], node.name))
        scope = self.stack[-1].scope

        if not node.bases:
            scope.prefix.pop()
            self.visit(
                ast.Assign(
                    [ast_store('prototype')],
                    ast.Dict(
                        [
                            ast.Str('constructor'),
                            ast.Str('__mro__')
                        ],
                        [
                            ast_load(node.name),
                            ast.List([ast_load(node.name)], ast.Load())
                        ]
                    )
                )
            )
            scope.prefix.append('prototype')
            self.visit(
                ast.Assign(
                    [ast_store('__bind__')],
                    FunctionDef(
                        '',
                        arguments([ast_load('self')], None, None, []),
                        [
                            ast.For(
                                ast_store('i'),
                                ast_load('this'),
                                [
                                    ast.If(
                                        ast.Compare(
                                            ast_call(
                                                ast_load('type'),
                                                ast.Subscript(
                                                    ast_load('this'),
                                                    ast.Index(ast_load('i')),
                                                    ast.Load(),
                                                )
                                            ),
                                            [ast.Is()],
                                            [ast.Str('function')]
                                        ),
                                        [
                                            ast.Assign(
                                                [ast.Subscript(
                                                    ast_load('this'),
                                                    ast.Index(ast_load('i')),
                                                    ast.Store(),
                                                )],
                                                ast_call(
                                                    ast.Attribute(
                                                        ast.Subscript(
                                                            ast_load('this'),
                                                            ast.Index(ast_load('i')),
                                                            ast.Load(),
                                                        ),
                                                        'bind',
                                                        ast.Load(),
                                                    ),
                                                    ast_load('self'),
                                                    ast_load('self'),
                                                )
                                            ),
                                        ],
                                        []
                                    )
                                ],
                                []
                            ),
                        ],
                        []
                    )
                )
            )
        else:
            base = node.bases[0]
            self.visit(
                ast.Assign(
                    [ast_store(node.name, 'prototype')],
                    ast_call(
                        FunctionDef(
                            '',
                            arguments([], None, None, []),
                            [
                                ast.Assign(
                                    [ast_store('tmp')],
                                    FunctionDef(
                                        '',
                                        arguments([], None, None, []),
                                        [],
                                        []
                                    )
                                ),
                                ast.Assign(
                                    [ast_store('tmp', 'prototype')],
                                    ast.Attribute(base, 'prototype', ast.Load()),
                                ),
                                ast.Return(
                                    ast_call(
                                        ast_load('new'),
                                        ast_load('tmp'),
                                    )
                                )
                            ],
                            []
                        ),
                    )
                )
            )
            self.visit(
                ast.Assign(
                    [ast_store(node.name, 'prototype.constructor')],
                    ast_load(node.name)
                )
            )
            self.visit(
                ast.Assign(
                    [ast_store(node.name, 'prototype.__base__')],
                    base,
                )
            )
            self.visit(
                ast.Assign(
                    [ast_store(node.name, 'prototype.__mro__')],
                    ast_call(
                        ast_load(node.name, 'prototype.__mro__.concat'),
                        ast_load(node.name),
                    )
                )
            )

        for stmt in node.body:
            self.visit(stmt)

        self.pop()

        if node.decorator_list:
            arg = ast_load(node.name)
            for decorator in node.decorator_list:
                arg = ast_call(decorator, arg)
            self.visit(
                ast.Assign(
                    [ast_store(node.name)],
                    arg
                )
            )
Ejemplo n.º 21
0
    def from_phpast(self, node):
    if node is None:
            return py.Name('None', py.Load(**pos(node)), **pos(node))

    if isinstance(node, basestring):
        return py.Str(node, **pos(node))

        if isinstance(node, (int, float, long)):
        return py.Num(node, **pos(node))

    if isinstance(node, php.Array):
        if node.nodes:
            if node.nodes[0].key is not None:
                keys = []
                values = []
                for elem in node.nodes:
                        keys.append(self.from_phpast(elem.key))
                        values.append(self.from_phpast(elem.value))
                    ds = py.Dict(keys, values, **pos(node))
            else:
                    ds = py.List([self.from_phpast(x.value) for x in node.nodes],
                               py.Load(**pos(node)),
                               **pos(node))
        else:
                ds = py.List([], py.Load(**pos(node)), **pos(node))
            return py.Call(py.Name('PHPArray', py.Load(**pos(node)), **pos(node)),
                           [ds],
                           [], None, None, **pos(node))

    if isinstance(node, php.InlineHTML):
        args = [py.Str(node.data, **pos(node))]
        return py.Call(py.Name('inline_html',
                               py.Load(**pos(node)),
                               **pos(node)),
                       args, [], None, None,
                       **pos(node))

    if isinstance(node, php.Echo):
        return py.Call(py.Name('echo', py.Load(**pos(node)),
                               **pos(node)),
                           map(self.from_phpast, node.nodes),
                       [], None, None,
                       **pos(node))

    if isinstance(node, php.Print):
            return py.Print(None, [self.from_phpast(node.node)], True, **pos(node))

    if isinstance(node, php.Exit):
        args = []
        if node.expr is not None:
                args.append(self.from_phpast(node.expr))
            return py.Raise(py.Call(py.Name('SystemExit', py.Load(**pos(node)),
                                        **pos(node)),
                                args, [], None, None, **pos(node)),
                        None, None, **pos(node))

    if isinstance(node, php.Return):
            if len(self.scope_stack) > 0:
                # return from function
        if node.node is None:
            return py.Return(None, **pos(node))
        else:
                    return py.Return(self.from_phpast(node.node), **pos(node))
            else:
                # return from script
                args = [self.from_phpast(node.node)]
                return py.Raise(py.Call(py.Name('_GlobalReturn', py.Load(**pos(node)),
                                                **pos(node)),
                                        args, [], None, None, **pos(node)),
                                None, None, **pos(node))

    if isinstance(node, php.Break):
        assert node.node is None, 'level on break not supported'
        return py.Break(**pos(node))

    if isinstance(node, php.Continue):
        assert node.node is None, 'level on continue not supported'
        return py.Continue(**pos(node))

    if isinstance(node, php.Silence):
            return self.from_phpast(node.expr)

    if isinstance(node, php.Block):
            return self.from_phpast(php.If(1, node, [], None, lineno=node.lineno))

    if isinstance(node, php.Unset):
            return py.Delete(map(self.from_phpast, node.nodes), **pos(node))

    if isinstance(node, php.IsSet) and len(node.nodes) == 1:
        if isinstance(node.nodes[0], php.ArrayOffset):
                return py.Compare(self.from_phpast(node.nodes[0].expr),
                              [py.In(**pos(node))],
                                  [self.from_phpast(node.nodes[0].node)],
                              **pos(node))
        if isinstance(node.nodes[0], php.ObjectProperty):
            return py.Call(py.Name('hasattr', py.Load(**pos(node)),
                                   **pos(node)),
                               [self.from_phpast(node.nodes[0].node),
                                self.from_phpast(node.nodes[0].name)],
                           [], None, None, **pos(node))
        if isinstance(node.nodes[0], php.Variable):
                variable_name = node.nodes[0].name[1:]
                static_name = self.get_static(variable_name)
                if static_name:
                    variable_name = static_name

                globs = py.Call(py.Name('globals', py.Load(**pos(node)),
                                        **pos(node)),
                                [], [], None, None, **pos(node))

                locs = py.Call(py.Name('locals', py.Load(**pos(node)),
                                        **pos(node)),
                                [], [], None, None, **pos(node))

                return py.Compare(py.Str(static_name, **pos(node)),
                              [py.In(**pos(node))],
                                  [py.Call(py.Name('all_vars', py.Load(**pos(node)),
                                               **pos(node)),
                                           [globs, locs], [], None, None, **pos(node))],
                              **pos(node))
            return py.Compare(self.from_phpast(node.nodes[0]),
                          [py.IsNot(**pos(node))],
                          [py.Name('None', py.Load(**pos(node)), **pos(node))],
                          **pos(node))

    if isinstance(node, php.Empty):
            return self.from_phpast(php.UnaryOp('!',
                                       php.BinaryOp('&&',
                                                    php.IsSet([node.expr],
                                                              lineno=node.lineno),
                                                    node.expr,
                                                    lineno=node.lineno),
                                       lineno=node.lineno))

    if isinstance(node, php.Assignment):
        if (isinstance(node.node, php.ArrayOffset)
            and node.node.expr is None):
                return py.Call(py.Attribute(self.from_phpast(node.node.node),
                                        'append', py.Load(**pos(node)),
                                        **pos(node)),
                               [self.from_phpast(node.expr)],
                           [], None, None, **pos(node))
        if (isinstance(node.node, php.ObjectProperty)
            and isinstance(node.node.name, php.BinaryOp)):
            return to_stmt(py.Call(py.Name('setattr', py.Load(**pos(node)),
                                   **pos(node)),
                           [from_phpast(node.node.node),
                            from_phpast(node.node.name),
                            from_phpast(node.expr)],
                           [], None, None, **pos(node)))
        return py.Assign([store(from_phpast(node.node))],
                         from_phpast(node.expr),
                         **pos(node))

    if isinstance(node, php.ListAssignment):
            return py.Assign([py.Tuple(map(store, map(self.from_phpast, node.nodes)),
                                   py.Store(**pos(node)),
                                   **pos(node))],
                             self.from_phpast(node.expr),
                          **pos(node))

    if isinstance(node, php.AssignOp):
            return self.from_phpast(php.Assignment(node.left,
                                          php.BinaryOp(node.op[:-1],
                                                       node.left,
                                                       node.right,
                                                       lineno=node.lineno),
                                          False,
                                          lineno=node.lineno))

    if isinstance(node, (php.PreIncDecOp, php.PostIncDecOp)):
            return self.from_phpast(php.Assignment(node.expr,
                                          php.BinaryOp(node.op[0],
                                                       node.expr,
                                                       1,
                                                       lineno=node.lineno),
                                          False,
                                          lineno=node.lineno))

    if isinstance(node, php.ArrayOffset):
            return py.Subscript(self.from_phpast(node.node),
                                py.Index(self.from_phpast(node.expr), **pos(node)),
                            py.Load(**pos(node)),
                            **pos(node))

    if isinstance(node, php.ObjectProperty):
        if isinstance(node.name, (php.Variable, php.BinaryOp)):
            return py.Call(py.Name('getattr', py.Load(**pos(node)),
                                   **pos(node)),
                               [self.from_phpast(node.node),
                                self.from_phpast(node.name)],
                           [], None, None, **pos(node))            
            return py.Attribute(self.from_phpast(node.node),
                            node.name,
                            py.Load(**pos(node)),
                            **pos(node))

    if isinstance(node, php.Constant):
        name = node.name
        if name.lower() == 'true': name = 'True'
        if name.lower() == 'false': name = 'False'
        if name.lower() == 'null': name = 'None'
        return py.Name(name, py.Load(**pos(node)), **pos(node))

    if isinstance(node, php.Variable):
        name = node.name[1:]
            static_name = self.get_static(name)
            if static_name:
                name = static_name
        if name == 'this': name = 'self'
        return py.Name(name, py.Load(**pos(node)), **pos(node))
Ejemplo n.º 22
0
from hypothesis import assume
from hypothesis._strategies import booleans
from hypothesis.searchstrategy import SearchStrategy
from hypothesis.strategies import integers, lists, binary, sampled_from, recursive, dictionaries
from hypothesis.strategies import text, composite, one_of, floats, complex_numbers, characters, none

comparison_operators = sampled_from([
    ast.Eq(),
    ast.NotEq(),
    ast.Lt(),
    ast.LtE(),
    ast.Gt(),
    ast.GtE(),
    ast.Is(),
    ast.IsNot(),
    ast.In(),
    ast.NotIn()
])

# region: Literals


@composite
def Num(draw) -> ast.AST:
    def to_node(n) -> ast.AST:
        if isinstance(n, int):
            return ast.Num(n) if n >= 0 else ast.UnaryOp(
                ast.USub(), ast.Num(abs(n)))
        elif isinstance(n, float):
            return ast.Num(n) if math.copysign(1.0, n) > 0.0 else ast.UnaryOp(
 def is_not(self, left, right):
     return ast.Compare(left=to_ast(left),
                        ops=[ast.IsNot()],
                        comparators=[to_ast(right)])
Ejemplo n.º 24
0
    def visit_Compare(self, node):
        if isinstance(node.ops, ast.LtE):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = [ast.Gt()]

                print('changing lessThanEqual {} to greaterThan.'.format(
                    self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.GtE):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.op = [ast.Lt()]

                print('changing GTE {} to LT.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.Gt):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = [ast.LtE()]

                print('changing GT {} to LtE.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.Lt):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = [ast.GtE()]

                print('changing LT {} to GtE.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.Eq):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = [ast.NotEq()]

                print('changing eq {} to neq.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.NotEq):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = ast.Eq()

                print('changing neq {} to eq.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.Is):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = ast.IsNot()

                print('changing is {} to isnot.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.IsNot):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = ast.Is()

                print('changing isnot {} to is.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.In):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = ast.NotIn()

                print('changing In {} to InNot.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        if isinstance(node.ops, ast.NotIn):
            self.counter += 1

            if self.counter == self.nodeToMutate:
                new_node = ast.Compare()
                new_node.left = node.left
                new_node.comparators = node.comparators

                new_node.ops = ast.In()

                print('changing Notin {} to in.'.format(self.counter))
                return ast.copy_location(new_node, node)  # helps debugging

        return self.generic_visit(node)
Ejemplo n.º 25
0
def from_phpast(node):
    if node is None:
        return py.Pass(**pos(node))

    if isinstance(node, str):
        return py.Str(node, **pos(node))

    if isinstance(node, (int, float)):
        return py.Num(node, **pos(node))

    if isinstance(node, php.Array):
        if node.nodes:
            if node.nodes[0].key is not None:
                keys = []
                values = []
                for elem in node.nodes:
                    keys.append(from_phpast(elem.key))
                    values.append(from_phpast(elem.value))
                return py.Dict(keys, values, **pos(node))
            else:
                return py.List([from_phpast(x.value) for x in node.nodes],
                               py.Load(**pos(node)), **pos(node))
        else:
            return py.List([], py.Load(**pos(node)), **pos(node))

    if isinstance(node, php.InlineHTML):
        args = [py.Str(node.data, **pos(node))]
        return py.Call(
            py.Name('inline_html', py.Load(**pos(node)), **pos(node)), args,
            [], None, None, **pos(node))

    if isinstance(node, php.Echo):
        return py.Call(py.Name('echo', py.Load(**pos(node)), **pos(node)),
                       list(map(from_phpast, node.nodes)), [], None, None,
                       **pos(node))

    if isinstance(node, php.Print):
        return py.Print(None, [from_phpast(node.node)], True, **pos(node))

    if isinstance(node, php.Exit):
        args = []
        if node.expr is not None:
            args.append(from_phpast(node.expr))
        return py.Raise(
            py.Call(py.Name('Exit', py.Load(**pos(node)), **pos(node)), args,
                    [], None, None, **pos(node)), None, None, **pos(node))

    if isinstance(node, php.Return):
        if node.node is None:
            return py.Return(None, **pos(node))
        else:
            return py.Return(from_phpast(node.node), **pos(node))

    if isinstance(node, php.Break):
        assert node.node is None, 'level on break not supported'
        return py.Break(**pos(node))

    if isinstance(node, php.Continue):
        assert node.node is None, 'level on continue not supported'
        return py.Continue(**pos(node))

    if isinstance(node, php.Silence):
        return from_phpast(node.expr)

    if isinstance(node, php.Block):
        return from_phpast(php.If(1, node, [], None, lineno=node.lineno))

    if isinstance(node, php.Unset):
        return py.Delete(list(map(from_phpast, node.nodes)), **pos(node))

    if isinstance(node, php.IsSet) and len(node.nodes) == 1:
        if isinstance(node.nodes[0], php.ArrayOffset):
            return py.Compare(from_phpast(node.nodes[0].expr),
                              [py.In(**pos(node))],
                              [from_phpast(node.nodes[0].node)], **pos(node))
        if isinstance(node.nodes[0], php.ObjectProperty):
            return py.Call(
                py.Name('hasattr', py.Load(**pos(node)), **pos(node)), [
                    from_phpast(node.nodes[0].node),
                    from_phpast(node.nodes[0].name)
                ], [], None, None, **pos(node))
        if isinstance(node.nodes[0], php.Variable):
            return py.Compare(py.Str(
                node.nodes[0].name[1:], **pos(node)), [py.In(**pos(node))], [
                    py.Call(py.Name('vars', py.Load(**pos(node)), **pos(node)),
                            [], [], None, None, **pos(node))
                ], **pos(node))
        return py.Compare(from_phpast(node.nodes[0]), [py.IsNot(**pos(node))],
                          [py.Name('None', py.Load(**pos(node)), **pos(node))],
                          **pos(node))

    if isinstance(node, php.Empty):
        return from_phpast(
            php.UnaryOp('!',
                        php.BinaryOp('&&',
                                     php.IsSet([node.expr],
                                               lineno=node.lineno),
                                     node.expr,
                                     lineno=node.lineno),
                        lineno=node.lineno))

    if isinstance(node, php.Assignment):
        if (isinstance(node.node, php.ArrayOffset) and node.node.expr is None):
            return py.Call(
                py.Attribute(from_phpast(node.node.node), 'append',
                             py.Load(**pos(node)), **pos(node)),
                [from_phpast(node.expr)], [], None, None, **pos(node))
        if (isinstance(node.node, php.ObjectProperty)
                and isinstance(node.node.name, php.BinaryOp)):
            return to_stmt(
                py.Call(py.Name('setattr', py.Load(**pos(node)), **pos(node)),
                        [
                            from_phpast(node.node.node),
                            from_phpast(node.node.name),
                            from_phpast(node.expr)
                        ], [], None, None, **pos(node)))
        return py.Assign([store(from_phpast(node.node))],
                         from_phpast(node.expr), **pos(node))

    if isinstance(node, php.ListAssignment):
        return py.Assign([
            py.Tuple(list(map(store, list(map(from_phpast, node.nodes)))),
                     py.Store(**pos(node)), **pos(node))
        ], from_phpast(node.expr), **pos(node))

    if isinstance(node, php.AssignOp):
        return from_phpast(
            php.Assignment(node.left,
                           php.BinaryOp(node.op[:-1],
                                        node.left,
                                        node.right,
                                        lineno=node.lineno),
                           False,
                           lineno=node.lineno))

    if isinstance(node, (php.PreIncDecOp, php.PostIncDecOp)):
        return from_phpast(
            php.Assignment(node.expr,
                           php.BinaryOp(node.op[0],
                                        node.expr,
                                        1,
                                        lineno=node.lineno),
                           False,
                           lineno=node.lineno))

    if isinstance(node, php.ArrayOffset):
        return py.Subscript(from_phpast(node.node),
                            py.Index(from_phpast(node.expr), **pos(node)),
                            py.Load(**pos(node)), **pos(node))

    if isinstance(node, php.ObjectProperty):
        if isinstance(node.name, (php.Variable, php.BinaryOp)):
            return py.Call(
                py.Name('getattr', py.Load(**pos(node)), **pos(node)),
                [from_phpast(node.node),
                 from_phpast(node.name)], [], None, None, **pos(node))
        return py.Attribute(from_phpast(node.node), node.name,
                            py.Load(**pos(node)), **pos(node))

    if isinstance(node, php.Constant):
        name = node.name
        if name.lower() == 'true': name = 'True'
        if name.lower() == 'false': name = 'False'
        if name.lower() == 'null': name = 'None'
        return py.Name(name, py.Load(**pos(node)), **pos(node))

    if isinstance(node, php.Variable):
        name = node.name[1:]
        if name == 'this': name = 'self'
        return py.Name(name, py.Load(**pos(node)), **pos(node))

    if isinstance(node, php.Global):
        return py.Global([var.name[1:] for var in node.nodes], **pos(node))

    if isinstance(node, php.Include):
        once = py.Name('True' if node.once else 'False', py.Load(**pos(node)),
                       **pos(node))
        return py.Call(py.Name('include', py.Load(**pos(node)), **pos(node)),
                       [from_phpast(node.expr), once], [], None, None,
                       **pos(node))

    if isinstance(node, php.Require):
        once = py.Name('True' if node.once else 'False', py.Load(**pos(node)),
                       **pos(node))
        return py.Call(py.Name('require', py.Load(**pos(node)), **pos(node)),
                       [from_phpast(node.expr), once], [], None, None,
                       **pos(node))

    if isinstance(node, php.UnaryOp):
        op = unary_ops.get(node.op)
        assert op is not None, "unknown unary operator: '%s'" % node.op
        op = op(**pos(node))
        return py.UnaryOp(op, from_phpast(node.expr), **pos(node))

    if isinstance(node, php.BinaryOp):
        if node.op == '.':
            pattern, pieces = build_format(node.left, node.right)
            if pieces:
                return py.BinOp(
                    py.Str(pattern, **pos(node)), py.Mod(**pos(node)),
                    py.Tuple(list(map(from_phpast, pieces)),
                             py.Load(**pos(node)), **pos(node)), **pos(node))
            else:
                return py.Str(pattern % (), **pos(node))
        if node.op in bool_ops:
            op = bool_ops[node.op](**pos(node))
            return py.BoolOp(op,
                             [from_phpast(node.left),
                              from_phpast(node.right)], **pos(node))
        if node.op in cmp_ops:
            op = cmp_ops[node.op](**pos(node))
            return py.Compare(from_phpast(node.left), [op],
                              [from_phpast(node.right)], **pos(node))
        op = binary_ops.get(node.op)
        if node.op == 'instanceof':
            return py.Call(
                func=py.Name(id='isinstance', ctx=py.Load(**pos(node))),
                args=[from_phpast(node.left),
                      from_phpast(node.right)],
                keywords=[],
                starargs=None,
                kwargs=None)
        assert op is not None, "unknown binary operator: '%s'" % node.op
        op = op(**pos(node))
        return py.BinOp(from_phpast(node.left), op, from_phpast(node.right),
                        **pos(node))

    if isinstance(node, php.TernaryOp):
        return py.IfExp(from_phpast(node.expr), from_phpast(node.iftrue),
                        from_phpast(node.iffalse), **pos(node))

    if isinstance(node, php.Cast):
        return py.Call(
            py.Name(casts.get(node.type, node.type), py.Load(**pos(node)),
                    **pos(node)), [from_phpast(node.expr)], [], None, None,
            **pos(node))

    if isinstance(node, php.If):
        orelse = []
        if node.else_:
            for else_ in map(from_phpast, deblock(node.else_.node)):
                orelse.append(to_stmt(else_))
        for elseif in reversed(node.elseifs):
            orelse = [
                py.If(
                    from_phpast(elseif.expr),
                    list(
                        map(to_stmt,
                            list(map(from_phpast, deblock(elseif.node))))),
                    orelse, **pos(node))
            ]
        return py.If(
            from_phpast(node.expr),
            list(map(to_stmt, list(map(from_phpast, deblock(node.node))))),
            orelse, **pos(node))

    if isinstance(node, php.For):
        assert node.test is None or len(node.test) == 1, \
            'only a single test is supported in for-loops'
        return from_phpast(
            php.Block((node.start or []) + [
                php.While(node.test[0] if node.test else 1,
                          php.Block(deblock(node.node) + (node.count or []),
                                    lineno=node.lineno),
                          lineno=node.lineno)
            ],
                      lineno=node.lineno))

    if isinstance(node, php.Foreach):
        if node.keyvar is None:
            target = py.Name(node.valvar.name[1:], py.Store(**pos(node)),
                             **pos(node))
        else:
            target = py.Tuple([
                py.Name(node.keyvar.name[1:], py.Store(**pos(node))),
                py.Name(node.valvar.name[1:], py.Store(**pos(node)))
            ], py.Store(**pos(node)), **pos(node))
        return py.For(
            target, from_phpast(node.expr),
            list(map(to_stmt, list(map(from_phpast, deblock(node.node))))), [],
            **pos(node))

    if isinstance(node, php.While):
        return py.While(
            from_phpast(node.expr),
            list(map(to_stmt, list(map(from_phpast, deblock(node.node))))), [],
            **pos(node))

    if isinstance(node, php.DoWhile):
        condition = php.If(php.UnaryOp('!', node.expr, lineno=node.lineno),
                           php.Break(None, lineno=node.lineno), [],
                           None,
                           lineno=node.lineno)
        return from_phpast(
            php.While(1,
                      php.Block(deblock(node.node) + [condition],
                                lineno=node.lineno),
                      lineno=node.lineno))

    if isinstance(node, php.Try):
        return py.TryExcept(
            list(map(to_stmt, list(map(from_phpast, node.nodes)))), [
                py.ExceptHandler(
                    py.Name(catch.class_, py.Load(**pos(node)), **pos(node)),
                    store(from_phpast(catch.var)),
                    list(map(to_stmt, list(map(from_phpast, catch.nodes)))),
                    **pos(node)) for catch in node.catches
            ], [], **pos(node))

    if isinstance(node, php.Throw):
        return py.Raise(from_phpast(node.node), None, None, **pos(node))

    if isinstance(node, php.Function):
        args = []
        defaults = []
        for param in node.params:
            args.append(
                py.Name(param.name[1:], py.Param(**pos(node)), **pos(node)))
            if param.default is not None:
                defaults.append(from_phpast(param.default))
        body = list(map(to_stmt, list(map(from_phpast, node.nodes))))
        if not body: body = [py.Pass(**pos(node))]
        return py.FunctionDef(node.name,
                              py.arguments(args, None, None, defaults), body,
                              [], **pos(node))

    if isinstance(node, php.Method):
        args = []
        defaults = []
        decorator_list = []
        if 'static' in node.modifiers:
            decorator_list.append(
                py.Name('classmethod', py.Load(**pos(node)), **pos(node)))
            args.append(py.Name('cls', py.Param(**pos(node)), **pos(node)))
        else:
            args.append(py.Name('self', py.Param(**pos(node)), **pos(node)))
        for param in node.params:
            args.append(
                py.Name(param.name[1:], py.Param(**pos(node)), **pos(node)))
            if param.default is not None:
                defaults.append(from_phpast(param.default))
        body = list(map(to_stmt, list(map(from_phpast, node.nodes))))
        if not body: body = [py.Pass(**pos(node))]
        return py.FunctionDef(node.name,
                              py.arguments(args, None, None, defaults), body,
                              decorator_list, **pos(node))

    if isinstance(node, php.Class):
        name = node.name
        bases = []
        extends = node.extends or 'object'
        bases.append(py.Name(extends, py.Load(**pos(node)), **pos(node)))
        body = list(map(to_stmt, list(map(from_phpast, node.nodes))))
        for stmt in body:
            if (isinstance(stmt, py.FunctionDef)
                    and stmt.name in (name, '__construct')):
                stmt.name = '__init__'
        if not body: body = [py.Pass(**pos(node))]
        return py.ClassDef(name, bases, body, [], **pos(node))

    if isinstance(node, (php.ClassConstants, php.ClassVariables)):
        assert len(node.nodes) == 1, \
            'only one class-level assignment supported per line'
        if isinstance(node.nodes[0], php.ClassConstant):
            name = php.Constant(node.nodes[0].name, lineno=node.lineno)
        else:
            name = php.Variable(node.nodes[0].name, lineno=node.lineno)
        initial = node.nodes[0].initial
        if initial is None:
            initial = php.Constant('None', lineno=node.lineno)
        return py.Assign([store(from_phpast(name))], from_phpast(initial),
                         **pos(node))

    if isinstance(node, (php.FunctionCall, php.New)):
        if isinstance(node.name, str):
            name = py.Name(node.name, py.Load(**pos(node)), **pos(node))
        else:
            name = py.Subscript(
                py.Call(py.Name('vars', py.Load(**pos(node)), **pos(node)), [],
                        [], None, None, **pos(node)),
                py.Index(from_phpast(node.name), **pos(node)),
                py.Load(**pos(node)), **pos(node))
        args, kwargs = build_args(node.params)
        return py.Call(name, args, kwargs, None, None, **pos(node))

    if isinstance(node, php.MethodCall):
        args, kwargs = build_args(node.params)
        return py.Call(
            py.Attribute(from_phpast(node.node), node.name,
                         py.Load(**pos(node)), **pos(node)), args, kwargs,
            None, None, **pos(node))

    if isinstance(node, php.StaticMethodCall):
        class_ = node.class_
        if class_ == 'self': class_ = 'cls'
        args, kwargs = build_args(node.params)
        return py.Call(
            py.Attribute(py.Name(class_, py.Load(**pos(node)),
                                 **pos(node)), node.name, py.Load(**pos(node)),
                         **pos(node)), args, kwargs, None, None, **pos(node))

    if isinstance(node, php.StaticProperty):
        class_ = node.node
        name = node.name
        if isinstance(name, php.Variable):
            name = name.name[1:]
        return py.Attribute(py.Name(class_, py.Load(**pos(node)), **pos(node)),
                            name, py.Load(**pos(node)), **pos(node))

    return py.Call(py.Name('XXX', py.Load(**pos(node)),
                           **pos(node)), [py.Str(str(node), **pos(node))], [],
                   None, None, **pos(node))
    def __init__(self, base_node):
        BaseMutator.__init__(self, base_node)
        self.original_ops = base_node.ops

        index_count = 0
        for op in base_node.ops:

            if type(op) in [
                    ast.Gt, ast.GtE, ast.Lt, ast.LtE, ast.Eq, ast.NotEq,
                    ast.Is, ast.IsNot, ast.In, ast.NotIn
            ]:
                if type(op) is ast.Eq:
                    ops_mutant_Eq = copy.deepcopy(base_node.ops)
                    ops_mutant_Eq[index_count] = ast.NotEq()
                    self.mutations.append({"ops": ops_mutant_Eq})

                if type(op) is ast.NotEq:
                    ops_mutant_NotEq = copy.deepcopy(base_node.ops)
                    ops_mutant_NotEq[index_count] = ast.Eq()
                    self.mutations.append({"ops": ops_mutant_NotEq})

                if type(op) is ast.LtE:
                    ops_mutant_LtE = copy.deepcopy(base_node.ops)
                    ops_mutant_LtE[index_count] = ast.Gt()
                    self.mutations.append({"ops": ops_mutant_LtE})

                if type(op) is ast.GtE:
                    ops_mutant_GtE = copy.deepcopy(base_node.ops)
                    ops_mutant_GtE[index_count] = ast.Lt()
                    self.mutations.append({"ops": ops_mutant_GtE})

                if type(op) is ast.Lt:
                    ops_mutant_Lt = copy.deepcopy(base_node.ops)
                    ops_mutant_Lt[index_count] = ast.GtE()
                    self.mutations.append({"ops": ops_mutant_Lt})

                if type(op) is ast.Gt:
                    ops_mutant_Gt = copy.deepcopy(base_node.ops)
                    ops_mutant_Gt[index_count] = ast.LtE()
                    self.mutations.append({"ops": ops_mutant_Gt})

                if type(op) is ast.Is:
                    ops_mutant_Is = copy.deepcopy(base_node.ops)
                    ops_mutant_Is[index_count] = ast.IsNot()
                    self.mutations.append({"ops": ops_mutant_Is})

                if type(op) is ast.IsNot:
                    ops_mutant_IsNot = copy.deepcopy(base_node.ops)
                    ops_mutant_IsNot[index_count] = ast.Is()
                    self.mutations.append({"ops": ops_mutant_IsNot})

                if type(op) is ast.In:
                    ops_mutant_In = copy.deepcopy(base_node.ops)
                    ops_mutant_In[index_count] = ast.NotIn()
                    self.mutations.append({"ops": ops_mutant_In})

                if type(op) is ast.NotIn:
                    ops_mutant_NotIn = copy.deepcopy(base_node.ops)
                    ops_mutant_NotIn[index_count] = ast.In()
                    self.mutations.append({"ops": ops_mutant_NotIn})

            index_count += 1
Ejemplo n.º 27
0
 Symbol.new("/"): compile_divide,
 Symbol.new("//"): compile_floordivide,
 Symbol.new("%"): lambda p: ast.BinOp(build_ast(p[1]), ast.Mod(), build_ast(p[2])),
 Symbol.new("&"): lambda p: ast.BinOp(build_ast(p[1]), ast.BitAnd(), build_ast(p[2])),
 Symbol.new("**"): lambda p: ast.BinOp(build_ast(p[1]), ast.Pow(), build_ast(p[2])),
 Symbol.new(">>"): lambda p: ast.BinOp(build_ast(p[1]), ast.RShift(), build_ast(p[2])),
 Symbol.new("<<"): lambda p: ast.BinOp(build_ast(p[1]), ast.LShift(), build_ast(p[2])),
 Symbol.new("^"): lambda p: ast.BinOp(build_ast(p[1]), ast.BitXor(), build_ast(p[2])),
 Symbol.new("<"): lambda p: ast.Compare(build_ast(p[1]), [ast.Lt() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new(">"): lambda p: ast.Compare(build_ast(p[1]), [ast.Gt() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new("<="): lambda p: ast.Compare(build_ast(p[1]), [ast.LtE() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new(">="): lambda p: ast.Compare(build_ast(p[1]), [ast.GtE() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new("=="): compile_equals,
 Symbol.new("!="): lambda p: ast.Compare(build_ast(p[1]), [ast.NotEq() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new("is"): lambda p: ast.Compare(build_ast(p[1]), [ast.Is() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new("is-not"): lambda p: ast.Compare(build_ast(p[1]), [ast.IsNot() for x in p[1::2]], [build_ast(x) for x in p[2::2]]),
 Symbol.new("define"): compile_define,
 Symbol.new("dict-set"): lambda p: ast.Assign([ast.Subscript(build_ast(p[1]), ast.Index(build_ast(p[2])), ast.Store())], build_ast(p[3])),
 #Symbol.new("caadr"): lambda p: compiler.ast.Subscript(compiler.ast.Subscript(build_ast(p[1]), 0, compiler.ast.Const(1)), 0, compiler.ast.Const(0)),
 Symbol.new("caar"): lambda p: ast.Subscript(ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(0)), ast.Load()), ast.Index(ast.Num(0)), ast.Load()),
 #Symbol.new("cadddr"): lambda p: compiler.ast.Subscript(build_ast(p[1]), 0, compiler.ast.Const(3)),
 #Symbol.new("caddr"): lambda p: compiler.ast.Subscript(build_ast(p[1]), 0, compiler.ast.Const(2)),
 Symbol.new("cadr"): lambda p: ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(1)), ast.Load()),
 Symbol.new("car"): lambda p: ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(0)), ast.Load()),
 Symbol.new("cdar"): lambda p: ast.Subscript(ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(0)), ast.Load()), ast.Slice(ast.Num(1), None, None), ast.Load()),
 #Symbol.new("cddr"): lambda p: compiler.ast.Slice(build_ast(p[1]), 0, compiler.ast.Const(2), None),
 Symbol.new("cdr"): lambda p: ast.Subscript(build_ast(p[1]), ast.Slice(ast.Num(1), None, None), ast.Load()),
 Symbol.new("cons"): lambda p: ast.BinOp(ast.List([build_ast(p[1])], ast.Load()), ast.Add(), build_ast(p[2])),
 #Symbol.new("append"): lambda p: ast.Call(ast.Attribute(ast.Name("functools", ast.Load()), "reduce", ast.Load()), [ast.Attribute(ast.Name("operator", ast.Load()), "add", ast.Load()), build_ast(p[1])], [], None, None),
 #Symbol.new("apply"): lambda p: ast.Call(build_ast(p[1]), [build_ast(p[2])], [], None, None),
 Symbol.new("if"): lambda p: ast.IfExp(build_ast(p[1]), build_ast(p[2]), build_ast(p[3]) if len(p) >= 4 else ast.Name("None", ast.Load())),
Ejemplo n.º 28
0
def as_ast(dct):
    """See https://docs.python.org/2/library/ast.html"""
    if dct['ast_type'] == "Module":
        return ast.Module(dct["body"])
    elif dct['ast_type'] == "Interactive":
        return ast.Interactive(dct["body"])
    elif dct['ast_type'] == "Expression":
        return ast.Expression(dct["body"])
    elif dct['ast_type'] == "Suite":
        return ast.Suite(dct["body"])
    elif dct['ast_type'] == "FunctionDef":
        return ast.FunctionDef(dct["name"], dct["args"], dct["body"],
                               dct["decorator_list"])
    elif dct['ast_type'] == "ClassDef":
        return ast.ClassDef(dct["name"], dct["bases"], dct["body"],
                            dct["decorator_list"])
    elif dct['ast_type'] == "Return":
        return ast.Return(dct["value"])
    elif dct['ast_type'] == "Delete":
        return ast.Delete(dct["targets"])
    elif dct['ast_type'] == "Assign":
        return ast.Assign(dct["targets"], dct["value"])
    elif dct['ast_type'] == "AugAssign":
        return ast.AugAssign(dct["target"], dct["op"], dct["value"])
    elif dct['ast_type'] == "Print":
        return ast.Print(dct["dest"], dct["values"], dct["nl"])
    elif dct['ast_type'] == "For":
        return ast.For(dct["target"], dct["iter"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "While":
        return ast.While(dct["test"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "If":
        return ast.If(dct["test"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "With":
        return ast.With(dct["context_expr"], dct["optional_vars"], dct["body"])
    elif dct['ast_type'] == "Raise":
        return ast.Raise(dct["type"], dct["inst"], dct["tback"])
    elif dct['ast_type'] == "TryExcept":
        return ast.TryExcept(dct["body"], dct["handlers"], dct["orelse"])
    elif dct['ast_type'] == "TryFinally":
        return ast.TryFinally(dct["body"], dct["finalbody"])
    elif dct['ast_type'] == "Assert":
        return ast.Assert(dct["test"], dct["msg"])
    elif dct['ast_type'] == "Import":
        return ast.Import(dct["names"])
    elif dct['ast_type'] == "ImportFrom":
        return ast.ImportFrom(dct["module"], dct["names"], dct["level"])
    elif dct['ast_type'] == "Exec":
        return ast.Exec(dct["body"], dct["globals"], dct["locals"])
    elif dct['ast_type'] == "Global":
        return ast.Global(dct["names"])
    elif dct['ast_type'] == "Expr":
        return ast.Expr(dct["value"])
    elif dct['ast_type'] == "Pass":
        return ast.Pass()
    elif dct['ast_type'] == "Break":
        return ast.Break()
    elif dct['ast_type'] == "Continue":
        return ast.Continue()
    elif dct['ast_type'] == "BoolOp":
        return ast.BoolOp(dct["op"], dct["values"])
    elif dct['ast_type'] == "BinOp":
        return ast.BinOp(dct["left"], dct["op"], dct["right"])
    elif dct['ast_type'] == "UnaryOp":
        return ast.UnaryOp(dct["op"], dct["operand"])
    elif dct['ast_type'] == "Lambda":
        return ast.Lambda(dct["args"], dct["body"])
    elif dct['ast_type'] == "IfExp":
        return ast.IfExp(dct["test"], dct["body"], dct["orelse"])
    elif dct['ast_type'] == "Dict":
        return ast.Dict(dct["keys"], dct["values"])
    elif dct['ast_type'] == "Set":
        return ast.Set(dct["elts"])
    elif dct['ast_type'] == "ListComp":
        return ast.ListComp(dct["elt"], dct["generators"])
    elif dct['ast_type'] == "SetComp":
        return ast.SetComp(dct["elt"], dct["generators"])
    elif dct['ast_type'] == "DictComp":
        return ast.DictComp(dct["key"], dct["value"], dct["generators"])
    elif dct['ast_type'] == "GeneratorExp":
        return ast.GeneratorExp(dct["elt"], dct["generators"])
    elif dct['ast_type'] == "Yield":
        return ast.Yield(dct["value"])
    elif dct['ast_type'] == "Compare":
        return ast.Compare(dct["left"], dct["ops"], dct["comparators"])
    elif dct['ast_type'] == "Call":
        return ast.Call(dct["func"], dct["args"], dct["keywords"],
                        dct["starargs"], dct["kwargs"])
    elif dct['ast_type'] == "Repr":
        return ast.Repr(dct["value"])
    elif dct['ast_type'] == "Num":
        return ast.Num(dct["n"])
    elif dct['ast_type'] == "Str":
        # Converting to ASCII
        return ast.Str(dct["s"].encode('ascii', 'ignore'))
    elif dct['ast_type'] == "Attribute":
        return ast.Attribute(dct["value"], dct["attr"], dct["ctx"])
    elif dct['ast_type'] == "Subscript":
        return ast.Subscript(dct["value"], dct["slice"], dct["ctx"])
    elif dct['ast_type'] == "Name":
        return ast.Name(dct["id"], dct["ctx"])
    elif dct['ast_type'] == "List":
        return ast.List(dct["elts"], dct["ctx"])
    elif dct['ast_type'] == "Tuple":
        return ast.Tuple(dct["elts"], dct["ctx"])
    elif dct['ast_type'] == "Load":
        return ast.Load()
    elif dct['ast_type'] == "Store":
        return ast.Store()
    elif dct['ast_type'] == "Del":
        return ast.Del()
    elif dct['ast_type'] == "AugLoad":
        return ast.AugLoad()
    elif dct['ast_type'] == "AugStore":
        return ast.AugStore()
    elif dct['ast_type'] == "Param":
        return ast.Param()
    elif dct['ast_type'] == "Ellipsis":
        return ast.Ellipsis()
    elif dct['ast_type'] == "Slice":
        return ast.Slice(dct["lower"], dct["upper"], dct["step"])
    elif dct['ast_type'] == "ExtSlice":
        return ast.ExtSlice(dct["dims"])
    elif dct['ast_type'] == "Index":
        return ast.Index(dct["value"])
    elif dct['ast_type'] == "And":
        return ast.And()
    elif dct['ast_type'] == "Or":
        return ast.Or()
    elif dct['ast_type'] == "Add":
        return ast.Add()
    elif dct['ast_type'] == "Sub":
        return ast.Sub()
    elif dct['ast_type'] == "Mult":
        return ast.Mult()
    elif dct['ast_type'] == "Div":
        return ast.Div()
    elif dct['ast_type'] == "Mod":
        return ast.Mod()
    elif dct['ast_type'] == "Pow":
        return ast.Pow()
    elif dct['ast_type'] == "LShift":
        return ast.LShift()
    elif dct['ast_type'] == "RShift":
        return ast.RShift()
    elif dct['ast_type'] == "BitOr":
        return ast.BitOr()
    elif dct['ast_type'] == "BitXor":
        return ast.BitXor()
    elif dct['ast_type'] == "BitAnd":
        return ast.BitAnd()
    elif dct['ast_type'] == "FloorDiv":
        return ast.FloorDiv()
    elif dct['ast_type'] == "Invert":
        return ast.Invert()
    elif dct['ast_type'] == "Not":
        return ast.Not()
    elif dct['ast_type'] == "UAdd":
        return ast.UAdd()
    elif dct['ast_type'] == "USub":
        return ast.USub()
    elif dct['ast_type'] == "Eq":
        return ast.Eq()
    elif dct['ast_type'] == "NotEq":
        return ast.NotEq()
    elif dct['ast_type'] == "Lt":
        return ast.Lt()
    elif dct['ast_type'] == "LtE":
        return ast.LtE()
    elif dct['ast_type'] == "Gt":
        return ast.Gt()
    elif dct['ast_type'] == "GtE":
        return ast.GtE()
    elif dct['ast_type'] == "Is":
        return ast.Is()
    elif dct['ast_type'] == "IsNot":
        return ast.IsNot()
    elif dct['ast_type'] == "In":
        return ast.In()
    elif dct['ast_type'] == "NotIn":
        return ast.NotIn()
    elif dct['ast_type'] == "comprehension":
        return ast.comprehension(dct["target"], dct["iter"], dct["ifs"])
    elif dct['ast_type'] == "ExceptHandler":
        return ast.ExceptHandler(dct["type"], dct["name"], dct["body"])
    elif dct['ast_type'] == "arguments":
        return ast.arguments(dct["args"], dct["vararg"], dct["kwarg"],
                             dct["defaults"])
    elif dct['ast_type'] == "keyword":
        return ast.keyword(dct["arg"], dct["value"])
    elif dct['ast_type'] == "alias":
        return ast.alias(dct["name"], dct["asname"])
    else:
        return dct
Ejemplo n.º 29
0
    def test_empty_init(self):
        # Jython 2.5.0 did not allow empty constructors for many ast node types
        # but CPython ast nodes do allow this.  For the moment, I don't see a
        # reason to allow construction of the super types (like ast.AST and
        # ast.stmt) as well as the op types that are implemented as enums in
        # Jython (like boolop), but I've left them in but commented out for
        # now.  We may need them in the future since CPython allows this, but
        # it may fall under implementation detail.

        #ast.AST()
        ast.Add()
        ast.And()
        ast.Assert()
        ast.Assign()
        ast.Attribute()
        ast.AugAssign()
        ast.AugLoad()
        ast.AugStore()
        ast.BinOp()
        ast.BitAnd()
        ast.BitOr()
        ast.BitXor()
        ast.BoolOp()
        ast.Break()
        ast.Call()
        ast.ClassDef()
        ast.Compare()
        ast.Continue()
        ast.Del()
        ast.Delete()
        ast.Dict()
        ast.Div()
        ast.Ellipsis()
        ast.Eq()
        ast.Exec()
        ast.Expr()
        ast.Expression()
        ast.ExtSlice()
        ast.FloorDiv()
        ast.For()
        ast.FunctionDef()
        ast.GeneratorExp()
        ast.Global()
        ast.Gt()
        ast.GtE()
        ast.If()
        ast.IfExp()
        ast.Import()
        ast.ImportFrom()
        ast.In()
        ast.Index()
        ast.Interactive()
        ast.Invert()
        ast.Is()
        ast.IsNot()
        ast.LShift()
        ast.Lambda()
        ast.List()
        ast.ListComp()
        ast.Load()
        ast.Lt()
        ast.LtE()
        ast.Mod()
        ast.Module()
        ast.Mult()
        ast.Name()
        ast.Not()
        ast.NotEq()
        ast.NotIn()
        ast.Num()
        ast.Or()
        ast.Param()
        ast.Pass()
        ast.Pow()
        ast.Print()
        ast.RShift()
        ast.Raise()
        ast.Repr()
        ast.Return()
        ast.Slice()
        ast.Store()
        ast.Str()
        ast.Sub()
        ast.Subscript()
        ast.Suite()
        ast.TryExcept()
        ast.TryFinally()
        ast.Tuple()
        ast.UAdd()
        ast.USub()
        ast.UnaryOp()
        ast.While()
        ast.With()
        ast.Yield()
        ast.alias()
        ast.arguments()
        #ast.boolop()
        #ast.cmpop()
        ast.comprehension()
        #ast.excepthandler()
        #ast.expr()
        #ast.expr_context()
        ast.keyword()