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