2 * Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
3 * 2004 Erich Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
7 /* This file is part of Ragel.
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.
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.
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
25 #include "cscodegen.h"
34 using std::ostringstream;
48 void csharpLineDirective( ostream &out, const char *fileName, int line )
50 if ( noLineDirectives )
53 /* Write the preprocessor line info for to the input file. */
54 out << "#line " << line << " \"";
55 for ( const char *pc = fileName; *pc != 0; pc++ ) {
63 if ( noLineDirectives )
69 void CSharpFsmCodeGen::genLineDirective( ostream &out )
71 std::streambuf *sbuf = out.rdbuf();
72 output_filter *filter = static_cast<output_filter*>(sbuf);
73 csharpLineDirective( out, filter->fileName, filter->line + 1 );
77 /* Init code gen with in parameters. */
78 CSharpFsmCodeGen::CSharpFsmCodeGen( ostream &out )
84 unsigned int CSharpFsmCodeGen::arrayTypeSize( unsigned long maxVal )
86 long long maxValLL = (long long) maxVal;
87 HostType *arrayType = keyOps->typeSubsumes( maxValLL );
88 assert( arrayType != 0 );
89 return arrayType->size;
92 string CSharpFsmCodeGen::ARRAY_TYPE( unsigned long maxVal )
94 return ARRAY_TYPE( maxVal, false );
97 string CSharpFsmCodeGen::ARRAY_TYPE( unsigned long maxVal, bool forceSigned )
99 long long maxValLL = (long long) maxVal;
102 arrayType = keyOps->typeSubsumes(true, maxValLL);
104 arrayType = keyOps->typeSubsumes( maxValLL );
105 assert( arrayType != 0 );
107 string ret = arrayType->data1;
108 if ( arrayType->data2 != 0 ) {
110 ret += arrayType->data2;
115 /* Write out the fsm name. */
116 string CSharpFsmCodeGen::FSM_NAME()
121 /* Emit the offset of the start state as a decimal integer. */
122 string CSharpFsmCodeGen::START_STATE_ID()
125 ret << redFsm->startState->id;
129 /* Write out the array of actions. */
130 std::ostream &CSharpFsmCodeGen::ACTIONS_ARRAY()
133 int totalActions = 1;
134 for ( GenActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
135 /* Write out the length, which will never be the last character. */
136 out << act->key.length() << ", ";
137 /* Put in a line break every 8 */
138 if ( totalActions++ % 8 == 7 )
141 for ( GenActionTable::Iter item = act->key; item.lte(); item++ ) {
142 out << item->value->actionId;
143 if ( ! (act.last() && item.last()) )
146 /* Put in a line break every 8 */
147 if ( totalActions++ % 8 == 7 )
156 string CSharpFsmCodeGen::ACCESS()
159 if ( accessExpr != 0 )
160 INLINE_LIST( ret, accessExpr, 0, false );
165 string CSharpFsmCodeGen::P()
172 INLINE_LIST( ret, pExpr, 0, false );
178 string CSharpFsmCodeGen::PE()
185 INLINE_LIST( ret, peExpr, 0, false );
191 string CSharpFsmCodeGen::vEOF()
198 INLINE_LIST( ret, eofExpr, 0, false );
204 string CSharpFsmCodeGen::vCS()
208 ret << ACCESS() << "cs";
210 /* Emit the user supplied method of retrieving the key. */
212 INLINE_LIST( ret, csExpr, 0, false );
218 string CSharpFsmCodeGen::TOP()
222 ret << ACCESS() + "top";
225 INLINE_LIST( ret, topExpr, 0, false );
231 string CSharpFsmCodeGen::STACK()
234 if ( stackExpr == 0 )
235 ret << ACCESS() + "stack";
238 INLINE_LIST( ret, stackExpr, 0, false );
244 string CSharpFsmCodeGen::ACT()
248 ret << ACCESS() + "act";
251 INLINE_LIST( ret, actExpr, 0, false );
257 string CSharpFsmCodeGen::TOKSTART()
260 if ( tokstartExpr == 0 )
261 ret << ACCESS() + "ts";
264 INLINE_LIST( ret, tokstartExpr, 0, false );
270 string CSharpFsmCodeGen::TOKEND()
273 if ( tokendExpr == 0 )
274 ret << ACCESS() + "te";
277 INLINE_LIST( ret, tokendExpr, 0, false );
283 string CSharpFsmCodeGen::GET_WIDE_KEY()
285 if ( redFsm->anyConditions() )
291 string CSharpFsmCodeGen::GET_WIDE_KEY( RedStateAp *state )
293 if ( state->stateCondList.length() > 0 )
299 string CSharpFsmCodeGen::GET_KEY()
302 if ( getKeyExpr != 0 ) {
303 /* Emit the user supplied method of retrieving the key. */
305 INLINE_LIST( ret, getKeyExpr, 0, false );
309 /* Expression for retrieving the key, use simple dereference. */
310 ret << "(*" << P() << ")";
315 /* Write out level number of tabs. Makes the nested binary search nice
317 string CSharpFsmCodeGen::TABS( int level )
320 while ( level-- > 0 )
325 /* Write out a key from the fsm code gen. Depends on wether or not the key is
327 string CSharpFsmCodeGen::KEY( Key key )
330 if ( keyOps->isSigned || !hostLang->explicitUnsigned )
333 ret << (unsigned long) key.getVal() << 'u';
337 string CSharpFsmCodeGen::ALPHA_KEY( Key key )
340 if (key.getVal() > 0xFFFF) {
343 ret << "'\\u" << std::hex << std::setw(4) << std::setfill('0') <<
346 //ret << "(char) " << key.getVal();
350 void CSharpFsmCodeGen::EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish )
352 /* The parser gives fexec two children. The double brackets are for D
353 * code. If the inline list is a single word it will get interpreted as a
354 * C-style cast by the D compiler. */
355 ret << "{" << P() << " = ((";
356 INLINE_LIST( ret, item->children, targState, inFinish );
360 void CSharpFsmCodeGen::LM_SWITCH( ostream &ret, GenInlineItem *item,
361 int targState, int inFinish )
364 " switch( " << ACT() << " ) {\n";
366 for ( GenInlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
367 /* Write the case label, the action and the case break. */
369 ret << " default:\n";
371 ret << " case " << lma->lmId << ":\n";
373 /* Write the block and close it off. */
375 INLINE_LIST( ret, lma->children, targState, inFinish );
386 void CSharpFsmCodeGen::SET_ACT( ostream &ret, GenInlineItem *item )
388 ret << ACT() << " = " << item->lmId << ";";
391 void CSharpFsmCodeGen::SET_TOKEND( ostream &ret, GenInlineItem *item )
393 /* The tokend action sets tokend. */
394 ret << TOKEND() << " = " << P();
395 if ( item->offset != 0 )
396 out << "+" << item->offset;
400 void CSharpFsmCodeGen::GET_TOKEND( ostream &ret, GenInlineItem *item )
405 void CSharpFsmCodeGen::INIT_TOKSTART( ostream &ret, GenInlineItem *item )
407 ret << TOKSTART() << " = " << NULL_ITEM() << ";";
410 void CSharpFsmCodeGen::INIT_ACT( ostream &ret, GenInlineItem *item )
412 ret << ACT() << " = 0;";
415 void CSharpFsmCodeGen::SET_TOKSTART( ostream &ret, GenInlineItem *item )
417 ret << TOKSTART() << " = " << P() << ";";
420 void CSharpFsmCodeGen::SUB_ACTION( ostream &ret, GenInlineItem *item,
421 int targState, bool inFinish )
423 if ( item->children->length() > 0 ) {
424 /* Write the block and close it off. */
426 INLINE_LIST( ret, item->children, targState, inFinish );
432 /* Write out an inline tree structure. Walks the list and possibly calls out
433 * to virtual functions than handle language specific items in the tree. */
434 void CSharpFsmCodeGen::INLINE_LIST( ostream &ret, GenInlineList *inlineList,
435 int targState, bool inFinish )
437 for ( GenInlineList::Iter item = *inlineList; item.lte(); item++ ) {
438 switch ( item->type ) {
439 case GenInlineItem::Text:
442 case GenInlineItem::Goto:
443 GOTO( ret, item->targState->id, inFinish );
445 case GenInlineItem::Call:
446 CALL( ret, item->targState->id, targState, inFinish );
448 case GenInlineItem::Next:
449 NEXT( ret, item->targState->id, inFinish );
451 case GenInlineItem::Ret:
452 RET( ret, inFinish );
454 case GenInlineItem::PChar:
457 case GenInlineItem::Char:
460 case GenInlineItem::Hold:
463 case GenInlineItem::Exec:
464 EXEC( ret, item, targState, inFinish );
466 case GenInlineItem::Curs:
467 CURS( ret, inFinish );
469 case GenInlineItem::Targs:
470 TARGS( ret, inFinish, targState );
472 case GenInlineItem::Entry:
473 ret << item->targState->id;
475 case GenInlineItem::GotoExpr:
476 GOTO_EXPR( ret, item, inFinish );
478 case GenInlineItem::CallExpr:
479 CALL_EXPR( ret, item, targState, inFinish );
481 case GenInlineItem::NextExpr:
482 NEXT_EXPR( ret, item, inFinish );
484 case GenInlineItem::LmSwitch:
485 LM_SWITCH( ret, item, targState, inFinish );
487 case GenInlineItem::LmSetActId:
488 SET_ACT( ret, item );
490 case GenInlineItem::LmSetTokEnd:
491 SET_TOKEND( ret, item );
493 case GenInlineItem::LmGetTokEnd:
494 GET_TOKEND( ret, item );
496 case GenInlineItem::LmInitTokStart:
497 INIT_TOKSTART( ret, item );
499 case GenInlineItem::LmInitAct:
500 INIT_ACT( ret, item );
502 case GenInlineItem::LmSetTokStart:
503 SET_TOKSTART( ret, item );
505 case GenInlineItem::SubAction:
506 SUB_ACTION( ret, item, targState, inFinish );
508 case GenInlineItem::Break:
509 BREAK( ret, targState );
514 /* Write out paths in line directives. Escapes any special characters. */
515 string CSharpFsmCodeGen::LDIR_PATH( char *path )
518 for ( char *pc = path; *pc != 0; pc++ ) {
527 void CSharpFsmCodeGen::ACTION( ostream &ret, GenAction *action, int targState, bool inFinish )
529 /* Write the preprocessor line info for going into the source file. */
530 csharpLineDirective( ret, sourceFileName, action->loc.line );
532 /* Write the block and close it off. */
534 INLINE_LIST( ret, action->inlineList, targState, inFinish );
538 void CSharpFsmCodeGen::CONDITION( ostream &ret, GenAction *condition )
541 csharpLineDirective( ret, sourceFileName, condition->loc.line );
542 INLINE_LIST( ret, condition->inlineList, 0, false );
545 string CSharpFsmCodeGen::ERROR_STATE()
548 if ( redFsm->errState != 0 )
549 ret << redFsm->errState->id;
555 string CSharpFsmCodeGen::FIRST_FINAL_STATE()
558 if ( redFsm->firstFinState != 0 )
559 ret << redFsm->firstFinState->id;
561 ret << redFsm->nextStateId;
565 void CSharpFsmCodeGen::writeInit()
570 out << "\t" << vCS() << " = " << START() << ";\n";
572 /* If there are any calls, then the stack top needs initialization. */
573 if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
574 out << "\t" << TOP() << " = 0;\n";
576 if ( hasLongestMatch ) {
578 " " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
579 " " << TOKEND() << " = " << NULL_ITEM() << ";\n"
580 " " << ACT() << " = 0;\n";
585 string CSharpFsmCodeGen::DATA_PREFIX()
588 return FSM_NAME() + "_";
592 /* Emit the alphabet data type. */
593 string CSharpFsmCodeGen::ALPH_TYPE()
595 string ret = keyOps->alphType->data1;
596 if ( keyOps->alphType->data2 != 0 ) {
598 ret += + keyOps->alphType->data2;
603 /* Emit the alphabet data type. */
604 string CSharpFsmCodeGen::WIDE_ALPH_TYPE()
607 if ( redFsm->maxKey <= keyOps->maxKey )
610 long long maxKeyVal = redFsm->maxKey.getLongLong();
611 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
612 assert( wideType != 0 );
614 ret = wideType->data1;
615 if ( wideType->data2 != 0 ) {
617 ret += wideType->data2;
623 void CSharpFsmCodeGen::STATE_IDS()
625 if ( redFsm->startState != 0 )
626 STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
629 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
632 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
636 if ( entryPointNames.length() > 0 ) {
637 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
638 STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) <<
639 " = " << entryPointIds[en.pos()] << ";\n";
646 void CSharpFsmCodeGen::writeStart()
648 out << START_STATE_ID();
651 void CSharpFsmCodeGen::writeFirstFinal()
653 out << FIRST_FINAL_STATE();
656 void CSharpFsmCodeGen::writeError()
658 out << ERROR_STATE();
664 string CSharpCodeGen::GET_KEY()
667 if ( getKeyExpr != 0 ) {
668 /* Emit the user supplied method of retrieving the key. */
670 INLINE_LIST( ret, getKeyExpr, 0, false );
674 /* Expression for retrieving the key, use simple dereference. */
675 ret << "data[" << P() << "]";
679 string CSharpCodeGen::NULL_ITEM()
684 string CSharpCodeGen::POINTER()
686 // XXX C# has no pointers
687 // multiple items seperated by commas can also be pointer types.
691 string CSharpCodeGen::PTR_CONST()
696 std::ostream &CSharpCodeGen::OPEN_ARRAY( string type, string name )
698 out << "static readonly " << type << "[] " << name << " = ";
701 out << "Encoding.ASCII.Get";
703 out << "new " << type << " [] {\n";
707 std::ostream &CSharpCodeGen::CLOSE_ARRAY()
709 return out << "};\n";
712 std::ostream &CSharpCodeGen::STATIC_VAR( string type, string name )
714 out << "const " << type << " " << name;
718 string CSharpCodeGen::ARR_OFF( string ptr, string offset )
720 // XXX C# can't do pointer arithmetic
721 return "&" + ptr + "[" + offset + "]";
724 string CSharpCodeGen::CAST( string type )
726 return "(" + type + ")";
729 string CSharpCodeGen::UINT( )
734 std::ostream &CSharpCodeGen::SWITCH_DEFAULT()
736 out << " default: break;\n";
740 string CSharpCodeGen::CTRL_FLOW()
745 void CSharpCodeGen::writeExports()
747 if ( exportList.length() > 0 ) {
748 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
749 out << "const " << ALPH_TYPE() << " " << DATA_PREFIX() <<
750 "ex_" << ex->name << " = " << KEY(ex->key) << ";\n";
757 * End C#-specific code.
760 void CSharpFsmCodeGen::finishRagelDef()
762 if ( codeStyle == GenGoto || codeStyle == GenFGoto ||
763 codeStyle == GenIpGoto || codeStyle == GenSplit )
765 /* For directly executable machines there is no required state
766 * ordering. Choose a depth-first ordering to increase the
767 * potential for fall-throughs. */
768 redFsm->depthFirstOrdering();
771 /* The frontend will do this for us, but it may be a good idea to
772 * force it if the intermediate file is edited. */
773 redFsm->sortByStateId();
776 /* Choose default transitions and the single transition. */
777 redFsm->chooseDefaultSpan();
779 /* Maybe do flat expand, otherwise choose single. */
780 if ( codeStyle == GenFlat || codeStyle == GenFFlat )
783 redFsm->chooseSingle();
785 /* If any errors have occured in the input file then don't write anything. */
786 if ( gblErrorCount > 0 )
789 if ( codeStyle == GenSplit )
790 redFsm->partitionFsm( numSplitPartitions );
792 if ( codeStyle == GenIpGoto || codeStyle == GenSplit )
793 redFsm->setInTrans();
795 /* Anlayze Machine will find the final action reference counts, among
796 * other things. We will use these in reporting the usage
797 * of fsm directives in action code. */
800 /* Determine if we should use indicies. */
804 ostream &CSharpFsmCodeGen::source_warning( const InputLoc &loc )
806 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
810 ostream &CSharpFsmCodeGen::source_error( const InputLoc &loc )
813 assert( sourceFileName != 0 );
814 cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";