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 void XMLCodeGen::writeGoto( InlineItem *item )
189 if ( pd->generatingSectionSubset )
190 out << "<goto>-1</goto>";
192 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
193 out << "<goto>" << targ->value->alg.stateNum << "</goto>";
197 void XMLCodeGen::writeCall( InlineItem *item )
199 if ( pd->generatingSectionSubset )
200 out << "<call>-1</call>";
202 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
203 out << "<call>" << targ->value->alg.stateNum << "</call>";
207 void XMLCodeGen::writeNext( InlineItem *item )
209 if ( pd->generatingSectionSubset )
210 out << "<next>-1</next>";
212 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
213 out << "<next>" << targ->value->alg.stateNum << "</next>";
217 void XMLCodeGen::writeGotoExpr( InlineItem *item )
219 out << "<goto_expr>";
220 writeInlineList( item->children );
221 out << "</goto_expr>";
224 void XMLCodeGen::writeCallExpr( InlineItem *item )
226 out << "<call_expr>";
227 writeInlineList( item->children );
228 out << "</call_expr>";
231 void XMLCodeGen::writeNextExpr( InlineItem *item )
233 out << "<next_expr>";
234 writeInlineList( item->children );
235 out << "</next_expr>";
238 void XMLCodeGen::writeEntry( InlineItem *item )
240 if ( pd->generatingSectionSubset )
241 out << "<entry>-1</entry>";
243 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
244 out << "<entry>" << targ->value->alg.stateNum << "</entry>";
248 void XMLCodeGen::writeActionExec( InlineItem *item )
251 writeInlineList( item->children );
255 void XMLCodeGen::writeLmOnLast( InlineItem *item )
257 out << "<set_tokend>1</set_tokend>";
259 if ( item->longestMatchPart->action != 0 ) {
260 out << "<sub_action>";
261 writeInlineList( item->longestMatchPart->action->inlineList );
262 out << "</sub_action>";
266 void XMLCodeGen::writeLmOnNext( InlineItem *item )
268 out << "<set_tokend>0</set_tokend>";
269 out << "<hold></hold>";
271 if ( item->longestMatchPart->action != 0 ) {
272 out << "<sub_action>";
273 writeInlineList( item->longestMatchPart->action->inlineList );
274 out << "</sub_action>";
278 void XMLCodeGen::writeLmOnLagBehind( InlineItem *item )
280 out << "<exec><get_tokend></get_tokend></exec>";
282 if ( item->longestMatchPart->action != 0 ) {
283 out << "<sub_action>";
284 writeInlineList( item->longestMatchPart->action->inlineList );
285 out << "</sub_action>";
289 void XMLCodeGen::writeLmSwitch( InlineItem *item )
292 LongestMatch *longestMatch = item->longestMatch;
293 out << "<lm_switch>\n";
295 if ( longestMatch->lmSwitchHandlesError ) {
296 /* If the switch handles error then we should have also forced the
298 assert( fsm->errState != 0 );
300 out << " <sub_action id=\"0\">";
301 out << "<goto>" << fsm->errState->alg.stateNum << "</goto>";
302 out << "</sub_action>\n";
305 for ( LmPartList::Iter lmi = *longestMatch->longestMatchList; lmi.lte(); lmi++ ) {
306 if ( lmi->inLmSelect && lmi->action != 0 ) {
307 /* Open the action. Write it with the context that sets up _p
308 * when doing control flow changes from inside the machine. */
309 out << " <sub_action id=\"" << lmi->longestMatchId << "\">";
310 out << "<exec><get_tokend></get_tokend></exec>";
311 writeInlineList( lmi->action->inlineList );
312 out << "</sub_action>\n";
316 out << " </lm_switch>";
319 void XMLCodeGen::writeInlineList( InlineList *inlineList )
321 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
322 switch ( item->type ) {
323 case InlineItem::Text:
326 case InlineItem::Goto:
329 case InlineItem::GotoExpr:
330 writeGotoExpr( item );
332 case InlineItem::Call:
335 case InlineItem::CallExpr:
336 writeCallExpr( item );
338 case InlineItem::Next:
341 case InlineItem::NextExpr:
342 writeNextExpr( item );
344 case InlineItem::Break:
345 out << "<break></break>";
347 case InlineItem::Ret:
348 out << "<ret></ret>";
350 case InlineItem::PChar:
351 out << "<pchar></pchar>";
353 case InlineItem::Char:
354 out << "<char></char>";
356 case InlineItem::Curs:
357 out << "<curs></curs>";
359 case InlineItem::Targs:
360 out << "<targs></targs>";
362 case InlineItem::Entry:
366 case InlineItem::Hold:
367 out << "<hold></hold>";
369 case InlineItem::Exec:
370 writeActionExec( item );
373 case InlineItem::LmSetActId:
374 out << "<set_act>" <<
375 item->longestMatchPart->longestMatchId <<
378 case InlineItem::LmSetTokEnd:
379 out << "<set_tokend>1</set_tokend>";
382 case InlineItem::LmOnLast:
383 writeLmOnLast( item );
385 case InlineItem::LmOnNext:
386 writeLmOnNext( item );
388 case InlineItem::LmOnLagBehind:
389 writeLmOnLagBehind( item );
391 case InlineItem::LmSwitch:
392 writeLmSwitch( item );
395 case InlineItem::LmInitAct:
396 out << "<init_act></init_act>";
398 case InlineItem::LmInitTokStart:
399 out << "<init_tokstart></init_tokstart>";
401 case InlineItem::LmSetTokStart:
402 out << "<set_tokstart></set_tokstart>";
408 void XMLCodeGen::writeAction( Action *action )
410 out << " <action id=\"" << action->actionId << "\"";
411 if ( action->name != 0 )
412 out << " name=\"" << action->name << "\"";
413 out << " line=\"" << action->loc.line << "\" col=\"" << action->loc.col << "\">";
414 writeInlineList( action->inlineList );
415 out << "</action>\n";
418 void xmlEscapeHost( std::ostream &out, char *data, long len )
420 char *end = data + len;
421 while ( data != end ) {
423 case '<': out << "<"; break;
424 case '>': out << ">"; break;
425 case '&': out << "&"; break;
426 default: out << *data; break;
432 void XMLCodeGen::writeStateActions( StateAp *state )
434 RedActionTable *toStateActions = 0;
435 if ( state->toStateActionTable.length() > 0 )
436 toStateActions = actionTableMap.find( state->toStateActionTable );
438 RedActionTable *fromStateActions = 0;
439 if ( state->fromStateActionTable.length() > 0 )
440 fromStateActions = actionTableMap.find( state->fromStateActionTable );
442 RedActionTable *eofActions = 0;
443 if ( state->eofActionTable.length() > 0 )
444 eofActions = actionTableMap.find( state->eofActionTable );
446 if ( toStateActions != 0 || fromStateActions != 0 || eofActions != 0 ) {
447 out << " <state_actions>";
448 if ( toStateActions != 0 )
449 out << toStateActions->id;
453 if ( fromStateActions != 0 )
454 out << " " << fromStateActions->id;
458 if ( eofActions != 0 )
459 out << " " << eofActions->id;
461 out << " x"; out << "</state_actions>\n";
465 void XMLCodeGen::writeStateConditions( StateAp *state )
467 if ( state->stateCondList.length() > 0 ) {
468 out << " <cond_list length=\"" << state->stateCondList.length() << "\">\n";
469 for ( StateCondList::Iter scdi = state->stateCondList; scdi.lte(); scdi++ ) {
471 writeKey( scdi->lowKey );
473 writeKey( scdi->highKey );
475 out << scdi->condSpace->condSpaceId;
478 out << " </cond_list>\n";
482 void XMLCodeGen::writeStateList()
484 /* Write the list of states. */
485 out << " <state_list length=\"" << fsm->stateList.length() << "\">\n";
486 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
487 out << " <state id=\"" << st->alg.stateNum << "\"";
488 if ( st->isFinState() )
489 out << " final=\"t\"";
492 writeStateActions( st );
493 writeStateConditions( st );
494 writeTransList( st );
496 out << " </state>\n";
501 out << " </state_list>\n";
504 bool XMLCodeGen::writeNameInst( NameInst *nameInst )
506 bool written = false;
507 if ( nameInst->parent != 0 )
508 written = writeNameInst( nameInst->parent );
510 if ( nameInst->name != 0 ) {
513 out << nameInst->name;
520 void XMLCodeGen::writeEntryPoints()
522 /* List of entry points other than start state. */
523 if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) {
524 out << " <entry_points";
525 if ( pd->lmRequiresErrorState )
526 out << " error=\"t\"";
528 for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) {
529 /* Get the name instantiation from nameIndex. */
530 NameInst *nameInst = pd->nameIndex[en->key];
531 StateAp *state = en->value;
532 out << " <entry name=\"";
533 writeNameInst( nameInst );
534 out << "\">" << state->alg.stateNum << "</entry>\n";
536 out << " </entry_points>\n";
540 void XMLCodeGen::writeMachine()
542 /* Open the machine. */
543 out << " <machine>\n";
546 reduceActionTables();
549 writeActionTableList();
553 out << " <start_state>" << fsm->startState->alg.stateNum <<
557 if ( fsm->errState != 0 ) {
558 out << " <error_state>" << fsm->errState->alg.stateNum <<
565 out << " </machine>\n";
569 void XMLCodeGen::writeConditions()
571 if ( condData->condSpaceMap.length() > 0 ) {
572 long nextCondSpaceId = 0;
573 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ )
574 cs->condSpaceId = nextCondSpaceId++;
576 out << " <cond_space_list length=\"" << condData->condSpaceMap.length() << "\">\n";
577 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
578 out << " <cond_space id=\"" << cs->condSpaceId <<
579 "\" length=\"" << cs->condSet.length() << "\">";
580 writeKey( cs->baseKey );
581 for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
582 out << " " << (*csi)->actionId;
583 out << "</cond_space>\n";
585 out << " </cond_space_list>\n";
589 void XMLCodeGen::writeExports()
591 if ( pd->exportList.length() > 0 ) {
592 out << " <exports>\n";
593 for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ ) {
594 out << " <ex name=\"" << exp->name << "\">";
595 writeKey( exp->key );
598 out << " </exports>\n";
602 void XMLCodeGen::writeXML()
604 /* Open the definition. */
605 out << "<ragel_def name=\"" << fsmName << "\">\n";
608 out << " <alphtype>" << keyOps->alphType->internalName << "</alphtype>\n";
610 /* Getkey expression. */
611 if ( pd->getKeyExpr != 0 ) {
613 writeInlineList( pd->getKeyExpr );
614 out << "</getkey>\n";
617 /* Access expression. */
618 if ( pd->accessExpr != 0 ) {
620 writeInlineList( pd->accessExpr );
621 out << "</access>\n";
625 * Variable expressions.
628 if ( pd->pExpr != 0 ) {
630 writeInlineList( pd->pExpr );
631 out << "</p_expr>\n";
634 if ( pd->peExpr != 0 ) {
636 writeInlineList( pd->peExpr );
637 out << "</pe_expr>\n";
640 if ( pd->csExpr != 0 ) {
642 writeInlineList( pd->csExpr );
643 out << "</cs_expr>\n";
646 if ( pd->topExpr != 0 ) {
647 out << " <top_expr>";
648 writeInlineList( pd->topExpr );
649 out << "</top_expr>\n";
652 if ( pd->stackExpr != 0 ) {
653 out << " <stack_expr>";
654 writeInlineList( pd->stackExpr );
655 out << "</stack_expr>\n";
658 if ( pd->actExpr != 0 ) {
659 out << " <act_expr>";
660 writeInlineList( pd->actExpr );
661 out << "</act_expr>\n";
664 if ( pd->tokstartExpr != 0 ) {
665 out << " <tokstart_expr>";
666 writeInlineList( pd->tokstartExpr );
667 out << "</tokstart_expr>\n";
670 if ( pd->tokendExpr != 0 ) {
671 out << " <tokend_expr>";
672 writeInlineList( pd->tokendExpr );
673 out << "</tokend_expr>\n";