cscodegen.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #ifndef _CSCODEGEN_H
  23. #define _CSCODEGEN_H
  24. #include <iostream>
  25. #include <string>
  26. #include <stdio.h>
  27. #include "common.h"
  28. #include "gendata.h"
  29. using std::string;
  30. using std::ostream;
  31. /* Integer array line length. */
  32. #define IALL 8
  33. /* Forwards. */
  34. struct RedFsmAp;
  35. struct RedStateAp;
  36. struct CodeGenData;
  37. struct GenAction;
  38. struct NameInst;
  39. struct GenInlineItem;
  40. struct GenInlineList;
  41. struct RedAction;
  42. struct LongestMatch;
  43. struct LongestMatchPart;
  44. string itoa( int i );
  45. /*
  46. * class CSharpFsmCodeGen
  47. */
  48. class CSharpFsmCodeGen : public CodeGenData
  49. {
  50. public:
  51. CSharpFsmCodeGen( ostream &out );
  52. virtual ~CSharpFsmCodeGen() {}
  53. virtual void finishRagelDef();
  54. virtual void writeInit();
  55. virtual void writeStart();
  56. virtual void writeFirstFinal();
  57. virtual void writeError();
  58. protected:
  59. string FSM_NAME();
  60. string START_STATE_ID();
  61. ostream &ACTIONS_ARRAY();
  62. string GET_WIDE_KEY();
  63. string GET_WIDE_KEY( RedStateAp *state );
  64. string TABS( int level );
  65. string KEY( Key key );
  66. string ALPHA_KEY( Key key );
  67. string LDIR_PATH( char *path );
  68. void ACTION( ostream &ret, GenAction *action, int targState, bool inFinish );
  69. void CONDITION( ostream &ret, GenAction *condition );
  70. string ALPH_TYPE();
  71. string WIDE_ALPH_TYPE();
  72. string ARRAY_TYPE( unsigned long maxVal );
  73. string ARRAY_TYPE( unsigned long maxVal, bool forceSigned );
  74. virtual string ARR_OFF( string ptr, string offset ) = 0;
  75. virtual string CAST( string type ) = 0;
  76. virtual string UINT() = 0;
  77. virtual string NULL_ITEM() = 0;
  78. virtual string POINTER() = 0;
  79. virtual string GET_KEY();
  80. virtual ostream &SWITCH_DEFAULT() = 0;
  81. string P();
  82. string PE();
  83. string vEOF();
  84. string ACCESS();
  85. string vCS();
  86. string STACK();
  87. string TOP();
  88. string TOKSTART();
  89. string TOKEND();
  90. string ACT();
  91. string DATA_PREFIX();
  92. string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
  93. string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
  94. string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
  95. string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
  96. string I() { return "_" + DATA_PREFIX() + "indicies"; }
  97. string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
  98. string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
  99. string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
  100. string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
  101. string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
  102. string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
  103. string A() { return "_" + DATA_PREFIX() + "actions"; }
  104. string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
  105. string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
  106. string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
  107. string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
  108. string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
  109. string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
  110. string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
  111. string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
  112. string START() { return DATA_PREFIX() + "start"; }
  113. string ERROR() { return DATA_PREFIX() + "error"; }
  114. string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
  115. string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
  116. void INLINE_LIST( ostream &ret, GenInlineList *inlineList, int targState, bool inFinish );
  117. virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
  118. virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
  119. virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
  120. virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
  121. virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
  122. virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem,
  123. int targState, bool inFinish ) = 0;
  124. virtual void RET( ostream &ret, bool inFinish ) = 0;
  125. virtual void BREAK( ostream &ret, int targState ) = 0;
  126. virtual void CURS( ostream &ret, bool inFinish ) = 0;
  127. virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
  128. void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
  129. void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, int inFinish );
  130. void SET_ACT( ostream &ret, GenInlineItem *item );
  131. void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
  132. void INIT_ACT( ostream &ret, GenInlineItem *item );
  133. void SET_TOKSTART( ostream &ret, GenInlineItem *item );
  134. void SET_TOKEND( ostream &ret, GenInlineItem *item );
  135. void GET_TOKEND( ostream &ret, GenInlineItem *item );
  136. void SUB_ACTION( ostream &ret, GenInlineItem *item,
  137. int targState, bool inFinish );
  138. void STATE_IDS();
  139. string ERROR_STATE();
  140. string FIRST_FINAL_STATE();
  141. virtual string PTR_CONST() = 0;
  142. virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
  143. virtual ostream &CLOSE_ARRAY() = 0;
  144. virtual ostream &STATIC_VAR( string type, string name ) = 0;
  145. virtual string CTRL_FLOW() = 0;
  146. ostream &source_warning(const InputLoc &loc);
  147. ostream &source_error(const InputLoc &loc);
  148. unsigned int arrayTypeSize( unsigned long maxVal );
  149. bool outLabelUsed;
  150. bool testEofUsed;
  151. bool againLabelUsed;
  152. bool useIndicies;
  153. public:
  154. /* Determine if we should use indicies. */
  155. virtual void calcIndexSize() {}
  156. void genLineDirective( ostream &out );
  157. };
  158. class CSharpCodeGen : virtual public CSharpFsmCodeGen
  159. {
  160. public:
  161. CSharpCodeGen( ostream &out ) : CSharpFsmCodeGen(out) {}
  162. virtual string GET_KEY();
  163. virtual string NULL_ITEM();
  164. virtual string POINTER();
  165. virtual ostream &SWITCH_DEFAULT();
  166. virtual ostream &OPEN_ARRAY( string type, string name );
  167. virtual ostream &CLOSE_ARRAY();
  168. virtual ostream &STATIC_VAR( string type, string name );
  169. virtual string ARR_OFF( string ptr, string offset );
  170. virtual string CAST( string type );
  171. virtual string UINT();
  172. virtual string PTR_CONST();
  173. virtual string CTRL_FLOW();
  174. virtual void writeExports();
  175. };
  176. #define MAX(a, b) (a > b ? a : b)
  177. #endif