From: Baptiste L. <bl...@us...> - 2004-08-08 21:41:26
|
Update of /cvsroot/cpptool/CppParser/src/pyrfta In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27348/src/pyrfta Modified Files: ast.py dumpast.py Log Message: * added a few more expressions Index: ast.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/ast.py,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** ast.py 8 Aug 2004 20:28:07 -0000 1.10 --- ast.py 8 Aug 2004 21:41:17 -0000 1.11 *************** *** 55,60 **** class Initializer(ASTElement): ! def __init( self, node ): self.node = node class AssignInitializer(Initializer): --- 55,61 ---- class Initializer(ASTElement): ! def __init__( self, node ): self.node = node + self.expression = None class AssignInitializer(Initializer): *************** *** 71,80 **** 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 ) class SimpleDeclaration(Declaration): --- 72,75 ---- *************** *** 120,123 **** --- 115,122 ---- self.declarator_id = id + class PointerOperator(ASTElement): + def __init__( self, node ): + self.node = node + class DeclaratorId(ASTElement): def __init__( self, node ): *************** *** 360,364 **** class ReturnStatement(Statement): ! pass class GotoStatement(Statement): --- 359,365 ---- class ReturnStatement(Statement): ! def __init__( self, node ): ! Statement.__init__( self, node ) ! self.expression = None class GotoStatement(Statement): *************** *** 423,426 **** --- 424,431 ---- self.id_node = None + class CastExpression(ExpressionBase): + def __init__( self, node ): + ExpressionBase.__init__( self, node ) + class PostIncDecExpression(ExpressionBase): def __init__( self, node ): *************** *** 469,472 **** --- 474,482 ---- self.index = None + class BracedExpression(ExpressionBase): + def __init__( self, node ): + ExpressionBase.__init__( self, node ) + self.expression = None + class NewOperatorExpression(ExpressionBase): def __init__( self, node ): *************** *** 475,478 **** --- 485,497 ---- self.new_type_id_node = None + class SizeOfExpression(ExpressionBase): + def __init__( self, node ): + ExpressionBase.__init__( self, node ) + self.expression = None + + class ThisRefExpression(ExpressionBase): + def __init__( self, node ): + ExpressionBase.__init__( self, node ) + class UnaryOperatorExpression(ExpressionBase): def __init__( self, node ): *************** *** 521,524 **** --- 540,550 ---- return ErrorStatement( statement ) + def buildCaseStatement( self, statement ): + ast = CaseStatement( statement ) + value_node = nodetools.getFirstChildNamed( statement, 'constant_expression' ) + ast.setValue( self.buildConstantExpression( value_node ) ) + self.buildTargetStatement( ast, statement ) + return ast + def buildCompoundStatement( self, statement ): ast = CompoundStatement( statement ) *************** *** 529,549 **** return ast - def buildLabelStatement( self, statement ): - return self.buildTargetStatement( LabelStatement( statement ), statement ) - - def buildCaseStatement( self, statement ): - ast = CaseStatement( statement ) - value_node = nodetools.getFirstChildNamed( statement, 'constant_expression' ) - ast.setValue( self.buildConstantExpression( value_node ) ) - self.buildTargetStatement( ast, statement ) - return ast - def buildDefaultStatement( self, statement ): return self.buildTargetStatement( DefaultStatement( statement ), statement ) ! def buildTargetStatement( self, ast, label_statement_node ): ! target_statement_node = nodetools.getFirstChildNamed( label_statement_node, 'target_statement' ) ! ast.setStatement( self.buildStatement( nodetools.getFirstChild( target_statement_node ) ) ) ! return ast def buildIfStatement( self, statement ): --- 555,566 ---- return ast def buildDefaultStatement( self, statement ): return self.buildTargetStatement( DefaultStatement( statement ), statement ) ! def buildDoWhileStatement( self, statement ): ! return DoWhileStatement( statement ) ! ! def buildLabelStatement( self, statement ): ! return self.buildTargetStatement( LabelStatement( statement ), statement ) def buildIfStatement( self, statement ): *************** *** 566,575 **** return ast def buildWhileStatement( self, statement ): return WhileStatement( statement ) - def buildDoWhileStatement( self, statement ): - return DoWhileStatement( statement ) - def buildForStatement( self, statement ): ast = ForStatement( statement ) --- 583,594 ---- return ast + def buildTargetStatement( self, ast, label_statement_node ): + target_statement_node = nodetools.getFirstChildNamed( label_statement_node, 'target_statement' ) + ast.setStatement( self.buildStatement( nodetools.getFirstChild( target_statement_node ) ) ) + return ast + def buildWhileStatement( self, statement ): return WhileStatement( statement ) def buildForStatement( self, statement ): ast = ForStatement( statement ) *************** *** 622,626 **** def buildReturnStatement( self, statement ): ! return ReturnStatement( statement ) def buildGotoStatement( self, statement ): --- 641,649 ---- def buildReturnStatement( self, statement ): ! ast = ReturnStatement( statement ) ! expression_node = nodetools.tryGetFirstChildNamed( statement, 'expression' ) ! if expression_node: ! ast.expression = self.dispatchBuildExpression( expression_node ) ! return ast def buildGotoStatement( self, statement ): *************** *** 676,680 **** 'unary_operator' : self.buildUnaryOperatorExpression, 'assignment_operator' : self.buildAssignmentOperatorExpression, ! 'new_operator' : self.buildNewOperatorExpression } --- 699,708 ---- 'unary_operator' : self.buildUnaryOperatorExpression, 'assignment_operator' : self.buildAssignmentOperatorExpression, ! 'new_operator' : self.buildNewOperatorExpression, ! 'this_ref' : self.buildThisRef, ! 'sizeof_operator' : self.buildSizeOfExpression, ! 'braced_expr' : self.buildBracedExpression, ! 'expression' : self.buildExpression, ! 'cast_expression' : self.buildCastExpression } *************** *** 715,718 **** --- 743,751 ---- return ast + def buildBracedExpression( self, expression ): + ast = BracedExpression( expression ) + ast.expression = self.dispatchBuildExpression( nodetools.getFirstCompositeChild( expression ) ) + return ast + def buildCallOrConversionExpression( self, expression ): parameters_node = nodetools.tryGetFirstChildNamed( expression, 'expression_list' ) *************** *** 734,737 **** --- 767,775 ---- return ast + def buildCastExpression( self, expression ): + ast = CastExpression( expression ) + #todo + return ast + def buildConstantExpression( self, expression ): return self.dispatchBuildExpression( nodetools.getFirstChild( expression ) ) *************** *** 775,778 **** --- 813,824 ---- return ast + def buildSizeOfExpression( self, expression ): + ast = SizeOfExpression( expression ) + ast.expression = self.dispatchBuildExpression( nodetools.getFirstCompositeChild( expression ) ) + return ast + + def buildThisRef( self, expression ): + return ThisRefExpression( expression ) + def buildUnaryOperatorExpression( self, expression ): ast = UnaryOperatorExpression( expression ) *************** *** 892,895 **** --- 938,956 ---- declarator = Declarator( declarator_node ) self.buildDeclarator( declarator, declarator_node ) + + assign_node = nodetools.tryGetFirstChildNamed( init_declarator, 'assign_initializer' ) + constructor_node = nodetools.tryGetFirstChildNamed( init_declarator, 'expression_list' ) + if assign_node: + initializer = AssignInitializer( assign_node ) + value = self.dispatchBuildExpression( nodetools.tryGetFirstChildNamed( assign_node, + 'assignment_expression' ) ) + initializer.expression = value + elif constructor_node: + initializer = ConstructorInitializer( constructor_node ) + initializer.expression = self.dispatchBuildExpression( constructor_node ) + else: # we ignore array & structure initializer for the time being + initializer = None + declarator.initializer = initializer + ast.addDeclarator( declarator ) return ast Index: dumpast.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/dumpast.py,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** dumpast.py 8 Aug 2004 20:28:07 -0000 1.8 --- dumpast.py 8 Aug 2004 21:41:17 -0000 1.9 *************** *** 32,35 **** --- 32,57 ---- def __init__( self, source ): self.source = source + + 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 _nodeTreeIdentifier( self, node ): + source = '' + if node.hasValidToken() and not node.token.isSilent: + source += node.text + for child in node.enumChildren(): + source += self._nodeTreeIdentifier( child ) + return source def visitASTNode( self, ast ): *************** *** 38,41 **** --- 60,73 ---- return getattr( self, visit_method )( ast ) + def visitAssignInitializer( self, ast ): + info = ASTInfo( ast ) + info.attr( 'expression', self.visitASTNode( ast.expression ) ) + return info + + def visitConstructorInitializer( self, ast ): + info = ASTInfo( ast ) + info.attr( 'expression', self.visitASTNode( ast.expression ) ) + return info + def visitTranslationUnit( self, ast ): info = ASTInfo( ast ) *************** *** 47,61 **** info.attr('declarations').append( self.visitASTNode( declaration ) ) return info - - def visitFunctionDefinition( self, ast ): - 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 --- 79,89 ---- info.attr('declarations').append( self.visitASTNode( declaration ) ) return info ! def visitDeclarator( self, ast ): info = ASTInfo( ast ) + # + ptr operators + info.attr( 'declator_id', self._nodeTreeIdentifier( ast.declarator_id.node ) ) + if ast.initializer: + info.attr( 'initializer', self.visitASTNode( ast.initializer ) ) return info *************** *** 73,98 **** 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 _nodeTreeIdentifier( self, node ): ! source = '' ! if node.hasValidToken() and not node.token.isSilent: ! source += node.text ! for child in node.enumChildren(): ! source += self._nodeTreeIdentifier( child ) ! return source def visitStandardDeclaration( self, ast ): --- 101,116 ---- info.attr( 'text', text ) return info + + def visitFunctionDefinition( self, ast ): + 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 visitStandardDeclaration( self, ast ): *************** *** 103,111 **** return info ! def visitDeclarator( self, ast ): ! info = ASTInfo( ast ) ! # + ptr operators ! info.attr( 'declator_id', self._nodeTreeIdentifier( ast.declarator_id.node ) ) ! return info ## Statements --- 121,127 ---- return info ! def visitTypedefDeclaration( self, ast ): ! #todo ! return ASTInfo( ast ) ## Statements *************** *** 147,150 **** --- 163,169 ---- return self.visitLabelStatement( ast ) + def visitErrorStatement( self, ast ): + return self.visitErrorDeclaration( ast ) + def visitExpressionCondition( self, ast ): return self.visitASTNode( ast.expression ) *************** *** 171,174 **** --- 190,196 ---- return self.visitASTNode( ast.init_declaration ) + def visitForInitStatement( self, ast ): + return self.visitASTNode( ast.init_statement ) + def visitIfStatement( self, ast ): info = ASTInfo( ast ) *************** *** 184,187 **** --- 206,215 ---- return info + def visitReturnStatement( self, ast ): + info = ASTInfo( ast ) + if ast.expression: + info.attr( 'expression', self.visitASTNode( ast.expression ) ) + return info + def visitSwitchStatement( self, ast ): info = ASTInfo( ast ) *************** *** 223,226 **** --- 251,259 ---- info.attr( 'right', self.visitASTNode( ast.right_expression ) ) return info + + def visitBracedExpression( self, ast ): + info = ASTInfo( ast ) + info.attr( 'expression', self.visitASTNode( ast.expression ) ) + return info def visitCallExpression( self, ast ): *************** *** 241,244 **** --- 274,280 ---- return info + def visitCastExpression( self, ast ): + return ASTInfo( ast ) + def visitDeleteOperatorExpression( self, ast ): info = ASTInfo( ast ) *************** *** 279,282 **** --- 315,327 ---- return info + def visitSizeOfExpression( self, ast ): + info = ASTInfo( ast ) + info.attr( 'expression', self.visitASTNode( ast.expression ) ) + return info + + def visitThisRefExpression( self, ast ): + info = ASTInfo( ast ) + return info + def visitUnaryOperatorExpression( self, ast ): info = ASTInfo( ast ) |