Minor refactoring. Added inputdata.h, which was left behind during previous
[external/ragel.git] / ragel / xmlcodegen.cpp
index 66bddca..627e35c 100644 (file)
 #include "parsedata.h"
 #include "fsmgraph.h"
 #include "gendata.h"
+#include "inputdata.h"
 #include <string.h>
 
 using namespace std;
 
-XMLCodeGen::XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm, 
-               std::ostream &out, XmlParser &xmlParser )
+GenBase::GenBase( char *fsmName, ParseData *pd, FsmAp *fsm )
 :
        fsmName(fsmName),
        pd(pd),
        fsm(fsm),
-       out(out),
-       xmlParser(xmlParser),
        nextActionTableId(0)
 {
 }
 
+void GenBase::appendTrans( TransListVect &outList, Key lowKey, 
+               Key highKey, TransAp *trans )
+{
+       if ( trans->toState != 0 || trans->actionTable.length() > 0 )
+               outList.append( TransEl( lowKey, highKey, trans ) );
+}
+
+void GenBase::reduceActionTables()
+{
+       /* Reduce the actions tables to a set. */
+       for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
+               RedActionTable *actionTable = 0;
+
+               /* Reduce To State Actions. */
+               if ( st->toStateActionTable.length() > 0 ) {
+                       if ( actionTableMap.insert( st->toStateActionTable, &actionTable ) )
+                               actionTable->id = nextActionTableId++;
+               }
+
+               /* Reduce From State Actions. */
+               if ( st->fromStateActionTable.length() > 0 ) {
+                       if ( actionTableMap.insert( st->fromStateActionTable, &actionTable ) )
+                               actionTable->id = nextActionTableId++;
+               }
+
+               /* Reduce EOF actions. */
+               if ( st->eofActionTable.length() > 0 ) {
+                       if ( actionTableMap.insert( st->eofActionTable, &actionTable ) )
+                               actionTable->id = nextActionTableId++;
+               }
+
+               /* Loop the transitions and reduce their actions. */
+               for ( TransList::Iter trans = st->outList; trans.lte(); trans++ ) {
+                       if ( trans->actionTable.length() > 0 ) {
+                               if ( actionTableMap.insert( trans->actionTable, &actionTable ) )
+                                       actionTable->id = nextActionTableId++;
+                       }
+               }
+       }
+}
+
+XMLCodeGen::XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm, std::ostream &out )
+:
+       GenBase(fsmName, pd, fsm),
+       out(out)
+{
+}
+
 
 void XMLCodeGen::writeActionList()
 {
@@ -85,47 +131,6 @@ void XMLCodeGen::writeActionTableList()
        delete[] tables;
 }
 
-void XMLCodeGen::reduceActionTables()
-{
-       /* Reduce the actions tables to a set. */
-       for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
-               RedActionTable *actionTable = 0;
-
-               /* Reduce To State Actions. */
-               if ( st->toStateActionTable.length() > 0 ) {
-                       if ( actionTableMap.insert( st->toStateActionTable, &actionTable ) )
-                               actionTable->id = nextActionTableId++;
-               }
-
-               /* Reduce From State Actions. */
-               if ( st->fromStateActionTable.length() > 0 ) {
-                       if ( actionTableMap.insert( st->fromStateActionTable, &actionTable ) )
-                               actionTable->id = nextActionTableId++;
-               }
-
-               /* Reduce EOF actions. */
-               if ( st->eofActionTable.length() > 0 ) {
-                       if ( actionTableMap.insert( st->eofActionTable, &actionTable ) )
-                               actionTable->id = nextActionTableId++;
-               }
-
-               /* Loop the transitions and reduce their actions. */
-               for ( TransList::Iter trans = st->outList; trans.lte(); trans++ ) {
-                       if ( trans->actionTable.length() > 0 ) {
-                               if ( actionTableMap.insert( trans->actionTable, &actionTable ) )
-                                       actionTable->id = nextActionTableId++;
-                       }
-               }
-       }
-}
-
-void XMLCodeGen::appendTrans( TransListVect &outList, Key lowKey, 
-               Key highKey, TransAp *trans )
-{
-       if ( trans->toState != 0 || trans->actionTable.length() > 0 )
-               outList.append( TransEl( lowKey, highKey, trans ) );
-}
-
 void XMLCodeGen::writeKey( Key key )
 {
        if ( keyOps->isSigned )
@@ -442,36 +447,40 @@ void XMLCodeGen::writeInlineList( InlineList *inlineList )
        }
 }
 
-void XMLCodeGen::makeKey( GenInlineList *outList, Key key )
+BackendGen::BackendGen( char *fsmName, ParseData *pd, FsmAp *fsm, InputData &inputData )
+:
+       GenBase(fsmName, pd, fsm),
+       inputData(inputData)
 {
 }
 
-void XMLCodeGen::makeText( GenInlineList *outList, InlineItem *item )
+
+void BackendGen::makeText( GenInlineList *outList, InlineItem *item )
 {
-       GenInlineItem *inlineItem = new GenInlineItem( GenInputLoc(), GenInlineItem::Text );
+       GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), GenInlineItem::Text );
        inlineItem->data = item->data;
 
        outList->append( inlineItem );
 }
 
-void XMLCodeGen::makeTargetItem( GenInlineList *outList, long targetId, GenInlineItem::Type type )
+void BackendGen::makeTargetItem( GenInlineList *outList, long entryId, GenInlineItem::Type type )
 {
        long targetState;
        if ( pd->generatingSectionSubset )
                targetState = -1;
        else {
-               EntryMapEl *targ = fsm->entryPoints.find( targetId );
+               EntryMapEl *targ = fsm->entryPoints.find( entryId );
                targetState = targ->value->alg.stateNum;
        }
 
        /* Make the item. */
-       GenInlineItem *inlineItem = new GenInlineItem( GenInputLoc(), type );
+       GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), type );
        inlineItem->targId = targetState;
        outList->append( inlineItem );
 }
 
 /* Make a sublist item with a given type. */
-void XMLCodeGen::makeSubList( GenInlineList *outList, 
+void BackendGen::makeSubList( GenInlineList *outList, 
                InlineList *inlineList, GenInlineItem::Type type )
 {
        /* Fill the sub list. */
@@ -479,12 +488,12 @@ void XMLCodeGen::makeSubList( GenInlineList *outList,
        makeGenInlineList( subList, inlineList );
 
        /* Make the item. */
-       GenInlineItem *inlineItem = new GenInlineItem( GenInputLoc(), type );
+       GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), type );
        inlineItem->children = subList;
        outList->append( inlineItem );
 }
 
-void XMLCodeGen::makeLmOnLast( GenInlineList *outList, InlineItem *item )
+void BackendGen::makeLmOnLast( GenInlineList *outList, InlineItem *item )
 {
        makeSetTokend( outList, 1 );
 
@@ -495,10 +504,10 @@ void XMLCodeGen::makeLmOnLast( GenInlineList *outList, InlineItem *item )
        }
 }
 
-void XMLCodeGen::makeLmOnNext( GenInlineList *outList, InlineItem *item )
+void BackendGen::makeLmOnNext( GenInlineList *outList, InlineItem *item )
 {
        makeSetTokend( outList, 0 );
-       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Hold ) );
+       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Hold ) );
 
        if ( item->longestMatchPart->action != 0 ) {
                makeSubList( outList, 
@@ -507,20 +516,20 @@ void XMLCodeGen::makeLmOnNext( GenInlineList *outList, InlineItem *item )
        }
 }
 
-void XMLCodeGen::makeExecGetTokend( GenInlineList *outList )
+void BackendGen::makeExecGetTokend( GenInlineList *outList )
 {
        /* Make the Exec item. */
-       GenInlineItem *execItem = new GenInlineItem( GenInputLoc(), GenInlineItem::Exec );
+       GenInlineItem *execItem = new GenInlineItem( InputLoc(), GenInlineItem::Exec );
        execItem->children = new GenInlineList;
 
        /* Make the GetTokEnd */
-       GenInlineItem *getTokend = new GenInlineItem( GenInputLoc(), GenInlineItem::LmGetTokEnd );
+       GenInlineItem *getTokend = new GenInlineItem( InputLoc(), GenInlineItem::LmGetTokEnd );
        execItem->children->append( getTokend );
 
        outList->append( execItem );
 }
 
-void XMLCodeGen::makeLmOnLagBehind( GenInlineList *outList, InlineItem *item )
+void BackendGen::makeLmOnLagBehind( GenInlineList *outList, InlineItem *item )
 {
        /* Jump to the tokend. */
        makeExecGetTokend( outList );
@@ -532,9 +541,9 @@ void XMLCodeGen::makeLmOnLagBehind( GenInlineList *outList, InlineItem *item )
        }
 }
 
-void XMLCodeGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
+void BackendGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
 {
-       GenInlineItem *lmSwitch = new GenInlineItem( GenInputLoc(), GenInlineItem::LmSwitch );
+       GenInlineItem *lmSwitch = new GenInlineItem( InputLoc(), GenInlineItem::LmSwitch );
        GenInlineList *lmList = lmSwitch->children = new GenInlineList;
        LongestMatch *longestMatch = item->longestMatch;
 
@@ -548,12 +557,14 @@ void XMLCodeGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
                 * error state. */
                assert( fsm->errState != 0 );
 
-               GenInlineItem *errCase = new GenInlineItem( GenInputLoc(), GenInlineItem::SubAction );
+               GenInlineItem *errCase = new GenInlineItem( InputLoc(), GenInlineItem::SubAction );
                errCase->lmId = 0;
                errCase->children = new GenInlineList;
 
-               makeTargetItem( errCase->children, 
-                               fsm->errState->alg.stateNum, GenInlineItem::Goto );
+               /* Make the item. */
+               GenInlineItem *gotoItem = new GenInlineItem( InputLoc(), GenInlineItem::Goto );
+               gotoItem->targId = fsm->errState->alg.stateNum;
+               errCase->children->append( gotoItem );
 
                lmList->append( errCase );
        }
@@ -566,7 +577,7 @@ void XMLCodeGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
                        else {
                                /* Open the action. Write it with the context that sets up _p 
                                 * when doing control flow changes from inside the machine. */
-                               GenInlineItem *lmCase = new GenInlineItem( GenInputLoc(), 
+                               GenInlineItem *lmCase = new GenInlineItem( InputLoc(), 
                                                GenInlineItem::SubAction );
                                lmCase->lmId = lmi->longestMatchId;
                                lmCase->children = new GenInlineList;
@@ -580,7 +591,7 @@ void XMLCodeGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
        }
 
        if ( needDefault ) {
-               GenInlineItem *defCase = new GenInlineItem( GenInputLoc(), 
+               GenInlineItem *defCase = new GenInlineItem( InputLoc(), 
                                GenInlineItem::SubAction );
                defCase->lmId = -1;
                defCase->children = new GenInlineList;
@@ -593,21 +604,21 @@ void XMLCodeGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
        outList->append( lmSwitch );
 }
 
-void XMLCodeGen::makeSetTokend( GenInlineList *outList, long offset )
+void BackendGen::makeSetTokend( GenInlineList *outList, long offset )
 {
-       GenInlineItem *inlineItem = new GenInlineItem( GenInputLoc(), GenInlineItem::LmSetTokEnd );
+       GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), GenInlineItem::LmSetTokEnd );
        inlineItem->offset = offset;
        outList->append( inlineItem );
 }
 
-void XMLCodeGen::makeSetAct( GenInlineList *outList, long lmId )
+void BackendGen::makeSetAct( GenInlineList *outList, long lmId )
 {
-       GenInlineItem *inlineItem = new GenInlineItem( GenInputLoc(), GenInlineItem::LmSetActId );
+       GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), GenInlineItem::LmSetActId );
        inlineItem->lmId = lmId;
        outList->append( inlineItem );
 }
 
-void XMLCodeGen::makeGenInlineList( GenInlineList *outList, InlineList *inList )
+void BackendGen::makeGenInlineList( GenInlineList *outList, InlineList *inList )
 {
        for ( InlineList::Iter item = *inList; item.lte(); item++ ) {
                switch ( item->type ) {
@@ -633,29 +644,29 @@ void XMLCodeGen::makeGenInlineList( GenInlineList *outList, InlineList *inList )
                        makeSubList( outList, item->children, GenInlineItem::NextExpr );
                        break;
                case InlineItem::Break:
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Break ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Break ) );
                        break;
                case InlineItem::Ret: 
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Ret ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Ret ) );
                        break;
                case InlineItem::PChar:
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::PChar ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::PChar ) );
                        break;
                case InlineItem::Char: 
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Char ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Char ) );
                        break;
                case InlineItem::Curs: 
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Curs ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Curs ) );
                        break;
                case InlineItem::Targs: 
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Targs ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Targs ) );
                        break;
                case InlineItem::Entry:
                        makeTargetItem( outList, item->nameTarg->id, GenInlineItem::Entry );
                        break;
 
                case InlineItem::Hold:
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::Hold ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Hold ) );
                        break;
                case InlineItem::Exec:
                        makeSubList( outList, item->children, GenInlineItem::Exec );
@@ -682,14 +693,14 @@ void XMLCodeGen::makeGenInlineList( GenInlineList *outList, InlineList *inList )
                        break;
 
                case InlineItem::LmInitAct:
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::LmInitAct ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::LmInitAct ) );
                        break;
                case InlineItem::LmInitTokStart:
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::LmInitTokStart ) );
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::LmInitTokStart ) );
                        break;
                case InlineItem::LmSetTokStart:
-                       outList->append( new GenInlineItem( GenInputLoc(), GenInlineItem::LmSetTokStart ) );
-                       xmlParser.cgd->hasLongestMatch = true;
+                       outList->append( new GenInlineItem( InputLoc(), GenInlineItem::LmSetTokStart ) );
+                       inputData.cgd->hasLongestMatch = true;
                        break;
                }
        }
@@ -1003,23 +1014,23 @@ void XMLCodeGen::writeXML()
                "</ragel_def>\n";
 }
 
-void XMLCodeGen::makeExports()
+void BackendGen::makeExports()
 {
        for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ )
-               xmlParser.cgd->exportList.append( new Export( exp->name, exp->key ) );
+               inputData.cgd->exportList.append( new Export( exp->name, exp->key ) );
 }
 
-void XMLCodeGen::makeAction( Action *action )
+void BackendGen::makeAction( Action *action )
 {
        GenInlineList *genList = new GenInlineList;
        makeGenInlineList( genList, action->inlineList );
 
-       xmlParser.cgd->newAction( xmlParser.curAction++, action->name, 
+       inputData.cgd->newAction( curAction++, action->name, 
                        action->loc.line, action->loc.col, genList );
 }
 
 
-void XMLCodeGen::makeActionList()
+void BackendGen::makeActionList()
 {
        /* Determine which actions to write. */
        int nextActionId = 0;
@@ -1029,8 +1040,8 @@ void XMLCodeGen::makeActionList()
        }
 
        /* Write the list. */
-       xmlParser.cgd->initActionList( nextActionId );
-       xmlParser.curAction = 0;
+       inputData.cgd->initActionList( nextActionId );
+       curAction = 0;
 
        for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
                if ( act->actionId >= 0 )
@@ -1038,7 +1049,7 @@ void XMLCodeGen::makeActionList()
        }
 }
 
-void XMLCodeGen::makeActionTableList()
+void BackendGen::makeActionTableList()
 {
        /* Must first order the action tables based on their id. */
        int numTables = nextActionTableId;
@@ -1046,30 +1057,33 @@ void XMLCodeGen::makeActionTableList()
        for ( ActionTableMap::Iter at = actionTableMap; at.lte(); at++ )
                tables[at->id] = at;
 
-       xmlParser.cgd->initActionTableList( numTables );
-       xmlParser.curActionTable = 0;
+       inputData.cgd->initActionTableList( numTables );
+       curActionTable = 0;
 
        for ( int t = 0; t < numTables; t++ ) {
                long length = tables[t]->key.length();
 
                /* Collect the action table. */
-               RedAction *redAct = xmlParser.cgd->allActionTables + xmlParser.curActionTable;
-               redAct->actListId = xmlParser.curActionTable;
+               RedAction *redAct = inputData.cgd->allActionTables + curActionTable;
+               redAct->actListId = curActionTable;
                redAct->key.setAsNew( length );
 
                for ( ActionTable::Iter atel = tables[t]->key; atel.lte(); atel++ ) {
                        redAct->key[atel.pos()].key = 0;
-                       redAct->key[atel.pos()].value = xmlParser.cgd->allActions + 
+                       redAct->key[atel.pos()].value = inputData.cgd->allActions + 
                                        atel->value->actionId;
                }
 
-               xmlParser.curActionTable += 1;
+               /* Insert into the action table map. */
+               inputData.cgd->redFsm->actionMap.insert( redAct );
+
+               curActionTable += 1;
        }
 
        delete[] tables;
 }
 
-void XMLCodeGen::makeConditions()
+void BackendGen::makeConditions()
 {
        if ( condData->condSpaceMap.length() > 0 ) {
                long nextCondSpaceId = 0;
@@ -1077,20 +1091,20 @@ void XMLCodeGen::makeConditions()
                        cs->condSpaceId = nextCondSpaceId++;
 
                long listLength = condData->condSpaceMap.length();
-               xmlParser.cgd->initCondSpaceList( listLength );
-               xmlParser.curCondSpace = 0;
+               inputData.cgd->initCondSpaceList( listLength );
+               curCondSpace = 0;
 
                for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
                        long id = cs->condSpaceId;
-                       xmlParser.cgd->newCondSpace( xmlParser.curCondSpace, id, cs->baseKey );
+                       inputData.cgd->newCondSpace( curCondSpace, id, cs->baseKey );
                        for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
-                               xmlParser.cgd->condSpaceItem( xmlParser.curCondSpace, (*csi)->actionId );
-                       xmlParser.curCondSpace += 1;
+                               inputData.cgd->condSpaceItem( curCondSpace, (*csi)->actionId );
+                       curCondSpace += 1;
                }
        }
 }
 
-bool XMLCodeGen::makeNameInst( std::string &res, NameInst *nameInst )
+bool BackendGen::makeNameInst( std::string &res, NameInst *nameInst )
 {
        bool written = false;
        if ( nameInst->parent != 0 )
@@ -1106,12 +1120,12 @@ bool XMLCodeGen::makeNameInst( std::string &res, NameInst *nameInst )
        return written;
 }
 
-void XMLCodeGen::makeEntryPoints()
+void BackendGen::makeEntryPoints()
 {
        /* List of entry points other than start state. */
        if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) {
                if ( pd->lmRequiresErrorState )
-                       xmlParser.cgd->setForcedErrorState();
+                       inputData.cgd->setForcedErrorState();
 
                for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) {
                        /* Get the name instantiation from nameIndex. */
@@ -1119,12 +1133,12 @@ void XMLCodeGen::makeEntryPoints()
                        std::string name;
                        makeNameInst( name, nameInst );
                        StateAp *state = en->value;
-                       xmlParser.cgd->addEntryPoint( strdup(name.c_str()), state->alg.stateNum );
+                       inputData.cgd->addEntryPoint( strdup(name.c_str()), state->alg.stateNum );
                }
        }
 }
 
-void XMLCodeGen::makeStateActions( StateAp *state )
+void BackendGen::makeStateActions( StateAp *state )
 {
        RedActionTable *toStateActions = 0;
        if ( state->toStateActionTable.length() > 0 )
@@ -1153,11 +1167,11 @@ void XMLCodeGen::makeStateActions( StateAp *state )
                if ( eofActions != 0 )
                        eof = eofActions->id;
 
-               xmlParser.cgd->setStateActions( xmlParser.curState, to, from, eof );
+               inputData.cgd->setStateActions( curState, to, from, eof );
        }
 }
 
-void XMLCodeGen::makeEofTrans( StateAp *state )
+void BackendGen::makeEofTrans( StateAp *state )
 {
        RedActionTable *eofActions = 0;
        if ( state->eofActionTable.length() > 0 )
@@ -1171,25 +1185,25 @@ void XMLCodeGen::makeEofTrans( StateAp *state )
                if ( eofActions != 0 )
                        action = eofActions->id;
 
-               xmlParser.cgd->setEofTrans( xmlParser.curState, targ, action );
+               inputData.cgd->setEofTrans( curState, targ, action );
        }
 }
 
-void XMLCodeGen::makeStateConditions( StateAp *state )
+void BackendGen::makeStateConditions( StateAp *state )
 {
        if ( state->stateCondList.length() > 0 ) {
                long length = state->stateCondList.length();
-               xmlParser.cgd->initStateCondList( xmlParser.curState, length );
-               xmlParser.curStateCond = 0;
+               inputData.cgd->initStateCondList( curState, length );
+               curStateCond = 0;
 
                for ( StateCondList::Iter scdi = state->stateCondList; scdi.lte(); scdi++ ) {
-                       xmlParser.cgd->addStateCond( xmlParser.curState, scdi->lowKey, scdi->highKey, 
+                       inputData.cgd->addStateCond( curState, scdi->lowKey, scdi->highKey, 
                                        scdi->condSpace->condSpaceId );
                }
        }
 }
 
-void XMLCodeGen::makeTrans( Key lowKey, Key highKey, TransAp *trans )
+void BackendGen::makeTrans( Key lowKey, Key highKey, TransAp *trans )
 {
        /* First reduce the action. */
        RedActionTable *actionTable = 0;
@@ -1204,11 +1218,10 @@ void XMLCodeGen::makeTrans( Key lowKey, Key highKey, TransAp *trans )
        if ( actionTable != 0 )
                action = actionTable->id;
 
-       xmlParser.cgd->newTrans( xmlParser.curState, xmlParser.curTrans++, lowKey, highKey, targ, action );
+       inputData.cgd->newTrans( curState, curTrans++, lowKey, highKey, targ, action );
 }
 
-
-void XMLCodeGen::makeTransList( StateAp *state )
+void BackendGen::makeTransList( StateAp *state )
 {
        TransListVect outList;
 
@@ -1221,22 +1234,22 @@ void XMLCodeGen::makeTransList( StateAp *state )
                }
        }
 
-       xmlParser.cgd->initTransList( xmlParser.curState, outList.length() );
-       xmlParser.curTrans = 0;
+       inputData.cgd->initTransList( curState, outList.length() );
+       curTrans = 0;
 
        for ( TransListVect::Iter tvi = outList; tvi.lte(); tvi++ )
                makeTrans( tvi->lowKey, tvi->highKey, tvi->value );
 
-       xmlParser.cgd->finishTransList( xmlParser.curState );
+       inputData.cgd->finishTransList( curState );
 }
 
 
-void XMLCodeGen::makeStateList()
+void BackendGen::makeStateList()
 {
        /* Write the list of states. */
        long length = fsm->stateList.length();
-       xmlParser.cgd->initStateList( length );
-       xmlParser.curState = 0;
+       inputData.cgd->initStateList( length );
+       curState = 0;
        for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
                makeStateActions( st );
                makeEofTrans( st );
@@ -1244,19 +1257,19 @@ void XMLCodeGen::makeStateList()
                makeTransList( st );
 
                long id = st->alg.stateNum;
-               xmlParser.cgd->setId( xmlParser.curState, id );
+               inputData.cgd->setId( curState, id );
 
                if ( st->isFinState() )
-                       xmlParser.cgd->setFinal( xmlParser.curState );
+                       inputData.cgd->setFinal( curState );
 
-               xmlParser.curState += 1;
+               curState += 1;
        }
 }
 
 
-void XMLCodeGen::makeMachine()
+void BackendGen::makeMachine()
 {
-       xmlParser.cgd->createMachine();
+       inputData.cgd->createMachine();
 
        /* Action tables. */
        reduceActionTables();
@@ -1266,46 +1279,73 @@ void XMLCodeGen::makeMachine()
        makeConditions();
 
        /* Start State. */
-       xmlParser.cgd->setStartState( fsm->startState->alg.stateNum );
+       inputData.cgd->setStartState( fsm->startState->alg.stateNum );
 
        /* Error state. */
        if ( fsm->errState != 0 )
-               xmlParser.cgd->setErrorState( fsm->errState->alg.stateNum );
+               inputData.cgd->setErrorState( fsm->errState->alg.stateNum );
 
        makeEntryPoints();
        makeStateList();
+
+       inputData.cgd->closeMachine();
 }
 
-void XMLCodeGen::makeBackend()
+void BackendGen::open_ragel_def( char *fsmName )
 {
-       /* Open the definition. */
-       xmlParser.open_ragel_def( fsmName );
+       CodeGenMapEl *mapEl = inputData.codeGenMap.find( fsmName );
+       if ( mapEl != 0 )
+               inputData.cgd = mapEl->value;
+       else {
+               inputData.cgd = makeCodeGen( inputData.sourceFileName, fsmName, 
+                               *inputData.outStream, inputData.wantComplete );
+               inputData.codeGenMap.insert( fsmName, inputData.cgd );
+       }
+}
+
+void BackendGen::close_ragel_def()
+{
+       /* Do this before distributing transitions out to singles and defaults
+        * makes life easier. */
+       inputData.cgd->redFsm->maxKey = inputData.cgd->findMaxKey();
+
+       inputData.cgd->redFsm->assignActionLocs();
 
+       /* Find the first final state (The final state with the lowest id). */
+       inputData.cgd->redFsm->findFirstFinState();
+
+       /* Call the user's callback. */
+       inputData.cgd->finishRagelDef();
+}
+
+
+void BackendGen::makeBackend()
+{
        /* Alphabet type. */
-       xmlParser.cgd->setAlphType( keyOps->alphType->internalName );
+       inputData.cgd->setAlphType( keyOps->alphType->internalName );
        
        /* Getkey expression. */
        if ( pd->getKeyExpr != 0 ) {
-               xmlParser.cgd->getKeyExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->getKeyExpr, pd->getKeyExpr );
+               inputData.cgd->getKeyExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->getKeyExpr, pd->getKeyExpr );
        }
 
        /* Access expression. */
        if ( pd->accessExpr != 0 ) {
-               xmlParser.cgd->accessExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->accessExpr, pd->accessExpr );
+               inputData.cgd->accessExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->accessExpr, pd->accessExpr );
        }
 
        /* PrePush expression. */
        if ( pd->prePushExpr != 0 ) {
-               xmlParser.cgd->prePushExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->prePushExpr, pd->prePushExpr );
+               inputData.cgd->prePushExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->prePushExpr, pd->prePushExpr );
        }
 
        /* PostPop expression. */
        if ( pd->postPopExpr != 0 ) {
-               xmlParser.cgd->postPopExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->postPopExpr, pd->postPopExpr );
+               inputData.cgd->postPopExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->postPopExpr, pd->postPopExpr );
        }
 
        /*
@@ -1313,57 +1353,59 @@ void XMLCodeGen::makeBackend()
         */
 
        if ( pd->pExpr != 0 ) {
-               xmlParser.cgd->pExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->pExpr, pd->pExpr );
+               inputData.cgd->pExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->pExpr, pd->pExpr );
        }
        
        if ( pd->peExpr != 0 ) {
-               xmlParser.cgd->peExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->peExpr, pd->peExpr );
+               inputData.cgd->peExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->peExpr, pd->peExpr );
        }
 
        if ( pd->eofExpr != 0 ) {
-               xmlParser.cgd->eofExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->eofExpr, pd->eofExpr );
+               inputData.cgd->eofExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->eofExpr, pd->eofExpr );
        }
        
        if ( pd->csExpr != 0 ) {
-               xmlParser.cgd->csExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->csExpr, pd->csExpr );
+               inputData.cgd->csExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->csExpr, pd->csExpr );
        }
        
        if ( pd->topExpr != 0 ) {
-               xmlParser.cgd->topExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->topExpr, pd->topExpr );
+               inputData.cgd->topExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->topExpr, pd->topExpr );
        }
        
        if ( pd->stackExpr != 0 ) {
-               xmlParser.cgd->stackExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->stackExpr, pd->stackExpr );
+               inputData.cgd->stackExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->stackExpr, pd->stackExpr );
        }
        
        if ( pd->actExpr != 0 ) {
-               xmlParser.cgd->actExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->actExpr, pd->actExpr );
+               inputData.cgd->actExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->actExpr, pd->actExpr );
        }
        
        if ( pd->tokstartExpr != 0 ) {
-               xmlParser.cgd->tokstartExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->tokstartExpr, pd->tokstartExpr );
+               inputData.cgd->tokstartExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->tokstartExpr, pd->tokstartExpr );
        }
        
        if ( pd->tokendExpr != 0 ) {
-               xmlParser.cgd->tokendExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->tokendExpr, pd->tokendExpr );
+               inputData.cgd->tokendExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->tokendExpr, pd->tokendExpr );
        }
        
        if ( pd->dataExpr != 0 ) {
-               xmlParser.cgd->dataExpr = new GenInlineList;
-               makeGenInlineList( xmlParser.cgd->dataExpr, pd->dataExpr );
+               inputData.cgd->dataExpr = new GenInlineList;
+               makeGenInlineList( inputData.cgd->dataExpr, pd->dataExpr );
        }
        
        makeExports();
        makeMachine();
+
+       close_ragel_def();
 }