From: Baptiste L. <bl...@us...> - 2004-08-08 15:55:12
|
Update of /cvsroot/cpptool/CppParser/src/pyrfta In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6127/src/pyrfta Modified Files: ast.py dumpast.py nodetools.py Log Message: * enhanced wrapping of for, switch, case and default statements Index: nodetools.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/nodetools.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** nodetools.py 8 Aug 2004 14:50:29 -0000 1.3 --- nodetools.py 8 Aug 2004 15:55:03 -0000 1.4 *************** *** 3,7 **** def getFirstChild( node ): if node.childCount == 0: ! raise LogicError( "Fail to get first chil in node %s" % node ) return node.childAt(0) --- 3,7 ---- def getFirstChild( node ): if node.childCount == 0: ! raise AssertionError( "Fail to get first chil in node %s" % node ) return node.childAt(0) *************** *** 15,19 **** if child.name == name: return child ! raise LogicError( "Fail to find children named '%s' in node %s" % (name,node) ) def tryGetFirstChildNamed( node, name ): --- 15,19 ---- if child.name == name: return child ! raise AssertionError( "Fail to find children named '%s' in node %s" % (name,node) ) def tryGetFirstChildNamed( node, name ): *************** *** 31,35 **** return child if exception_if_not_found: ! raise LogicError( "Fail to find identifier child in node %s" % node ) return None --- 31,35 ---- return child if exception_if_not_found: ! raise AssertionError( "Fail to find identifier child in node %s" % node ) return None *************** *** 39,43 **** return child if exception_if_not_found: ! raise LogicError( "Fail to find string child in node %s" % node) return None --- 39,43 ---- return child if exception_if_not_found: ! raise AssertionError( "Fail to find string child in node %s" % node) return None *************** *** 46,48 **** if child.hasValidToken(): return child ! raise LogicError( "Fail to find a child node with token in %s." % node ) \ No newline at end of file --- 46,48 ---- if child.hasValidToken(): return child ! raise AssertionError( "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.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ast.py 8 Aug 2004 14:50:29 -0000 1.4 --- ast.py 8 Aug 2004 15:55:03 -0000 1.5 *************** *** 263,267 **** class CaseStatement(Statement): ! pass class DefaultStatement(Statement): --- 263,276 ---- class CaseStatement(Statement): ! def __init__( self, node ): ! Statement.__init__( self, node ) ! self.value = None ! self.statement = None ! ! def setValue( self, value ): ! self.value = value ! ! def setStatement( self, statement ): ! self.statement = statement class DefaultStatement(Statement): *************** *** 272,276 **** class SwitchStatement(Statement): ! pass class WhileStatement(Statement): --- 281,294 ---- class SwitchStatement(Statement): ! def __init__( self, node ): ! Statement.__init__( self, node ) ! self.condition = None ! self.statement = None ! ! def setCondition( self, condition ): ! self.condition = condition ! ! def setStatement( self, statement ): ! self.statement = statement class WhileStatement(Statement): *************** *** 281,285 **** class ForStatement(Statement): ! pass class BreakStatement(Statement): --- 299,332 ---- class ForStatement(Statement): ! def __init__( self, node ): ! Statement.__init__( self, node ) ! self.init = None ! self.condition = None ! self.loop_expression = None ! self.loop_statement = None ! ! def setInit( self, init ): ! self.init = init ! ! def setCondition( self, condition ): ! self.condition = condition ! ! def setLoopExpression( self, expression ): ! self.loop_expression = expression ! ! def setLoopStatement( self, statement ): ! self.loop_statement = statement ! ! class ForInitStatement: ! def __init__( self, statement ): ! self.init_statement = statement ! ! class ForInitDeclaration: ! def __init__( self, simple_declaration ): ! self.init_declaration = simple_declaration ! ! class Condition(ASTElement): ! def __init__( self, node ): ! self.node = node class BreakStatement(Statement): *************** *** 427,431 **** elif declaration_specifier.name == 'declaration_specifier': return self.buildStandardSimpleDeclaration( declaration, declaration_specifier ) ! raise LogicError( "Don't know how to build simple declaration for: '%s'" % declaration_specifier.name ) else: return self.buildDeclaratorsSimpleDeclaration( declaration ) --- 474,478 ---- elif declaration_specifier.name == 'declaration_specifier': return self.buildStandardSimpleDeclaration( declaration, declaration_specifier ) ! raise AssertionError( "Don't know how to build simple declaration for: '%s'" % declaration_specifier.name ) else: return self.buildDeclaratorsSimpleDeclaration( declaration ) *************** *** 559,562 **** --- 606,612 ---- ##################################### + def buildStatement( self, statement_node ): + return self.statement_builders[ statement_node.name ]( statement_node ) + def buildErrorStatement( self, statement ): return ErrorStatement( statement ) *************** *** 566,570 **** for child in statement.enumChildren(): if not child.hasValidToken(): ! statement = self.statement_builders[ child.name ]( child ) ast.addStatement( statement ) return ast --- 616,620 ---- for child in statement.enumChildren(): if not child.hasValidToken(): ! statement = self.buildStatement( child ) ast.addStatement( statement ) return ast *************** *** 574,578 **** def buildCaseStatement( self, statement ): ! return CaseStatement( statement ) def buildDefaultStatement( self, statement ): --- 624,633 ---- def buildCaseStatement( self, statement ): ! ast = CaseStatement( statement ) ! value_node = nodetools.getFirstChildNamed( statement, 'constant_expression' ) ! #ast.setValue( ast.buildConstantExpression( value_node ) ) ! target_statement_node = nodetools.getFirstChildNamed( statement, 'target_statement' ) ! ast.setStatement( self.buildStatement( nodetools.getFirstChild( target_statement_node ) ) ) ! return ast def buildDefaultStatement( self, statement ): *************** *** 583,587 **** def buildSwitchStatement( self, statement ): ! return SwitchStatement( statement ) def buildWhileStatement( self, statement ): --- 638,647 ---- def buildSwitchStatement( self, statement ): ! ast = SwitchStatement( statement ) ! condition_node = nodetools.getFirstChildNamed( statement, 'condition' ) ! ast.setCondition( self.buildCondition( condition_node ) ) ! statement_node = nodetools.getFirstChildNamed( statement, 'condition_statement' ) ! ast.setStatement( self.buildStatement( nodetools.getFirstChild( statement_node ) ) ) ! return ast def buildWhileStatement( self, statement ): *************** *** 592,596 **** def buildForStatement( self, statement ): ! return ForStatement( statement ) def buildBreakStatement( self, statement ): --- 652,683 ---- def buildForStatement( self, statement ): ! ast = ForStatement( statement ) ! for_init_statement_node = nodetools.getFirstChildNamed( statement, 'for_init_statement' ) ! init_node = nodetools.tryGetFirstChildNamed( for_init_statement_node, 'expression_statement' ) ! if init_node: ! init = ForInitStatement( self.buildExpressionStatement( init_node ) ) ! else: ! init_declaration_node = nodetools.getFirstChildNamed( for_init_statement_node, ! 'simple_declaration' ) ! init = ForInitDeclaration( self.buildSimpleDeclaration( init_declaration_node ) ) ! ast.setInit( init ) ! ! for_condition_node = nodetools.tryGetFirstChildNamed( statement, 'for_condition' ) ! if for_condition_node: ! ast.setCondition( self.buildCondition( for_condition_node ) ) ! ! ## expression_node = nodetools.tryGetFirstChildNamed( statement, 'expression' ) ! ## if expression_node: ! ## ast.setLoopExpression( self.buildExpression( expression_node ) ) ! ! for_loop_statement_node = nodetools.getFirstChild( nodetools.getFirstChildNamed( statement, ! 'for_loop_statement' ) ) ! loop_statement = self.buildStatement( for_loop_statement_node ) ! ast.setLoopStatement( loop_statement ) ! return ast ! ! def buildCondition( self, condition_node ): ! ast = Condition( condition_node ) ! return ast def buildBreakStatement( self, statement ): Index: dumpast.py =================================================================== RCS file: /cvsroot/cpptool/CppParser/src/pyrfta/dumpast.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** dumpast.py 8 Aug 2004 14:50:29 -0000 1.3 --- dumpast.py 8 Aug 2004 15:55:03 -0000 1.4 *************** *** 11,14 **** --- 11,15 ---- def __init__( self, name_or_ast ): self._attributes = {} + self._order = [] self._children = [] if type(name_or_ast) == str: *************** *** 21,28 **** def attr( self, name, value = None ): return self._attributes.setdefault( name, value ) def attributes( self ): ! return self._attributes.items() class ASTInfoBuilder: --- 22,31 ---- def attr( self, name, value = None ): + if not self._attributes.has_key( name ): + self._order.append( name ) return self._attributes.setdefault( name, value ) def attributes( self ): ! return [ (name,self._attributes[name]) for name in self._order ] class ASTInfoBuilder: *************** *** 112,115 **** --- 115,133 ---- def visitForStatement( self, ast ): info = ASTInfo( ast ) + if ast.init: + info.attr( 'init', self.visitASTNode( ast.init ) ) + if ast.condition: + info.attr( 'condition', self.visitASTNode( ast.condition ) ) + if ast.loop_expression: + info.attr( 'loop_expression', self.visitASTNode( ast.loop_expression ) ) + if ast.loop_statement: + info.attr( 'loop_statement', self.visitASTNode( ast.loop_statement ) ) + return info + + def visitForInitDeclaration( self, ast ): + return self.visitASTNode( ast.init_declaration ) + + def visitCondition( self, ast ): + info = ASTInfo( ast ) return info *************** *** 135,149 **** info.attr( 'declaration', self.visitASTNode( ast.declaration ) ) return info ! def printASTInfo( info, indent = 0 ): margin = '| ' * indent indent += 1 ! print margin + info.name() + '[' attribute_margin = margin + '|-' for name, value in info.attributes(): if isinstance(value, ASTInfo): ! printASTInfo( value, indent ) elif type(value) == type([]): printASTInfoChildren( name, value, attribute_margin, indent-1 ) --- 153,187 ---- info.attr( 'declaration', self.visitASTNode( ast.declaration ) ) return info + + def visitSwitchStatement( self, ast ): + info = ASTInfo( ast ) + info.attr( 'condition', self.visitASTNode( ast.condition ) ) + info.attr( 'statement', self.visitASTNode( ast.statement ) ) + return info + + def visitCaseStatement( self, ast ): + info = ASTInfo( ast ) + # info.attr( 'value', self.visitASTNode( ast.value ) ) + info.attr( 'statement', self.visitASTNode( ast.statement ) ) + return info + + def visitBreakStatement( self, ast ): + return ASTInfo( ast ) + + def visitDefaultStatement( self, ast ): + return ASTInfo( ast ) ! def printASTInfo( info, indent = 0, attribute_name = '' ): margin = '| ' * indent indent += 1 ! if attribute_name: ! attribute_name += ': ' ! print margin + attribute_name + info.name() + '[' attribute_margin = margin + '|-' for name, value in info.attributes(): if isinstance(value, ASTInfo): ! printASTInfo( value, indent, name ) elif type(value) == type([]): printASTInfoChildren( name, value, attribute_margin, indent-1 ) |