///////////////////////////////////////////////////////////////////// // ConfigureParser.cpp - builds and configures parsers // // ver 2.1 // // // // Lanaguage: Visual C++ 2005 // // Platform: Dell Dimension 9150, Windows XP SP2 // // Application: Prototype for CSE687 Pr1, Sp06 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// #include "Parser.h" #include "SemiExpression.h" #include "Tokenizer.h" #include "ActionsAndRules.h" #include "ConfigureParser.h" //----< destructor releases all parts >------------------------------ ConfigParseToConsole::~ConfigParseToConsole() { // when Builder goes out of scope, everything must be deallocated delete pHandlePush; delete pBeginningOfScope; delete pHandlePop; delete pEndOfScope; delete pPushFunction; delete pFunctionDefinition; delete pFR; delete pRepo; delete pParser; delete pSemi; delete pToker; } //----< attach toker to a file stream or stringstream >------------ bool ConfigParseToConsole::Attach(const std::string& name, bool isFile) { if(pToker == 0) return false; return pToker->attach(name, isFile); } //----< Here's where alll the parts get assembled >---------------- Parser* ConfigParseToConsole::Build() { try { // add Parser's main parts pToker = new Toker; pToker->returnComments(); pSemi = new SemiExp(pToker); pParser = new Parser(pSemi); pRepo = new Repository(pToker, nullptr); // add code folding rules pFR = new codeFoldingRules; pParser->addFoldingRules(pFR); // configure to manage scope // these must come first - they return true on match // so rule checking continues pBeginningOfScope = new BeginningOfScope(); pHandlePush = new HandlePush(pRepo); pBeginningOfScope->addAction(pHandlePush); pParser->addRule(pBeginningOfScope); pEndOfScope = new EndOfScope(); pHandlePop = new HandlePop(pRepo); pEndOfScope->addAction(pHandlePop); pParser->addRule(pEndOfScope); // configure to detect and act on function definitions // these will stop further rule checking by returning false pFunctionDefinition = new FunctionDefinition; pPushFunction = new PushFunction(pRepo); // no action pFunctionDefinition->addAction(pPushFunction); pParser->addRule(pFunctionDefinition); return pParser; } catch(std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; return 0; } } //----< construct ConfigParserToQueue >------------------------------ ConfigParseToQueue::ConfigParseToQueue() : pQueue(new std::queue()) {} //----< destructor releases all parts >------------------------------ ConfigParseToQueue::~ConfigParseToQueue() { // when Builder goes out of scope, everything must be deallocated delete pHandlePush; delete pBeginningOfScope; delete pHandlePop; delete pEndOfScope; delete pPushFunction; delete pFunctionDefinition; delete pFR; delete pRepo; delete pParser; delete pSemi; delete pToker; delete pQueue; } //----< give access to queue >------------------------------------- std::queue* ConfigParseToQueue::GetQueue() { return pQueue; } //----< attach toker to a file stream or stringstream >------------ bool ConfigParseToQueue::Attach(const std::string& name, bool isFile) { if(pToker == 0) return false; return pToker->attach(name, isFile); } //----< Here's where alll the parts get assembled >---------------- Parser* ConfigParseToQueue::Build() { try { // add Parser's main parts pToker = new Toker; pToker->returnComments(); pSemi = new SemiExp(pToker); pParser = new Parser(pSemi); pRepo = new Repository(pToker, pQueue); // add code folding rules pFR = new codeFoldingRules; pParser->addFoldingRules(pFR); // configure to manage scope // these must come first - they return true on match // so rule checking continues pBeginningOfScope = new BeginningOfScope(); pHandlePush = new HandlePush(pRepo); pBeginningOfScope->addAction(pHandlePush); pParser->addRule(pBeginningOfScope); pEndOfScope = new EndOfScope(); pHandlePop = new HandlePop(pRepo); pEndOfScope->addAction(pHandlePop); pParser->addRule(pEndOfScope); // configure to detect and act on function definitions // these will stop further rule checking by returning false pFunctionDefinition = new FunctionDefinition; pPushFunction = new PushFunction(pRepo); // no action pFunctionDefinition->addAction(pPushFunction); pParser->addRule(pFunctionDefinition); return pParser; } catch(std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; return 0; } } #ifdef TEST_CONFIGUREPARSER #include #include int main(int argc, char* argv[]) { std::cout << "\n Testing ConfigureParser module\n " << std::string(32,'=') << std::endl; // collecting tokens from files, named on the command line if(argc < 2) { std::cout << "\n please enter name of file to process on command line\n\n"; return 1; } for(int i=1; inext()) pParser->parse(); std::cout << "\n\n"; } catch(std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; } std::cout << "\n\n"; } } #endif