2 * Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
3 * 2004 Erich Ocean <eric.ocean@ampede.com>
4 * 2005 Alan West <alan@alanz.com>
7 /* This file is part of Ragel.
9 * Ragel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Ragel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Ragel; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 /* Integer array line length. */
49 struct LongestMatchPart;
51 inline string itoa( int i )
54 sprintf( buf, "%i", i );
61 class FsmCodeGen : public CodeGenData
64 FsmCodeGen( ostream &out );
65 virtual ~FsmCodeGen() {}
67 virtual void writeOutData() = 0;
68 virtual void writeOutInit();
69 virtual void writeOutExec() = 0;
70 virtual void writeOutEOF() = 0;
74 friend struct CodeGenData;
77 string START_STATE_ID();
78 ostream &ACTIONS_ARRAY();
79 string GET_WIDE_KEY();
80 string GET_WIDE_KEY( RedStateAp *state );
81 string TABS( int level );
83 string LDIR_PATH( char *path );
84 void ACTION( ostream &ret, Action *action, int targState, bool inFinish );
85 void CONDITION( ostream &ret, Action *condition );
87 string WIDE_ALPH_TYPE();
88 string ARRAY_TYPE( unsigned long maxVal );
90 virtual string ARR_OFF( string ptr, string offset ) = 0;
91 virtual string CAST( string type ) = 0;
92 virtual string UINT() = 0;
93 virtual string NULL_ITEM() = 0;
94 virtual string POINTER() = 0;
95 virtual string GET_KEY();
96 virtual ostream &SWITCH_DEFAULT() = 0;
98 string P() { return "p"; }
99 string PE() { return "pe"; }
103 string STACK() { return ACCESS() + "stack"; }
104 string TOP() { return ACCESS() + "top"; }
105 string TOKSTART() { return ACCESS() + "tokstart"; }
106 string TOKEND() { return ACCESS() + "tokend"; }
107 string ACT() { return ACCESS() + "act"; }
109 string DATA_PREFIX();
110 string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
111 string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
112 string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
113 string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
114 string I() { return "_" + DATA_PREFIX() + "indicies"; }
115 string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
116 string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
117 string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
118 string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
119 string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
120 string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
121 string A() { return "_" + DATA_PREFIX() + "actions"; }
122 string TA() { return "_" + DATA_PREFIX() + "trans_actions_wi"; }
123 string TT() { return "_" + DATA_PREFIX() + "trans_targs_wi"; }
124 string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
125 string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
126 string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
127 string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
128 string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
129 string START() { return DATA_PREFIX() + "start"; }
130 string ERROR() { return DATA_PREFIX() + "error"; }
131 string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
132 string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
134 void INLINE_LIST( ostream &ret, InlineList *inlineList, int targState, bool inFinish );
135 virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
136 virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
137 virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
138 virtual void GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
139 virtual void NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
140 virtual void CALL_EXPR( ostream &ret, InlineItem *ilItem,
141 int targState, bool inFinish ) = 0;
142 virtual void RET( ostream &ret, bool inFinish ) = 0;
143 virtual void BREAK( ostream &ret, int targState ) = 0;
144 virtual void CURS( ostream &ret, bool inFinish ) = 0;
145 virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
146 void EXEC( ostream &ret, InlineItem *item, int targState, int inFinish );
147 void EXECTE( ostream &ret, InlineItem *item, int targState, int inFinish );
148 void LM_SWITCH( ostream &ret, InlineItem *item, int targState, int inFinish );
149 void SET_ACT( ostream &ret, InlineItem *item );
150 void INIT_TOKSTART( ostream &ret, InlineItem *item );
151 void INIT_ACT( ostream &ret, InlineItem *item );
152 void SET_TOKSTART( ostream &ret, InlineItem *item );
153 void SET_TOKEND( ostream &ret, InlineItem *item );
154 void GET_TOKEND( ostream &ret, InlineItem *item );
155 void SUB_ACTION( ostream &ret, InlineItem *item,
156 int targState, bool inFinish );
158 string ERROR_STATE();
159 string FIRST_FINAL_STATE();
161 virtual string PTR_CONST() = 0;
162 virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
163 virtual ostream &START_ARRAY_LINE() = 0;
164 virtual ostream &ARRAY_ITEM( int item, int count, bool last ) = 0;
165 virtual ostream &END_ARRAY_LINE() = 0;
166 virtual ostream &CLOSE_ARRAY() = 0;
167 virtual ostream &STATIC_VAR( string type, string name ) = 0;
169 virtual string CTRL_FLOW() = 0;
171 ostream &source_warning(const InputLoc &loc);
172 ostream &source_error(const InputLoc &loc);
174 unsigned int arrayTypeSize( unsigned long maxVal );
176 /* Set up labelNeeded flag for each state. Differs for each goto style so
178 virtual void setLabelsNeeded() {}
186 void prepareMachine();
188 virtual void finishRagelDef();
189 virtual void writeStatement( InputLoc &loc, int nargs, char **args );
191 /* Determine if we should use indicies. */
192 virtual void calcIndexSize() {}
195 class CCodeGen : virtual public FsmCodeGen
198 CCodeGen( ostream &out ) : FsmCodeGen(out) {}
200 virtual string NULL_ITEM();
201 virtual string POINTER();
202 virtual ostream &SWITCH_DEFAULT();
203 virtual ostream &OPEN_ARRAY( string type, string name );
204 virtual ostream &START_ARRAY_LINE();
205 virtual ostream &ARRAY_ITEM( int item, int count, bool last );
206 virtual ostream &END_ARRAY_LINE();
207 virtual ostream &CLOSE_ARRAY();
208 virtual ostream &STATIC_VAR( string type, string name );
209 virtual string ARR_OFF( string ptr, string offset );
210 virtual string CAST( string type );
211 virtual string UINT();
212 virtual string PTR_CONST();
213 virtual string CTRL_FLOW();
216 class DCodeGen : virtual public FsmCodeGen
219 DCodeGen( ostream &out ) : FsmCodeGen(out) {}
221 virtual string NULL_ITEM();
222 virtual string POINTER();
223 virtual ostream &SWITCH_DEFAULT();
224 virtual ostream &OPEN_ARRAY( string type, string name );
225 virtual ostream &CLOSE_ARRAY();
226 virtual ostream &START_ARRAY_LINE();
227 virtual ostream &ARRAY_ITEM( int item, int count, bool last );
228 virtual ostream &END_ARRAY_LINE();
229 virtual ostream &STATIC_VAR( string type, string name );
230 virtual string ARR_OFF( string ptr, string offset );
231 virtual string CAST( string type );
232 virtual string UINT();
233 virtual string PTR_CONST();
234 virtual string CTRL_FLOW();
237 class JavaCodeGen : virtual public FsmCodeGen
244 JavaCodeGen( ostream &out ) : FsmCodeGen(out) {}
246 virtual string NULL_ITEM();
247 virtual string POINTER();
248 virtual ostream &SWITCH_DEFAULT();
249 virtual ostream &OPEN_ARRAY( string type, string name );
250 virtual ostream &START_ARRAY_LINE();
251 virtual ostream &ARRAY_ITEM( int item, int count, bool last );
252 virtual ostream &END_ARRAY_LINE();
253 virtual ostream &CLOSE_ARRAY();
254 virtual ostream &STATIC_VAR( string type, string name );
255 virtual string ARR_OFF( string ptr, string offset );
256 virtual string CAST( string type );
257 virtual string UINT();
258 virtual string PTR_CONST();
259 virtual string GET_KEY();
260 virtual string CTRL_FLOW();
263 #endif /* _FSMCODEGEN_H */