Improved graphviz output and got -M and -S working again.
[external/ragel.git] / ragel / rubycodegen.cpp
1 /*
2  *  2007 Victor Hugo Borja <vic@rubyforge.org>
3  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
4  */
5
6 /*  This file is part of Ragel.
7  *
8  *  Ragel is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  * 
13  *  Ragel is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  * 
18  *  You should have received a copy of the GNU General Public License
19  *  along with Ragel; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
21  */
22
23 #include <iomanip>
24 #include <sstream>
25 #include "redfsm.h"
26 #include "gendata.h"
27 #include "ragel.h"
28 #include "rubycodegen.h"
29 #include "xmlparse.h"
30 #include "pcheck.h"
31 #include "vector.h"
32 #include "version.h"
33 #include "common.h"
34
35 #include "ragel.h"
36 #include "rubytable.h"
37 #include "rubyftable.h"
38 #include "rubyflat.h"
39 #include "rubyfflat.h"
40 #include "rbxgoto.h"
41
42 using std::ostream;
43 using std::ostringstream;
44 using std::string;
45 using std::cerr;
46 using std::endl;
47 using std::istream;
48 using std::ifstream;
49 using std::ostream;
50 using std::ios;
51 using std::cin;
52 using std::cout;
53 using std::cerr;
54 using std::endl;
55
56 /* Target ruby impl */
57
58 /* Target language and output style. */
59 extern CodeStyle codeStyle;
60
61 /* Io globals. */
62 extern istream *inStream;
63 extern ostream *outStream;
64 extern output_filter *outFilter;
65 extern const char *outputFileName;
66
67 /* Graphviz dot file generation. */
68 extern bool graphvizDone;
69
70 extern int numSplitPartitions;
71
72 /*
73  * Callbacks invoked by the XML data parser.
74  */
75
76
77 void rubyLineDirective( ostream &out, const char *fileName, int line )
78 {
79         /* Write a comment containing line info. */
80         out << "# line " << line  << " \"";
81         for ( const char *pc = fileName; *pc != 0; pc++ ) {
82                 if ( *pc == '\\' )
83                         out << "\\\\";
84                 else
85                         out << *pc;
86         }
87         out << "\"\n";
88 }
89
90 void RubyCodeGen::genLineDirective( ostream &out )
91 {
92         std::streambuf *sbuf = out.rdbuf();
93         output_filter *filter = static_cast<output_filter*>(sbuf);
94         rubyLineDirective( out, filter->fileName, filter->line + 1 );
95 }
96
97 string RubyCodeGen::DATA_PREFIX()
98 {
99         if ( !noPrefix )
100                 return FSM_NAME() + "_";
101         return "";
102 }
103
104 std::ostream &RubyCodeGen::STATIC_VAR( string type, string name )
105 {
106         out << 
107                 "class << self\n"
108                 "       attr_accessor :" << name << "\n"
109                 "end\n"
110                 "self." << name;
111         return out;
112 }
113
114
115 std::ostream &RubyCodeGen::OPEN_ARRAY( string type, string name )
116 {
117         out << 
118                 "class << self\n"
119                 "       attr_accessor :" << name << "\n"
120                 "       private :" << name << ", :" << name << "=\n"
121                 "end\n"
122                 "self." << name << " = [\n";
123         return out;
124 }
125
126 std::ostream &RubyCodeGen::CLOSE_ARRAY()
127 {
128         out << "]\n";
129         return out;
130 }
131
132
133 string RubyCodeGen::ARR_OFF( string ptr, string offset )
134 {
135         return ptr + "[" + offset + "]";
136 }
137
138 string RubyCodeGen::NULL_ITEM()
139 {
140         return "nil";
141 }
142
143
144 string RubyCodeGen::P()
145
146         ostringstream ret;
147         if ( pExpr == 0 )
148                 ret << "p";
149         else {
150                 //ret << "(";
151                 INLINE_LIST( ret, pExpr, 0, false );
152                 //ret << ")";
153         }
154         return ret.str();
155 }
156
157 string RubyCodeGen::PE()
158 {
159         ostringstream ret;
160         if ( peExpr == 0 )
161                 ret << "pe";
162         else {
163                 //ret << "(";
164                 INLINE_LIST( ret, peExpr, 0, false );
165                 //ret << ")";
166         }
167         return ret.str();
168 }
169
170 string RubyCodeGen::EOFV()
171 {
172         ostringstream ret;
173         if ( eofExpr == 0 )
174                 ret << "eof";
175         else {
176                 //ret << "(";
177                 INLINE_LIST( ret, eofExpr, 0, false );
178                 //ret << ")";
179         }
180         return ret.str();
181 }
182
183 string RubyCodeGen::CS()
184 {
185         ostringstream ret;
186         if ( csExpr == 0 )
187                 ret << ACCESS() << "cs";
188         else {
189                 //ret << "(";
190                 INLINE_LIST( ret, csExpr, 0, false );
191                 //ret << ")";
192         }
193         return ret.str();
194 }
195
196 string RubyCodeGen::TOP()
197 {
198         ostringstream ret;
199         if ( topExpr == 0 )
200                 ret << ACCESS() + "top";
201         else {
202                 //ret << "(";
203                 INLINE_LIST( ret, topExpr, 0, false );
204                 //ret << ")";
205         }
206         return ret.str();
207 }
208
209 string RubyCodeGen::STACK()
210 {
211         ostringstream ret;
212         if ( stackExpr == 0 )
213                 ret << ACCESS() + "stack";
214         else {
215                 //ret << "(";
216                 INLINE_LIST( ret, stackExpr, 0, false );
217                 //ret << ")";
218         }
219         return ret.str();
220 }
221
222 string RubyCodeGen::ACT()
223 {
224         ostringstream ret;
225         if ( actExpr == 0 )
226                 ret << ACCESS() + "act";
227         else {
228                 //ret << "(";
229                 INLINE_LIST( ret, actExpr, 0, false );
230                 //ret << ")";
231         }
232         return ret.str();
233 }
234
235 string RubyCodeGen::TOKSTART()
236 {
237         ostringstream ret;
238         if ( tokstartExpr == 0 )
239                 ret << ACCESS() + "ts";
240         else {
241                 //ret << "(";
242                 INLINE_LIST( ret, tokstartExpr, 0, false );
243                 //ret << ")";
244         }
245         return ret.str();
246 }
247
248 string RubyCodeGen::TOKEND()
249 {
250         ostringstream ret;
251         if ( tokendExpr == 0 )
252                 ret << ACCESS() + "te";
253         else {
254                 //ret << "(";
255                 INLINE_LIST( ret, tokendExpr, 0, false );
256                 //ret << ")";
257         }
258         return ret.str();
259 }
260
261 string RubyCodeGen::DATA()
262 {
263         ostringstream ret;
264         if ( dataExpr == 0 )
265                 ret << ACCESS() + "data";
266         else {
267                 //ret << "(";
268                 INLINE_LIST( ret, dataExpr, 0, false );
269                 //ret << ")";
270         }
271         return ret.str();
272 }
273
274 /* Write out the fsm name. */
275 string RubyCodeGen::FSM_NAME()
276 {
277         return fsmName;
278 }
279
280
281 void RubyCodeGen::ACTION( ostream &ret, GenAction *action, int targState, bool inFinish )
282 {
283         /* Write the preprocessor line info for going into the source file. */
284         rubyLineDirective( ret, sourceFileName, action->loc.line );
285
286         /* Write the block and close it off. */
287         ret << "                begin\n";
288         INLINE_LIST( ret, action->inlineList, targState, inFinish );
289         ret << "                end\n";
290         rubyLineDirective( ret, sourceFileName, action->loc.line );
291 }
292
293
294
295 string RubyCodeGen::GET_WIDE_KEY()
296 {
297         if ( redFsm->anyConditions() ) 
298                 return "_widec";
299         else
300                 return GET_KEY();
301 }
302
303 string RubyCodeGen::GET_WIDE_KEY( RedStateAp *state )
304 {
305         if ( state->stateCondList.length() > 0 )
306                 return "_widec";
307         else
308                 return GET_KEY();
309 }
310
311 string RubyCodeGen::GET_KEY()
312 {
313         ostringstream ret;
314         if ( getKeyExpr != 0 ) { 
315                 /* Emit the user supplied method of retrieving the key. */
316                 ret << "(";
317                 INLINE_LIST( ret, getKeyExpr, 0, false );
318                 ret << ")";
319         }
320         else {
321                 /* Expression for retrieving the key, use simple dereference. */
322                 ret << DATA() << "[" << P() << "]";
323         }
324         return ret.str();
325 }
326
327 string RubyCodeGen::KEY( Key key )
328 {
329         ostringstream ret;
330         if ( keyOps->isSigned || !hostLang->explicitUnsigned )
331                 ret << key.getVal();
332         else
333                 ret << (unsigned long) key.getVal();
334         return ret.str();
335 }
336
337
338 /* Write out level number of tabs. Makes the nested binary search nice
339  * looking. */
340 string RubyCodeGen::TABS( int level )
341 {
342         string result;
343         while ( level-- > 0 )
344                 result += "\t";
345         return result;
346 }
347
348 string RubyCodeGen::INT( int i )
349 {
350         ostringstream ret;
351         ret << i;
352         return ret.str();
353 }
354
355 void RubyCodeGen::CONDITION( ostream &ret, GenAction *condition )
356 {
357         ret << "\n";
358         rubyLineDirective( ret, sourceFileName, condition->loc.line );
359         INLINE_LIST( ret, condition->inlineList, 0, false );
360 }
361
362 /* Emit the alphabet data type. */
363 string RubyCodeGen::ALPH_TYPE()
364 {
365         string ret = keyOps->alphType->data1;
366         if ( keyOps->alphType->data2 != 0 ) {
367                 ret += " ";
368                 ret += + keyOps->alphType->data2;
369         }
370         return ret;
371 }
372
373 /* Emit the alphabet data type. */
374 string RubyCodeGen::WIDE_ALPH_TYPE()
375 {
376         string ret;
377         if ( redFsm->maxKey <= keyOps->maxKey )
378                 ret = ALPH_TYPE();
379         else {
380                 long long maxKeyVal = redFsm->maxKey.getLongLong();
381                 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
382                 assert( wideType != 0 );
383
384                 ret = wideType->data1;
385                 if ( wideType->data2 != 0 ) {
386                         ret += " ";
387                         ret += wideType->data2;
388                 }
389         }
390         return ret;
391 }
392
393
394 string RubyCodeGen::ARRAY_TYPE( unsigned long maxVal )
395 {
396         long long maxValLL = (long long) maxVal;
397         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
398         assert( arrayType != 0 );
399
400         string ret = arrayType->data1;
401         if ( arrayType->data2 != 0 ) {
402                 ret += " ";
403                 ret += arrayType->data2;
404         }
405         return ret;
406 }
407
408 /* Write out the array of actions. */
409 std::ostream &RubyCodeGen::ACTIONS_ARRAY()
410 {
411         START_ARRAY_LINE();
412         int totalActions = 0;
413         ARRAY_ITEM( INT(0), ++totalActions, false );
414         for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
415                 /* Write out the length, which will never be the last character. */
416                 ARRAY_ITEM( INT(act->key.length()), ++totalActions, false );
417
418                 for ( GenActionTable::Iter item = act->key; item.lte(); item++ ) {
419                         ARRAY_ITEM( INT(item->value->actionId), ++totalActions, (act.last() && item.last()) );
420                 }
421         }
422         END_ARRAY_LINE();
423         return out;
424 }
425
426 void RubyCodeGen::STATE_IDS()
427 {
428         if ( redFsm->startState != 0 )
429                 STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
430
431         if ( !noFinal )
432                 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
433
434         if ( !noError )
435                 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
436
437         out << "\n";
438
439         if ( entryPointNames.length() > 0 ) {
440                 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
441                         STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) << 
442                                         " = " << entryPointIds[en.pos()] << ";\n";
443                 }
444                 out << "\n";
445         }
446 }
447
448 std::ostream &RubyCodeGen::START_ARRAY_LINE()
449 {
450         out << "\t";
451         return out;
452 }
453
454 std::ostream &RubyCodeGen::ARRAY_ITEM( string item, int count, bool last )
455 {
456         out << item;
457         if ( !last )
458         {
459                 out << ", ";
460                 if ( count % IALL == 0 )
461                 {
462                         END_ARRAY_LINE();
463                         START_ARRAY_LINE();
464                 }
465         }
466         return out;
467 }
468
469 std::ostream &RubyCodeGen::END_ARRAY_LINE()
470 {
471         out << "\n";
472         return out;
473 }
474
475 /* Emit the offset of the start state as a decimal integer. */
476 string RubyCodeGen::START_STATE_ID()
477 {
478         ostringstream ret;
479         ret << redFsm->startState->id;
480         return ret.str();
481 };
482
483 string RubyCodeGen::ERROR_STATE()
484 {
485         ostringstream ret;
486         if ( redFsm->errState != 0 )
487                 ret << redFsm->errState->id;
488         else
489                 ret << "-1";
490         return ret.str();
491 }
492
493 string RubyCodeGen::FIRST_FINAL_STATE()
494 {
495         ostringstream ret;
496         if ( redFsm->firstFinState != 0 )
497                 ret << redFsm->firstFinState->id;
498         else
499                 ret << redFsm->nextStateId;
500         return ret.str();
501 }
502
503 string RubyCodeGen::ACCESS()
504 {
505         ostringstream ret;
506         if ( accessExpr != 0 )
507                 INLINE_LIST( ret, accessExpr, 0, false );
508         return ret.str();
509 }
510
511 /* Write out an inline tree structure. Walks the list and possibly calls out
512  * to virtual functions than handle language specific items in the tree. */
513 void RubyCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList, 
514                 int targState, bool inFinish )
515 {
516         for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
517                 switch ( item->type ) {
518                 case GenInlineItem::Text:
519                         ret << item->data;
520                         break;
521                 case GenInlineItem::Goto:
522                         GOTO( ret, item->targState->id, inFinish );
523                         break;
524                 case GenInlineItem::Call:
525                         CALL( ret, item->targState->id, targState, inFinish );
526                         break;
527                 case GenInlineItem::Next:
528                         NEXT( ret, item->targState->id, inFinish );
529                         break;
530                 case GenInlineItem::Ret:
531                         RET( ret, inFinish );
532                         break;
533                 case GenInlineItem::PChar:
534                         ret << P();
535                         break;
536                 case GenInlineItem::Char:
537                         ret << GET_KEY();
538                         break;
539                 case GenInlineItem::Hold:
540                         ret << P() << " = " << P() << " - 1;";
541                         break;
542                 case GenInlineItem::Exec:
543                         EXEC( ret, item, targState, inFinish );
544                         break;
545                 case GenInlineItem::Curs:
546                         ret << "(_ps)";
547                         break;
548                 case GenInlineItem::Targs:
549                         ret << "(" << CS() << ")";
550                         break;
551                 case GenInlineItem::Entry:
552                         ret << item->targState->id;
553                         break;
554                 case GenInlineItem::GotoExpr:
555                         GOTO_EXPR( ret, item, inFinish );
556                         break;
557                 case GenInlineItem::CallExpr:
558                         CALL_EXPR( ret, item, targState, inFinish );
559                         break;
560                 case GenInlineItem::NextExpr:
561                         NEXT_EXPR( ret, item, inFinish );
562                         break;
563                 case GenInlineItem::LmSwitch:
564                         LM_SWITCH( ret, item, targState, inFinish );
565                         break;
566                 case GenInlineItem::LmSetActId:
567                         SET_ACT( ret, item );
568                         break;
569                 case GenInlineItem::LmSetTokEnd:
570                         SET_TOKEND( ret, item );
571                         break;
572                 case GenInlineItem::LmGetTokEnd:
573                         GET_TOKEND( ret, item );
574                         break;
575                 case GenInlineItem::LmInitTokStart:
576                         INIT_TOKSTART( ret, item );
577                         break;
578                 case GenInlineItem::LmInitAct:
579                         INIT_ACT( ret, item );
580                         break;
581                 case GenInlineItem::LmSetTokStart:
582                         SET_TOKSTART( ret, item );
583                         break;
584                 case GenInlineItem::SubAction:
585                         SUB_ACTION( ret, item, targState, inFinish );
586                         break;
587                 case GenInlineItem::Break:
588                         BREAK( ret, targState );
589                         break;
590                 }
591         }
592 }
593
594
595 void RubyCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish )
596 {
597         /* The parser gives fexec two children. The double brackets are for D
598          * code. If the inline list is a single word it will get interpreted as a
599          * C-style cast by the D compiler. */
600         ret << " begin " << P() << " = ((";
601         INLINE_LIST( ret, item->children, targState, inFinish );
602         ret << "))-1; end\n";
603 }
604
605 void RubyCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item, 
606                 int targState, int inFinish )
607 {
608         ret << 
609                 "       case " << ACT() << "\n";
610
611         for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
612                 /* Write the case label, the action and the case break. */
613                 if ( lma->lmId < 0 )
614                         ret << "        else\n";
615                 else
616                         ret << "        when " << lma->lmId << " then\n";
617
618
619                 /* Write the block and close it off. */
620                 ret << "        begin";
621                 INLINE_LIST( ret, lma->children, targState, inFinish );
622                 ret << "end\n";
623         }
624
625         ret << "end \n\t";
626 }
627
628 void RubyCodeGen::SET_ACT( ostream &ret, GenInlineItem *item )
629 {
630         ret << ACT() << " = " << item->lmId << ";";
631 }
632
633 void RubyCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item )
634 {
635         ret << TOKSTART() << " = " << NULL_ITEM() << ";";
636 }
637
638 void RubyCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item )
639 {
640         ret << ACT() << " = 0\n";
641 }
642
643 void RubyCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item )
644 {
645         ret << TOKSTART() << " = " << P() << "\n";
646 }
647
648 void RubyCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item )
649 {
650         /* The tokend action sets tokend. */
651         ret << TOKEND() << " = " << P();
652         if ( item->offset != 0 ) 
653                 out << "+" << item->offset;
654         out << "\n";
655 }
656
657 void RubyCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item )
658 {
659         ret << TOKEND();
660 }
661
662 void RubyCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item, 
663                 int targState, bool inFinish )
664 {
665         if ( item->children->length() > 0 ) {
666                 /* Write the block and close it off. */
667                 ret << " begin ";
668                 INLINE_LIST( ret, item->children, targState, inFinish );
669                 ret << " end\n";
670         }
671 }
672
673 int RubyCodeGen::TRANS_ACTION( RedTransAp *trans )
674 {
675         /* If there are actions, emit them. Otherwise emit zero. */
676         int act = 0;
677         if ( trans->action != 0 )
678                 act = trans->action->location+1;
679         return act;
680 }
681
682 ostream &RubyCodeGen::source_warning( const InputLoc &loc )
683 {
684         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
685         return cerr;
686 }
687
688 ostream &RubyCodeGen::source_error( const InputLoc &loc )
689 {
690         gblErrorCount += 1;
691         assert( sourceFileName != 0 );
692         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
693         return cerr;
694 }
695
696 void RubyCodeGen::finishRagelDef()
697 {
698
699         if ( codeStyle == GenGoto || codeStyle == GenFGoto || 
700                         codeStyle == GenIpGoto || codeStyle == GenSplit )
701         {
702                 /* For directly executable machines there is no required state
703                  * ordering. Choose a depth-first ordering to increase the
704                  * potential for fall-throughs. */
705                 redFsm->depthFirstOrdering();
706         }
707         else {
708                 /* The frontend will do this for us, but it may be a good idea to
709                  * force it if the intermediate file is edited. */
710                 redFsm->sortByStateId();
711         }
712
713         /* Choose default transitions and the single transition. */
714         redFsm->chooseDefaultSpan();
715                 
716         /* Maybe do flat expand, otherwise choose single. */
717         if ( codeStyle == GenFlat || codeStyle == GenFFlat )
718                 redFsm->makeFlat();
719         else
720                 redFsm->chooseSingle();
721
722         /* If any errors have occured in the input file then don't write anything. */
723         if ( gblErrorCount > 0 )
724                 return;
725         
726         if ( codeStyle == GenSplit )
727                 redFsm->partitionFsm( numSplitPartitions );
728
729         if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
730                 redFsm->setInTrans();
731
732         /* Anlayze Machine will find the final action reference counts, among
733          * other things. We will use these in reporting the usage
734          * of fsm directives in action code. */
735         analyzeMachine();
736
737         /* Determine if we should use indicies. */
738         calcIndexSize();
739 }
740
741
742 /* Determine if we should use indicies or not. */
743 void RubyCodeGen::calcIndexSize()
744 {
745         int sizeWithInds = 0, sizeWithoutInds = 0;
746
747         /* Calculate cost of using with indicies. */
748         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
749                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
750                                 (st->defTrans == 0 ? 0 : 1);
751                 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
752         }
753         sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
754         if ( redFsm->anyActions() )
755                 sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
756
757         /* Calculate the cost of not using indicies. */
758         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
759                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
760                                 (st->defTrans == 0 ? 0 : 1);
761                 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
762                 if ( redFsm->anyActions() )
763                         sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex;
764         }
765
766         /* If using indicies reduces the size, use them. */
767         useIndicies = sizeWithInds < sizeWithoutInds;
768 }
769
770 unsigned int RubyCodeGen::arrayTypeSize( unsigned long maxVal )
771 {
772         long long maxValLL = (long long) maxVal;
773         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
774         assert( arrayType != 0 );
775         return arrayType->size;
776 }
777
778
779 void RubyCodeGen::writeInit()
780 {
781         out << "begin\n";
782         
783         out << "        " << P() << " ||= 0\n";
784
785         if ( !noEnd ) 
786                 out << "        " << PE() << " ||= " << DATA() << ".length\n";
787
788         if ( !noCS )
789                 out << "        " << CS() << " = " << START() << "\n";
790
791         /* If there are any calls, then the stack top needs initialization. */
792         if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
793                 out << "        " << TOP() << " = 0\n";
794
795         if ( hasLongestMatch ) {
796                 out <<
797                         "       " << TOKSTART() << " = " << NULL_ITEM() << "\n"
798                         "       " << TOKEND() << " = " << NULL_ITEM() << "\n"
799                         "       " << ACT() << " = 0\n";
800         }
801
802         out << "end\n";
803 }
804
805 void RubyCodeGen::writeExports()
806 {
807         if ( exportList.length() > 0 ) {
808                 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
809                         STATIC_VAR( ALPH_TYPE(), DATA_PREFIX() + "ex_" + ex->name ) 
810                                         << " = " << KEY(ex->key) << "\n";
811                 }
812                 out << "\n";
813         }
814 }
815
816 void RubyCodeGen::writeStart()
817 {
818         out << START_STATE_ID();
819 }
820
821 void RubyCodeGen::writeFirstFinal()
822 {
823         out << FIRST_FINAL_STATE();
824 }
825
826 void RubyCodeGen::writeError()
827 {
828         out << ERROR_STATE();
829 }
830
831
832 /*
833  * Local Variables:
834  * mode: c++
835  * indent-tabs-mode: 1
836  * c-file-style: "bsd"
837  * End:
838  */