コード例 #1
0
ファイル: ast.py プロジェクト: holdenli/CS-444
def build_constructors(node):
    constructors = ASTNode('Constructors')
    for cons_decl in node.children:
        cons = ASTNode('ConstructorDeclaration')
        if hasattr(cons_decl, 'decl_order'):
            cons.decl_order = cons_decl.decl_order
        else:
            cons.decl_order = -1

        # Extract modifiers.
        cons.add(flatten_leaves(cons_decl[0], 'Modifiers'))

        # Extract name.
        cons.add(cons_decl[1][0][0]) # Name

        # Extract parameters.
        if cons_decl[1][2].name == 'FormalParameterList':
            cons.add(build_parameters(cons_decl[1][2]))
        else:
            cons.add(ASTNode('Parameters'))

        # Extract body.
        cons.add(build_block(cons_decl[2]))

        constructors.add(cons)

    return constructors
コード例 #2
0
ファイル: ast.py プロジェクト: holdenli/CS-444
def build_fields(node):
    fields = ASTNode('Fields')
    for field_decl in node.children:
        field = ASTNode('FieldDeclaration')
        if hasattr(field_decl, 'decl_order'):
            field.decl_order = field_decl.decl_order
        else:
            field.decl_order = -1

        # Extract modifiers.
        field.add(flatten_leaves(field_decl[0], 'Modifiers'))

        # Extract type.
        field.add(build_type(field_decl[1]))

        # Extract name.
        field.add(field_decl[2][0][0][0])

        # Extract initializer.
        var_declr = field_decl[2][0] # VariableDeclarator
        if len(var_declr.children) > 1:
            initializer = build_expr(var_declr[2][0])
            field.add(ASTNode('Initializer', None, [initializer]))
        else:
            field.add(ASTNode('Initializer')) # No initializer

        fields.add(field)

    return fields
コード例 #3
0
ファイル: ast.py プロジェクト: holdenli/CS-444
def build_methods(node):
    methods = ASTNode('Methods')
    for method_decl in node.children:
        method = ASTNode('MethodDeclaration')
        if hasattr(method_decl, 'decl_order'):
            method.decl_order = method_decl.decl_order
        else:
            method.decl_order = -1

        # Extract modifiers.
        method.add(flatten_leaves(method_decl[0][0], 'Modifiers'))

        # Extract return type.
        if method_decl[0][1].name == 'Void':
            method.add(ASTNode('Type', None, [method_decl[0][1]]))
        else:
            method.add(build_type(method_decl[0][1])) # Non-void.

        # Name.
        method.add(method_decl[0][2][0])
 
        # Extract parameters.
        if method_decl[0][2][2].name == 'FormalParameterList':
            method.add(build_parameters(method_decl[0][2][2]))
        else:
            method.add(ASTNode('Parameters'))

        # Extract body.
        # We have two levels, for differentiating between:
        # 1. Methods with no body (i.e., abstract)
        # 2. Methods with an empty body (i.e., {})
        # 3. Methods with a nonempty body
        body = ASTNode('MethodBody')
        if method_decl[1].name != 'SemiColon' and \
                method_decl[1][0].name != 'SemiColon':
            body.add(build_block(method_decl[1][0]))
        method.add(body)

        methods.add(method)

    return methods