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::writeLmSwitch( InlineItem *item )
180 LongestMatch *longestMatch = item->longestMatch;
183 if ( longestMatch->lmSwitchHandlesError )
184 out << " handles_error=\"t\"";
187 for ( LmPartList::Iter lmi = *longestMatch->longestMatchList; lmi.lte(); lmi++ ) {
188 if ( lmi->inLmSelect && lmi->action != 0 ) {
189 /* Open the action. Write it with the context that sets up _p
190 * when doing control flow changes from inside the machine. */
191 out << " <sub_action id=\"" << lmi->longestMatchId << "\">";
192 writeInlineList( lmi->action->inlineList, item );
193 out << "</sub_action>\n";
197 out << " </lm_switch><exec><get_tokend></get_tokend></exec>";
200 void XMLCodeGen::writeText( InlineItem *item )
202 if ( item->prev == 0 || item->prev->type != InlineItem::Text )
204 xmlEscapeHost( out, item->data, strlen(item->data) );
205 if ( item->next == 0 || item->next->type != InlineItem::Text )
209 bool isLmItem( InlineItem *context )
211 return context != 0 && (
212 context->type == InlineItem::LmOnLast ||
213 context->type == InlineItem::LmOnNext ||
214 context->type == InlineItem::LmOnLagBehind ||
215 context->type == InlineItem::LmSwitch );
218 void XMLCodeGen::writeCtrlFlow( InlineItem *item, InlineItem *context )
220 if ( isLmItem( context ) ) {
221 out << "<sub_action>";
222 out << "<exec><get_tokend></get_tokend></exec>";
225 switch ( item->type ) {
226 case InlineItem::Goto:
227 writeGoto( item, context );
229 case InlineItem::GotoExpr:
230 writeGotoExpr( item, context );
232 case InlineItem::Call:
233 writeCall( item, context );
235 case InlineItem::CallExpr:
236 writeCallExpr( item, context );
238 case InlineItem::Next:
239 writeNext( item, context );
241 case InlineItem::NextExpr:
242 writeNextExpr( item, context );
244 case InlineItem::Break:
245 out << "<break></break>";
247 case InlineItem::Ret:
248 out << "<ret></ret>";
253 if ( isLmItem( context ) )
254 out << "</sub_action>";
257 void XMLCodeGen::writePtrMod( InlineItem *item, InlineItem *context )
259 if ( isLmItem( context ) ) {
260 switch ( item->type ) {
261 case InlineItem::Hold:
262 out << "<holdte></holdte>";
264 case InlineItem::Exec:
265 writeActionExecTE( item );
271 switch ( item->type ) {
272 case InlineItem::Hold:
273 out << "<hold></hold>";
275 case InlineItem::Exec:
276 writeActionExec( item );
284 void XMLCodeGen::writeGoto( InlineItem *item, InlineItem *context )
286 if ( pd->generatingSectionSubset )
287 out << "<goto>-1</goto>";
289 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
290 out << "<goto>" << targ->value->alg.stateNum << "</goto>";
294 void XMLCodeGen::writeCall( InlineItem *item, InlineItem *context )
296 if ( pd->generatingSectionSubset )
297 out << "<call>-1</call>";
299 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
300 out << "<call>" << targ->value->alg.stateNum << "</call>";
304 void XMLCodeGen::writeNext( InlineItem *item, InlineItem *context )
306 if ( pd->generatingSectionSubset )
307 out << "<next>-1</next>";
309 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
310 out << "<next>" << targ->value->alg.stateNum << "</next>";
314 void XMLCodeGen::writeGotoExpr( InlineItem *item, InlineItem *context )
316 out << "<goto_expr>";
317 writeInlineList( item->children, 0 );
318 out << "</goto_expr>";
321 void XMLCodeGen::writeCallExpr( InlineItem *item, InlineItem *context )
323 out << "<call_expr>";
324 writeInlineList( item->children, 0 );
325 out << "</call_expr>";
328 void XMLCodeGen::writeNextExpr( InlineItem *item, InlineItem *context )
330 out << "<next_expr>";
331 writeInlineList( item->children, 0 );
332 out << "</next_expr>";
335 void XMLCodeGen::writeEntry( InlineItem * item )
337 if ( pd->generatingSectionSubset )
338 out << "<entry>-1</entry>";
340 EntryMapEl *targ = fsm->entryPoints.find( item->nameTarg->id );
341 out << "<entry>" << targ->value->alg.stateNum << "</entry>";
345 void XMLCodeGen::writeActionExec( InlineItem *item )
348 writeInlineList( item->children, 0 );
352 void XMLCodeGen::writeActionExecTE( InlineItem *item )
355 writeInlineList( item->children, 0 );
359 void XMLCodeGen::writeLmOnLast( InlineItem *item )
361 out << "<set_tokend>1</set_tokend>";
362 if ( item->longestMatchPart->action != 0 ) {
363 out << "<sub_action>";
364 writeInlineList( item->longestMatchPart->action->inlineList, item );
365 out << "</sub_action>";
367 out << "<exec><get_tokend></get_tokend></exec>";
370 void XMLCodeGen::writeLmOnNext( InlineItem *item )
372 out << "<set_tokend>0</set_tokend>";
373 if ( item->longestMatchPart->action != 0 ) {
374 out << "<sub_action>";
375 writeInlineList( item->longestMatchPart->action->inlineList, item );
376 out << "</sub_action>";
378 out << "<exec><get_tokend></get_tokend></exec>";
381 void XMLCodeGen::writeLmOnLagBehind( InlineItem *item )
383 if ( item->longestMatchPart->action != 0 ) {
384 out << "<sub_action>";
385 writeInlineList( item->longestMatchPart->action->inlineList, item );
386 out << "</sub_action>";
388 out << "<exec><get_tokend></get_tokend></exec>";
392 void XMLCodeGen::writeInlineList( InlineList *inlineList, InlineItem *context )
394 for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
395 switch ( item->type ) {
396 case InlineItem::Text:
399 case InlineItem::Goto: case InlineItem::GotoExpr:
400 case InlineItem::Call: case InlineItem::CallExpr:
401 case InlineItem::Next: case InlineItem::NextExpr:
402 case InlineItem::Break: case InlineItem::Ret:
403 writeCtrlFlow( item, context );
405 case InlineItem::PChar:
406 out << "<pchar></pchar>";
408 case InlineItem::Char:
409 out << "<char></char>";
411 case InlineItem::Curs:
412 out << "<curs></curs>";
414 case InlineItem::Targs:
415 out << "<targs></targs>";
417 case InlineItem::Entry:
421 case InlineItem::Hold:
422 case InlineItem::Exec:
423 writePtrMod( item, context );
426 case InlineItem::LmSwitch:
427 writeLmSwitch( item );
429 case InlineItem::LmSetActId:
430 out << "<set_act>" <<
431 item->longestMatchPart->longestMatchId <<
434 case InlineItem::LmSetTokEnd:
435 out << "<set_tokend>1</set_tokend>";
437 case InlineItem::LmOnLast:
438 writeLmOnLast( item );
440 case InlineItem::LmOnNext:
441 writeLmOnNext( item );
443 case InlineItem::LmOnLagBehind:
444 writeLmOnLagBehind( item );
446 case InlineItem::LmInitAct:
447 out << "<init_act></init_act>";
449 case InlineItem::LmInitTokStart:
450 out << "<init_tokstart></init_tokstart>";
452 case InlineItem::LmSetTokStart:
453 out << "<set_tokstart></set_tokstart>";
459 void XMLCodeGen::writeAction( Action *action )
461 out << " <action id=\"" << action->actionId << "\"";
462 if ( action->name != 0 )
463 out << " name=\"" << action->name << "\"";
464 out << " line=\"" << action->loc.line << "\" col=\"" << action->loc.col << "\">";
465 writeInlineList( action->inlineList, 0 );
466 out << "</action>\n";
469 void xmlEscapeHost( std::ostream &out, char *data, long len )
471 char *end = data + len;
472 while ( data != end ) {
474 case '<': out << "<"; break;
475 case '>': out << ">"; break;
476 case '&': out << "&"; break;
477 default: out << *data; break;
483 void XMLCodeGen::writeStateActions( StateAp *state )
485 RedActionTable *toStateActions = 0;
486 if ( state->toStateActionTable.length() > 0 )
487 toStateActions = actionTableMap.find( state->toStateActionTable );
489 RedActionTable *fromStateActions = 0;
490 if ( state->fromStateActionTable.length() > 0 )
491 fromStateActions = actionTableMap.find( state->fromStateActionTable );
493 RedActionTable *eofActions = 0;
494 if ( state->eofActionTable.length() > 0 )
495 eofActions = actionTableMap.find( state->eofActionTable );
497 if ( toStateActions != 0 || fromStateActions != 0 || eofActions != 0 ) {
498 out << " <state_actions>";
499 if ( toStateActions != 0 )
500 out << toStateActions->id;
504 if ( fromStateActions != 0 )
505 out << " " << fromStateActions->id;
509 if ( eofActions != 0 )
510 out << " " << eofActions->id;
512 out << " x"; out << "</state_actions>\n";
516 void XMLCodeGen::writeStateConditions( StateAp *state )
518 if ( state->stateCondList.length() > 0 ) {
519 out << " <cond_list length=\"" << state->stateCondList.length() << "\">\n";
520 for ( StateCondList::Iter scdi = state->stateCondList; scdi.lte(); scdi++ ) {
522 writeKey( scdi->lowKey );
524 writeKey( scdi->highKey );
526 out << scdi->condSpace->condSpaceId;
529 out << " </cond_list>\n";
533 void XMLCodeGen::writeStateList()
535 /* Write the list of states. */
536 out << " <state_list length=\"" << fsm->stateList.length() << "\">\n";
537 for ( StateList::Iter st = fsm->stateList; st.lte(); st++ ) {
538 out << " <state id=\"" << st->alg.stateNum << "\"";
539 if ( st->isFinState() )
540 out << " final=\"t\"";
543 writeStateActions( st );
544 writeStateConditions( st );
545 writeTransList( st );
547 out << " </state>\n";
552 out << " </state_list>\n";
555 bool XMLCodeGen::writeNameInst( NameInst *nameInst )
557 bool written = false;
558 if ( nameInst->parent != 0 )
559 written = writeNameInst( nameInst->parent );
561 if ( nameInst->name != 0 ) {
564 out << nameInst->name;
571 void XMLCodeGen::writeEntryPoints()
573 /* List of entry points other than start state. */
574 if ( fsm->entryPoints.length() > 0 || pd->lmRequiresErrorState ) {
575 out << " <entry_points";
576 if ( pd->lmRequiresErrorState )
577 out << " error=\"t\"";
579 for ( EntryMap::Iter en = fsm->entryPoints; en.lte(); en++ ) {
580 /* Get the name instantiation from nameIndex. */
581 NameInst *nameInst = pd->nameIndex[en->key];
582 StateAp *state = en->value;
583 out << " <entry name=\"";
584 writeNameInst( nameInst );
585 out << "\">" << state->alg.stateNum << "</entry>\n";
587 out << " </entry_points>\n";
591 void XMLCodeGen::writeMachine()
593 /* Open the machine. */
594 out << " <machine>\n";
597 reduceActionTables();
600 writeActionTableList();
604 out << " <start_state>" << fsm->startState->alg.stateNum <<
608 if ( fsm->errState != 0 ) {
609 out << " <error_state>" << fsm->errState->alg.stateNum <<
616 out << " </machine>\n";
620 void XMLCodeGen::writeConditions()
622 if ( condData->condSpaceMap.length() > 0 ) {
623 long nextCondSpaceId = 0;
624 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ )
625 cs->condSpaceId = nextCondSpaceId++;
627 out << " <cond_space_list length=\"" << condData->condSpaceMap.length() << "\">\n";
628 for ( CondSpaceMap::Iter cs = condData->condSpaceMap; cs.lte(); cs++ ) {
629 out << " <cond_space id=\"" << cs->condSpaceId <<
630 "\" length=\"" << cs->condSet.length() << "\">";
631 writeKey( cs->baseKey );
632 for ( CondSet::Iter csi = cs->condSet; csi.lte(); csi++ )
633 out << " " << (*csi)->actionId;
634 out << "</cond_space>\n";
636 out << " </cond_space_list>\n";
640 void XMLCodeGen::writeExports()
642 if ( pd->exportList.length() > 0 ) {
643 out << " <exports>\n";
644 for ( ExportList::Iter exp = pd->exportList; exp.lte(); exp++ ) {
645 out << " <ex name=\"" << exp->name << "\">";
646 writeKey( exp->key );
649 out << " </exports>\n";
653 void XMLCodeGen::writeXML()
655 /* Open the definition. */
656 out << "<ragel_def name=\"" << fsmName << "\">\n";
659 out << " <alphtype>" <<
660 (keyOps->alphType - hostLang->hostTypes) << "</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";