xmlcodegen.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2005-2007 Adrian Thurston <thurston@complang.org>
  3. */
  4. /* This file is part of Ragel.
  5. *
  6. * Ragel is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Ragel is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ragel; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _XMLCODEGEN_H
  21. #define _XMLCODEGEN_H
  22. #include <iostream>
  23. #include "avltree.h"
  24. #include "fsmgraph.h"
  25. #include "parsedata.h"
  26. #include "redfsm.h"
  27. /* Forwards. */
  28. struct TransAp;
  29. struct FsmAp;
  30. struct ParseData;
  31. struct GenInlineList;
  32. struct CodeGenData;
  33. struct RedActionTable
  34. :
  35. public AvlTreeEl<RedActionTable>
  36. {
  37. RedActionTable( const ActionTable &key )
  38. :
  39. key(key),
  40. id(0)
  41. { }
  42. const ActionTable &getKey()
  43. { return key; }
  44. ActionTable key;
  45. int id;
  46. };
  47. typedef AvlTree<RedActionTable, ActionTable, CmpActionTable> ActionTableMap;
  48. struct NextRedTrans
  49. {
  50. Key lowKey, highKey;
  51. TransAp *trans;
  52. TransAp *next;
  53. void load() {
  54. if ( trans != 0 ) {
  55. next = trans->next;
  56. lowKey = trans->lowKey;
  57. highKey = trans->highKey;
  58. }
  59. }
  60. NextRedTrans( TransAp *t ) {
  61. trans = t;
  62. load();
  63. }
  64. void increment() {
  65. trans = next;
  66. load();
  67. }
  68. };
  69. struct GenBase
  70. {
  71. GenBase( char *fsmName, ParseData *pd, FsmAp *fsm );
  72. void appendTrans( TransListVect &outList, Key lowKey, Key highKey, TransAp *trans );
  73. void reduceActionTables();
  74. char *fsmName;
  75. ParseData *pd;
  76. FsmAp *fsm;
  77. ActionTableMap actionTableMap;
  78. int nextActionTableId;
  79. };
  80. class XMLCodeGen : protected GenBase
  81. {
  82. public:
  83. XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm, std::ostream &out );
  84. void writeXML( );
  85. private:
  86. void writeStateActions( StateAp *state );
  87. void writeStateList();
  88. void writeStateConditions( StateAp *state );
  89. void writeKey( Key key );
  90. void writeText( InlineItem *item );
  91. void writeGoto( InlineItem *item );
  92. void writeGotoExpr( InlineItem *item );
  93. void writeCall( InlineItem *item );
  94. void writeCallExpr( InlineItem *item );
  95. void writeNext( InlineItem *item );
  96. void writeNextExpr( InlineItem *item );
  97. void writeEntry( InlineItem *item );
  98. void writeLmOnLast( InlineItem *item );
  99. void writeLmOnNext( InlineItem *item );
  100. void writeLmOnLagBehind( InlineItem *item );
  101. void writeExports();
  102. bool writeNameInst( NameInst *nameInst );
  103. void writeEntryPoints();
  104. void writeConditions();
  105. void writeInlineList( InlineList *inlineList );
  106. void writeActionList();
  107. void writeActionTableList();
  108. void reduceTrans( TransAp *trans );
  109. void writeTransList( StateAp *state );
  110. void writeEofTrans( StateAp *state );
  111. void writeTrans( Key lowKey, Key highKey, TransAp *defTrans );
  112. void writeAction( Action *action );
  113. void writeLmSwitch( InlineItem *item );
  114. void writeMachine();
  115. void writeActionExec( InlineItem *item );
  116. std::ostream &out;
  117. };
  118. class BackendGen : protected GenBase
  119. {
  120. public:
  121. BackendGen( char *fsmName, ParseData *pd, FsmAp *fsm, CodeGenData *cgd );
  122. void makeBackend( );
  123. private:
  124. void makeGenInlineList( GenInlineList *outList, InlineList *inList );
  125. void makeKey( GenInlineList *outList, Key key );
  126. void makeText( GenInlineList *outList, InlineItem *item );
  127. void makeLmOnLast( GenInlineList *outList, InlineItem *item );
  128. void makeLmOnNext( GenInlineList *outList, InlineItem *item );
  129. void makeLmOnLagBehind( GenInlineList *outList, InlineItem *item );
  130. void makeActionExec( GenInlineList *outList, InlineItem *item );
  131. void makeLmSwitch( GenInlineList *outList, InlineItem *item );
  132. void makeSetTokend( GenInlineList *outList, long offset );
  133. void makeSetAct( GenInlineList *outList, long lmId );
  134. void makeSubList( GenInlineList *outList, InlineList *inlineList,
  135. GenInlineItem::Type type );
  136. void makeTargetItem( GenInlineList *outList, NameInst *nameTarg, GenInlineItem::Type type );
  137. void makeExecGetTokend( GenInlineList *outList );
  138. void makeExports();
  139. void makeMachine();
  140. void makeActionList();
  141. void makeAction( Action *action );
  142. void makeActionTableList();
  143. void makeConditions();
  144. void makeEntryPoints();
  145. bool makeNameInst( std::string &out, NameInst *nameInst );
  146. void makeStateList();
  147. void makeStateActions( StateAp *state );
  148. void makeEofTrans( StateAp *state );
  149. void makeStateConditions( StateAp *state );
  150. void makeTransList( StateAp *state );
  151. void makeTrans( Key lowKey, Key highKey, TransAp *trans );
  152. void close_ragel_def();
  153. CodeGenData *cgd;
  154. /* Collected during parsing. */
  155. int curAction;
  156. int curActionTable;
  157. int curTrans;
  158. int curState;
  159. int curCondSpace;
  160. int curStateCond;
  161. };
  162. #endif