2 * Copyright 2004-2006 Adrian Thurston <thurston@complang.org>
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
29 std::ostream &CSharpFFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
32 if ( state->toStateAction != 0 )
33 act = state->toStateAction->actListId+1;
38 std::ostream &CSharpFFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
41 if ( state->fromStateAction != 0 )
42 act = state->fromStateAction->actListId+1;
47 std::ostream &CSharpFFlatCodeGen::EOF_ACTION( RedStateAp *state )
50 if ( state->eofAction != 0 )
51 act = state->eofAction->actListId+1;
56 /* Write out the function for a transition. */
57 std::ostream &CSharpFFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
60 if ( trans->action != 0 )
61 action = trans->action->actListId+1;
66 /* Write out the function switch. This switch is keyed on the values
67 * of the func index. */
68 std::ostream &CSharpFFlatCodeGen::TO_STATE_ACTION_SWITCH()
70 /* Loop the actions. */
71 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
72 if ( redAct->numToStateRefs > 0 ) {
73 /* Write the entry label. */
74 out << "\tcase " << redAct->actListId+1 << ":\n";
76 /* Write each action in the list of action items. */
77 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
78 ACTION( out, item->value, 0, false );
84 genLineDirective( out );
88 /* Write out the function switch. This switch is keyed on the values
89 * of the func index. */
90 std::ostream &CSharpFFlatCodeGen::FROM_STATE_ACTION_SWITCH()
92 /* Loop the actions. */
93 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
94 if ( redAct->numFromStateRefs > 0 ) {
95 /* Write the entry label. */
96 out << "\tcase " << redAct->actListId+1 << ":\n";
98 /* Write each action in the list of action items. */
99 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
100 ACTION( out, item->value, 0, false );
106 genLineDirective( out );
110 std::ostream &CSharpFFlatCodeGen::EOF_ACTION_SWITCH()
112 /* Loop the actions. */
113 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
114 if ( redAct->numEofRefs > 0 ) {
115 /* Write the entry label. */
116 out << "\tcase " << redAct->actListId+1 << ":\n";
118 /* Write each action in the list of action items. */
119 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
120 ACTION( out, item->value, 0, true );
126 genLineDirective( out );
130 /* Write out the function switch. This switch is keyed on the values
131 * of the func index. */
132 std::ostream &CSharpFFlatCodeGen::ACTION_SWITCH()
134 /* Loop the actions. */
135 for ( GenActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
136 if ( redAct->numTransRefs > 0 ) {
137 /* Write the entry label. */
138 out << "\tcase " << redAct->actListId+1 << ":\n";
140 /* Write each action in the list of action items. */
141 for ( GenActionTable::Iter item = redAct->key; item.lte(); item++ )
142 ACTION( out, item->value, 0, false );
148 genLineDirective( out );
152 void CSharpFFlatCodeGen::writeData()
154 if ( redFsm->anyConditions() ) {
155 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
160 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
165 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
170 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
176 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
181 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
186 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
191 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
196 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
201 if ( redFsm->anyActions() ) {
202 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
208 if ( redFsm->anyToStateActions() ) {
209 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
215 if ( redFsm->anyFromStateActions() ) {
216 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
217 FROM_STATE_ACTIONS();
222 if ( redFsm->anyEofActions() ) {
223 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
229 if ( redFsm->anyEofTrans() ) {
230 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
239 void CSharpFFlatCodeGen::writeExec()
242 outLabelUsed = false;
247 " " << slenType << " _slen";
249 if ( redFsm->anyRegCurStateRef() )
253 out << " " << transType << " _trans";
255 if ( redFsm->anyConditions() )
261 " " << "int _keys;\n"
262 " " << indsType << " _inds;\n";
264 " " << PTR_CONST() << WIDE_ALPH_TYPE() << POINTER() << "_keys;\n"
265 " " << PTR_CONST() << ARRAY_TYPE(redFsm->maxIndex) << POINTER() << "_inds;\n";*/
267 if ( redFsm->anyConditions() ) {
269 " " << condsType << " _conds;\n"
270 " " << WIDE_ALPH_TYPE() << " _widec;\n";
276 " if ( " << P() << " == " << PE() << " )\n"
277 " goto _test_eof;\n";
280 if ( redFsm->errState != 0 ) {
283 " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
289 if ( redFsm->anyFromStateActions() ) {
291 " switch ( " << FSA() << "[" << vCS() << "] ) {\n";
292 FROM_STATE_ACTION_SWITCH();
298 if ( redFsm->anyConditions() )
303 if ( redFsm->anyEofTrans() )
304 out << "_eof_trans:\n";
306 if ( redFsm->anyRegCurStateRef() )
307 out << " _ps = " << vCS() << ";\n";
310 " " << vCS() << " = " << TT() << "[_trans];\n\n";
312 if ( redFsm->anyRegActions() ) {
314 " if ( " << TA() << "[_trans] == 0 )\n"
317 " switch ( " << TA() << "[_trans] ) {\n";
324 if ( redFsm->anyRegActions() || redFsm->anyActionGotos() ||
325 redFsm->anyActionCalls() || redFsm->anyActionRets() )
328 if ( redFsm->anyToStateActions() ) {
330 " switch ( " << TSA() << "[" << vCS() << "] ) {\n";
331 TO_STATE_ACTION_SWITCH();
337 if ( redFsm->errState != 0 ) {
340 " if ( " << vCS() << " == " << redFsm->errState->id << " )\n"
346 " if ( ++" << P() << " != " << PE() << " )\n"
351 " " << P() << " += 1;\n"
356 out << " _test_eof: {}\n";
358 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
360 " if ( " << P() << " == " << vEOF() << " )\n"
363 if ( redFsm->anyEofTrans() ) {
365 " if ( " << ET() << "[" << vCS() << "] > 0 ) {\n"
366 " _trans = " << CAST(transType) << " (" << ET() <<
367 "[" << vCS() << "] - 1);\n"
368 " goto _eof_trans;\n"
372 if ( redFsm->anyEofActions() ) {
374 " switch ( " << EA() << "[" << vCS() << "] ) {\n";
386 out << " _out: {}\n";