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