From: thurston Date: Sun, 26 Oct 2008 03:20:16 +0000 (+0000) Subject: some refactoring X-Git-Tag: 2.0_alpha~88 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e3d82ff422072045d596765fbe55643432790d8e;p=external%2Fragel.git some refactoring git-svn-id: http://svn.complang.org/ragel/trunk@498 052ea7fc-9027-0410-9066-f65837a77df0 --- diff --git a/ragel/main.cpp b/ragel/main.cpp index 40fc456..98123ca 100644 --- a/ragel/main.cpp +++ b/ragel/main.cpp @@ -487,14 +487,8 @@ void process( const char *inputFileName, const char *intermed ) outputActive = false; } - XmlScanner xmlScanner( xmlFileName, cin ); - XmlParser xmlParser( inputFileName, xmlFileName, outputActive, wantComplete ); - xmlParser.init(); - - xmlParser.openOutput(); - /* Write the machines, then the surrounding code. */ - generate( xmlParser ); + generateReduced( inputFileName, xmlFileName, outputActive, wantComplete ); /* Close the input and the intermediate file. */ delete inFile; @@ -503,10 +497,6 @@ void process( const char *inputFileName, const char *intermed ) if ( gblErrorCount > 0 ) exit(1); -// xml_parse( *xmlInFile, xmlFileName, -// outputActive, wantComplete, -// xmlScanner, xmlParser ); - /* If writing to a file, delete the ostream, causing it to flush. * Standard out is flushed automatically. */ if ( outputFileName != 0 ) { diff --git a/ragel/parsedata.cpp b/ragel/parsedata.cpp index b9579d2..0e47803 100644 --- a/ragel/parsedata.cpp +++ b/ragel/parsedata.cpp @@ -1427,7 +1427,7 @@ void ParseData::prepareMachineGenTBWrapped( GraphDictEl *graphDictEl ) sectionGraph->setStateNumbers( 0 ); } -void ParseData::genBackend( XmlParser &xmlParser ) +void ParseData::generateReduced( XmlParser &xmlParser ) { beginProcessing(); @@ -1551,25 +1551,9 @@ void writeMachines( std::ostream &out, std::string hostData, } } -void generate( XmlParser &xmlParser ) +void generateSpecificReduced( XmlParser &xmlParser ) { - if ( machineSpec == 0 && machineName == 0 ) { - /* No machine spec or machine name given. Generate everything. */ - for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) { - ParseData *pd = parser->value->pd; - if ( pd->instanceList.length() > 0 ) - pd->prepareMachineGen( 0 ); - } - - if ( gblErrorCount == 0 ) { - for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) { - ParseData *pd = parser->value->pd; - if ( pd->instanceList.length() > 0 ) - pd->genBackend( xmlParser ); - } - } - } - else if ( parserDict.length() > 0 ) { + if ( parserDict.length() > 0 ) { /* There is either a machine spec or machine name given. */ ParseData *parseData = 0; GraphDictEl *graphDictEl = 0; @@ -1598,10 +1582,46 @@ void generate( XmlParser &xmlParser ) /* Section/Machine to emit was found. Prepare and emit it. */ parseData->prepareMachineGen( graphDictEl ); if ( gblErrorCount == 0 ) - parseData->genBackend( xmlParser ); + parseData->generateReduced( xmlParser ); + } + } + + + for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) { + if ( ii->type == InputItem::Write ) { + CodeGenMapEl *mapEl = xmlParser.codeGenMap.find( (char*)ii->name.c_str() ); + xmlParser.cgd = mapEl->value; + ::keyOps = &xmlParser.cgd->thisKeyOps; + + xmlParser.cgd->writeStatement( ii->loc, ii->writeArgs.length()-1, ii->writeArgs.data ); } + else + xmlParser.cgd->out << ii->data.str(); } +} + +void generateReduced( const char *sourceFileName, const char *xmlFileName, + bool outputActive, bool wantComplete ) +{ + /* No machine spec or machine name given. Generate everything. */ + for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) { + ParseData *pd = parser->value->pd; + if ( pd->instanceList.length() > 0 ) + pd->prepareMachineGen( 0 ); + } + + XmlParser xmlParser( sourceFileName, xmlFileName, outputActive, wantComplete ); + xmlParser.init(); + xmlParser.openOutput(); + if ( gblErrorCount > 0 ) + return; + + for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) { + ParseData *pd = parser->value->pd; + if ( pd->instanceList.length() > 0 ) + pd->generateReduced( xmlParser ); + } for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) { if ( ii->type == InputItem::Write ) { diff --git a/ragel/parsedata.h b/ragel/parsedata.h index 8149b15..60049ce 100644 --- a/ragel/parsedata.h +++ b/ragel/parsedata.h @@ -217,7 +217,7 @@ struct ParseData void prepareMachineGen( GraphDictEl *graphDictEl ); void prepareMachineGenTBWrapped( GraphDictEl *graphDictEl ); void generateXML( ostream &out, XmlParser &xmlParser ); - void genBackend( XmlParser &xmlParser ); + void generateReduced( XmlParser &xmlParser ); FsmAp *sectionGraph; bool generatingSectionSubset; diff --git a/ragel/ragel.h b/ragel/ragel.h index e49aefc..eb5352c 100644 --- a/ragel/ragel.h +++ b/ragel/ragel.h @@ -78,9 +78,11 @@ struct XmlParser; void terminateAllParsers( ); void writeMachines( std::ostream &out, std::string hostData, const char *inputFileName, XmlParser &xmlParser ); -void generate( XmlParser &xmlParser ); void xmlEscapeHost( std::ostream &out, char *data, long len ); +void generateReduced( const char *sourceFileName, const char *xmlFileName, + bool outputActive, bool wantComplete ); + typedef Vector ArgsVector; extern ArgsVector includePaths; diff --git a/ragel/xmlcodegen.cpp b/ragel/xmlcodegen.cpp index 0d0f223..451b9d0 100644 --- a/ragel/xmlcodegen.cpp +++ b/ragel/xmlcodegen.cpp @@ -1291,10 +1291,38 @@ void BackendGen::makeMachine() xmlParser.cgd->closeMachine(); } +void BackendGen::open_ragel_def( char *fsmName ) +{ + CodeGenMapEl *mapEl = xmlParser.codeGenMap.find( fsmName ); + if ( mapEl != 0 ) + xmlParser.cgd = mapEl->value; + else { + xmlParser.cgd = makeCodeGen( xmlParser.sourceFileName, fsmName, + *xmlParser.outStream, xmlParser.wantComplete ); + xmlParser.codeGenMap.insert( fsmName, xmlParser.cgd ); + } +} + +void BackendGen::close_ragel_def() +{ + /* Do this before distributing transitions out to singles and defaults + * makes life easier. */ + xmlParser.cgd->redFsm->maxKey = xmlParser.cgd->findMaxKey(); + + xmlParser.cgd->redFsm->assignActionLocs(); + + /* Find the first final state (The final state with the lowest id). */ + xmlParser.cgd->redFsm->findFirstFinState(); + + /* Call the user's callback. */ + xmlParser.cgd->finishRagelDef(); +} + + void BackendGen::makeBackend() { /* Open the definition. */ - xmlParser.open_ragel_def( fsmName ); + open_ragel_def( fsmName ); /* Alphabet type. */ xmlParser.cgd->setAlphType( keyOps->alphType->internalName ); @@ -1380,7 +1408,7 @@ void BackendGen::makeBackend() makeExports(); makeMachine(); - xmlParser.close_ragel_def(); + close_ragel_def(); } diff --git a/ragel/xmlcodegen.h b/ragel/xmlcodegen.h index 22d6dec..ca8894d 100644 --- a/ragel/xmlcodegen.h +++ b/ragel/xmlcodegen.h @@ -175,6 +175,9 @@ private: void makeStateConditions( StateAp *state ); void makeTransList( StateAp *state ); void makeTrans( Key lowKey, Key highKey, TransAp *trans ); + + void open_ragel_def( char *fsmName ); + void close_ragel_def(); }; diff --git a/redfsm/xmlparse.kh b/redfsm/xmlparse.kh index 1590e20..4e37b45 100644 --- a/redfsm/xmlparse.kh +++ b/redfsm/xmlparse.kh @@ -168,9 +168,6 @@ struct XmlParser int token( int tokenId, int col, int line ); int token( XMLTag *tag, int col, int line ); - void open_ragel_def( char *fsmName ); - void close_ragel_def(); - void openOutput(); /* Report an error encountered by the parser. */ @@ -209,4 +206,7 @@ int xml_parse( std::istream &input, const char *fileName, bool outputActive, bool wantComplete, XmlScanner &scanner, XmlParser &parser ); +CodeGenData *makeCodeGen( const char *sourceFileName, + const char *fsmName, ostream &out, bool wantComplete ); + #endif /* _XMLPARSE_H */ diff --git a/redfsm/xmlparse.kl b/redfsm/xmlparse.kl index 1c22651..8fbb3c9 100644 --- a/redfsm/xmlparse.kl +++ b/redfsm/xmlparse.kl @@ -103,32 +103,6 @@ void XmlParser::openOutput() } } -void XmlParser::open_ragel_def( char *fsmName ) -{ - CodeGenMapEl *mapEl = codeGenMap.find( fsmName ); - if ( mapEl != 0 ) - cgd = mapEl->value; - else { - cgd = makeCodeGen( sourceFileName, fsmName, *outStream, wantComplete ); - codeGenMap.insert( fsmName, cgd ); - } -} - -void XmlParser::close_ragel_def() -{ - /* Do this before distributing transitions out to singles and defaults - * makes life easier. */ - cgd->redFsm->maxKey = cgd->findMaxKey(); - - cgd->redFsm->assignActionLocs(); - - /* Find the first final state (The final state with the lowest id). */ - cgd->redFsm->findFirstFinState(); - - /* Call the user's callback. */ - cgd->finishRagelDef(); -} - %%{ parser XmlParser;