2 * Copyright 2005-2007 Adrian Thurston <thurston@complang.org>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "xmlcodegen.h"
26 #include "parsedata.h"
33 GenBase::GenBase( char *fsmName, ParseData *pd, FsmAp *fsm, XmlParser &xmlParser )
43 void GenBase::appendTrans( TransListVect &outList, Key lowKey,
44 Key highKey, TransAp *trans )
46 if ( trans->toState != 0 || trans->actionTable.length() > 0 )
47 outList.append( TransEl( lowKey, highKey, trans ) );
50 void GenBase::reduceActionTables()
52 /* Reduce the actions tables to a set. */
53 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
54 RedActionTable *actionTable = 0;
56 /* Reduce To State Actions. */
57 if ( st->toStateActionTable.length() > 0 ) {
58 if ( actionTableMap.insert( st->toStateActionTable, &actionTable ) )
59 actionTable->id = nextActionTableId++;
62 /* Reduce From State Actions. */
63 if ( st->fromStateActionTable.length() > 0 ) {
64 if ( actionTableMap.insert( st->fromStateActionTable, &actionTable ) )
65 actionTable->id = nextActionTableId++;
68 /* Reduce EOF actions. */
69 if ( st->eofActionTable.length() > 0 ) {
70 if ( actionTableMap.insert( st->eofActionTable, &actionTable ) )
71 actionTable->id = nextActionTableId++;
74 /* Loop the transitions and reduce their actions. */
75 for ( TransList::Iter trans = st->outList; trans.lte(); trans++ ) {
76 if ( trans->actionTable.length() > 0 ) {
77 if ( actionTableMap.insert( trans->actionTable, &actionTable ) )
78 actionTable->id = nextActionTableId++;
84 XMLCodeGen::XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm,
85 std::ostream &out, XmlParser &xmlParser )
87 GenBase(fsmName, pd, fsm, xmlParser),
93 void XMLCodeGen::writeActionList()
95 /* Determine which actions to write. */
97 for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
98 if ( act->numRefs() > 0 || act->numCondRefs > 0 )
99 act->actionId = nextActionId++;
102 /* Write the list. */
103 out << " <action_list length=\"" << nextActionId << "\">\n";
104 for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
105 if ( act->actionId >= 0 )
108 out << " </action_list>\n";
111 void XMLCodeGen::writeActionTableList()
113 /* Must first order the action tables based on their id. */
114 int numTables = nextActionTableId;
115 RedActionTable **tables = new RedActionTable*[numTables];
116 for ( ActionTableMap::Iter at = actionTableMap; at.lte(); at++ )
119 out << " <action_table_list length=\"" << numTables << "\">\n";
120 for ( int t = 0; t < numTables; t++ ) {
121 out << " <action_table id=\"" << t << "\" length=\"" <<
122 tables[t]->key.length() << "\">";
123 for ( ActionTable::Iter atel = tables[t]->key; atel.lte(); atel++ ) {
124 out << atel->value->actionId;
128 out << "</action_table>\n";
130 out << " </action_table_list>\n";
135 void XMLCodeGen::writeKey( Key key )
137 if ( keyOps->isSigned )
140 out << (unsigned long) key.getVal();
143 void XMLCodeGen::writeTrans( Key lowKey, Key highKey, TransAp *trans )
145 /* First reduce the action. */
146 RedActionTable *actionTable = 0;
147 if ( trans->actionTable.length() > 0 )
148 actionTable = actionTableMap.find( trans->actionTable );
150 /* Write the transition. */
156 if ( trans->toState != 0 )
157 out << " " << trans->toState->alg.stateNum;
161 if ( actionTable != 0 )
162 out << " " << actionTable->id;
168 void XMLCodeGen::writeTransList( StateAp *state )
170 TransListVect outList;
172 /* If there is only are no ranges the task is simple. */
173 if ( state->outList.length() > 0 ) {
174 /* Loop each source range. */
175 for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) {
176 /* Reduce the transition. If it reduced to anything then add it. */
177 appendTrans( outList, trans->lowKey, trans->highKey, trans );
181 out << " <trans_list length=\"" << outList.length() << "\">\n";
182 for ( TransListVect::Iter tvi = outList; tvi.lte(); tvi++ )
183 writeTrans( tvi->lowKey, tvi->highKey, tvi->value );
184 out << " </trans_list>\n";
187 void XMLCodeGen::writeEofTrans( StateAp *state )
189 RedActionTable *eofActions = 0;
190 if ( state->eofActionTable.length() > 0 )
191 eofActions = actionTableMap.find( state->eofActionTable );
193 /* The <eof_t> is used when there is an eof target, otherwise the eof
194 * action goes into state actions. */
195 if ( state->eofTarget != 0 ) {
196 out << " <eof_t>" << state->eofTarget->alg.stateNum;
198 if ( eofActions != 0 )
199 out << " " << eofActions->id;
203 out << "</eof_t>" << endl;
207 void XMLCodeGen::writeText( InlineItem *item )
209 if ( item->prev == 0 || item->prev->type != InlineItem::Text )
211 xmlEscapeHost( out, item->data, strlen(item->data) );
212 if ( item->next == 0 || item->next->type != InlineItem::Text )
216 void XMLCodeGen::writeGoto( InlineItem *item )
218 if ( pd->generatingSectionSubset )
219 out << "<goto>-1</goto>";
221 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
222 out << "<goto>" << targ->value->alg.stateNum << "</goto>";
226 void XMLCodeGen::writeCall( InlineItem *item )
228 if ( pd->generatingSectionSubset )
229 out << "<call>-1</call>";
231 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
232 out << "<call>" << targ->value->alg.stateNum << "</call>";
236 void XMLCodeGen::writeNext( InlineItem *item )
238 if ( pd->generatingSectionSubset )
239 out << "<next>-1</next>";
241 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
242 out << "<next>" << targ->value->alg.stateNum << "</next>";
246 void XMLCodeGen::writeGotoExpr( InlineItem *item )
248 out << "<goto_expr>";
249 writeInlineList( item->children );
250 out << "</goto_expr>";
253 void XMLCodeGen::writeCallExpr( InlineItem *item )
255 out << "<call_expr>";
256 writeInlineList( item->children );
257 out << "</call_expr>";
260 void XMLCodeGen::writeNextExpr( InlineItem *item )
262 out << "<next_expr>";
263 writeInlineList( item->children );
264 out << "</next_expr>";
267 void XMLCodeGen::writeEntry( InlineItem *item )
269 if ( pd->generatingSectionSubset )
270 out << "<entry>-1</entry>";
272 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
273 out << "<entry>" << targ->value->alg.stateNum << "</entry>";
277 void XMLCodeGen::writeActionExec( InlineItem *item )
280 writeInlineList( item->children );
284 void XMLCodeGen::writeLmOnLast( InlineItem *item )
286 out << "<set_tokend>1</set_tokend>";
288 if ( item->longestMatchPart->action != 0 ) {
289 out << "<sub_action>";
290 writeInlineList( item->longestMatchPart->action->inlineList );
291 out << "</sub_action>";
295 void XMLCodeGen::writeLmOnNext( InlineItem *item )
297 out << "<set_tokend>0</set_tokend>";
298 out << "<hold></hold>";
300 if ( item->longestMatchPart->action != 0 ) {
301 out << "<sub_action>";
302 writeInlineList( item->longestMatchPart->action->inlineList );
303 out << "</sub_action>";
307 void XMLCodeGen::writeLmOnLagBehind( InlineItem *item )
309 out << "<exec><get_tokend></get_tokend></exec>";
311 if ( item->longestMatchPart->action != 0 ) {
312 out << "<sub_action>";
313 writeInlineList( item->longestMatchPart->action->inlineList );
314 out << "</sub_action>";
318 void XMLCodeGen::writeLmSwitch( InlineItem *item )
320 LongestMatch *longestMatch = item->longestMatch;
321 out << "<lm_switch>\n";
323 /* We can't put the <exec> here because we may need to handle the error
324 * case and in that case p should not be changed. Instead use a default
325 * label in the switch to adjust p when user actions are not set. An id of
326 * -1 indicates the default. */
328 if ( longestMatch->lmSwitchHandlesError ) {
329 /* If the switch handles error then we should have also forced the
331 assert( fsm->errState != 0 );
333 out << " <sub_action id=\"0\">";
334 out << "<goto>" << fsm->errState->alg.stateNum << "</goto>";
335 out << "</sub_action>\n";
338 bool needDefault = false;
339 for ( LmPartList::Iter lmi = *longestMatch->longestMatchList; lmi.lte(); lmi++ ) {
340 if ( lmi->inLmSelect ) {
341 if ( lmi->action == 0 )
344 /* Open the action. Write it with the context that sets up _p
345 * when doing control flow changes from inside the machine. */
346 out << " <sub_action id=\"" << lmi->longestMatchId << "\">";
347 out << "<exec><get_tokend></get_tokend></exec>";
348 writeInlineList( lmi->action->inlineList );
349 out << "</sub_action>\n";
355 out << " <sub_action id=\"-1\"><exec><get_tokend>"
356 "</get_tokend></exec></sub_action>\n";
359 out << " </lm_switch>";
362 void XMLCodeGen::writeInlineList( InlineList *inlineList )
364 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
365 switch ( item->type ) {
366 case InlineItem::Text:
369 case InlineItem::Goto:
372 case InlineItem::GotoExpr:
373 writeGotoExpr( item );
375 case InlineItem::Call:
378 case InlineItem::CallExpr:
379 writeCallExpr( item );
381 case InlineItem::Next:
384 case InlineItem::NextExpr:
385 writeNextExpr( item );
387 case InlineItem::Break:
388 out << "<break></break>";
390 case InlineItem::Ret:
391 out << "<ret></ret>";
393 case InlineItem::PChar:
394 out << "<pchar></pchar>";
396 case InlineItem::Char:
397 out << "<char></char>";
399 case InlineItem::Curs:
400 out << "<curs></curs>";
402 case InlineItem::Targs:
403 out << "<targs></targs>";
405 case InlineItem::Entry:
409 case InlineItem::Hold:
410 out << "<hold></hold>";
412 case InlineItem::Exec:
413 writeActionExec( item );
416 case InlineItem::LmSetActId:
417 out << "<set_act>" <<
418 item->longestMatchPart->longestMatchId <<
421 case InlineItem::LmSetTokEnd:
422 out << "<set_tokend>1</set_tokend>";
425 case InlineItem::LmOnLast:
426 writeLmOnLast( item );
428 case InlineItem::LmOnNext:
429 writeLmOnNext( item );
431 case InlineItem::LmOnLagBehind:
432 writeLmOnLagBehind( item );
434 case InlineItem::LmSwitch:
435 writeLmSwitch( item );
438 case InlineItem::LmInitAct:
439 out << "<init_act></init_act>";
441 case InlineItem::LmInitTokStart:
442 out << "<init_tokstart></init_tokstart>";
444 case InlineItem::LmSetTokStart:
445 out << "<set_tokstart></set_tokstart>";
451 BackendGen::BackendGen( char *fsmName, ParseData *pd, FsmAp *fsm, XmlParser &xmlParser )
453 GenBase(fsmName, pd, fsm, xmlParser)
458 void BackendGen::makeText( GenInlineList *outList, InlineItem *item )
460 GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), GenInlineItem::Text );
461 inlineItem->data = item->data;
463 outList->append( inlineItem );
466 void BackendGen::makeTargetItem( GenInlineList *outList, long entryId, GenInlineItem::Type type )
469 if ( pd->generatingSectionSubset )
472 EntryMapEl *targ = fsm->entryPoints.find( entryId );
473 targetState = targ->value->alg.stateNum;
477 GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), type );
478 inlineItem->targId = targetState;
479 outList->append( inlineItem );
482 /* Make a sublist item with a given type. */
483 void BackendGen::makeSubList( GenInlineList *outList,
484 InlineList *inlineList, GenInlineItem::Type type )
486 /* Fill the sub list. */
487 GenInlineList *subList = new GenInlineList;
488 makeGenInlineList( subList, inlineList );
491 GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), type );
492 inlineItem->children = subList;
493 outList->append( inlineItem );
496 void BackendGen::makeLmOnLast( GenInlineList *outList, InlineItem *item )
498 makeSetTokend( outList, 1 );
500 if ( item->longestMatchPart->action != 0 ) {
501 makeSubList( outList,
502 item->longestMatchPart->action->inlineList,
503 GenInlineItem::SubAction );
507 void BackendGen::makeLmOnNext( GenInlineList *outList, InlineItem *item )
509 makeSetTokend( outList, 0 );
510 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Hold ) );
512 if ( item->longestMatchPart->action != 0 ) {
513 makeSubList( outList,
514 item->longestMatchPart->action->inlineList,
515 GenInlineItem::SubAction );
519 void BackendGen::makeExecGetTokend( GenInlineList *outList )
521 /* Make the Exec item. */
522 GenInlineItem *execItem = new GenInlineItem( InputLoc(), GenInlineItem::Exec );
523 execItem->children = new GenInlineList;
525 /* Make the GetTokEnd */
526 GenInlineItem *getTokend = new GenInlineItem( InputLoc(), GenInlineItem::LmGetTokEnd );
527 execItem->children->append( getTokend );
529 outList->append( execItem );
532 void BackendGen::makeLmOnLagBehind( GenInlineList *outList, InlineItem *item )
534 /* Jump to the tokend. */
535 makeExecGetTokend( outList );
537 if ( item->longestMatchPart->action != 0 ) {
538 makeSubList( outList,
539 item->longestMatchPart->action->inlineList,
540 GenInlineItem::SubAction );
544 void BackendGen::makeLmSwitch( GenInlineList *outList, InlineItem *item )
546 GenInlineItem *lmSwitch = new GenInlineItem( InputLoc(), GenInlineItem::LmSwitch );
547 GenInlineList *lmList = lmSwitch->children = new GenInlineList;
548 LongestMatch *longestMatch = item->longestMatch;
550 /* We can't put the <exec> here because we may need to handle the error
551 * case and in that case p should not be changed. Instead use a default
552 * label in the switch to adjust p when user actions are not set. An id of
553 * -1 indicates the default. */
555 if ( longestMatch->lmSwitchHandlesError ) {
556 /* If the switch handles error then we should have also forced the
558 assert( fsm->errState != 0 );
560 GenInlineItem *errCase = new GenInlineItem( InputLoc(), GenInlineItem::SubAction );
562 errCase->children = new GenInlineList;
565 GenInlineItem *gotoItem = new GenInlineItem( InputLoc(), GenInlineItem::Goto );
566 gotoItem->targId = fsm->errState->alg.stateNum;
567 errCase->children->append( gotoItem );
569 lmList->append( errCase );
572 bool needDefault = false;
573 for ( LmPartList::Iter lmi = *longestMatch->longestMatchList; lmi.lte(); lmi++ ) {
574 if ( lmi->inLmSelect ) {
575 if ( lmi->action == 0 )
578 /* Open the action. Write it with the context that sets up _p
579 * when doing control flow changes from inside the machine. */
580 GenInlineItem *lmCase = new GenInlineItem( InputLoc(),
581 GenInlineItem::SubAction );
582 lmCase->lmId = lmi->longestMatchId;
583 lmCase->children = new GenInlineList;
585 makeExecGetTokend( lmCase->children );
586 makeGenInlineList( lmCase->children, lmi->action->inlineList );
588 lmList->append( lmCase );
594 GenInlineItem *defCase = new GenInlineItem( InputLoc(),
595 GenInlineItem::SubAction );
597 defCase->children = new GenInlineList;
599 makeExecGetTokend( defCase->children );
601 lmList->append( defCase );
604 outList->append( lmSwitch );
607 void BackendGen::makeSetTokend( GenInlineList *outList, long offset )
609 GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), GenInlineItem::LmSetTokEnd );
610 inlineItem->offset = offset;
611 outList->append( inlineItem );
614 void BackendGen::makeSetAct( GenInlineList *outList, long lmId )
616 GenInlineItem *inlineItem = new GenInlineItem( InputLoc(), GenInlineItem::LmSetActId );
617 inlineItem->lmId = lmId;
618 outList->append( inlineItem );
621 void BackendGen::makeGenInlineList( GenInlineList *outList, InlineList *inList )
623 for ( InlineList::Iter item = *inList; item.lte(); item++ ) {
624 switch ( item->type ) {
625 case InlineItem::Text:
626 makeText( outList, item );
628 case InlineItem::Goto:
629 makeTargetItem( outList, item->nameTarg->id, GenInlineItem::Goto );
631 case InlineItem::GotoExpr:
632 makeSubList( outList, item->children, GenInlineItem::GotoExpr );
634 case InlineItem::Call:
635 makeTargetItem( outList, item->nameTarg->id, GenInlineItem::Call );
637 case InlineItem::CallExpr:
638 makeSubList( outList, item->children, GenInlineItem::CallExpr );
640 case InlineItem::Next:
641 makeTargetItem( outList, item->nameTarg->id, GenInlineItem::Next );
643 case InlineItem::NextExpr:
644 makeSubList( outList, item->children, GenInlineItem::NextExpr );
646 case InlineItem::Break:
647 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Break ) );
649 case InlineItem::Ret:
650 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Ret ) );
652 case InlineItem::PChar:
653 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::PChar ) );
655 case InlineItem::Char:
656 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Char ) );
658 case InlineItem::Curs:
659 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Curs ) );
661 case InlineItem::Targs:
662 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Targs ) );
664 case InlineItem::Entry:
665 makeTargetItem( outList, item->nameTarg->id, GenInlineItem::Entry );
668 case InlineItem::Hold:
669 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::Hold ) );
671 case InlineItem::Exec:
672 makeSubList( outList, item->children, GenInlineItem::Exec );
675 case InlineItem::LmSetActId:
676 makeSetAct( outList, item->longestMatchPart->longestMatchId );
678 case InlineItem::LmSetTokEnd:
679 makeSetTokend( outList, 1 );
682 case InlineItem::LmOnLast:
683 makeLmOnLast( outList, item );
685 case InlineItem::LmOnNext:
686 makeLmOnNext( outList, item );
688 case InlineItem::LmOnLagBehind:
689 makeLmOnLagBehind( outList, item );
691 case InlineItem::LmSwitch:
692 makeLmSwitch( outList, item );
695 case InlineItem::LmInitAct:
696 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::LmInitAct ) );
698 case InlineItem::LmInitTokStart:
699 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::LmInitTokStart ) );
701 case InlineItem::LmSetTokStart:
702 outList->append( new GenInlineItem( InputLoc(), GenInlineItem::LmSetTokStart ) );
703 xmlParser.cgd->hasLongestMatch = true;
710 void XMLCodeGen::writeAction( Action *action )
712 out << " <action id=\"" << action->actionId << "\"";
713 if ( action->name != 0 )
714 out << " name=\"" << action->name << "\"";
715 out << " line=\"" << action->loc.line << "\" col=\"" << action->loc.col << "\">";
716 writeInlineList( action->inlineList );
717 out << "</action>\n";
720 void xmlEscapeHost( std::ostream &out, char *data, long len )
722 char *end = data + len;
723 while ( data != end ) {
725 case '<': out << "<"; break;
726 case '>': out << ">"; break;
727 case '&': out << "&"; break;
728 default: out << *data; break;
734 void XMLCodeGen::writeStateActions( StateAp *state )
736 RedActionTable *toStateActions = 0;
737 if ( state->toStateActionTable.length() > 0 )
738 toStateActions = actionTableMap.find( state->toStateActionTable );
740 RedActionTable *fromStateActions = 0;
741 if ( state->fromStateActionTable.length() > 0 )
742 fromStateActions = actionTableMap.find( state->fromStateActionTable );
744 /* EOF actions go out here only if the state has no eof target. If it has
745 * an eof target then an eof transition will be used instead. */
746 RedActionTable *eofActions = 0;
747 if ( state->eofTarget == 0 && state->eofActionTable.length() > 0 )
748 eofActions = actionTableMap.find( state->eofActionTable );
750 if ( toStateActions != 0 || fromStateActions != 0 || eofActions != 0 ) {
751 out << " <state_actions>";
752 if ( toStateActions != 0 )
753 out << toStateActions->id;
757 if ( fromStateActions != 0 )
758 out << " " << fromStateActions->id;
762 if ( eofActions != 0 )
763 out << " " << eofActions->id;
767 out << "</state_actions>\n";
771 void XMLCodeGen::writeStateConditions( StateAp *state )
773 if ( state->stateCondList.length() > 0 ) {
774 out << " <cond_list length=\"" << state->stateCondList.length() << "\">\n";
775 for ( StateCondList::Iter scdi = state->stateCondList; scdi.lte(); scdi++ ) {
777 writeKey( scdi->lowKey );
779 writeKey( scdi->highKey );
781 out << scdi->condSpace->condSpaceId;
784 out << " </cond_list>\n";
788 void XMLCodeGen::writeStateList()
790 /* Write the list of states. */
791 out << " <state_list length=\"" << fsm->stateList.length() << "\">\n";
792 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
793 out << " <state id=\"" << st->alg.stateNum << "\"";
794 if ( st->isFinState() )
795 out << " final=\"t\"";
798 writeStateActions( st );
800 writeStateConditions( st );
801 writeTransList( st );
803 out << " </state>\n";
808 out << " </state_list>\n";
811 bool XMLCodeGen::writeNameInst( NameInst *nameInst )
813 bool written = false;
814 if ( nameInst->parent != 0 )
815 written = writeNameInst( nameInst->parent );
817 if ( nameInst->name != 0 ) {
820 out << nameInst->name;
827 void XMLCodeGen::writeEntryPoints()
829 /* List of entry points other than start state. */
830 if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) {
831 out << " <entry_points";
832 if ( pd->lmRequiresErrorState )
833 out << " error=\"t\"";
835 for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) {
836 /* Get the name instantiation from nameIndex. */
837 NameInst *nameInst = pd->nameIndex[en->key];
838 StateAp *state = en->value;
839 out << " <entry name=\"";
840 writeNameInst( nameInst );
841 out << "\">" << state->alg.stateNum << "</entry>\n";
843 out << " </entry_points>\n";
847 void XMLCodeGen::writeMachine()
849 /* Open the machine. */
850 out << " <machine>\n";
853 reduceActionTables();
856 writeActionTableList();
860 out << " <start_state>" << fsm->startState->alg.stateNum <<
864 if ( fsm->errState != 0 ) {
865 out << " <error_state>" << fsm->errState->alg.stateNum <<
872 out << " </machine>\n";
876 void XMLCodeGen::writeConditions()
878 if ( condData->condSpaceMap.length() > 0 ) {
879 long nextCondSpaceId = 0;
880 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ )
881 cs->condSpaceId = nextCondSpaceId++;
883 out << " <cond_space_list length=\"" << condData->condSpaceMap.length() << "\">\n";
884 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
885 out << " <cond_space id=\"" << cs->condSpaceId <<
886 "\" length=\"" << cs->condSet.length() << "\">";
887 writeKey( cs->baseKey );
888 for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
889 out << " " << (*csi)->actionId;
890 out << "</cond_space>\n";
892 out << " </cond_space_list>\n";
896 void XMLCodeGen::writeExports()
898 if ( pd->exportList.length() > 0 ) {
899 out << " <exports>\n";
900 for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ ) {
901 out << " <ex name=\"" << exp->name << "\">";
902 writeKey( exp->key );
905 out << " </exports>\n";
909 void XMLCodeGen::writeXML()
911 /* Open the definition. */
912 out << "<ragel_def name=\"" << fsmName << "\">\n";
915 out << " <alphtype>" << keyOps->alphType->internalName << "</alphtype>\n";
917 /* Getkey expression. */
918 if ( pd->getKeyExpr != 0 ) {
920 writeInlineList( pd->getKeyExpr );
921 out << "</getkey>\n";
924 /* Access expression. */
925 if ( pd->accessExpr != 0 ) {
927 writeInlineList( pd->accessExpr );
928 out << "</access>\n";
931 /* PrePush expression. */
932 if ( pd->prePushExpr != 0 ) {
934 writeInlineList( pd->prePushExpr );
935 out << "</prepush>\n";
938 /* PostPop expression. */
939 if ( pd->postPopExpr != 0 ) {
941 writeInlineList( pd->postPopExpr );
942 out << "</postpop>\n";
946 * Variable expressions.
949 if ( pd->pExpr != 0 ) {
951 writeInlineList( pd->pExpr );
952 out << "</p_expr>\n";
955 if ( pd->peExpr != 0 ) {
957 writeInlineList( pd->peExpr );
958 out << "</pe_expr>\n";
961 if ( pd->eofExpr != 0 ) {
962 out << " <eof_expr>";
963 writeInlineList( pd->eofExpr );
964 out << "</eof_expr>\n";
967 if ( pd->csExpr != 0 ) {
969 writeInlineList( pd->csExpr );
970 out << "</cs_expr>\n";
973 if ( pd->topExpr != 0 ) {
974 out << " <top_expr>";
975 writeInlineList( pd->topExpr );
976 out << "</top_expr>\n";
979 if ( pd->stackExpr != 0 ) {
980 out << " <stack_expr>";
981 writeInlineList( pd->stackExpr );
982 out << "</stack_expr>\n";
985 if ( pd->actExpr != 0 ) {
986 out << " <act_expr>";
987 writeInlineList( pd->actExpr );
988 out << "</act_expr>\n";
991 if ( pd->tokstartExpr != 0 ) {
992 out << " <tokstart_expr>";
993 writeInlineList( pd->tokstartExpr );
994 out << "</tokstart_expr>\n";
997 if ( pd->tokendExpr != 0 ) {
998 out << " <tokend_expr>";
999 writeInlineList( pd->tokendExpr );
1000 out << "</tokend_expr>\n";
1003 if ( pd->dataExpr != 0 ) {
1004 out << " <data_expr>";
1005 writeInlineList( pd->dataExpr );
1006 out << "</data_expr>\n";
1017 void BackendGen::makeExports()
1019 for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ )
1020 xmlParser.cgd->exportList.append( new Export( exp->name, exp->key ) );
1023 void BackendGen::makeAction( Action *action )
1025 GenInlineList *genList = new GenInlineList;
1026 makeGenInlineList( genList, action->inlineList );
1028 xmlParser.cgd->newAction( xmlParser.curAction++, action->name,
1029 action->loc.line, action->loc.col, genList );
1033 void BackendGen::makeActionList()
1035 /* Determine which actions to write. */
1036 int nextActionId = 0;
1037 for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
1038 if ( act->numRefs() > 0 || act->numCondRefs > 0 )
1039 act->actionId = nextActionId++;
1042 /* Write the list. */
1043 xmlParser.cgd->initActionList( nextActionId );
1044 xmlParser.curAction = 0;
1046 for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
1047 if ( act->actionId >= 0 )
1052 void BackendGen::makeActionTableList()
1054 /* Must first order the action tables based on their id. */
1055 int numTables = nextActionTableId;
1056 RedActionTable **tables = new RedActionTable*[numTables];
1057 for ( ActionTableMap::Iter at = actionTableMap; at.lte(); at++ )
1058 tables[at->id] = at;
1060 xmlParser.cgd->initActionTableList( numTables );
1061 xmlParser.curActionTable = 0;
1063 for ( int t = 0; t < numTables; t++ ) {
1064 long length = tables[t]->key.length();
1066 /* Collect the action table. */
1067 RedAction *redAct = xmlParser.cgd->allActionTables + xmlParser.curActionTable;
1068 redAct->actListId = xmlParser.curActionTable;
1069 redAct->key.setAsNew( length );
1071 for ( ActionTable::Iter atel = tables[t]->key; atel.lte(); atel++ ) {
1072 redAct->key[atel.pos()].key = 0;
1073 redAct->key[atel.pos()].value = xmlParser.cgd->allActions +
1074 atel->value->actionId;
1077 /* Insert into the action table map. */
1078 xmlParser.cgd->redFsm->actionMap.insert( redAct );
1080 xmlParser.curActionTable += 1;
1086 void BackendGen::makeConditions()
1088 if ( condData->condSpaceMap.length() > 0 ) {
1089 long nextCondSpaceId = 0;
1090 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ )
1091 cs->condSpaceId = nextCondSpaceId++;
1093 long listLength = condData->condSpaceMap.length();
1094 xmlParser.cgd->initCondSpaceList( listLength );
1095 xmlParser.curCondSpace = 0;
1097 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
1098 long id = cs->condSpaceId;
1099 xmlParser.cgd->newCondSpace( xmlParser.curCondSpace, id, cs->baseKey );
1100 for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
1101 xmlParser.cgd->condSpaceItem( xmlParser.curCondSpace, (*csi)->actionId );
1102 xmlParser.curCondSpace += 1;
1107 bool BackendGen::makeNameInst( std::string &res, NameInst *nameInst )
1109 bool written = false;
1110 if ( nameInst->parent != 0 )
1111 written = makeNameInst( res, nameInst->parent );
1113 if ( nameInst->name != 0 ) {
1116 res += nameInst->name;
1123 void BackendGen::makeEntryPoints()
1125 /* List of entry points other than start state. */
1126 if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) {
1127 if ( pd->lmRequiresErrorState )
1128 xmlParser.cgd->setForcedErrorState();
1130 for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) {
1131 /* Get the name instantiation from nameIndex. */
1132 NameInst *nameInst = pd->nameIndex[en->key];
1134 makeNameInst( name, nameInst );
1135 StateAp *state = en->value;
1136 xmlParser.cgd->addEntryPoint( strdup(name.c_str()), state->alg.stateNum );
1141 void BackendGen::makeStateActions( StateAp *state )
1143 RedActionTable *toStateActions = 0;
1144 if ( state->toStateActionTable.length() > 0 )
1145 toStateActions = actionTableMap.find( state->toStateActionTable );
1147 RedActionTable *fromStateActions = 0;
1148 if ( state->fromStateActionTable.length() > 0 )
1149 fromStateActions = actionTableMap.find( state->fromStateActionTable );
1151 /* EOF actions go out here only if the state has no eof target. If it has
1152 * an eof target then an eof transition will be used instead. */
1153 RedActionTable *eofActions = 0;
1154 if ( state->eofTarget == 0 && state->eofActionTable.length() > 0 )
1155 eofActions = actionTableMap.find( state->eofActionTable );
1157 if ( toStateActions != 0 || fromStateActions != 0 || eofActions != 0 ) {
1159 if ( toStateActions != 0 )
1160 to = toStateActions->id;
1163 if ( fromStateActions != 0 )
1164 from = fromStateActions->id;
1167 if ( eofActions != 0 )
1168 eof = eofActions->id;
1170 xmlParser.cgd->setStateActions( xmlParser.curState, to, from, eof );
1174 void BackendGen::makeEofTrans( StateAp *state )
1176 RedActionTable *eofActions = 0;
1177 if ( state->eofActionTable.length() > 0 )
1178 eofActions = actionTableMap.find( state->eofActionTable );
1180 /* The EOF trans is used when there is an eof target, otherwise the eof
1181 * action goes into state actions. */
1182 if ( state->eofTarget != 0 ) {
1183 long targ = state->eofTarget->alg.stateNum;
1185 if ( eofActions != 0 )
1186 action = eofActions->id;
1188 xmlParser.cgd->setEofTrans( xmlParser.curState, targ, action );
1192 void BackendGen::makeStateConditions( StateAp *state )
1194 if ( state->stateCondList.length() > 0 ) {
1195 long length = state->stateCondList.length();
1196 xmlParser.cgd->initStateCondList( xmlParser.curState, length );
1197 xmlParser.curStateCond = 0;
1199 for ( StateCondList::Iter scdi = state->stateCondList; scdi.lte(); scdi++ ) {
1200 xmlParser.cgd->addStateCond( xmlParser.curState, scdi->lowKey, scdi->highKey,
1201 scdi->condSpace->condSpaceId );
1206 void BackendGen::makeTrans( Key lowKey, Key highKey, TransAp *trans )
1208 /* First reduce the action. */
1209 RedActionTable *actionTable = 0;
1210 if ( trans->actionTable.length() > 0 )
1211 actionTable = actionTableMap.find( trans->actionTable );
1214 if ( trans->toState != 0 )
1215 targ = trans->toState->alg.stateNum;
1218 if ( actionTable != 0 )
1219 action = actionTable->id;
1221 xmlParser.cgd->newTrans( xmlParser.curState, xmlParser.curTrans++, lowKey, highKey, targ, action );
1224 void BackendGen::makeTransList( StateAp *state )
1226 TransListVect outList;
1228 /* If there is only are no ranges the task is simple. */
1229 if ( state->outList.length() > 0 ) {
1230 /* Loop each source range. */
1231 for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) {
1232 /* Reduce the transition. If it reduced to anything then add it. */
1233 appendTrans( outList, trans->lowKey, trans->highKey, trans );
1237 xmlParser.cgd->initTransList( xmlParser.curState, outList.length() );
1238 xmlParser.curTrans = 0;
1240 for ( TransListVect::Iter tvi = outList; tvi.lte(); tvi++ )
1241 makeTrans( tvi->lowKey, tvi->highKey, tvi->value );
1243 xmlParser.cgd->finishTransList( xmlParser.curState );
1247 void BackendGen::makeStateList()
1249 /* Write the list of states. */
1250 long length = fsm->stateList.length();
1251 xmlParser.cgd->initStateList( length );
1252 xmlParser.curState = 0;
1253 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
1254 makeStateActions( st );
1256 makeStateConditions( st );
1257 makeTransList( st );
1259 long id = st->alg.stateNum;
1260 xmlParser.cgd->setId( xmlParser.curState, id );
1262 if ( st->isFinState() )
1263 xmlParser.cgd->setFinal( xmlParser.curState );
1265 xmlParser.curState += 1;
1270 void BackendGen::makeMachine()
1272 xmlParser.cgd->createMachine();
1274 /* Action tables. */
1275 reduceActionTables();
1278 makeActionTableList();
1282 xmlParser.cgd->setStartState( fsm->startState->alg.stateNum );
1285 if ( fsm->errState != 0 )
1286 xmlParser.cgd->setErrorState( fsm->errState->alg.stateNum );
1291 xmlParser.cgd->closeMachine();
1294 void BackendGen::makeBackend()
1296 /* Open the definition. */
1297 xmlParser.open_ragel_def( fsmName );
1299 /* Alphabet type. */
1300 xmlParser.cgd->setAlphType( keyOps->alphType->internalName );
1302 /* Getkey expression. */
1303 if ( pd->getKeyExpr != 0 ) {
1304 xmlParser.cgd->getKeyExpr = new GenInlineList;
1305 makeGenInlineList( xmlParser.cgd->getKeyExpr, pd->getKeyExpr );
1308 /* Access expression. */
1309 if ( pd->accessExpr != 0 ) {
1310 xmlParser.cgd->accessExpr = new GenInlineList;
1311 makeGenInlineList( xmlParser.cgd->accessExpr, pd->accessExpr );
1314 /* PrePush expression. */
1315 if ( pd->prePushExpr != 0 ) {
1316 xmlParser.cgd->prePushExpr = new GenInlineList;
1317 makeGenInlineList( xmlParser.cgd->prePushExpr, pd->prePushExpr );
1320 /* PostPop expression. */
1321 if ( pd->postPopExpr != 0 ) {
1322 xmlParser.cgd->postPopExpr = new GenInlineList;
1323 makeGenInlineList( xmlParser.cgd->postPopExpr, pd->postPopExpr );
1327 * Variable expressions.
1330 if ( pd->pExpr != 0 ) {
1331 xmlParser.cgd->pExpr = new GenInlineList;
1332 makeGenInlineList( xmlParser.cgd->pExpr, pd->pExpr );
1335 if ( pd->peExpr != 0 ) {
1336 xmlParser.cgd->peExpr = new GenInlineList;
1337 makeGenInlineList( xmlParser.cgd->peExpr, pd->peExpr );
1340 if ( pd->eofExpr != 0 ) {
1341 xmlParser.cgd->eofExpr = new GenInlineList;
1342 makeGenInlineList( xmlParser.cgd->eofExpr, pd->eofExpr );
1345 if ( pd->csExpr != 0 ) {
1346 xmlParser.cgd->csExpr = new GenInlineList;
1347 makeGenInlineList( xmlParser.cgd->csExpr, pd->csExpr );
1350 if ( pd->topExpr != 0 ) {
1351 xmlParser.cgd->topExpr = new GenInlineList;
1352 makeGenInlineList( xmlParser.cgd->topExpr, pd->topExpr );
1355 if ( pd->stackExpr != 0 ) {
1356 xmlParser.cgd->stackExpr = new GenInlineList;
1357 makeGenInlineList( xmlParser.cgd->stackExpr, pd->stackExpr );
1360 if ( pd->actExpr != 0 ) {
1361 xmlParser.cgd->actExpr = new GenInlineList;
1362 makeGenInlineList( xmlParser.cgd->actExpr, pd->actExpr );
1365 if ( pd->tokstartExpr != 0 ) {
1366 xmlParser.cgd->tokstartExpr = new GenInlineList;
1367 makeGenInlineList( xmlParser.cgd->tokstartExpr, pd->tokstartExpr );
1370 if ( pd->tokendExpr != 0 ) {
1371 xmlParser.cgd->tokendExpr = new GenInlineList;
1372 makeGenInlineList( xmlParser.cgd->tokendExpr, pd->tokendExpr );
1375 if ( pd->dataExpr != 0 ) {
1376 xmlParser.cgd->dataExpr = new GenInlineList;
1377 makeGenInlineList( xmlParser.cgd->dataExpr, pd->dataExpr );
1383 xmlParser.close_ragel_def();