rubyflat.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
  3. * Copyright 2007 Victor Hugo Borja <vic@rubyforge.org>
  4. */
  5. /* This file is part of Ragel.
  6. *
  7. * Ragel is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ragel is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ragel; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "rubyflat.h"
  22. #include "ragel.h"
  23. #include "redfsm.h"
  24. #include "gendata.h"
  25. using std::ostream;
  26. using std::string;
  27. std::ostream &RubyFlatCodeGen::TO_STATE_ACTION_SWITCH()
  28. {
  29. /* Walk the list of functions, printing the cases. */
  30. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  31. /* Write out referenced actions. */
  32. if ( act->numToStateRefs > 0 ) {
  33. /* Write the case label, the action and the case break */
  34. out << "\twhen " << act->actionId << " then\n";
  35. ACTION( out, act, 0, false );
  36. }
  37. }
  38. genLineDirective( out );
  39. return out;
  40. }
  41. std::ostream &RubyFlatCodeGen::FROM_STATE_ACTION_SWITCH()
  42. {
  43. /* Walk the list of functions, printing the cases. */
  44. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  45. /* Write out referenced actions. */
  46. if ( act->numFromStateRefs > 0 ) {
  47. /* Write the case label, the action and the case break */
  48. out << "\twhen " << act->actionId << " then\n";
  49. ACTION( out, act, 0, false );
  50. }
  51. }
  52. genLineDirective( out );
  53. return out;
  54. }
  55. std::ostream &RubyFlatCodeGen::EOF_ACTION_SWITCH()
  56. {
  57. /* Walk the list of functions, printing the cases. */
  58. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  59. /* Write out referenced actions. */
  60. if ( act->numEofRefs > 0 ) {
  61. /* Write the case label, the action and the case break */
  62. out << "\twhen " << act->actionId << " then\n";
  63. ACTION( out, act, 0, true );
  64. }
  65. }
  66. genLineDirective( out );
  67. return out;
  68. }
  69. std::ostream &RubyFlatCodeGen::ACTION_SWITCH()
  70. {
  71. /* Walk the list of functions, printing the cases. */
  72. for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
  73. /* Write out referenced actions. */
  74. if ( act->numTransRefs > 0 ) {
  75. /* Write the case label, the action and the case break */
  76. out << "\twhen " << act->actionId << " then\n";
  77. ACTION( out, act, 0, false );
  78. }
  79. }
  80. genLineDirective( out );
  81. return out;
  82. }
  83. std::ostream &RubyFlatCodeGen::KEYS()
  84. {
  85. START_ARRAY_LINE();
  86. int totalTrans = 0;
  87. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  88. /* Emit just low key and high key. */
  89. ARRAY_ITEM( KEY( st->lowKey ), ++totalTrans, false );
  90. ARRAY_ITEM( KEY( st->highKey ), ++totalTrans, false );
  91. if ( ++totalTrans % IALL == 0 )
  92. out << "\n\t";
  93. }
  94. /* Output one last number so we don't have to figure out when the last
  95. * entry is and avoid writing a comma. */
  96. ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
  97. END_ARRAY_LINE();
  98. return out;
  99. }
  100. std::ostream &RubyFlatCodeGen::INDICIES()
  101. {
  102. int totalTrans = 0;
  103. START_ARRAY_LINE();
  104. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  105. if ( st->transList != 0 ) {
  106. /* Walk the singles. */
  107. unsigned long long span = keyOps->span( st->lowKey, st->highKey );
  108. for ( unsigned long long pos = 0; pos < span; pos++ ) {
  109. ARRAY_ITEM( KEY( st->transList[pos]->id ), ++totalTrans, false );
  110. }
  111. }
  112. /* The state's default index goes next. */
  113. if ( st->defTrans != 0 )
  114. ARRAY_ITEM( KEY( st->defTrans->id ), ++totalTrans, false );
  115. }
  116. /* Output one last number so we don't have to figure out when the last
  117. * entry is and avoid writing a comma. */
  118. ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
  119. END_ARRAY_LINE();
  120. return out;
  121. }
  122. std::ostream &RubyFlatCodeGen::FLAT_INDEX_OFFSET()
  123. {
  124. START_ARRAY_LINE();
  125. int totalStateNum = 0, curIndOffset = 0;
  126. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  127. /* Write the index offset. */
  128. ARRAY_ITEM( INT( curIndOffset ), ++totalStateNum, st.last() );
  129. /* Move the index offset ahead. */
  130. if ( st->transList != 0 )
  131. curIndOffset += keyOps->span( st->lowKey, st->highKey );
  132. if ( st->defTrans != 0 )
  133. curIndOffset += 1;
  134. }
  135. END_ARRAY_LINE();
  136. return out;
  137. }
  138. std::ostream &RubyFlatCodeGen::KEY_SPANS()
  139. {
  140. START_ARRAY_LINE();
  141. int totalStateNum = 0;
  142. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  143. /* Write singles length. */
  144. unsigned long long span = 0;
  145. if ( st->transList != 0 )
  146. span = keyOps->span( st->lowKey, st->highKey );
  147. ARRAY_ITEM( INT( span ), ++totalStateNum, st.last() );
  148. }
  149. END_ARRAY_LINE();
  150. return out;
  151. }
  152. std::ostream &RubyFlatCodeGen::TO_STATE_ACTIONS()
  153. {
  154. START_ARRAY_LINE();
  155. int totalStateNum = 0;
  156. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  157. /* Write any eof action. */
  158. ARRAY_ITEM( INT( TO_STATE_ACTION(st) ), ++totalStateNum, st.last() );
  159. }
  160. END_ARRAY_LINE();
  161. return out;
  162. }
  163. std::ostream &RubyFlatCodeGen::FROM_STATE_ACTIONS()
  164. {
  165. START_ARRAY_LINE();
  166. int totalStateNum = 0;
  167. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  168. /* Write any eof action. */
  169. ARRAY_ITEM( INT( FROM_STATE_ACTION(st) ), ++totalStateNum, st.last() );
  170. }
  171. END_ARRAY_LINE();
  172. return out;
  173. }
  174. std::ostream &RubyFlatCodeGen::EOF_ACTIONS()
  175. {
  176. START_ARRAY_LINE();
  177. int totalStateNum = 0;
  178. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  179. /* Write any eof action. */
  180. ARRAY_ITEM( INT( EOF_ACTION(st) ), ++totalStateNum, st.last() );
  181. }
  182. END_ARRAY_LINE();
  183. return out;
  184. }
  185. std::ostream &RubyFlatCodeGen::EOF_TRANS()
  186. {
  187. START_ARRAY_LINE();
  188. int totalStateNum = 0;
  189. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  190. /* Write any eof action. */
  191. long trans = 0;
  192. if ( st->eofTrans != 0 ) {
  193. assert( st->eofTrans->pos >= 0 );
  194. trans = st->eofTrans->pos+1;
  195. }
  196. /* Write any eof action. */
  197. ARRAY_ITEM( INT(trans), ++totalStateNum, st.last() );
  198. }
  199. END_ARRAY_LINE();
  200. return out;
  201. }
  202. std::ostream &RubyFlatCodeGen::TRANS_TARGS()
  203. {
  204. /* Transitions must be written ordered by their id. */
  205. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  206. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  207. transPtrs[trans->id] = trans;
  208. /* Keep a count of the num of items in the array written. */
  209. START_ARRAY_LINE();
  210. int totalStates = 0;
  211. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  212. /* Save the position. Needed for eofTargs. */
  213. RedTransAp *trans = transPtrs[t];
  214. trans->pos = t;
  215. /* Write out the target state. */
  216. ARRAY_ITEM( INT( trans->targ->id ), ++totalStates, t >= redFsm->transSet.length()-1 );
  217. }
  218. END_ARRAY_LINE();
  219. delete[] transPtrs;
  220. return out;
  221. }
  222. std::ostream &RubyFlatCodeGen::TRANS_ACTIONS()
  223. {
  224. /* Transitions must be written ordered by their id. */
  225. RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
  226. for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
  227. transPtrs[trans->id] = trans;
  228. /* Keep a count of the num of items in the array written. */
  229. START_ARRAY_LINE();
  230. int totalAct = 0;
  231. for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
  232. /* Write the function for the transition. */
  233. RedTransAp *trans = transPtrs[t];
  234. ARRAY_ITEM( INT( TRANS_ACTION( trans ) ), ++totalAct, t >= redFsm->transSet.length()-1 );
  235. }
  236. END_ARRAY_LINE();
  237. delete[] transPtrs;
  238. return out;
  239. }
  240. void RubyFlatCodeGen::LOCATE_TRANS()
  241. {
  242. out <<
  243. " _keys = " << vCS() << " << 1\n"
  244. " _inds = " << IO() << "[" << vCS() << "]\n"
  245. " _slen = " << SP() << "[" << vCS() << "]\n"
  246. " _wide = " << GET_WIDE_KEY() << "\n"
  247. " _trans = if ( _slen > 0 && \n"
  248. " " << K() << "[_keys] <= _wide && \n"
  249. " " << "_wide <= " << K() << "[_keys + 1] \n"
  250. " ) then\n"
  251. " " << I() << "[ _inds + _wide - " << K() << "[_keys] ] \n"
  252. " else \n"
  253. " " << I() << "[ _inds + _slen ]\n"
  254. " end\n"
  255. "";
  256. }
  257. std::ostream &RubyFlatCodeGen::COND_INDEX_OFFSET()
  258. {
  259. START_ARRAY_LINE();
  260. int totalStateNum = 0, curIndOffset = 0;
  261. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  262. /* Write the index offset. */
  263. ARRAY_ITEM( INT( curIndOffset ), ++totalStateNum, st.last() );
  264. /* Move the index offset ahead. */
  265. if ( st->condList != 0 )
  266. curIndOffset += keyOps->span( st->condLowKey, st->condHighKey );
  267. }
  268. END_ARRAY_LINE();
  269. return out;
  270. }
  271. void RubyFlatCodeGen::COND_TRANSLATE()
  272. {
  273. out <<
  274. " _widec = " << GET_KEY() << "\n"
  275. " _keys = " << vCS() << " << 1\n"
  276. " _conds = " << CO() << "[" << vCS() << "]\n"
  277. " _slen = " << CSP() << "[" << vCS() << "]\n"
  278. " _wide = " << GET_WIDE_KEY() << "\n"
  279. " _cond = if ( _slen > 0 && \n"
  280. " " << CK() << "[_keys] <= _wide &&\n"
  281. " " << "_wide <= " << CK() << "[_keys + 1]\n"
  282. " ) then \n"
  283. " " << C() << "[ _conds + _wide - " << CK() << "[_keys]" << " ]\n"
  284. " else\n"
  285. " 0\n"
  286. " end\n";
  287. out <<
  288. " case _cond \n";
  289. for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
  290. GenCondSpace *condSpace = csi;
  291. out << " when " << condSpace->condSpaceId + 1 << " then\n";
  292. out << TABS(2) << "_widec = " << "(" <<
  293. KEY(condSpace->baseKey) << " + (" << GET_KEY() <<
  294. " - " << KEY(keyOps->minKey) << "))\n";
  295. for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
  296. out << TABS(2) << "if ( ";
  297. CONDITION( out, *csi );
  298. Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
  299. out <<
  300. " ) then \n" <<
  301. TABS(3) << " _widec += " << condValOffset << "\n"
  302. "end\n";
  303. }
  304. }
  305. out <<
  306. " end # _cond switch \n";
  307. }
  308. std::ostream &RubyFlatCodeGen::CONDS()
  309. {
  310. int totalTrans = 0;
  311. START_ARRAY_LINE();
  312. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  313. if ( st->condList != 0 ) {
  314. /* Walk the singles. */
  315. unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
  316. for ( unsigned long long pos = 0; pos < span; pos++ ) {
  317. if ( st->condList[pos] != 0 )
  318. ARRAY_ITEM( INT( st->condList[pos]->condSpaceId + 1 ), ++totalTrans, false );
  319. else
  320. ARRAY_ITEM( INT( 0 ), ++totalTrans, false );
  321. }
  322. }
  323. }
  324. /* Output one last number so we don't have to figure out when the last
  325. * entry is and avoid writing a comma. */
  326. ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
  327. END_ARRAY_LINE();
  328. return out;
  329. }
  330. std::ostream &RubyFlatCodeGen::COND_KEYS()
  331. {
  332. START_ARRAY_LINE();
  333. int totalTrans = 0;
  334. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  335. /* Emit just cond low key and cond high key. */
  336. ARRAY_ITEM( KEY( st->condLowKey ), ++totalTrans, false );
  337. ARRAY_ITEM( KEY( st->condHighKey ), ++totalTrans, false );
  338. }
  339. /* Output one last number so we don't have to figure out when the last
  340. * entry is and avoid writing a comma. */
  341. ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
  342. END_ARRAY_LINE();
  343. return out;
  344. }
  345. std::ostream &RubyFlatCodeGen::COND_KEY_SPANS()
  346. {
  347. START_ARRAY_LINE();
  348. int totalStateNum = 0;
  349. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  350. /* Write singles length. */
  351. unsigned long long span = 0;
  352. if ( st->condList != 0 )
  353. span = keyOps->span( st->condLowKey, st->condHighKey );
  354. ARRAY_ITEM( INT( span ), ++totalStateNum, false );
  355. }
  356. END_ARRAY_LINE();
  357. return out;
  358. }
  359. void RubyFlatCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
  360. {
  361. out <<
  362. " begin\n"
  363. " " << vCS() << " = " << gotoDest << "\n"
  364. " _trigger_goto = true\n"
  365. " _goto_level = _again\n"
  366. " break\n"
  367. " end\n";
  368. }
  369. void RubyFlatCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
  370. {
  371. if ( prePushExpr != 0 ) {
  372. out << "begin\n";
  373. INLINE_LIST( out, prePushExpr, 0, false );
  374. }
  375. out <<
  376. " begin\n"
  377. " " << STACK() << "[" << TOP() << "] = " << vCS() << "\n"
  378. " " << TOP() << "+= 1\n"
  379. " " << vCS() << " = " << callDest << "\n"
  380. " _trigger_goto = true\n"
  381. " _goto_level = _again\n"
  382. " break\n"
  383. " end\n";
  384. if ( prePushExpr != 0 )
  385. out << "end\n";
  386. }
  387. void RubyFlatCodeGen::CALL_EXPR(ostream &out, GenInlineItem *ilItem, int targState, bool inFinish )
  388. {
  389. if ( prePushExpr != 0 ) {
  390. out << "begin\n";
  391. INLINE_LIST( out, prePushExpr, 0, false );
  392. }
  393. out <<
  394. " begin\n"
  395. " " << STACK() << "[" << TOP() << "] = " << vCS() << "\n"
  396. " " << TOP() << " += 1\n"
  397. " " << vCS() << " = (";
  398. INLINE_LIST( out, ilItem->children, targState, inFinish );
  399. out << ")\n";
  400. out <<
  401. " _trigger_goto = true\n"
  402. " _goto_level = _again\n"
  403. " break\n"
  404. " end\n";
  405. if ( prePushExpr != 0 )
  406. out << "end\n";
  407. }
  408. void RubyFlatCodeGen::RET( ostream &out, bool inFinish )
  409. {
  410. out <<
  411. " begin\n"
  412. " " << TOP() << " -= 1\n"
  413. " " << vCS() << " = " << STACK() << "[" << TOP() << "]\n";
  414. if ( postPopExpr != 0 ) {
  415. out << "begin\n";
  416. INLINE_LIST( out, postPopExpr, 0, false );
  417. out << "end\n";
  418. }
  419. out <<
  420. " _trigger_goto = true\n"
  421. " _goto_level = _again\n"
  422. " break\n"
  423. " end\n";
  424. }
  425. void RubyFlatCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
  426. {
  427. ret << vCS() << " = " << nextDest << ";";
  428. }
  429. void RubyFlatCodeGen::GOTO_EXPR( ostream &out, GenInlineItem *ilItem, bool inFinish )
  430. {
  431. out <<
  432. " begin\n"
  433. " " << vCS() << " = (";
  434. INLINE_LIST( out, ilItem->children, 0, inFinish );
  435. out << ")\n";
  436. out <<
  437. " _trigger_goto = true\n"
  438. " _goto_level = _again\n"
  439. " break\n"
  440. " end\n";
  441. }
  442. void RubyFlatCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
  443. {
  444. ret << vCS() << " = (";
  445. INLINE_LIST( ret, ilItem->children, 0, inFinish );
  446. ret << ");";
  447. }
  448. void RubyFlatCodeGen::CURS( ostream &ret, bool inFinish )
  449. {
  450. ret << "(_ps)";
  451. }
  452. void RubyFlatCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
  453. {
  454. ret << "(" << vCS() << ")";
  455. }
  456. void RubyFlatCodeGen::BREAK( ostream &out, int targState )
  457. {
  458. out <<
  459. " begin\n"
  460. " " << P() << " += 1\n"
  461. " _trigger_goto = true\n"
  462. " _goto_level = _out\n"
  463. " break\n"
  464. " end\n";
  465. }
  466. int RubyFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
  467. {
  468. int act = 0;
  469. if ( state->toStateAction != 0 )
  470. act = state->toStateAction->location+1;
  471. return act;
  472. }
  473. int RubyFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  474. {
  475. int act = 0;
  476. if ( state->fromStateAction != 0 )
  477. act = state->fromStateAction->location+1;
  478. return act;
  479. }
  480. int RubyFlatCodeGen::EOF_ACTION( RedStateAp *state )
  481. {
  482. int act = 0;
  483. if ( state->eofAction != 0 )
  484. act = state->eofAction->location+1;
  485. return act;
  486. }
  487. int RubyFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
  488. {
  489. /* If there are actions, emit them. Otherwise emit zero. */
  490. int act = 0;
  491. if ( trans->action != 0 )
  492. act = trans->action->location+1;
  493. return act;
  494. }
  495. void RubyFlatCodeGen::writeData()
  496. {
  497. /* If there are any transtion functions then output the array. If there
  498. * are none, don't bother emitting an empty array that won't be used. */
  499. if ( redFsm->anyActions() ) {
  500. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
  501. ACTIONS_ARRAY();
  502. CLOSE_ARRAY() <<
  503. "\n";
  504. }
  505. if ( redFsm->anyConditions() ) {
  506. OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
  507. COND_KEYS();
  508. CLOSE_ARRAY() <<
  509. "\n";
  510. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
  511. COND_KEY_SPANS();
  512. CLOSE_ARRAY() <<
  513. "\n";
  514. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
  515. CONDS();
  516. CLOSE_ARRAY() <<
  517. "\n";
  518. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
  519. COND_INDEX_OFFSET();
  520. CLOSE_ARRAY() <<
  521. "\n";
  522. }
  523. OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
  524. KEYS();
  525. CLOSE_ARRAY() <<
  526. "\n";
  527. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
  528. KEY_SPANS();
  529. CLOSE_ARRAY() <<
  530. "\n";
  531. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
  532. FLAT_INDEX_OFFSET();
  533. CLOSE_ARRAY() <<
  534. "\n";
  535. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
  536. INDICIES();
  537. CLOSE_ARRAY() <<
  538. "\n";
  539. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
  540. TRANS_TARGS();
  541. CLOSE_ARRAY() <<
  542. "\n";
  543. if ( redFsm->anyActions() ) {
  544. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
  545. TRANS_ACTIONS();
  546. CLOSE_ARRAY() <<
  547. "\n";
  548. }
  549. if ( redFsm->anyToStateActions() ) {
  550. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  551. TO_STATE_ACTIONS();
  552. CLOSE_ARRAY() <<
  553. "\n";
  554. }
  555. if ( redFsm->anyFromStateActions() ) {
  556. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  557. FROM_STATE_ACTIONS();
  558. CLOSE_ARRAY() <<
  559. "\n";
  560. }
  561. if ( redFsm->anyEofActions() ) {
  562. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  563. EOF_ACTIONS();
  564. CLOSE_ARRAY() <<
  565. "\n";
  566. }
  567. if ( redFsm->anyEofTrans() ) {
  568. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
  569. EOF_TRANS();
  570. CLOSE_ARRAY() <<
  571. "\n";
  572. }
  573. STATE_IDS();
  574. }
  575. void RubyFlatCodeGen::writeExec()
  576. {
  577. out <<
  578. "begin # ragel flat\n"
  579. " testEof = false\n"
  580. " _slen, _trans, _keys, _inds";
  581. if ( redFsm->anyRegCurStateRef() )
  582. out << ", _ps";
  583. if ( redFsm->anyConditions() )
  584. out << ", _cond, _conds, _widec";
  585. if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
  586. || redFsm->anyFromStateActions() )
  587. out << ", _acts, _nacts";
  588. out << " = nil\n";
  589. out <<
  590. " _goto_level = 0\n"
  591. " _resume = 10\n"
  592. " _eof_trans = 15\n"
  593. " _again = 20\n"
  594. " _test_eof = 30\n"
  595. " _out = 40\n";
  596. out <<
  597. " while true\n"
  598. " _trigger_goto = false\n"
  599. " if _goto_level <= 0\n";
  600. if ( !noEnd ) {
  601. out <<
  602. " if " << P() << " == " << PE() << "\n"
  603. " _goto_level = _test_eof\n"
  604. " next\n"
  605. " end\n";
  606. }
  607. if ( redFsm->errState != 0 ) {
  608. out <<
  609. " if " << vCS() << " == " << redFsm->errState->id << "\n"
  610. " _goto_level = _out\n"
  611. " next\n"
  612. " end\n";
  613. }
  614. /* The resume label. */
  615. out <<
  616. " end\n"
  617. " if _goto_level <= _resume\n";
  618. if ( redFsm->anyFromStateActions() ) {
  619. out <<
  620. " _acts = " << FSA() << "[" << vCS() << "]\n"
  621. " _nacts = " << A() << "[_acts]\n"
  622. " _acts += 1\n"
  623. " while _nacts > 0\n"
  624. " _nacts -= 1\n"
  625. " _acts += 1\n"
  626. " case " << A() << "[_acts - 1]\n";
  627. FROM_STATE_ACTION_SWITCH();
  628. out <<
  629. " end # from state action switch\n"
  630. " end\n"
  631. " if _trigger_goto\n"
  632. " next\n"
  633. " end\n";
  634. }
  635. if ( redFsm->anyConditions() )
  636. COND_TRANSLATE();
  637. LOCATE_TRANS();
  638. if ( redFsm->anyEofTrans() ) {
  639. out <<
  640. " end\n"
  641. " if _goto_level <= _eof_trans\n";
  642. }
  643. if ( redFsm->anyRegCurStateRef() )
  644. out << " _ps = " << vCS() << "\n";
  645. out << " " << vCS() << " = " << TT() << "[_trans]\n";
  646. if ( redFsm->anyRegActions() ) {
  647. out <<
  648. " if " << TA() << "[_trans] != 0\n"
  649. " _acts = " << TA() << "[_trans]\n"
  650. " _nacts = " << A() << "[_acts]\n"
  651. " _acts += 1\n"
  652. " while _nacts > 0\n"
  653. " _nacts -= 1\n"
  654. " _acts += 1\n"
  655. " case " << A() << "[_acts - 1]\n";
  656. ACTION_SWITCH();
  657. out <<
  658. " end # action switch\n"
  659. " end\n"
  660. " end\n"
  661. " if _trigger_goto\n"
  662. " next\n"
  663. " end\n";
  664. }
  665. /* The again label. */
  666. out <<
  667. " end\n"
  668. " if _goto_level <= _again\n";
  669. if ( redFsm->anyToStateActions() ) {
  670. out <<
  671. " _acts = " << TSA() << "[" << vCS() << "]\n"
  672. " _nacts = " << A() << "[_acts]\n"
  673. " _acts += 1\n"
  674. " while _nacts > 0\n"
  675. " _nacts -= 1\n"
  676. " _acts += 1\n"
  677. " case " << A() << "[_acts - 1]\n";
  678. TO_STATE_ACTION_SWITCH() <<
  679. " end # to state action switch\n"
  680. " end\n"
  681. " if _trigger_goto\n"
  682. " next\n"
  683. " end\n";
  684. }
  685. if ( redFsm->errState != 0 ) {
  686. out <<
  687. " if " << vCS() << " == " << redFsm->errState->id << "\n"
  688. " _goto_level = _out\n"
  689. " next\n"
  690. " end\n";
  691. }
  692. out << " " << P() << " += 1\n";
  693. if ( !noEnd ) {
  694. out <<
  695. " if " << P() << " != " << PE() << "\n"
  696. " _goto_level = _resume\n"
  697. " next\n"
  698. " end\n";
  699. }
  700. else {
  701. out <<
  702. " _goto_level = _resume\n"
  703. " next\n";
  704. }
  705. /* The test_eof label. */
  706. out <<
  707. " end\n"
  708. " if _goto_level <= _test_eof\n";
  709. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  710. out <<
  711. " if " << P() << " == " << vEOF() << "\n";
  712. if ( redFsm->anyEofTrans() ) {
  713. out <<
  714. " if " << ET() << "[" << vCS() << "] > 0\n"
  715. " _trans = " << ET() << "[" << vCS() << "] - 1;\n"
  716. " _goto_level = _eof_trans\n"
  717. " next;\n"
  718. " end\n";
  719. }
  720. if ( redFsm->anyEofActions() ) {
  721. out <<
  722. " begin\n"
  723. " __acts = " << EA() << "[" << vCS() << "]\n"
  724. " __nacts = " << A() << "[__acts]\n" <<
  725. " __acts += 1\n"
  726. " while ( __nacts > 0 ) \n"
  727. " __nacts -= 1\n"
  728. " __acts += 1\n"
  729. " case ( "<< A() << "[__acts-1] ) \n";
  730. EOF_ACTION_SWITCH() <<
  731. " end\n"
  732. " end\n"
  733. " if _trigger_goto\n"
  734. " next\n"
  735. " end\n"
  736. " end\n";
  737. }
  738. out <<
  739. " end\n";
  740. }
  741. out <<
  742. " end\n"
  743. " if _goto_level <= _out\n"
  744. " break\n"
  745. " end\n";
  746. /* The loop for faking goto. */
  747. out <<
  748. " end\n";
  749. /* Wrapping the execute block. */
  750. out <<
  751. " end\n";
  752. }
  753. /*
  754. * Local Variables:
  755. * mode: c++
  756. * indent-tabs-mode: 1
  757. * c-file-style: "bsd"
  758. * End:
  759. */