/* * Copyright 2005-2018 Adrian Thurston * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /* * XML Output not included in 7.0 (yet -- possibly) */ #include "ragel.h" #include "xml.h" #include "parsedata.h" #include "fsmgraph.h" #include "gendata.h" #include "inputdata.h" #include #include "version.h" using std::endl; void InputData::processXML() { /* Compiles machines. */ prepareAllMachines(); if ( errorCount > 0 ) abortCompile( 1 ); createOutputStream(); if ( errorCount > 0 ) abortCompile( 1 ); /* * From this point on we should not be reporting any errors. */ openOutput(); writeXML( *outStream ); closeOutput(); } XMLCodeGen::XMLCodeGen( std::string fsmName, int machineId, FsmGbl *id, PdBase *pd, FsmAp *fsm, std::ostream &out ) : RedBase( id, pd, fsm, fsmName, machineId ), out(out) { } void XMLCodeGen::writeActionList() { /* Determine which actions to write. */ int nextActionId = 0; for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) { if ( act->numRefs() > 0 || act->numCondRefs > 0 ) act->actionId = nextActionId++; } /* Write the list. */ out << " \n"; for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) { if ( act->actionId >= 0 ) writeAction( act ); } out << " \n"; } void XMLCodeGen::writeActionTableList() { /* Must first order the action tables based on their id. */ int numTables = nextActionTableId; RedActionTable **tables = new RedActionTable*[numTables]; for ( ActionTableMap::Iter at = actionTableMap; at.lte(); at++ ) tables[at->id] = at; out << " \n"; for ( int t = 0; t < numTables; t++ ) { out << " key.length() << "\">"; for ( ActionTable::Iter atel = tables[t]->key; atel.lte(); atel++ ) { out << atel->value->actionId; if ( ! atel.last() ) out << " "; } out << "\n"; } out << " \n"; delete[] tables; } void XMLCodeGen::writeKey( Key key ) { if ( keyOps->isSigned ) out << key.getVal(); else out << (unsigned long) key.getVal(); } void XMLCodeGen::writeTrans( Key lowKey, Key highKey, TransAp *trans ) { /* Write the transition. */ out << " "; writeKey( lowKey ); out << " "; writeKey( highKey ); if ( trans->plain() ) { /* First reduce the action. */ RedActionTable *actionTable = 0; if ( trans->tdap()->actionTable.length() > 0 ) actionTable = actionTableMap.find( trans->tdap()->actionTable ); if ( trans->tdap()->toState != 0 ) out << " " << trans->tdap()->toState->alg.stateNum; else out << " x"; if ( actionTable != 0 ) out << " " << actionTable->id; else out << " x"; } else { for ( CondList::Iter ctel = trans->tcap()->condList; ctel.lte(); ctel++ ) { out << ""; out << trans->tcap()->condSpace->condSpaceId; /* First reduce the action. */ RedActionTable *actionTable = 0; if ( ctel->actionTable.length() > 0 ) actionTable = actionTableMap.find( ctel->actionTable ); if ( ctel->toState != 0 ) out << " " << ctel->toState->alg.stateNum; else out << " x"; if ( actionTable != 0 ) out << " " << actionTable->id; else out << " x"; out << ""; } } out << "\n"; } void XMLCodeGen::writeTransList( StateAp *state ) { TransListVect outList; out << " outList.length() << "\">\n"; /* If there is only are no ranges the task is simple. */ if ( state->outList.length() > 0 ) { /* Loop each source range. */ for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) { /* Reduce the transition. If it reduced to anything then add it. */ appendTrans( outList, trans->lowKey, trans->highKey, trans ); } } for ( TransListVect::Iter tvi = outList; tvi.lte(); tvi++ ) writeTrans( tvi->lowKey, tvi->highKey, tvi->value ); out << " \n"; } void XMLCodeGen::writeEofTrans( StateAp *state ) { RedActionTable *eofActions = 0; if ( state->eofActionTable.length() > 0 ) eofActions = actionTableMap.find( state->eofActionTable ); /* The is used when there is an eof target, otherwise the eof * action goes into state actions. */ if ( state->eofTarget != 0 ) { out << " " << state->eofTarget->alg.stateNum; if ( eofActions != 0 ) out << " " << eofActions->id; else out << " x"; out << "" << endl; } } void XMLCodeGen::writeText( InlineItem *item ) { if ( item->prev == 0 || item->prev->type != InlineItem::Text ) out << ""; xmlEscapeHost( out, item->data.c_str(), item->data.size() ); if ( item->next == 0 || item->next->type != InlineItem::Text ) out << ""; } void XMLCodeGen::writeGoto( InlineItem *item ) { if ( pd->generatingSectionSubset ) out << "-1"; else { EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id ); out << "" << targ->value->alg.stateNum << ""; } } void XMLCodeGen::writeCall( InlineItem *item ) { if ( pd->generatingSectionSubset ) out << "-1"; else { EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id ); out << "" << targ->value->alg.stateNum << ""; } } void XMLCodeGen::writeNext( InlineItem *item ) { if ( pd->generatingSectionSubset ) out << "-1"; else { EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id ); out << "" << targ->value->alg.stateNum << ""; } } void XMLCodeGen::writeGotoExpr( InlineItem *item ) { out << ""; writeInlineList( item->children ); out << ""; } void XMLCodeGen::writeCallExpr( InlineItem *item ) { out << ""; writeInlineList( item->children ); out << ""; } void XMLCodeGen::writeNextExpr( InlineItem *item ) { out << ""; writeInlineList( item->children ); out << ""; } void XMLCodeGen::writeEntry( InlineItem *item ) { if ( pd->generatingSectionSubset ) out << "-1"; else { EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id ); out << "" << targ->value->alg.stateNum << ""; } } void XMLCodeGen::writeActionExec( InlineItem *item ) { out << ""; writeInlineList( item->children ); out << ""; } void XMLCodeGen::writeLmOnLast( InlineItem *item ) { out << "1"; if ( item->longestMatchPart->action != 0 ) { out << ""; writeInlineList( item->longestMatchPart->action->inlineList ); out << ""; } } void XMLCodeGen::writeLmOnNext( InlineItem *item ) { out << "0"; out << ""; if ( item->longestMatchPart->action != 0 ) { out << ""; writeInlineList( item->longestMatchPart->action->inlineList ); out << ""; } } void XMLCodeGen::writeLmOnLagBehind( InlineItem *item ) { out << ""; if ( item->longestMatchPart->action != 0 ) { out << ""; writeInlineList( item->longestMatchPart->action->inlineList ); out << ""; } } void XMLCodeGen::writeLmSwitch( InlineItem *item ) { LongestMatch *longestMatch = item->longestMatch; out << "\n"; /* We can't put the here because we may need to handle the error * case and in that case p should not be changed. Instead use a default * label in the switch to adjust p when user actions are not set. An id of * -1 indicates the default. */ if ( longestMatch->lmSwitchHandlesError ) { /* If the switch handles error then we should have also forced the * error state. */ assert( fsm->errState != 0 ); out << " "; out << "" << fsm->errState->alg.stateNum << ""; out << "\n"; } bool needDefault = false; for ( LmPartList::Iter lmi = *longestMatch->longestMatchList; lmi.lte(); lmi++ ) { if ( lmi->inLmSelect ) { if ( lmi->action == 0 ) needDefault = true; else { /* Open the action. Write it with the context that sets up _p * when doing control flow changes from inside the machine. */ out << " longestMatchId << "\">"; out << ""; writeInlineList( lmi->action->inlineList ); out << "\n"; } } } if ( needDefault ) { out << " " "\n"; } out << " "; } void XMLCodeGen::writeInlineList( InlineList *inlineList ) { for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) { switch ( item->type ) { case InlineItem::Text: writeText( item ); break; case InlineItem::Goto: writeGoto( item ); break; case InlineItem::GotoExpr: writeGotoExpr( item ); break; case InlineItem::Call: writeCall( item ); break; case InlineItem::CallExpr: writeCallExpr( item ); break; case InlineItem::Next: writeNext( item ); break; case InlineItem::NextExpr: writeNextExpr( item ); break; case InlineItem::Break: out << ""; break; case InlineItem::Ret: out << ""; break; case InlineItem::PChar: out << ""; break; case InlineItem::Char: out << ""; break; case InlineItem::Curs: out << ""; break; case InlineItem::Targs: out << ""; break; case InlineItem::Entry: writeEntry( item ); break; case InlineItem::Hold: out << ""; break; case InlineItem::Exec: writeActionExec( item ); break; case InlineItem::LmSetActId: out << "" << item->longestMatchPart->longestMatchId << ""; break; case InlineItem::LmSetTokEnd: out << "1"; break; case InlineItem::LmOnLast: writeLmOnLast( item ); break; case InlineItem::LmOnNext: writeLmOnNext( item ); break; case InlineItem::LmOnLagBehind: writeLmOnLagBehind( item ); break; case InlineItem::LmSwitch: writeLmSwitch( item ); break; case InlineItem::LmInitAct: out << ""; break; case InlineItem::LmInitTokStart: out << ""; break; case InlineItem::LmSetTokStart: out << ""; break; /* Stubbed. */ case InlineItem::Ncall: case InlineItem::NcallExpr: case InlineItem::Nret: case InlineItem::Nbreak: case InlineItem::Stmt: case InlineItem::Subst: case InlineItem::NfaWrapAction: case InlineItem::NfaWrapConds: break; } } } void XMLCodeGen::writeAction( Action *action ) { out << " actionId << "\""; if ( !action->name.empty() ) out << " name=\"" << action->name << "\""; out << " line=\"" << action->loc.line << "\" col=\"" << action->loc.col << "\">"; writeInlineList( action->inlineList ); out << "\n"; } void xmlEscapeHost( std::ostream &out, const char *data, long len ) { const char *end = data + len; while ( data != end ) { switch ( *data ) { case '<': out << "<"; break; case '>': out << ">"; break; case '&': out << "&"; break; default: out << *data; break; } data += 1; } } void XMLCodeGen::writeStateActions( StateAp *state ) { RedActionTable *toStateActions = 0; if ( state->toStateActionTable.length() > 0 ) toStateActions = actionTableMap.find( state->toStateActionTable ); RedActionTable *fromStateActions = 0; if ( state->fromStateActionTable.length() > 0 ) fromStateActions = actionTableMap.find( state->fromStateActionTable ); /* EOF actions go out here only if the state has no eof target. If it has * an eof target then an eof transition will be used instead. */ RedActionTable *eofActions = 0; if ( state->eofTarget == 0 && state->eofActionTable.length() > 0 ) eofActions = actionTableMap.find( state->eofActionTable ); if ( toStateActions != 0 || fromStateActions != 0 || eofActions != 0 ) { out << " "; if ( toStateActions != 0 ) out << toStateActions->id; else out << "x"; if ( fromStateActions != 0 ) out << " " << fromStateActions->id; else out << " x"; if ( eofActions != 0 ) out << " " << eofActions->id; else out << " x"; out << "\n"; } } void XMLCodeGen::writeStateList() { /* Write the list of states. */ out << " stateList.length() << "\">\n"; for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) { out << " alg.stateNum << "\""; if ( st->isFinState() ) out << " final=\"t\""; out << ">\n"; writeStateActions( st ); writeEofTrans( st ); writeTransList( st ); out << " \n"; if ( !st.last() ) out << "\n"; } out << " \n"; } bool XMLCodeGen::writeNameInst( NameInst *nameInst ) { bool written = false; if ( nameInst->parent != 0 ) written = writeNameInst( nameInst->parent ); if ( !nameInst->name.empty() ) { if ( written ) out << '_'; out << nameInst->name; written = true; } return written; } void XMLCodeGen::writeEntryPoints() { /* List of entry points other than start state. */ if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) { out << " lmRequiresErrorState ) out << " error=\"t\""; out << ">\n"; for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) { /* Get the name instantiation from nameIndex. */ NameInst *nameInst = pd->nameIndex[en->key]; StateAp *state = en->value; out << " " << state->alg.stateNum << "\n"; } out << " \n"; } } void XMLCodeGen::writeMachine() { /* Open the machine. */ out << " \n"; /* Action tables. */ reduceActionTables(); writeActionList(); writeActionTableList(); writeConditions(); /* Start state. */ out << " " << fsm->startState->alg.stateNum << "\n"; /* Error state. */ if ( fsm->errState != 0 ) { out << " " << fsm->errState->alg.stateNum << "\n"; } writeEntryPoints(); writeStateList(); out << " \n"; } void XMLCodeGen::writeConditions() { CondData *condData = fsm->ctx->condData; if ( condData->condSpaceMap.length() > 0 ) { long nextCondSpaceId = 0; for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) cs->condSpaceId = nextCondSpaceId++; out << " condSpaceMap.length() << "\">\n"; for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) { out << " condSpaceId << "\" length=\"" << cs->condSet.length() << "\">"; for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ ) out << " " << (*csi)->actionId; out << "\n"; } out << " \n"; } } void XMLCodeGen::writeExports() { if ( pd->exportList.length() > 0 ) { out << " \n"; for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ ) { out << " name << "\">"; writeKey( exp->key ); out << "\n"; } out << " \n"; } } void XMLCodeGen::writeXML() { /* Open the definition. */ out << "\n"; /* Alphabet type. */ out << " " << keyOps->alphType->internalName << "\n"; /* Getkey expression. */ if ( pd->getKeyExpr != 0 ) { out << " "; writeInlineList( pd->getKeyExpr ); out << "\n"; } /* Access expression. */ if ( pd->accessExpr != 0 ) { out << " "; writeInlineList( pd->accessExpr ); out << "\n"; } /* PrePush expression. */ if ( pd->prePushExpr != 0 ) { out << " "; writeInlineList( pd->prePushExpr->inlineList ); out << "\n"; } /* PostPop expression. */ if ( pd->postPopExpr != 0 ) { out << " "; writeInlineList( pd->postPopExpr->inlineList ); out << "\n"; } /* * Variable expressions. */ if ( pd->pExpr != 0 ) { out << " "; writeInlineList( pd->pExpr ); out << "\n"; } if ( pd->peExpr != 0 ) { out << " "; writeInlineList( pd->peExpr ); out << "\n"; } if ( pd->eofExpr != 0 ) { out << " "; writeInlineList( pd->eofExpr ); out << "\n"; } if ( pd->csExpr != 0 ) { out << " "; writeInlineList( pd->csExpr ); out << "\n"; } if ( pd->topExpr != 0 ) { out << " "; writeInlineList( pd->topExpr ); out << "\n"; } if ( pd->stackExpr != 0 ) { out << " "; writeInlineList( pd->stackExpr ); out << "\n"; } if ( pd->actExpr != 0 ) { out << " "; writeInlineList( pd->actExpr ); out << "\n"; } if ( pd->tokstartExpr != 0 ) { out << " "; writeInlineList( pd->tokstartExpr ); out << "\n"; } if ( pd->tokendExpr != 0 ) { out << " "; writeInlineList( pd->tokendExpr ); out << "\n"; } if ( pd->dataExpr != 0 ) { out << " "; writeInlineList( pd->dataExpr ); out << "\n"; } writeExports(); writeMachine(); out << "\n"; } void InputData::writeLanguage( std::ostream &out ) { out << " lang=\""; switch ( hostLang->lang ) { case HostLang::C: out << "C"; break; case HostLang::D: out << "D"; break; case HostLang::Go: out << "Go"; break; case HostLang::Java: out << "Java"; break; case HostLang::Ruby: out << "Ruby"; break; case HostLang::CSharp: out << "C#"; break; case HostLang::OCaml: out << "OCaml"; break; case HostLang::Crack: out << "Crack"; break; case HostLang::Asm: out << "ASM"; break; case HostLang::Rust: out << "Rust"; break; case HostLang::Julia: out << "Julia"; break; case HostLang::JS: out << "JavaScript"; break; } out << "\""; } void InputData::writeXML( std::ostream &out ) { out << "\n"; for ( ParseDataDict::Iter pdel = parseDataDict; pdel.lte(); pdel++ ) { ParseData *pd = pdel->value; if ( pd->instanceList.length() > 0 ) pd->generateXML( *outStream ); } out << "\n"; }