Update of /cvsroot/cpptool/CppParser/examples/parser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17570/examples/parser Modified Files: symboldeclarator.h grammar_tree.txt scope.cpp scope.h symboldeclarator.cpp cpp_grammar.txt cppparsertest.cpp symboltabletest.cpp Log Message: * in the process of adding enum support to symbol table * refactored grammar for better enum tree structure Index: cpp_grammar.txt =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/cpp_grammar.txt,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** cpp_grammar.txt 19 Jun 2004 14:55:52 -0000 1.6 --- cpp_grammar.txt 21 Jun 2004 13:49:30 -0000 1.7 *************** *** 403,407 **** enum_specifier = :node( 'enum_specifier', ! 'enum' ?(id) '{' ?( list(enumerator_definition, ',') ) '}' ); forwarded_type_id = :node( 'forwarded_type_id', --- 403,410 ---- enum_specifier = :node( 'enum_specifier', ! 'enum' ?(id) '{' ! :node( 'enumerator_definitions', ! ?( list(enumerator_definition, ',') ) ) ! '}' ); forwarded_type_id = :node( 'forwarded_type_id', Index: symboldeclarator.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboldeclarator.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** symboldeclarator.cpp 20 Jun 2004 11:00:38 -0000 1.3 --- symboldeclarator.cpp 21 Jun 2004 13:49:30 -0000 1.4 *************** *** 131,134 **** --- 131,138 ---- if ( forwardClassSpecifier ) declareForwardClass( forwardClassSpecifier, parentScope ); + + NodePtr enumSpecifier = getChild( typeSpecifier, "enum_specifier" ); + if ( enumSpecifier ) + declareEnumerator( enumSpecifier, parentScope ); } *************** *** 306,308 **** --- 310,339 ---- } + + void + SymbolDeclarator::declareEnumerator( const NodePtr &enumSpecifier, + Scope &parentScope ) + { + Symbol enumSymbol( getChildSymbol( enumSpecifier ) ); + SymbolDeclarationPtr declaration = symbolTable_.declare( enumSymbol ); + EnumScopePtr enumScope( new EnumScope( declaration ) ); + declaration->setScope( enumScope ); + parentScope.add( enumScope ); // should be actual nested scope + // @todo setScope in declaration + + NodePtr definitions = safeGetChild( enumSpecifier, "enumerator_definitions" ); + NodeEnumerator enumDefinition = definitions->enumChildren(); + while ( enumDefinition.hasNext() ) + { + NodePtr definition = enumDefinition.nextPtr(); + if ( definition->name() == ConstString("enumerator_definition") ) + { + Symbol valueSymbol( getChildSymbol( definition ) ); + SymbolDeclarationPtr valueDeclaration = symbolTable_.declare( valueSymbol ); + enumScope->addValue( valueDeclaration ); + //parentScope.add( + } + } + } + } // namespace Parser Index: cppparsertest.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/cppparsertest.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** cppparsertest.cpp 19 Jun 2004 14:55:52 -0000 1.6 --- cppparsertest.cpp 21 Jun 2004 13:49:30 -0000 1.7 *************** *** 1099,1102 **** --- 1099,1103 ---- CPPPARSER_ASSERT_TREE( "enum MyEnumType{v1=1,v2=v1<<1,v3=v2<<1}", "enum_specifier", testNode( "enum_specifier", "enum", spaces_t(1), id_t("MyEnumType"), "{", + testNode( "enumerator_definitions", testNode( "enumerator_definition", id_t("v1"), "=", jokerNode( "constant_expression" ) ), *************** *** 1106,1110 **** ",", testNode( "enumerator_definition", id_t("v3"), "=", ! jokerNode( "constant_expression" ) ), "}" ) ); } --- 1107,1111 ---- ",", testNode( "enumerator_definition", id_t("v3"), "=", ! jokerNode( "constant_expression" ) ) ), "}" ) ); } Index: scope.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/scope.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** scope.h 20 Jun 2004 11:00:38 -0000 1.3 --- scope.h 21 Jun 2004 13:49:30 -0000 1.4 *************** *** 15,18 **** --- 15,19 ---- class BlockScope; class ClassScope; + class EnumScope; class MemberFunctionScope; class NamespaceScope; *************** *** 21,24 **** --- 22,26 ---- class TypedefScope; typedef boost::shared_ptr<ClassScope> ClassScopePtr; + typedef boost::shared_ptr<EnumScope> EnumScopePtr; typedef boost::shared_ptr<MemberFunctionScope> MemberFunctionScopePtr; typedef boost::shared_ptr<NamespaceScope> NamespaceScopePtr; *************** *** 30,33 **** --- 32,37 ---- typedef boost::shared_ptr<Type> TypePtr; + typedef CppUT::Enumerator<SymbolDeclarationPtr> SymbolDeclarationPtrEnum; + class ScopeVisitor { *************** *** 36,39 **** --- 40,44 ---- virtual void visit( BlockScope &scope ) = 0; virtual void visit( ClassScope &scope ) = 0; + virtual void visit( EnumScope &scope ) = 0; virtual void visit( MemberFunctionScope &scope ) = 0; virtual void visit( NamespaceScope &scope ) = 0; *************** *** 217,220 **** --- 222,244 ---- }; + class EnumScope : public DeclarationScope + { + public: + EnumScope( const SymbolDeclarationPtr &declaration ); + + SymbolDeclarationPtrEnum values() const; + + void addValue( const SymbolDeclarationPtr &valueSymbol ); + + public: // overridden from Scope + void accept( ScopeVisitor &visitor ); + SymbolDeclarationPtr resolve( const std::string &name ) const; + SymbolDeclarationPtr memberResolve( const std::string &name ) const; + std::string str() const; + bool isTypeDeclaration() const; + + private: + std::deque<SymbolDeclarationPtr> values_; + }; class Type Index: grammar_tree.txt =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/grammar_tree.txt,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** grammar_tree.txt 19 Jun 2004 15:25:16 -0000 1.3 --- grammar_tree.txt 21 Jun 2004 13:49:30 -0000 1.4 *************** *** 70,77 **** | enum_specifier[ id ! n enumerator_definition[ ! id ! constant_expression[...] ! ] ] | class_specifier[...] | forward_class_specifier[ --- 70,79 ---- | enum_specifier[ id ! enumerator_definitions[ ! n enumerator_definition[ ! id ! constant_expression[...] ! ] ] ! ] | class_specifier[...] | forward_class_specifier[ Index: scope.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/scope.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** scope.cpp 20 Jun 2004 11:00:38 -0000 1.3 --- scope.cpp 21 Jun 2004 13:49:30 -0000 1.4 *************** *** 492,495 **** --- 492,549 ---- + + // class EnumScope + // ////////////////////////////////////////////////////////////////// + + EnumScope::EnumScope( const SymbolDeclarationPtr &declaration ) + : DeclarationScope( declaration ) + { + } + + + SymbolDeclarationPtrEnum + EnumScope::values() const + { + return CppUT::enumStl( values_ ); + } + + void + EnumScope::addValue( const SymbolDeclarationPtr &valueSymbol ) + { + values_.push_back( valueSymbol ); + } + + + void + EnumScope::accept( ScopeVisitor &visitor ) + { + visitor.visit( *this ); + } + + SymbolDeclarationPtr + EnumScope::resolve( const std::string &name ) const + { + return SymbolDeclarationPtr(); + } + + SymbolDeclarationPtr + EnumScope::memberResolve( const std::string &name ) const + { + return SymbolDeclarationPtr(); + } + + std::string + EnumScope::str() const + { + return "EnumScope<name=" + declaration()->name() + ">"; + } + + + bool + EnumScope::isTypeDeclaration() const + { + return true; + } + // class FundamentalType // ////////////////////////////////////////////////////////////////// Index: symboldeclarator.h =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboldeclarator.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** symboldeclarator.h 20 Jun 2004 11:00:38 -0000 1.2 --- symboldeclarator.h 21 Jun 2004 13:49:30 -0000 1.3 *************** *** 45,48 **** --- 45,51 ---- Scope &parentScope ); + void declareEnumerator( const NodePtr &enumSpecifier, + Scope &parentScope ); + Symbol getChildSymbol( const NodePtr &node ) const; Index: symboltabletest.cpp =================================================================== RCS file: /cvsroot/cpptool/CppParser/examples/parser/symboltabletest.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** symboltabletest.cpp 20 Jun 2004 15:53:41 -0000 1.6 --- symboltabletest.cpp 21 Jun 2004 13:49:30 -0000 1.7 *************** *** 46,49 **** --- 46,52 ---- void checkNamespace( const NodePtr &namespaceNode ); void checkAnonymousNamespace( const NodePtr &namespaceNode ); + void checkEnumeration( const NodePtr &enumerationNode ); + void checkEnumerationValues( const Parser::EnumScopePtr &enumScope, + const NodePtr &valuesNode ); CppUT::Message makeMessage( const NodePtr &node, *************** *** 185,190 **** else if ( child->specificationName() == "anonymous_namespace" ) checkAnonymousNamespace( child ); else ! CppUT::fail( makeMessage( child, "don't know how to check node type." ) ); } } --- 188,198 ---- else if ( child->specificationName() == "anonymous_namespace" ) checkAnonymousNamespace( child ); + else if ( child->specificationName() == "enumeration" ) + checkEnumeration( child ); else ! { ! CppUT::fail( makeMessage( child, ! "don't know how to check node type: " + child->specificationName() ) ); ! } } } *************** *** 284,287 **** --- 292,341 ---- } + + void + DeclarationChecker::checkEnumeration( const NodePtr &enumerationNode ) + { + std::string enumName = enumerationNode->braceValue(); + Parser::SymbolDeclarationPtr declaration = memberResolve( enumName ); + CppUT::checkTrue( declaration, makeMessage( enumerationNode, "Declaration not found." ) ); + checkReferences( enumerationNode, declaration ); + Parser::EnumScopePtr enumScope = boost::dynamic_pointer_cast<Parser::EnumScope>( declaration->scope() ); + CppUT::checkTrue( enumScope, makeMessage( enumerationNode, + "Declaration found is not an enum:" + + declaration->scope()->str() ) ); + if ( enumerationNode->hasChild("values") ) + checkEnumerationValues( enumScope, enumerationNode->child("values") ); + } + + + void + DeclarationChecker::checkEnumerationValues( const Parser::EnumScopePtr &enumScope, + const NodePtr &valuesNode ) + { + // enumerate all member of the enum, and make list of name + // make list of expect names + // sequence assert equal + + Parser::SymbolDeclarationPtrEnum enumValues = enumScope->values(); + std::deque<std::string> actualValues; + while ( enumValues.hasNext() ) + actualValues.push_back( enumValues.next()->name() ); + + std::deque<std::string> expectedValues; + NodeEnumerator enumNode( valuesNode->children() ); + while ( enumNode.hasNext() ) + expectedValues.push_back( enumNode.next()->braceValue() ); + + CppUT::checkStlSequenceEqual( expectedValues, actualValues, + makeMessage( valuesNode, "Enumerator values do not match." ) ); + + // for each expected value: + // resolve value name + // check type + // check initializer if any + // check declarations + // check references + } + } // anonymous namespace |