mlfgoto.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "mlfgoto.h"
  24. #include "redfsm.h"
  25. #include "gendata.h"
  26. #include "bstmap.h"
  27. std::ostream &OCamlFGotoCodeGen::EXEC_ACTIONS()
  28. {
  29. /* Loop the actions. */
  30. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  31. if ( redAct->numTransRefs > 0 ) {
  32. /* We are at the start of a glob, write the case. */
  33. out << "and f" << redAct->actListId << " () =\n";
  34. out << "\tbegin try\n";
  35. /* Write each action in the list of action items. */
  36. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  37. ACTION( out, item->value, 0, false );
  38. out << "\twith Goto_again -> () end;\n";
  39. out << "\tdo_again ()\n";
  40. }
  41. }
  42. return out;
  43. }
  44. /* Write out the function switch. This switch is keyed on the values
  45. * of the func index. */
  46. std::ostream &OCamlFGotoCodeGen::TO_STATE_ACTION_SWITCH()
  47. {
  48. /* Loop the actions. */
  49. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  50. if ( redAct->numToStateRefs > 0 ) {
  51. /* Write the entry label. */
  52. out << "\t|" << redAct->actListId+1 << " ->\n";
  53. /* Write each action in the list of action items. */
  54. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  55. ACTION( out, item->value, 0, false );
  56. // out << "\tbreak;\n";
  57. }
  58. }
  59. genLineDirective( out );
  60. return out;
  61. }
  62. /* Write out the function switch. This switch is keyed on the values
  63. * of the func index. */
  64. std::ostream &OCamlFGotoCodeGen::FROM_STATE_ACTION_SWITCH()
  65. {
  66. /* Loop the actions. */
  67. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  68. if ( redAct->numFromStateRefs > 0 ) {
  69. /* Write the entry label. */
  70. out << "\t| " << redAct->actListId+1 << " ->\n";
  71. /* Write each action in the list of action items. */
  72. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  73. ACTION( out, item->value, 0, false );
  74. // out << "\tbreak;\n";
  75. }
  76. }
  77. genLineDirective( out );
  78. return out;
  79. }
  80. std::ostream &OCamlFGotoCodeGen::EOF_ACTION_SWITCH()
  81. {
  82. /* Loop the actions. */
  83. for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
  84. if ( redAct->numEofRefs > 0 ) {
  85. /* Write the entry label. */
  86. out << "\t| " << redAct->actListId+1 << " ->\n";
  87. /* Write each action in the list of action items. */
  88. for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
  89. ACTION( out, item->value, 0, true );
  90. // out << "\tbreak;\n";
  91. }
  92. }
  93. genLineDirective( out );
  94. return out;
  95. }
  96. std::ostream &OCamlFGotoCodeGen::FINISH_CASES()
  97. {
  98. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  99. /* States that are final and have an out action need a case. */
  100. if ( st->eofAction != 0 ) {
  101. /* Write the case label. */
  102. out << "\t\t| " << st->id << " -> ";
  103. /* Jump to the func. */
  104. out << "f" << st->eofAction->actListId << " ()\n";
  105. }
  106. }
  107. return out;
  108. }
  109. unsigned int OCamlFGotoCodeGen::TO_STATE_ACTION( RedStateAp *state )
  110. {
  111. int act = 0;
  112. if ( state->toStateAction != 0 )
  113. act = state->toStateAction->actListId+1;
  114. return act;
  115. }
  116. unsigned int OCamlFGotoCodeGen::FROM_STATE_ACTION( RedStateAp *state )
  117. {
  118. int act = 0;
  119. if ( state->fromStateAction != 0 )
  120. act = state->fromStateAction->actListId+1;
  121. return act;
  122. }
  123. unsigned int OCamlFGotoCodeGen::EOF_ACTION( RedStateAp *state )
  124. {
  125. int act = 0;
  126. if ( state->eofAction != 0 )
  127. act = state->eofAction->actListId+1;
  128. return act;
  129. }
  130. void OCamlFGotoCodeGen::writeData()
  131. {
  132. if ( redFsm->anyToStateActions() ) {
  133. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
  134. TO_STATE_ACTIONS();
  135. CLOSE_ARRAY() <<
  136. "\n";
  137. }
  138. if ( redFsm->anyFromStateActions() ) {
  139. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
  140. FROM_STATE_ACTIONS();
  141. CLOSE_ARRAY() <<
  142. "\n";
  143. }
  144. if ( redFsm->anyEofActions() ) {
  145. OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
  146. EOF_ACTIONS();
  147. CLOSE_ARRAY() <<
  148. "\n";
  149. }
  150. STATE_IDS();
  151. out << "exception Goto_again" << TOP_SEP();
  152. }
  153. void OCamlFGotoCodeGen::writeExec()
  154. {
  155. testEofUsed = false;
  156. outLabelUsed = false;
  157. out << " begin\n";
  158. if ( redFsm->anyRegCurStateRef() )
  159. out << " let _ps = ref 0 in\n";
  160. if ( redFsm->anyConditions() )
  161. out << " let _widec : " << WIDE_ALPH_TYPE() << " = ref 0 in\n";
  162. out << "\n";
  163. out << "\tlet rec do_start () =\n";
  164. if ( !noEnd ) {
  165. testEofUsed = true;
  166. out <<
  167. " if " << P() << " = " << PE() << " then\n"
  168. " do_test_eof ()\n"
  169. "\telse\n";
  170. }
  171. if ( redFsm->errState != 0 ) {
  172. outLabelUsed = true;
  173. out <<
  174. " if " << vCS() << " = " << redFsm->errState->id << " then\n"
  175. " do_out ()\n"
  176. "\telse\n";
  177. }
  178. out << "\tdo_resume ()\n";
  179. out << "and do_resume () =\n";
  180. if ( redFsm->anyFromStateActions() ) {
  181. out <<
  182. " begin match " << AT(FSA(),vCS()) << " with\n";
  183. FROM_STATE_ACTION_SWITCH();
  184. SWITCH_DEFAULT() <<
  185. " end;\n"
  186. "\n";
  187. }
  188. out <<
  189. " begin match " << vCS() << " with\n";
  190. STATE_GOTOS();
  191. SWITCH_DEFAULT() <<
  192. " end\n"
  193. "\n";
  194. TRANSITIONS() <<
  195. "\n";
  196. if ( redFsm->anyRegActions() )
  197. EXEC_ACTIONS() << "\n";
  198. out << "\tand do_again () =\n";
  199. if ( redFsm->anyToStateActions() ) {
  200. out <<
  201. " begin match " << AT(TSA(), vCS()) << " with\n";
  202. TO_STATE_ACTION_SWITCH();
  203. SWITCH_DEFAULT() <<
  204. " end;\n"
  205. "\n";
  206. }
  207. if ( redFsm->errState != 0 ) {
  208. outLabelUsed = true;
  209. out <<
  210. " match " << vCS() << " with\n" <<
  211. " | " << redFsm->errState->id << " -> do_out ()\n"
  212. " | _ ->\n";
  213. }
  214. out << "\t" << P() << " <- " << P() << " + 1;\n";
  215. if ( !noEnd ) {
  216. out <<
  217. " if " << P() << " <> " << PE() << " then\n"
  218. " do_resume ()\n"
  219. "\telse do_test_eof ()\n";
  220. }
  221. else {
  222. out <<
  223. " do_resume ()\n";
  224. }
  225. // if ( testEofUsed )
  226. out << "and do_test_eof () =\n";
  227. if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
  228. out <<
  229. " if " << P() << " = " << vEOF() << " then\n"
  230. " begin\n";
  231. if ( redFsm->anyEofTrans() ) {
  232. out <<
  233. " match " << vCS() << " with\n";
  234. for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
  235. if ( st->eofTrans != 0 )
  236. out << " | " << st->id << " -> tr" << st->eofTrans->id << " ()\n";
  237. }
  238. SWITCH_DEFAULT() << ";\n"; // fall through
  239. }
  240. if ( redFsm->anyEofActions() ) {
  241. out <<
  242. " try match " << AT(EA(), vCS()) << " with\n";
  243. EOF_ACTION_SWITCH();
  244. SWITCH_DEFAULT() <<
  245. " \n"
  246. " with Goto_again -> do_again () \n";
  247. }
  248. out <<
  249. " end\n"
  250. "\n";
  251. }
  252. else
  253. out << "\t()\n";
  254. if ( outLabelUsed )
  255. out << "\tand do_out () = ()\n";
  256. out << "\tin do_start ()\n";
  257. out << " end;\n";
  258. }