Update of /cvsroot/cpptool/rfta/src/rftaparser In directory sc8-pr-cvs1:/tmp/cvs-serv31122 Added Files: UnparsedDeclaratorMutatorTest.h UnparsedDeclaratorMutatorTest.cpp UnparsedDeclaratorMutator.h UnparsedDeclaratorMutator.cpp Log Message: -- declarator mutator, only search for identifier at the moment... --- NEW FILE: UnparsedDeclaratorMutatorTest.h --- // ////////////////////////////////////////////////////////////////////////// // Header file UnparsedDeclaratorMutatorTest.h for class UnparsedDeclaratorMutatorTest // (c)Copyright 2003, Andre Baresel. // Created: 2003/08/27 // ////////////////////////////////////////////////////////////////////////// #ifndef RFTA_UnparsedDeclaratorMutatorTest_H #define RFTA_UnparsedDeclaratorMutatorTest_H #include "ParserTesting.h" namespace Refactoring { /// Unit tests for UnparsedDeclaratorMutatorTest class UnparsedDeclaratorMutatorTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( UnparsedDeclaratorMutatorTest ); CPPUNIT_TEST( testSimpleDeclarator ); CPPUNIT_TEST( testBracedDeclarator ); CPPUNIT_TEST( testRootScopeDeclarator ); CPPUNIT_TEST( testScopedDeclarator ); CPPUNIT_TEST_SUITE_END(); public: /*! Constructs a UnparsedDeclaratorMutatorTest object. */ UnparsedDeclaratorMutatorTest(); /// Destructor. virtual ~UnparsedDeclaratorMutatorTest(); void setUp(); void tearDown(); void testSimpleDeclarator(); void testBracedDeclarator(); void testRootScopeDeclarator(); void testScopedDeclarator(); private: /// Prevents the use of the copy constructor. UnparsedDeclaratorMutatorTest( const UnparsedDeclaratorMutatorTest &other ); /// Prevents the use of the copy operator. void operator =( const UnparsedDeclaratorMutatorTest &other ); private: }; // Inlines methods for UnparsedDeclaratorMutatorTest: // ------------------------------------------------ } // namespace Refactoring #endif // RFTA_UnparsedDeclaratorMutatorTest_H --- NEW FILE: UnparsedDeclaratorMutatorTest.cpp --- // ////////////////////////////////////////////////////////////////////////// // Implementation file UnparsedDeclaratorMutatorTest.cpp for class UnparsedDeclaratorMutatorTest // (c)Copyright 2003, Andre Baresel. // Created: 2003/04/18 // ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "UnparsedDeclaratorMutator.h" #include "UnparsedDeclaratorMutatorTest.h" #include "KeyedString.h" #include <rfta/parser/ASTNode.h> #include <rfta/parser/ASTNodes.h> #include <rfta/parser/ParseContext.h> #include <rfta/parser/SourceASTNode.h> #include <boost/format.hpp> namespace Refactoring { RFTAPARSER_TEST_SUITE_REGISTRATION( UnparsedDeclaratorMutatorTest ); const std::string SPECIFIER = "SPECIFIER"; const std::string DECLARATOR = "DECLARATOR"; const std::string SPECIFIERLIST = "SPECIFIERLIST"; namespace Testing { static SourceASTNodePtr checkDeclaratorMutatorPass( const std::string &source, const CppUnit::SourceLine &sourceLine ) { SourceASTNodePtr sourceNode = SourceASTNode::create( source, source ); ParseContext context( sourceNode ); ASTNodePtr decl = ASTNode::create( ASTNodeTypes::unparsedDeclarator, sourceNode, 0, source.length(), sourceNode ); context.addNode( decl ); // do variable declarator parsing UnparsedDeclaratorMutator mutator; mutator.mutate(decl,sourceNode); return sourceNode; } } // namespace Testing #define RFTA_ASSERT_DECLARATOR_MUTATOR_PASS( source ) \ Refactoring::Testing::checkDeclaratorMutatorPass( source, CPPUNIT_SOURCELINE() ) UnparsedDeclaratorMutatorTest::UnparsedDeclaratorMutatorTest() : CppUnit::TestFixture() { } UnparsedDeclaratorMutatorTest::~UnparsedDeclaratorMutatorTest() { } void UnparsedDeclaratorMutatorTest::setUp() { } void UnparsedDeclaratorMutatorTest::tearDown() { } void UnparsedDeclaratorMutatorTest::testSimpleDeclarator() { Testing::KeyedString source; source.addKeyed("IDENT","x_0") << "[] = 0"; SourceASTNodePtr sourceAST = RFTA_ASSERT_DECLARATOR_MUTATOR_PASS( source ); ASTNodePtr node = sourceAST->getChildAt(0); RFTA_ASSERT_NODE_HAS( node, ASTNodeTypes::declarator, 0, source.length() ); RFTA_ASSERT_EQUAL( 1, node->getChildCount() ); RFTA_ASSERT_NODE_HAS( node->getChildAt(0), ASTNodeTypes::localScopeIdentifier, source.getKeyedIndex("IDENT",0), source.getKeyedLen ("IDENT",0) ); } void UnparsedDeclaratorMutatorTest::testBracedDeclarator() { Testing::KeyedString source; source << "((*"; source.addKeyed("IDENT","x_0") << ") const = 0"; SourceASTNodePtr sourceAST = RFTA_ASSERT_DECLARATOR_MUTATOR_PASS( source ); ASTNodePtr node = sourceAST->getChildAt(0); RFTA_ASSERT_NODE_HAS( node, ASTNodeTypes::declarator, 0, source.length() ); RFTA_ASSERT_EQUAL( 1, node->getChildCount() ); RFTA_ASSERT_NODE_HAS( node->getChildAt(0), ASTNodeTypes::localScopeIdentifier, source.getKeyedIndex("IDENT",0), source.getKeyedLen ("IDENT",0) ); } void UnparsedDeclaratorMutatorTest::testRootScopeDeclarator() { Testing::KeyedString source; source << "((*"; source.addKeyed("IDENT","::hello") << ") const = 0"; SourceASTNodePtr sourceAST = RFTA_ASSERT_DECLARATOR_MUTATOR_PASS( source ); ASTNodePtr node = sourceAST->getChildAt(0); RFTA_ASSERT_NODE_HAS( node, ASTNodeTypes::declarator, 0, source.length() ); RFTA_ASSERT_EQUAL( 1, node->getChildCount() ); RFTA_ASSERT_NODE_HAS( node->getChildAt(0), ASTNodeTypes::localScopeIdentifier, source.getKeyedIndex("IDENT",0), source.getKeyedLen ("IDENT",0) ); } void UnparsedDeclaratorMutatorTest::testScopedDeclarator() { Testing::KeyedString source; source << "((*"; source.addKeyed("IDENT","hello::hello2::hello3") << ") const = 0"; SourceASTNodePtr sourceAST = RFTA_ASSERT_DECLARATOR_MUTATOR_PASS( source ); ASTNodePtr node = sourceAST->getChildAt(0); RFTA_ASSERT_NODE_HAS( node, ASTNodeTypes::declarator, 0, source.length() ); RFTA_ASSERT_EQUAL( 1, node->getChildCount() ); RFTA_ASSERT_NODE_HAS( node->getChildAt(0), ASTNodeTypes::localScopeIdentifier, source.getKeyedIndex("IDENT",0), source.getKeyedLen ("IDENT",0) ); } } // namespace Refactoring --- NEW FILE: UnparsedDeclaratorMutator.h --- // ////////////////////////////////////////////////////////////////////////// // Header file UnparsedDeclaratorMutator.h for class UnparsedDeclaratorMutator // (c)Copyright 2003, Andre Baresel. // Created: 2003/08/27 // ////////////////////////////////////////////////////////////////////////// #ifndef RFTA_UnparsedDeclaratorMutator_H #define RFTA_UnparsedDeclaratorMutator_H #include <rfta/parser/Mutator.h> namespace Refactoring { /// Mutate an unparsed-declarator into a parsed declarator class UnparsedDeclaratorMutator : public Mutator , public boost::noncopyable { public: /*! Constructs a UnparsedDeclaratorMutator object. * * source range must not start with white spaces. */ UnparsedDeclaratorMutator(); /// Destructor. virtual ~UnparsedDeclaratorMutator(); void mutate( const ASTNodePtr &node, const SourceASTNodePtr &sourceNode) const; }; // Inlines methods for UnparsedDeclaratorMutator: // -------------------------------------------- } // namespace Refactoring #endif // RFTA_UnparsedDeclaratorMutator_H --- NEW FILE: UnparsedDeclaratorMutator.cpp --- // ////////////////////////////////////////////////////////////////////////// // Implementation file UnparsedDeclaratorMutator.cpp for class UnparsedDeclaratorMutator // (c)Copyright 2003, Andre Baresel. // Created: 2003/08/27 // ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "UnparsedDeclaratorMutator.h" #include <rfta/parser/ASTNodes.h> #include <rfta/parser/ParseContext.h> #include <rfta/parser/ParserTools.h> namespace Refactoring { UnparsedDeclaratorMutator::UnparsedDeclaratorMutator() { } UnparsedDeclaratorMutator::~UnparsedDeclaratorMutator() { } void UnparsedDeclaratorMutator::mutate(const ASTNodePtr &node, const SourceASTNodePtr &sourceNode) const { const char *start = sourceNode->getBlankedSourceStart() + node->getStartIndex(); const char *end = start + node->getLength(); const char *current = start; ASTNodePtr identifierNode; bool identFound = false; while (current != end && !identFound) { if (!isalpha(*current) && *current!=':') { current++; continue; } if (*current==':' && *(current+1) != ':') { current++; continue; } // read the identifier: Xtl::CStringEnumerator en( Xtl::CStringView( start, end ) ); en.setCurrent( current ); int identStartOfs = en.getCurrentIndex(); ParserTools::skip(en,ParserTools::SkipScopeOperatorPolicy()); while (current!=end) { Xtl::CStringView identifier; ParserTools::tryReadIdentifier( en, identifier ); current=en.getCurrentPos(); // check scope operator: if (*current==':') { if (*(current+1)==':') { ParserTools::skip(en,ParserTools::SkipScopeOperatorPolicy()); continue; } } break; } identifierNode = ASTNode::create( ASTNodeTypes::localScopeIdentifier, node, node->getStartIndex() + identStartOfs, (current-start)-identStartOfs, sourceNode); identFound = true; } node->addChild(identifierNode); node->mutateType(ASTNodeTypes::declarator); } } // namespace Refactoring |