2 * Copyright 2005-2007 Adrian Thurston <thurston@cs.queensu.ca>
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"
25 #include "parsedata.h"
31 XMLCodeGen::XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm,
43 void XMLCodeGen::writeActionList()
45 /* Determine which actions to write. */
47 for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
48 if ( act->numRefs() > 0 || act->numCondRefs > 0 )
49 act->actionId = nextActionId++;
53 out << " <action_list length=\"" << nextActionId << "\">\n";
54 for ( ActionList::Iter act = pd->actionList; act.lte(); act++ ) {
55 if ( act->actionId >= 0 )
58 out << " </action_list>\n";
61 void XMLCodeGen::writeActionTableList()
63 /* Must first order the action tables based on their id. */
64 int numTables = nextActionTableId;
65 RedActionTable **tables = new RedActionTable*[numTables];
66 for ( ActionTableMap::Iter at = actionTableMap; at.lte(); at++ )
69 out << " <action_table_list length=\"" << numTables << "\">\n";
70 for ( int t = 0; t < numTables; t++ ) {
71 out << " <action_table id=\"" << t << "\" length=\"" <<
72 tables[t]->key.length() << "\">";
73 for ( ActionTable::Iter atel = tables[t]->key; atel.lte(); atel++ ) {
74 out << atel->value->actionId;
78 out << "</action_table>\n";
80 out << " </action_table_list>\n";
85 void XMLCodeGen::reduceActionTables()
87 /* Reduce the actions tables to a set. */
88 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
89 RedActionTable *actionTable = 0;
91 /* Reduce To State Actions. */
92 if ( st->toStateActionTable.length() > 0 ) {
93 if ( actionTableMap.insert( st->toStateActionTable, &actionTable ) )
94 actionTable->id = nextActionTableId++;
97 /* Reduce From State Actions. */
98 if ( st->fromStateActionTable.length() > 0 ) {
99 if ( actionTableMap.insert( st->fromStateActionTable, &actionTable ) )
100 actionTable->id = nextActionTableId++;
103 /* Reduce EOF actions. */
104 if ( st->eofActionTable.length() > 0 ) {
105 if ( actionTableMap.insert( st->eofActionTable, &actionTable ) )
106 actionTable->id = nextActionTableId++;
109 /* Loop the transitions and reduce their actions. */
110 for ( TransList::Iter trans = st->outList; trans.lte(); trans++ ) {
111 if ( trans->actionTable.length() > 0 ) {
112 if ( actionTableMap.insert( trans->actionTable, &actionTable ) )
113 actionTable->id = nextActionTableId++;
119 void XMLCodeGen::appendTrans( TransListVect &outList, Key lowKey,
120 Key highKey, TransAp *trans )
122 if ( trans->toState != 0 || trans->actionTable.length() > 0 )
123 outList.append( TransEl( lowKey, highKey, trans ) );
126 void XMLCodeGen::writeKey( Key key )
128 if ( keyOps->isSigned )
131 out << (unsigned long) key.getVal();
134 void XMLCodeGen::writeTrans( Key lowKey, Key highKey, TransAp *trans )
136 /* First reduce the action. */
137 RedActionTable *actionTable = 0;
138 if ( trans->actionTable.length() > 0 )
139 actionTable = actionTableMap.find( trans->actionTable );
141 /* Write the transition. */
147 if ( trans->toState != 0 )
148 out << " " << trans->toState->alg.stateNum;
152 if ( actionTable != 0 )
153 out << " " << actionTable->id;
159 void XMLCodeGen::writeTransList( StateAp *state )
161 TransListVect outList;
163 /* If there is only are no ranges the task is simple. */
164 if ( state->outList.length() > 0 ) {
165 /* Loop each source range. */
166 for ( TransList::Iter trans = state->outList; trans.lte(); trans++ ) {
167 /* Reduce the transition. If it reduced to anything then add it. */
168 appendTrans( outList, trans->lowKey, trans->highKey, trans );
172 out << " <trans_list length=\"" << outList.length() << "\">\n";
173 for ( TransListVect::Iter tvi = outList; tvi.lte(); tvi++ )
174 writeTrans( tvi->lowKey, tvi->highKey, tvi->value );
175 out << " </trans_list>\n";
178 void XMLCodeGen::writeText( InlineItem *item )
180 if ( item->prev == 0 || item->prev->type != InlineItem::Text )
182 xmlEscapeHost( out, item->data, strlen(item->data) );
183 if ( item->next == 0 || item->next->type != InlineItem::Text )
187 bool isLmItem( InlineItem *context )
189 return context != 0 && (
190 context->type == InlineItem::LmOnLast ||
191 context->type == InlineItem::LmOnNext ||
192 context->type == InlineItem::LmOnLagBehind ||
193 context->type == InlineItem::LmSwitch );
196 void XMLCodeGen::writeCtrlFlow( InlineItem *item, InlineItem *context )
198 if ( isLmItem( context ) ) {
199 out << "<sub_action>";
200 out << "<exec><get_tokend></get_tokend></exec>";
203 switch ( item->type ) {
204 case InlineItem::Goto:
205 writeGoto( item, context );
207 case InlineItem::GotoExpr:
208 writeGotoExpr( item, context );
210 case InlineItem::Call:
211 writeCall( item, context );
213 case InlineItem::CallExpr:
214 writeCallExpr( item, context );
216 case InlineItem::Next:
217 writeNext( item, context );
219 case InlineItem::NextExpr:
220 writeNextExpr( item, context );
222 case InlineItem::Break:
223 out << "<break></break>";
225 case InlineItem::Ret:
226 out << "<ret></ret>";
231 if ( isLmItem( context ) )
232 out << "</sub_action>";
235 void XMLCodeGen::writePtrMod( InlineItem *item, InlineItem *context )
237 if ( isLmItem( context ) ) {
238 switch ( item->type ) {
239 case InlineItem::Hold:
240 out << "<holdte></holdte>";
242 case InlineItem::Exec:
243 writeActionExecTE( item );
249 switch ( item->type ) {
250 case InlineItem::Hold:
251 out << "<hold></hold>";
253 case InlineItem::Exec:
254 writeActionExec( item );
262 void XMLCodeGen::writeGoto( InlineItem *item, InlineItem *context )
264 if ( pd->generatingSectionSubset )
265 out << "<goto>-1</goto>";
267 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
268 out << "<goto>" << targ->value->alg.stateNum << "</goto>";
272 void XMLCodeGen::writeCall( InlineItem *item, InlineItem *context )
274 if ( pd->generatingSectionSubset )
275 out << "<call>-1</call>";
277 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
278 out << "<call>" << targ->value->alg.stateNum << "</call>";
282 void XMLCodeGen::writeNext( InlineItem *item, InlineItem *context )
284 if ( pd->generatingSectionSubset )
285 out << "<next>-1</next>";
287 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
288 out << "<next>" << targ->value->alg.stateNum << "</next>";
292 void XMLCodeGen::writeGotoExpr( InlineItem *item, InlineItem *context )
294 out << "<goto_expr>";
295 writeInlineList( item->children, 0 );
296 out << "</goto_expr>";
299 void XMLCodeGen::writeCallExpr( InlineItem *item, InlineItem *context )
301 out << "<call_expr>";
302 writeInlineList( item->children, 0 );
303 out << "</call_expr>";
306 void XMLCodeGen::writeNextExpr( InlineItem *item, InlineItem *context )
308 out << "<next_expr>";
309 writeInlineList( item->children, 0 );
310 out << "</next_expr>";
313 void XMLCodeGen::writeEntry( InlineItem * item )
315 if ( pd->generatingSectionSubset )
316 out << "<entry>-1</entry>";
318 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
319 out << "<entry>" << targ->value->alg.stateNum << "</entry>";
323 void XMLCodeGen::writeActionExec( InlineItem *item )
326 writeInlineList( item->children, 0 );
330 void XMLCodeGen::writeActionExecTE( InlineItem *item )
333 writeInlineList( item->children, 0 );
337 void XMLCodeGen::writeLmOnLast( InlineItem *item )
339 out << "<set_tokend>1</set_tokend>";
340 if ( item->longestMatchPart->action != 0 ) {
341 out << "<sub_action>";
342 writeInlineList( item->longestMatchPart->action->inlineList, item );
343 out << "</sub_action>";
345 out << "<exec><get_tokend></get_tokend></exec>";
348 void XMLCodeGen::writeLmOnNext( InlineItem *item )
350 out << "<set_tokend>0</set_tokend>";
351 if ( item->longestMatchPart->action != 0 ) {
352 out << "<sub_action>";
353 writeInlineList( item->longestMatchPart->action->inlineList, item );
354 out << "</sub_action>";
356 out << "<exec><get_tokend></get_tokend></exec>";
359 void XMLCodeGen::writeLmOnLagBehind( InlineItem *item )
361 if ( item->longestMatchPart->action != 0 ) {
362 out << "<sub_action>";
363 writeInlineList( item->longestMatchPart->action->inlineList, item );
364 out << "</sub_action>";
366 out << "<exec><get_tokend></get_tokend></exec>";
369 void XMLCodeGen::writeLmSwitch( InlineItem *item )
371 LongestMatch *longestMatch = item->longestMatch;
374 if ( longestMatch->lmSwitchHandlesError )
375 out << " handles_error=\"t\"";
378 for ( LmPartList::Iter lmi = *longestMatch->longestMatchList; lmi.lte(); lmi++ ) {
379 if ( lmi->inLmSelect && lmi->action != 0 ) {
380 /* Open the action. Write it with the context that sets up _p
381 * when doing control flow changes from inside the machine. */
382 out << " <sub_action id=\"" << lmi->longestMatchId << "\">";
383 writeInlineList( lmi->action->inlineList, item );
384 out << "</sub_action>\n";
388 out << " </lm_switch><exec><get_tokend></get_tokend></exec>";
391 void XMLCodeGen::writeInlineList( InlineList *inlineList, InlineItem *context )
393 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
394 switch ( item->type ) {
395 case InlineItem::Text:
398 case InlineItem::Goto: case InlineItem::GotoExpr:
399 case InlineItem::Call: case InlineItem::CallExpr:
400 case InlineItem::Next: case InlineItem::NextExpr:
401 case InlineItem::Break: case InlineItem::Ret:
402 writeCtrlFlow( item, context );
404 case InlineItem::PChar:
405 out << "<pchar></pchar>";
407 case InlineItem::Char:
408 out << "<char></char>";
410 case InlineItem::Curs:
411 out << "<curs></curs>";
413 case InlineItem::Targs:
414 out << "<targs></targs>";
416 case InlineItem::Entry:
420 case InlineItem::Hold:
421 case InlineItem::Exec:
422 writePtrMod( item, context );
425 case InlineItem::LmSetActId:
426 out << "<set_act>" <<
427 item->longestMatchPart->longestMatchId <<
430 case InlineItem::LmSetTokEnd:
431 out << "<set_tokend>1</set_tokend>";
434 case InlineItem::LmOnLast:
435 writeLmOnLast( item );
437 case InlineItem::LmOnNext:
438 writeLmOnNext( item );
440 case InlineItem::LmOnLagBehind:
441 writeLmOnLagBehind( item );
443 case InlineItem::LmSwitch:
444 writeLmSwitch( item );
447 case InlineItem::LmInitAct:
448 out << "<init_act></init_act>";
450 case InlineItem::LmInitTokStart:
451 out << "<init_tokstart></init_tokstart>";
453 case InlineItem::LmSetTokStart:
454 out << "<set_tokstart></set_tokstart>";
460 void XMLCodeGen::writeAction( Action *action )
462 out << " <action id=\"" << action->actionId << "\"";
463 if ( action->name != 0 )
464 out << " name=\"" << action->name << "\"";
465 out << " line=\"" << action->loc.line << "\" col=\"" << action->loc.col << "\">";
466 writeInlineList( action->inlineList, 0 );
467 out << "</action>\n";
470 void xmlEscapeHost( std::ostream &out, char *data, long len )
472 char *end = data + len;
473 while ( data != end ) {
475 case '<': out << "<"; break;
476 case '>': out << ">"; break;
477 case '&': out << "&"; break;
478 default: out << *data; break;
484 void XMLCodeGen::writeStateActions( StateAp *state )
486 RedActionTable *toStateActions = 0;
487 if ( state->toStateActionTable.length() > 0 )
488 toStateActions = actionTableMap.find( state->toStateActionTable );
490 RedActionTable *fromStateActions = 0;
491 if ( state->fromStateActionTable.length() > 0 )
492 fromStateActions = actionTableMap.find( state->fromStateActionTable );
494 RedActionTable *eofActions = 0;
495 if ( state->eofActionTable.length() > 0 )
496 eofActions = actionTableMap.find( state->eofActionTable );
498 if ( toStateActions != 0 || fromStateActions != 0 || eofActions != 0 ) {
499 out << " <state_actions>";
500 if ( toStateActions != 0 )
501 out << toStateActions->id;
505 if ( fromStateActions != 0 )
506 out << " " << fromStateActions->id;
510 if ( eofActions != 0 )
511 out << " " << eofActions->id;
513 out << " x"; out << "</state_actions>\n";
517 void XMLCodeGen::writeStateConditions( StateAp *state )
519 if ( state->stateCondList.length() > 0 ) {
520 out << " <cond_list length=\"" << state->stateCondList.length() << "\">\n";
521 for ( StateCondList::Iter scdi = state->stateCondList; scdi.lte(); scdi++ ) {
523 writeKey( scdi->lowKey );
525 writeKey( scdi->highKey );
527 out << scdi->condSpace->condSpaceId;
530 out << " </cond_list>\n";
534 void XMLCodeGen::writeStateList()
536 /* Write the list of states. */
537 out << " <state_list length=\"" << fsm->stateList.length() << "\">\n";
538 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
539 out << " <state id=\"" << st->alg.stateNum << "\"";
540 if ( st->isFinState() )
541 out << " final=\"t\"";
544 writeStateActions( st );
545 writeStateConditions( st );
546 writeTransList( st );
548 out << " </state>\n";
553 out << " </state_list>\n";
556 bool XMLCodeGen::writeNameInst( NameInst *nameInst )
558 bool written = false;
559 if ( nameInst->parent != 0 )
560 written = writeNameInst( nameInst->parent );
562 if ( nameInst->name != 0 ) {
565 out << nameInst->name;
572 void XMLCodeGen::writeEntryPoints()
574 /* List of entry points other than start state. */
575 if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) {
576 out << " <entry_points";
577 if ( pd->lmRequiresErrorState )
578 out << " error=\"t\"";
580 for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) {
581 /* Get the name instantiation from nameIndex. */
582 NameInst *nameInst = pd->nameIndex[en->key];
583 StateAp *state = en->value;
584 out << " <entry name=\"";
585 writeNameInst( nameInst );
586 out << "\">" << state->alg.stateNum << "</entry>\n";
588 out << " </entry_points>\n";
592 void XMLCodeGen::writeMachine()
594 /* Open the machine. */
595 out << " <machine>\n";
598 reduceActionTables();
601 writeActionTableList();
605 out << " <start_state>" << fsm->startState->alg.stateNum <<
609 if ( fsm->errState != 0 ) {
610 out << " <error_state>" << fsm->errState->alg.stateNum <<
617 out << " </machine>\n";
621 void XMLCodeGen::writeConditions()
623 if ( condData->condSpaceMap.length() > 0 ) {
624 long nextCondSpaceId = 0;
625 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ )
626 cs->condSpaceId = nextCondSpaceId++;
628 out << " <cond_space_list length=\"" << condData->condSpaceMap.length() << "\">\n";
629 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
630 out << " <cond_space id=\"" << cs->condSpaceId <<
631 "\" length=\"" << cs->condSet.length() << "\">";
632 writeKey( cs->baseKey );
633 for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
634 out << " " << (*csi)->actionId;
635 out << "</cond_space>\n";
637 out << " </cond_space_list>\n";
641 void XMLCodeGen::writeExports()
643 if ( pd->exportList.length() > 0 ) {
644 out << " <exports>\n";
645 for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ ) {
646 out << " <ex name=\"" << exp->name << "\">";
647 writeKey( exp->key );
650 out << " </exports>\n";
654 void XMLCodeGen::writeXML()
656 /* Open the definition. */
657 out << "<ragel_def name=\"" << fsmName << "\">\n";
660 out << " <alphtype>" << keyOps->alphType->internalName << "</alphtype>\n";
662 /* Getkey expression. */
663 if ( pd->getKeyExpr != 0 ) {
665 writeInlineList( pd->getKeyExpr, 0 );
666 out << "</getkey>\n";
669 /* Access expression. */
670 if ( pd->accessExpr != 0 ) {
672 writeInlineList( pd->accessExpr, 0 );
673 out << "</access>\n";
677 * Variable expressions.
680 if ( pd->pExpr != 0 ) {
682 writeInlineList( pd->pExpr, 0 );
683 out << "</p_expr>\n";
686 if ( pd->peExpr != 0 ) {
688 writeInlineList( pd->peExpr, 0 );
689 out << "</pe_expr>\n";
692 if ( pd->csExpr != 0 ) {
694 writeInlineList( pd->csExpr, 0 );
695 out << "</cs_expr>\n";
698 if ( pd->topExpr != 0 ) {
699 out << " <top_expr>";
700 writeInlineList( pd->topExpr, 0 );
701 out << "</top_expr>\n";
704 if ( pd->stackExpr != 0 ) {
705 out << " <stack_expr>";
706 writeInlineList( pd->stackExpr, 0 );
707 out << "</stack_expr>\n";
710 if ( pd->actExpr != 0 ) {
711 out << " <act_expr>";
712 writeInlineList( pd->actExpr, 0 );
713 out << "</act_expr>\n";
716 if ( pd->tokstartExpr != 0 ) {
717 out << " <tokstart_expr>";
718 writeInlineList( pd->tokstartExpr, 0 );
719 out << "</tokstart_expr>\n";
722 if ( pd->tokendExpr != 0 ) {
723 out << " <tokend_expr>";
724 writeInlineList( pd->tokendExpr, 0 );
725 out << "</tokend_expr>\n";