From: Baptiste L. <bl...@us...> - 2004-08-08 14:50:53
|
Update of /cvsroot/cpptool/CppParser/src/pyrfta In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30262/src/pyrfta Modified Files: ast.py dumpast.py nodetools.py Log Message: * enhanced simple declaration wrapping, now outputting declarator id Index: nodetools.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/nodetools.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** nodetools.py 8 Aug 2004 11:52:40 -0000 1.2 --- nodetools.py 8 Aug 2004 14:50:29 -0000 1.3 *************** *** 11,20 **** return node.childAt(0) ! def getFirstChildNamed( node, name, exception_if_not_found = True ): for child in node.enumChildren(): if child.name == name: return child - if exception_if_not_found: - raise LogicError( "Fail to find children named '%s' in node %s" % (name,node) ) return None --- 11,24 ---- return node.childAt(0) ! def getFirstChildNamed( node, name ): ! for child in node.enumChildren(): ! if child.name == name: ! return child ! raise LogicError( "Fail to find children named '%s' in node %s" % (name,node) ) ! ! def tryGetFirstChildNamed( node, name ): for child in node.enumChildren(): if child.name == name: return child return None *************** *** 37,38 **** --- 41,48 ---- raise LogicError( "Fail to find string child in node %s" % node) return None + + def getFirstTokenChildNode( node ): + for child in node.enumChildren(): + if child.hasValidToken(): + return child + raise LogicError( "Fail to find a child node with token in %s." % node ) \ No newline at end of file Index: ast.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/ast.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ast.py 8 Aug 2004 11:52:40 -0000 1.3 --- ast.py 8 Aug 2004 14:50:29 -0000 1.4 *************** *** 23,26 **** --- 23,29 ---- self.node = node + class ErrorDeclaration(Declaration): + pass + class NamespaceAliasDef(Declaration): def __init__( self, namespace_node ): *************** *** 68,75 **** self.declarator_node = nodetools.getFirstChildNamed( init_declarator_node, 'declarator' ) self.initializer = None ! assign_initializer_node = nodetools.getFirstChildNamed( init_declarator_node, 'assign_initializer', False ) if assign_initializer_node: self.initializer = AssignInitializer( assign_initializer_node ) ! expression_list_node = nodetools.getFirstChildNamed( init_declarator_node, 'expression_list', False ) if expression_list_node: self.initializer = ConstructorInitializer( expression_list_node ) --- 71,78 ---- self.declarator_node = nodetools.getFirstChildNamed( init_declarator_node, 'declarator' ) self.initializer = None ! assign_initializer_node = nodetools.tryGetFirstChildNamed( init_declarator_node, 'assign_initializer' ) if assign_initializer_node: self.initializer = AssignInitializer( assign_initializer_node ) ! expression_list_node = nodetools.tryGetFirstChildNamed( init_declarator_node, 'expression_list' ) if expression_list_node: self.initializer = ConstructorInitializer( expression_list_node ) *************** *** 94,104 **** class StandardDeclaration(SimpleDeclaration): def setDeclarationSpecifierNode( self, declaration_specifier_node ): ! self.storage_class_node = nodetools.getFirstChildNamed( declaration_specifier_node, 'storage_class_specifier', False ) ! self.function_spec_node = nodetools.getFirstChildNamed( declaration_specifier_node, 'function_specifier', False ) # self.type_specifier_node = nodetools.getFirstChildNamed( declaration_specifier_node, 'type_specifier' ) class FriendDeclaration(StandardDeclaration): pass # same as StandardDeclaration class TypeSpecifier(ASTElement): def __init__( self, type_specifier_node, type ): --- 97,127 ---- class StandardDeclaration(SimpleDeclaration): def setDeclarationSpecifierNode( self, declaration_specifier_node ): ! self.storage_class_node = nodetools.tryGetFirstChildNamed( declaration_specifier_node, 'storage_class_specifier' ) ! self.function_spec_node = nodetools.tryGetFirstChildNamed( declaration_specifier_node, 'function_specifier' ) # self.type_specifier_node = nodetools.getFirstChildNamed( declaration_specifier_node, 'type_specifier' ) + self.declarators = [] + + def addDeclarator( self, declarator ): + self.declarators.append( declarator ) class FriendDeclaration(StandardDeclaration): pass # same as StandardDeclaration + class Declarator(ASTElement): + def __init__( self, node ): + self.node = node + self.pointer_operators = [] + self.declarator_id = None + + def addPointerOperator( self, pointer_operator ): + self.pointer_operators.append( pointer_operator ) + + def setDeclaratorId( self, id ): + self.declarator_id = id + + class DeclaratorId(ASTElement): + def __init__( self, node ): + self.node = node + class TypeSpecifier(ASTElement): def __init__( self, type_specifier_node, type ): *************** *** 212,215 **** --- 235,242 ---- Type.__init__( self, node ) + class DefaultFunctionReturnType(Type): # int + def __init__( self, node ): + Type.__init__( self, node ) + ## ################################################################## ## ################################################################## *************** *** 280,284 **** class TryBlockStatement(Statement): ! pass class ASTBuilder: --- 307,323 ---- class TryBlockStatement(Statement): ! def __init__( self, node, body ): ! Statement.__init__( self, node ) ! self.body = body ! self.handlers = [] ! ! def addHandler( self, handler ): ! self.handlers.append( handler ) ! ! class ExceptionHandler(ASTElement): # catch ! def __init__( self, node, body ): ! self.node = node ! self.body = body ! # catch exception is missing, need more structure in input class ASTBuilder: *************** *** 293,297 **** 'namespace_alias_def' : self.buildNamespaceAliasDef, 'using_declaration' : self.buildUsingDeclaration, ! 'using_directive' : self.buildUsingDirective } self.type_builders = { --- 332,337 ---- 'namespace_alias_def' : self.buildNamespaceAliasDef, 'using_declaration' : self.buildUsingDeclaration, ! 'using_directive' : self.buildUsingDirective, ! 'declaration_error' : self.buildErrorDeclaration } self.type_builders = { *************** *** 344,347 **** --- 384,391 ---- ast.addDeclaration( self.declaration_builders[ declaration.name ]( declaration ) ) return ast + + def buildErrorDeclaration( self, declaration ): + ast = ErrorDeclaration( declaration ) + return ast def buildNamedNamespaceDef( self, declaration ): *************** *** 366,370 **** def buildLinkageSpecification( self, declaration ): ! declarations = nodetools.getFirstChildNamed( declaration, 'declarations', False ) if declarations is None: ast = LinkageSpecification( declaration ) # how to I build the child declaration ??? --- 410,414 ---- def buildLinkageSpecification( self, declaration ): ! declarations = nodetools.tryGetFirstChildNamed( declaration, 'declarations' ) if declarations is None: ast = LinkageSpecification( declaration ) # how to I build the child declaration ??? *************** *** 400,405 **** --- 444,469 ---- type_specifier_node = nodetools.getFirstChildNamed( declaration_specifier, 'type_specifier' ) ast.type_specifier = self.buildTypeSpecifier( type_specifier_node ) + + init_declarators = nodetools.tryGetFirstChildNamed( declaration, 'init_declarators' ) + if init_declarators: + for init_declarator in nodetools.getChildrenNamed( init_declarators, 'init_declarator' ): + declarator_node = nodetools.getFirstChildNamed( init_declarator, 'declarator' ) + declarator = Declarator( declarator_node ) + self.buildDeclarator( declarator, declarator_node ) + ast.addDeclarator( declarator ) return ast + def buildDeclarator( self, declarator, declarator_node ): + ptr_operator_declarator_node = nodetools.tryGetFirstChildNamed( declarator_node, 'ptr_operator_declarator' ) + if ptr_operator_declarator_node: + declarator.addPointerOperator( PointerOperator( ptr_operator_declarator_node ) ) + + braced_declarator_node = nodetools.tryGetFirstChildNamed( declarator_node, 'braced_declarator' ) + if braced_declarator_node: + self.buildDeclarator( declarator, braced_declarator_node ) + else: + declarator_id_node = nodetools.getFirstChildNamed( declarator_node, 'declarator_id' ) + declarator.setDeclaratorId( DeclaratorId( declarator_id_node ) ) + def buildTypeSpecifier( self, type_specifier ): index = 0 *************** *** 430,434 **** def buildClassBases( self, ast, type_node ): ! class_bases_node = nodetools.getFirstChildNamed( type_node, 'class_bases', False ) if class_bases_node: for child in nodetools.getChildrenNamed( class_bases_node, 'base_specifier' ): --- 494,498 ---- def buildClassBases( self, ast, type_node ): ! class_bases_node = nodetools.tryGetFirstChildNamed( type_node, 'class_bases' ) if class_bases_node: for child in nodetools.getChildrenNamed( class_bases_node, 'base_specifier' ): *************** *** 436,440 **** def buildClassMembers( self, ast, type_node ): ! members_node = nodetools.getFirstChildNamed( type_node, 'member_specification', False ) if members_node: for child in members_node.enumChildren(): --- 500,504 ---- def buildClassMembers( self, ast, type_node ): ! members_node = nodetools.tryGetFirstChildNamed( type_node, 'member_specification' ) if members_node: for child in members_node.enumChildren(): *************** *** 470,476 **** def buildFunctionDefinition( self, declaration ): ast = FunctionDefinition( declaration ) ! function_return_type_node = nodetools.getFirstChildNamed( declaration, 'function_return_type' ) if function_return_type_node: ast.setReturnType( FunctionReturnType(function_return_type_node) ) ## 'declarator_id' --- 534,542 ---- def buildFunctionDefinition( self, declaration ): ast = FunctionDefinition( declaration ) ! function_return_type_node = nodetools.tryGetFirstChildNamed( declaration, 'function_return_type' ) if function_return_type_node: ast.setReturnType( FunctionReturnType(function_return_type_node) ) + else: + ast.setReturnType( DefaultFunctionReturnType( declaration ) ) ## 'declarator_id' *************** *** 551,555 **** def buildTryBlockStatement( self, statement ): ! return TryBlockStatement( statement ) --- 617,627 ---- def buildTryBlockStatement( self, statement ): ! body = self.buildCompoundStatement( nodetools.getFirstChildNamed(statement, 'compound_statement') ) ! ast = TryBlockStatement( statement, body ) ! for child in nodetools.getChildrenNamed( statement, 'exception_handler' ): ! body_node = nodetools.getFirstChildNamed( child, 'compound_statement' ) ! handler = ExceptionHandler( child, self.buildCompoundStatement( body_node ) ) ! ast.addHandler( handler ) ! return ast Index: dumpast.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/dumpast.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** dumpast.py 8 Aug 2004 11:52:40 -0000 1.2 --- dumpast.py 8 Aug 2004 14:50:29 -0000 1.3 *************** *** 2,5 **** --- 2,6 ---- import pycppparser as cprs import ast + import nodetools def _getClassName( obj ): *************** *** 8,29 **** class ASTInfo: ! def __init__( self, name_or_ast = None ): ! self.__attributes = {} ! self.__children = [] if type(name_or_ast) == str: ! self.__name = name_or_ast else: ! self.__name = _getClassName( ast ) def name( self ): ! return self.__name def attr( self, name, value = None ): ! return self.__attributes.setdefault( name, value ) def attributes( self ): ! return self.__attributes.items() class ASTInfoBuilder: def visitASTNode( self, ast ): visit_method = 'visit' + _getClassName(ast) --- 9,33 ---- class ASTInfo: ! def __init__( self, name_or_ast ): ! self._attributes = {} ! self._children = [] if type(name_or_ast) == str: ! self._name = name_or_ast else: ! self._name = _getClassName( name_or_ast ) def name( self ): ! return self._name def attr( self, name, value = None ): ! return self._attributes.setdefault( name, value ) def attributes( self ): ! return self._attributes.items() class ASTInfoBuilder: + def __init__( self, source ): + self.source = source + def visitASTNode( self, ast ): visit_method = 'visit' + _getClassName(ast) *************** *** 44,49 **** --- 48,139 ---- name = (ast.body and 'FunctionDefinition') or 'FunctionPrototype' info = ASTInfo( name ) + info.attr( 'return_type', self.visitASTNode( ast.return_type ) ) + if ast.body: + info.attr( 'body', self.visitASTNode( ast.body ) ) + return info + + def visitFunctionReturnType( self, ast ): + info = ASTInfo( ast ) + return info + + def visitDefaultFunctionReturnType( self, ast ): + info = ASTInfo( ast ) + return info + + def visitErrorDeclaration( self, ast ): + info = ASTInfo( ast ) + location = nodetools.getFirstTokenChildNode( ast.node ).location + info.attr( 'location', 'Line: %d, column: %d' % self._location(location) ) + text = '' + for node in ast.node.enumChildren(): + text += node.text + info.attr( 'text', text ) + return info + + def _location( self, location ): + previousEOL = self.source.rfind( '\n', 0, location.startPos ) + 1 + line = self.source.count( '\n', 0, previousEOL ) + column = location.startPos - previousEOL + return (line+1,column+1) + + def _nodeTreeSource( self, node ): + source = '' + if node.hasValidToken(): + source += node.text + for child in node.enumChildren(): + source += self._nodeTreeSource( child ) + return source + + def visitStandardDeclaration( self, ast ): + info = ASTInfo( ast ) + info.attr( 'declarators', [] ) + for declarator in ast.declarators: + info.attr( 'declarators' ).append( self.visitASTNode( declarator ) ) + return info + + def visitDeclarator( self, ast ): + info = ASTInfo( ast ) + # + ptr operators + info.attr( 'declator_id', self._nodeTreeSource( ast.declarator_id.node ) ) + return info + + ## Statements + def visitCompoundStatement( self, ast ): + info = ASTInfo( ast ) + info.attr( 'statements', [] ) + for statement in ast.statements: + info.attr( 'statements' ).append( self.visitASTNode( statement ) ) return info + def visitExpressionStatement( self, ast ): + info = ASTInfo( ast ) + return info + + def visitForStatement( self, ast ): + info = ASTInfo( ast ) + return info + + def visitIfStatement( self, ast ): + info = ASTInfo( ast ) + return info + + def visitTryBlockStatement( self, ast ): + info = ASTInfo( ast ) + info.attr( 'body', self.visitASTNode( ast.body ) ) + info.attr( 'handlers', [] ) + for handler in ast.handlers: + info.attr( 'handlers' ).append( self.visitASTNode( handler ) ) + return info + + def visitExceptionHandler( self, ast ): + info = ASTInfo( ast ) + info.attr( 'body', self.visitASTNode( ast.body ) ) + return info + + def visitDeclarationStatement( self, ast ): + info = ASTInfo( ast ) + info.attr( 'declaration', self.visitASTNode( ast.declaration ) ) + return info + *************** *** 54,58 **** attribute_margin = margin + '|-' for name, value in info.attributes(): ! if type(value) == ASTInfo: printASTInfo( value, indent ) elif type(value) == type([]): --- 144,148 ---- attribute_margin = margin + '|-' for name, value in info.attributes(): ! if isinstance(value, ASTInfo): printASTInfo( value, indent ) elif type(value) == type([]): *************** *** 81,84 **** ast = ast.ASTBuilder().buildTranslationUnit( translation_unit_node ) ! info = ASTInfoBuilder().visitTranslationUnit( ast ) printASTInfo( info ) \ No newline at end of file --- 171,174 ---- ast = ast.ASTBuilder().buildTranslationUnit( translation_unit_node ) ! info = ASTInfoBuilder( source ).visitTranslationUnit( ast ) printASTInfo( info ) \ No newline at end of file |