cscodegen.cpp 18 KB

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