def __init__(self, tree): self.children = [[], []] # self.children is of length 2: # 0. List of length 1 or 2. # If length 1, it is just an ASTIdentifiers. # If length 2, the first is an arbitrary expression and the second is an # identifier (a field access of the first). # e.g.: # (i.j).k => [Expression for "(i.j)", Expression for "k"] # 1. List of argument expressions (possibly empty) if tree.children[0].value == 'Identifiers': self.children[0] = [ASTExpression.get_expr_node(tree.children[0])] if tree.children[2].value == 'ArgumentList': self.children[1] = ASTUtils.get_arg_list(tree.children[2]) else: prefix_exprs = [ASTExpression.get_expr_node(tree.children[0]), ASTExpression.get_expr_node(tree.children[2])] arg_list = [] if tree.children[4].value == 'ArgumentList': arg_list = ASTUtils.get_arg_list(tree.children[4]) self.children = [prefix_exprs, arg_list] super(ASTMethodInvocation, self).__init__()
def show(self, depth = 0, types = False): ASTUtils.println('ASTBinary, operator: {0} {1}'.format(self.operator, ASTUtils.type_string(self.expr_type, types)), depth) ASTUtils.println('Left operand:', depth) self.children[0].show(depth + 1, types) ASTUtils.println('Right operand:', depth) self.children[1].show(depth + 1, types)
def show(self, depth = 0, types = False): ASTUtils.println('ASTClassInstanceCreation {0}'.format( ASTUtils.type_string(self.expr_type)), depth) ASTUtils.println('Class type:', depth) self.children[0].show(depth + 1, types) for i, x in enumerate(self.children[1]): ASTUtils.println('Argument {0}:'.format(str(i)), depth) x.show(depth + 1, types)
def __init__(self, tree): # Children is of length 2: # 0. ASTType corresponding to class type. # 1. List of arguments (possibly empty). self.children = [ASTType(tree.children[1].children[0]), []] if tree.children[3].value == 'ArgumentList': self.children[1] = ASTUtils.get_arg_list(tree.children[3]) super(ASTClassInstanceCreation, self).__init__()
def __init__(self, tree): if isinstance(tree, str): # Allow creating an ASTIdentifiers node directly from a string. self.children = tree.split('.') elif tree.value == 'Identifier': self.children = [tree.lexeme] else: self.children = ASTUtils.get_ids_list(tree) self.first_definition = (None, None) super(ASTIdentifiers, self).__init__()
def show(self, depth = 0, types = False): ASTUtils.println('ASTIdentifiers: {0} {1}'.format(str(self), ASTUtils.type_string(self.expr_type, types)), depth)
def show(self, depth = 0, types = False): ASTUtils.println( 'ASTInstanceOf Type: {0} {1}'.format(str(self.type_node), ASTUtils.type_string(self.expr_type)), depth) self.children[0].show(depth + 1, types)
def show(self, depth = 0, types = False): ASTUtils.println('ASTMethodInvocation {0}'.format( ASTUtils.type_string(self.expr_type, types)), depth) if len(self.children[0]) == 1: ASTUtils.println('Method identifiers:', depth) self.children[0][0].show(depth + 1, types=False) else: ASTUtils.println('Expression:', depth) self.children[0][0].show(depth + 1, types) ASTUtils.println('Field access from expression:', depth) self.children[0][1].show(depth + 1, types=False) for i, x in enumerate(self.children[1]): ASTUtils.println('Argument {0}:'.format(str(i)), depth) x.show(depth + 1, types)
def show(self, depth = 0, types = False): ASTUtils.println('ASTThis {0}'.format(ASTUtils.type_string( self.expr_type)), depth)
def get_expr_node(tree): ''' Given a *Expression node, returns the appropriate ASTNode. Can also take a Primary[NoNewArray] node and return the appropriate ASTNode. ''' # TODO(mehdi): Is this Primary case even needed? See if they can fall into # the general case. # Handle the case where tree is a Primary[NoNewArray] node. if tree.value == 'Primary' or tree.value == 'PrimaryNoNewArray': # Before going down one node, handle the only case where there are # multiple children, the "( Expression )" case. if len(tree.children) == 3 and tree.children[0].value == '(': return ASTExpression.get_expr_node(tree.children[1]) child = tree.children[0] if child.value == 'ArrayCreationExpression': return ASTArrayCreation(child) elif child.value == 'PrimaryNoNewArray': child = child.children[0] if child.value == 'Literal': return ASTLiteral(child) elif child.value == 'this': return ASTThis(child) elif child.value == 'ClassInstanceCreationExpression': return ASTClassInstanceCreation(child) elif child.value == 'FieldAccess': return ASTFieldAccess(child) elif child.value == 'MethodInvocation': return ASTMethodInvocation(child) elif child.value == 'ArrayAccess': return ASTArrayAccess(child) elif child.value == 'Identifiers' or child.value == 'Identifier': return ASTIdentifiers(child) # Default to the general case where the tree is a long Expression parse tree # that widens based on the type of expression. child = ASTUtils.get_nonpath_child(tree) if child.value == 'Literal': return ASTLiteral(child) elif (child.value == 'UnaryExpression' or child.value == 'UnaryExpressionNotPlusMinus'): return ASTUnary(child) elif child.value == 'CastExpression': return ASTCast(child) elif child.value == 'Assignment': return ASTAssignment(child) elif child.value == 'ArrayAccess': return ASTArrayAccess(child) elif child.value == 'this': return ASTThis(child) elif child.value == 'super': return ASTSuper(child) elif child.value == 'FieldAccess': return ASTFieldAccess(child) elif child.value == 'MethodInvocation': return ASTMethodInvocation(child) elif child.value == 'ArrayCreationExpression': return ASTArrayCreation(child) elif child.value == 'ClassInstanceCreationExpression': return ASTClassInstanceCreation(child) elif child.value == 'PrimaryNoNewArray': return ASTExpression.get_expr_node(child.children[1]) elif child.value == 'Identifiers' or child.value == 'Identifier': return ASTIdentifiers(child) else: possible_binary_rules = [ 'ConditionalOrExpression', 'ConditionalAndExpression', 'InclusiveOrExpression', 'ExclusiveOrExpression', 'AndExpression', 'EqualityExpression', 'RelationalExpression', 'AdditiveExpression', 'MultiplicativeExpression'] # If it's not one of the possible binary types, we're in trouble... if child.value not in possible_binary_rules: raise ASTExpressionError( 'Invalid binary expression type "{0}"'.format(child.value)) return make_ast_binary_node(child) raise ASTExpressionError('Unhandled Expression')
def show(self, depth = 0, types = False): ASTUtils.println( 'Literal of type {0}: {1} {2}'.format(self.literal_type, self.children[0], ASTUtils.type_string(self.expr_type, types)), depth)
def show(self, depth = 0, types = False): ASTUtils.println( 'ASTFieldAccess {0}'.format(ASTUtils.type_string(self.expr_type, types)), depth) self.children[0].show(depth + 1, types) self.children[1].show(depth + 1, False)