Fixes for ruby 1.9.
[ragel.git] / rlgen-ruby / ruby-tabcodegen.cpp
blob42fd4793fd977da7494968cd85869c3528668618
1 /*
2 * Copyright 2007 Victor Hugo Borja <vic@rubyforge.org>
3 * 2007 Adrian Thurston <thurston@cs.queensu.ca>
4 */
6 /* This file is part of Ragel.
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.
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.
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
23 #include <iomanip>
24 #include <sstream>
25 #include "redfsm.h"
26 #include "gendata.h"
27 #include "rlgen-ruby.h"
28 #include "ruby-tabcodegen.h"
30 using std::ostream;
31 using std::ostringstream;
32 using std::string;
33 using std::cerr;
34 using std::endl;
38 void RubyTabCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
40 out <<
41 " begin\n"
42 " " << CS() << " = " << gotoDest << "\n"
43 " _trigger_goto = true\n"
44 " _goto_level = _again\n"
45 " break\n"
46 " end\n";
49 void RubyTabCodeGen::GOTO_EXPR( ostream &out, InlineItem *ilItem, bool inFinish )
51 out <<
52 " begin\n"
53 " " << CS() << " = (";
54 INLINE_LIST( out, ilItem->children, 0, inFinish );
55 out << ")\n";
56 out <<
57 " _trigger_goto = true\n"
58 " _goto_level = _again\n"
59 " break\n"
60 " end\n";
63 void RubyTabCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
65 if ( prePushExpr != 0 ) {
66 out << "begin\n";
67 INLINE_LIST( out, prePushExpr, 0, false );
70 out <<
71 " begin\n"
72 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
73 " " << TOP() << "+= 1\n"
74 " " << CS() << " = " << callDest << "\n"
75 " _trigger_goto = true\n"
76 " _goto_level = _again\n"
77 " break\n"
78 " end\n";
80 if ( prePushExpr != 0 )
81 out << "end\n";
84 void RubyTabCodeGen::CALL_EXPR(ostream &out, InlineItem *ilItem, int targState, bool inFinish )
86 if ( prePushExpr != 0 ) {
87 out << "begin\n";
88 INLINE_LIST( out, prePushExpr, 0, false );
91 out <<
92 " begin\n"
93 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
94 " " << TOP() << " += 1\n"
95 " " << CS() << " = (";
96 INLINE_LIST( out, ilItem->children, targState, inFinish );
97 out << ")\n";
99 out <<
100 " _trigger_goto = true\n"
101 " _goto_level = _again\n"
102 " break\n"
103 " end\n";
105 if ( prePushExpr != 0 )
106 out << "end\n";
109 void RubyTabCodeGen::RET( ostream &out, bool inFinish )
111 out <<
112 " begin\n"
113 " " << TOP() << " -= 1\n"
114 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
116 if ( postPopExpr != 0 ) {
117 out << "begin\n";
118 INLINE_LIST( out, postPopExpr, 0, false );
119 out << "end\n";
122 out <<
123 " _trigger_goto = true\n"
124 " _goto_level = _again\n"
125 " break\n"
126 " end\n";
129 void RubyTabCodeGen::BREAK( ostream &out, int targState )
131 out <<
132 " begin\n"
133 " _trigger_goto = true\n"
134 " _goto_level = _out\n"
135 " break\n"
136 " end\n";
139 void RubyTabCodeGen::COND_TRANSLATE()
141 out <<
142 " _widec = " << GET_KEY() << "\n"
143 " _keys = " << CO() << "[" << CS() << "]*2\n"
144 " _klen = " << CL() << "[" << CS() << "]\n"
145 " if _klen > 0\n"
146 " _lower = _keys\n"
147 " _upper = _keys + (_klen<<1) - 2\n"
148 " loop do\n"
149 " break if _upper < _lower\n"
150 " _mid = _lower + (((_upper-_lower) >> 1) & ~1)\n"
151 " if " << GET_WIDE_KEY() << " < " << CK() << "[_mid]\n"
152 " _upper = _mid - 2\n"
153 " elsif " << GET_WIDE_KEY() << " > " << CK() << "[_mid+1]\n"
154 " _lower = _mid + 2\n"
155 " else\n"
156 " case " << C() << "[" << CO() << "[" << CS() << "]"
157 " + ((_mid - _keys)>>1)]\n";
159 for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
160 CondSpace *condSpace = csi;
161 out << " when " << condSpace->condSpaceId << " then" ;
162 out << " _widec = " << KEY(condSpace->baseKey) <<
163 "+ (" << GET_KEY() << " - " << KEY(keyOps->minKey) << ")\n";
165 for ( CondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
166 Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
167 out << " _widec += " << condValOffset << " if ( ";
168 CONDITION( out, *csi );
169 out << " )\n";
173 out <<
174 " end # case\n"
175 " end\n"
176 " end # loop\n"
177 " end\n";
181 void RubyTabCodeGen::LOCATE_TRANS()
183 out <<
184 " _keys = " << KO() << "[" << CS() << "]\n"
185 " _trans = " << IO() << "[" << CS() << "]\n"
186 " _klen = " << SL() << "[" << CS() << "]\n"
187 " _break_match = false\n"
188 " \n"
189 " begin\n"
190 " if _klen > 0\n"
191 " _lower = _keys\n"
192 " _upper = _keys + _klen - 1\n"
193 "\n"
194 " loop do\n"
195 " break if _upper < _lower\n"
196 " _mid = _lower + ( (_upper - _lower) >> 1 )\n"
197 "\n"
198 " if " << GET_WIDE_KEY() << " < " << K() << "[_mid]\n"
199 " _upper = _mid - 1\n"
200 " elsif " << GET_WIDE_KEY() << " > " << K() << "[_mid]\n"
201 " _lower = _mid + 1\n"
202 " else\n"
203 " _trans += (_mid - _keys)\n"
204 " _break_match = true\n"
205 " break\n"
206 " end\n"
207 " end # loop\n"
208 " break if _break_match\n"
209 " _keys += _klen\n"
210 " _trans += _klen\n"
211 " end"
212 "\n"
213 " _klen = " << RL() << "[" << CS() << "]\n"
214 " if _klen > 0\n"
215 " _lower = _keys\n"
216 " _upper = _keys + (_klen << 1) - 2\n"
217 " loop do\n"
218 " break if _upper < _lower\n"
219 " _mid = _lower + (((_upper-_lower) >> 1) & ~1)\n"
220 " if " << GET_WIDE_KEY() << " < " << K() << "[_mid]\n"
221 " _upper = _mid - 2\n"
222 " elsif " << GET_WIDE_KEY() << " > " << K() << "[_mid+1]\n"
223 " _lower = _mid + 2\n"
224 " else\n"
225 " _trans += ((_mid - _keys) >> 1)\n"
226 " _break_match = true\n"
227 " break\n"
228 " end\n"
229 " end # loop\n"
230 " break if _break_match\n"
231 " _trans += _klen\n"
232 " end\n"
233 " end while false\n";
236 void RubyTabCodeGen::writeExec()
238 out <<
239 "begin\n"
240 " _klen, _trans, _keys";
242 if ( redFsm->anyRegCurStateRef() )
243 out << ", _ps";
244 if ( redFsm->anyConditions() )
245 out << ", _widec";
246 if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
247 || redFsm->anyFromStateActions() )
248 out << ", _acts, _nacts";
250 out << " = nil\n";
252 out <<
253 " _goto_level = 0\n"
254 " _resume = 10\n"
255 " _eof_trans = 15\n"
256 " _again = 20\n"
257 " _test_eof = 30\n"
258 " _out = 40\n";
260 out <<
261 " while true\n"
262 " _trigger_goto = false\n"
263 " if _goto_level <= 0\n";
265 if ( hasEnd ) {
266 out <<
267 " if " << P() << " == " << PE() << "\n"
268 " _goto_level = _test_eof\n"
269 " next\n"
270 " end\n";
273 if ( redFsm->errState != 0 ) {
274 out <<
275 " if " << CS() << " == " << redFsm->errState->id << "\n"
276 " _goto_level = _out\n"
277 " next\n"
278 " end\n";
281 /* The resume label. */
282 out <<
283 " end\n"
284 " if _goto_level <= _resume\n";
286 if ( redFsm->anyFromStateActions() ) {
287 out <<
288 " _acts = " << FSA() << "[" << CS() << "]\n"
289 " _nacts = " << A() << "[_acts]\n"
290 " _acts += 1\n"
291 " while _nacts > 0\n"
292 " _nacts -= 1\n"
293 " _acts += 1\n"
294 " case " << A() << "[_acts - 1]\n";
295 FROM_STATE_ACTION_SWITCH();
296 out <<
297 " end # from state action switch\n"
298 " end\n"
299 " if _trigger_goto\n"
300 " next\n"
301 " end\n";
304 if ( redFsm->anyConditions() )
305 COND_TRANSLATE();
307 LOCATE_TRANS();
309 if ( useIndicies )
310 out << " _trans = " << I() << "[_trans]\n";
312 if ( redFsm->anyEofTrans() ) {
313 out <<
314 " end\n"
315 " if _goto_level <= _eof_trans\n";
318 if ( redFsm->anyRegCurStateRef() )
319 out << " _ps = " << CS() << "\n";
321 out << " " << CS() << " = " << TT() << "[_trans]\n";
323 if ( redFsm->anyRegActions() ) {
324 out <<
325 " if " << TA() << "[_trans] != 0\n"
326 " _acts = " << TA() << "[_trans]\n"
327 " _nacts = " << A() << "[_acts]\n"
328 " _acts += 1\n"
329 " while _nacts > 0\n"
330 " _nacts -= 1\n"
331 " _acts += 1\n"
332 " case " << A() << "[_acts - 1]\n";
333 ACTION_SWITCH();
334 out <<
335 " end # action switch\n"
336 " end\n"
337 " end\n"
338 " if _trigger_goto\n"
339 " next\n"
340 " end\n";
343 /* The again label. */
344 out <<
345 " end\n"
346 " if _goto_level <= _again\n";
348 if ( redFsm->anyToStateActions() ) {
349 out <<
350 " _acts = " << TSA() << "[" << CS() << "]\n"
351 " _nacts = " << A() << "[_acts]\n"
352 " _acts += 1\n"
353 " while _nacts > 0\n"
354 " _nacts -= 1\n"
355 " _acts += 1\n"
356 " case " << A() << "[_acts - 1]\n";
357 TO_STATE_ACTION_SWITCH();
358 out <<
359 " end # to state action switch\n"
360 " end\n"
361 " if _trigger_goto\n"
362 " next\n"
363 " end\n";
366 if ( redFsm->errState != 0 ) {
367 out <<
368 " if " << CS() << " == " << redFsm->errState->id << "\n"
369 " _goto_level = _out\n"
370 " next\n"
371 " end\n";
374 out << " " << P() << " += 1\n";
376 if ( hasEnd ) {
377 out <<
378 " if " << P() << " != " << PE() << "\n"
379 " _goto_level = _resume\n"
380 " next\n"
381 " end\n";
383 else {
384 out <<
385 " _goto_level = _resume\n"
386 " next\n";
389 /* The test_eof label. */
390 out <<
391 " end\n"
392 " if _goto_level <= _test_eof\n";
394 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
395 out <<
396 " if " << P() << " == " << EOFV() << "\n";
398 if ( redFsm->anyEofTrans() ) {
399 out <<
400 " if " << ET() << "[" << CS() << "] > 0\n"
401 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
402 " _goto_level = _eof_trans\n"
403 " next;\n"
404 " end\n";
407 if ( redFsm->anyEofActions() ) {
408 out <<
409 " __acts = " << EA() << "[" << CS() << "]\n"
410 " __nacts = " << " " << A() << "[__acts]\n"
411 " __acts += 1\n"
412 " while __nacts > 0\n"
413 " __nacts -= 1\n"
414 " __acts += 1\n"
415 " case " << A() << "[__acts - 1]\n";
416 EOF_ACTION_SWITCH() <<
417 " end # eof action switch\n"
418 " end\n"
419 " if _trigger_goto\n"
420 " next\n"
421 " end\n";
424 out <<
425 "end\n";
428 out <<
429 " end\n"
430 " if _goto_level <= _out\n"
431 " break\n"
432 " end\n";
434 /* The loop for next. */
435 out <<
436 " end\n";
438 /* Wrapping the execute block. */
439 out <<
440 " end\n";
443 std::ostream &RubyTabCodeGen::FROM_STATE_ACTION_SWITCH()
445 /* Walk the list of functions, printing the cases. */
446 for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
447 /* Write out referenced actions. */
448 if ( act->numFromStateRefs > 0 ) {
449 /* Write the case label, the action */
450 out << " when " << act->actionId << " then\n";
451 ACTION( out, act, 0, false );
455 genLineDirective( out );
456 return out;
460 std::ostream &RubyTabCodeGen::TO_STATE_ACTION_SWITCH()
462 /* Walk the list of functions, printing the cases. */
463 for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
464 /* Write out referenced actions. */
465 if ( act->numToStateRefs > 0 ) {
466 /* Write the case label, the action and the case break. */
467 out << "when " << act->actionId << " then\n";
468 ACTION( out, act, 0, false );
472 genLineDirective( out );
473 return out;
476 std::ostream &RubyTabCodeGen::EOF_ACTION_SWITCH()
478 /* Walk the list of functions, printing the cases. */
479 for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
480 /* Write out referenced actions. */
481 if ( act->numEofRefs > 0 ) {
482 /* Write the case label, the action and the case break. */
483 out << "when " << act->actionId << " then\n";
484 ACTION( out, act, 0, true );
488 genLineDirective( out );
489 return out;
492 std::ostream &RubyTabCodeGen::ACTION_SWITCH()
494 /* Walk the list of functions, printing the cases. */
495 for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
496 /* Write out referenced actions. */
497 if ( act->numTransRefs > 0 ) {
498 /* Write the case label, the action and the case break. */
499 out << "when " << act->actionId << " then\n";
500 ACTION( out, act, 0, false );
504 genLineDirective( out );
505 return out;
509 void RubyTabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
511 ret << CS() << " = " << nextDest << ";";
514 void RubyTabCodeGen::NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
516 ret << CS() << " = (";
517 INLINE_LIST( ret, ilItem->children, 0, inFinish );
518 ret << ");";
522 int RubyTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
524 int act = 0;
525 if ( state->toStateAction != 0 )
526 act = state->toStateAction->location+1;
527 return act;
530 int RubyTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
532 int act = 0;
533 if ( state->fromStateAction != 0 )
534 act = state->fromStateAction->location+1;
535 return act;
538 int RubyTabCodeGen::EOF_ACTION( RedStateAp *state )
540 int act = 0;
541 if ( state->eofAction != 0 )
542 act = state->eofAction->location+1;
543 return act;
547 std::ostream &RubyTabCodeGen::COND_OFFSETS()
549 START_ARRAY_LINE();
550 int totalStateNum = 0, curKeyOffset = 0;
551 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
552 /* Write the key offset. */
553 ARRAY_ITEM( INT(curKeyOffset), ++totalStateNum, st.last() );
555 /* Move the key offset ahead. */
556 curKeyOffset += st->stateCondList.length();
558 END_ARRAY_LINE();
559 return out;
562 std::ostream &RubyTabCodeGen::KEY_OFFSETS()
564 START_ARRAY_LINE();
565 int totalStateNum = 0, curKeyOffset = 0;
566 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
567 /* Write the key offset. */
568 ARRAY_ITEM( INT(curKeyOffset), ++totalStateNum, st.last() );
570 /* Move the key offset ahead. */
571 curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
573 END_ARRAY_LINE();
574 return out;
578 std::ostream &RubyTabCodeGen::INDEX_OFFSETS()
580 START_ARRAY_LINE();
581 int totalStateNum = 0, curIndOffset = 0;
582 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
583 /* Write the index offset. */
584 ARRAY_ITEM( INT(curIndOffset), ++totalStateNum, st.last() );
586 /* Move the index offset ahead. */
587 curIndOffset += st->outSingle.length() + st->outRange.length();
588 if ( st->defTrans != 0 )
589 curIndOffset += 1;
591 END_ARRAY_LINE();
592 return out;
595 std::ostream &RubyTabCodeGen::COND_LENS()
597 START_ARRAY_LINE();
598 int totalStateNum = 0;
599 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
600 /* Write singles length. */
601 ARRAY_ITEM( INT(st->stateCondList.length()), ++totalStateNum, st.last() );
603 END_ARRAY_LINE();
604 return out;
608 std::ostream &RubyTabCodeGen::SINGLE_LENS()
610 START_ARRAY_LINE();
611 int totalStateNum = 0;
612 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
613 /* Write singles length. */
614 ARRAY_ITEM( INT(st->outSingle.length()), ++totalStateNum, st.last() );
616 END_ARRAY_LINE();
617 return out;
620 std::ostream &RubyTabCodeGen::RANGE_LENS()
622 START_ARRAY_LINE();
623 int totalStateNum = 0;
624 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
625 /* Emit length of range index. */
626 ARRAY_ITEM( INT(st->outRange.length()), ++totalStateNum, st.last() );
628 END_ARRAY_LINE();
629 return out;
632 std::ostream &RubyTabCodeGen::TO_STATE_ACTIONS()
634 START_ARRAY_LINE();
635 int totalStateNum = 0;
636 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
637 /* Write any eof action. */
638 ARRAY_ITEM( INT(TO_STATE_ACTION(st)), ++totalStateNum, st.last() );
640 END_ARRAY_LINE();
641 return out;
644 std::ostream &RubyTabCodeGen::FROM_STATE_ACTIONS()
646 START_ARRAY_LINE();
647 int totalStateNum = 0;
648 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
649 /* Write any eof action. */
650 ARRAY_ITEM( INT(FROM_STATE_ACTION(st)), ++totalStateNum, st.last() );
652 END_ARRAY_LINE();
653 return out;
656 std::ostream &RubyTabCodeGen::EOF_ACTIONS()
658 START_ARRAY_LINE();
659 int totalStateNum = 0;
660 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
661 /* Write any eof action. */
662 ARRAY_ITEM( INT(EOF_ACTION(st)), ++totalStateNum, st.last() );
664 END_ARRAY_LINE();
665 return out;
668 std::ostream &RubyTabCodeGen::EOF_TRANS()
670 START_ARRAY_LINE();
671 int totalStateNum = 0;
672 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
673 /* Write any eof action. */
674 long trans = 0;
675 if ( st->eofTrans != 0 )
676 trans = st->eofTrans->id+1;
678 /* Write any eof action. */
679 ARRAY_ITEM( INT(trans), ++totalStateNum, st.last() );
681 END_ARRAY_LINE();
682 return out;
685 std::ostream &RubyTabCodeGen::COND_KEYS()
687 START_ARRAY_LINE();
688 int totalTrans = 0;
689 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
690 /* Loop the state's transitions. */
691 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
692 /* Lower key. */
693 ARRAY_ITEM( KEY( sc->lowKey ), ++totalTrans, false );
694 ARRAY_ITEM( KEY( sc->highKey ), ++totalTrans, false );
698 /* Output one last number so we don't have to figure out when the last
699 * entry is and avoid writing a comma. */
700 ARRAY_ITEM( INT(0), ++totalTrans, true );
701 END_ARRAY_LINE();
702 return out;
705 std::ostream &RubyTabCodeGen::COND_SPACES()
707 START_ARRAY_LINE();
708 int totalTrans = 0;
709 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
710 /* Loop the state's transitions. */
711 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
712 /* Cond Space id. */
713 ARRAY_ITEM( KEY( sc->condSpace->condSpaceId ), ++totalTrans, false );
717 /* Output one last number so we don't have to figure out when the last
718 * entry is and avoid writing a comma. */
719 ARRAY_ITEM( INT(0), ++totalTrans, true );
720 END_ARRAY_LINE();
721 return out;
724 std::ostream &RubyTabCodeGen::KEYS()
726 START_ARRAY_LINE();
727 int totalTrans = 0;
728 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
729 /* Loop the singles. */
730 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
731 ARRAY_ITEM( KEY( stel->lowKey ), ++totalTrans, false );
734 /* Loop the state's transitions. */
735 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
736 /* Lower key. */
737 ARRAY_ITEM( KEY( rtel->lowKey ), ++totalTrans, false );
739 /* Upper key. */
740 ARRAY_ITEM( KEY( rtel->highKey ), ++totalTrans, false );
744 /* Output one last number so we don't have to figure out when the last
745 * entry is and avoid writing a comma. */
746 ARRAY_ITEM( INT(0), ++totalTrans, true );
747 END_ARRAY_LINE();
748 return out;
751 std::ostream &RubyTabCodeGen::INDICIES()
753 int totalTrans = 0;
754 START_ARRAY_LINE();
755 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
756 /* Walk the singles. */
757 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
758 ARRAY_ITEM( KEY( stel->value->id ), ++totalTrans, false );
761 /* Walk the ranges. */
762 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
763 ARRAY_ITEM( KEY( rtel->value->id ), ++totalTrans, false );
766 /* The state's default index goes next. */
767 if ( st->defTrans != 0 ) {
768 ARRAY_ITEM( KEY( st->defTrans->id ), ++totalTrans, false );
772 /* Output one last number so we don't have to figure out when the last
773 * entry is and avoid writing a comma. */
774 ARRAY_ITEM( INT(0), ++totalTrans, true );
775 END_ARRAY_LINE();
776 return out;
779 std::ostream &RubyTabCodeGen::TRANS_TARGS()
781 int totalTrans = 0;
782 START_ARRAY_LINE();
783 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
784 /* Walk the singles. */
785 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
786 RedTransAp *trans = stel->value;
787 ARRAY_ITEM( KEY( trans->targ->id ), ++totalTrans, false );
790 /* Walk the ranges. */
791 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
792 RedTransAp *trans = rtel->value;
793 ARRAY_ITEM( KEY( trans->targ->id ), ++totalTrans, false );
796 /* The state's default target state. */
797 if ( st->defTrans != 0 ) {
798 RedTransAp *trans = st->defTrans;
799 ARRAY_ITEM( KEY( trans->targ->id ), ++totalTrans, false );
803 /* Output one last number so we don't have to figure out when the last
804 * entry is and avoid writing a comma. */
805 ARRAY_ITEM( INT(0), ++totalTrans, true );
806 END_ARRAY_LINE();
807 return out;
811 std::ostream &RubyTabCodeGen::TRANS_ACTIONS()
813 int totalTrans = 0;
814 START_ARRAY_LINE();
815 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
816 /* Walk the singles. */
817 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
818 RedTransAp *trans = stel->value;
819 ARRAY_ITEM( INT(TRANS_ACTION( trans )), ++totalTrans, false );
822 /* Walk the ranges. */
823 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
824 RedTransAp *trans = rtel->value;
825 ARRAY_ITEM( INT(TRANS_ACTION( trans )), ++totalTrans, false );
828 /* The state's default index goes next. */
829 if ( st->defTrans != 0 ) {
830 RedTransAp *trans = st->defTrans;
831 ARRAY_ITEM( INT(TRANS_ACTION( trans )), ++totalTrans, false );
835 /* Output one last number so we don't have to figure out when the last
836 * entry is and avoid writing a comma. */
837 ARRAY_ITEM( INT(0), ++totalTrans, true );
838 END_ARRAY_LINE();
839 return out;
842 std::ostream &RubyTabCodeGen::TRANS_TARGS_WI()
844 /* Transitions must be written ordered by their id. */
845 RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
846 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
847 transPtrs[trans->id] = trans;
849 /* Keep a count of the num of items in the array written. */
850 START_ARRAY_LINE();
851 int totalStates = 0;
852 for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
853 /* Write out the target state. */
854 RedTransAp *trans = transPtrs[t];
855 ARRAY_ITEM( INT(trans->targ->id), ++totalStates, ( t >= redFsm->transSet.length()-1 ) );
857 END_ARRAY_LINE();
858 delete[] transPtrs;
859 return out;
863 std::ostream &RubyTabCodeGen::TRANS_ACTIONS_WI()
865 /* Transitions must be written ordered by their id. */
866 RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
867 for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
868 transPtrs[trans->id] = trans;
870 /* Keep a count of the num of items in the array written. */
871 START_ARRAY_LINE();
872 int totalAct = 0;
873 for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
874 /* Write the function for the transition. */
875 RedTransAp *trans = transPtrs[t];
876 ARRAY_ITEM( INT(TRANS_ACTION( trans )), ++totalAct,
877 ( t >= redFsm->transSet.length()-1 ) );
879 END_ARRAY_LINE();
880 delete[] transPtrs;
881 return out;
885 void RubyTabCodeGen::writeData()
887 /* If there are any transtion functions then output the array. If there
888 * are none, don't bother emitting an empty array that won't be used. */
889 if ( redFsm->anyActions() ) {
890 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
891 ACTIONS_ARRAY();
892 CLOSE_ARRAY() <<
893 "\n";
896 if ( redFsm->anyConditions() ) {
897 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
898 COND_OFFSETS();
899 CLOSE_ARRAY() <<
900 "\n";
902 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
903 COND_LENS();
904 CLOSE_ARRAY() <<
905 "\n";
907 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
908 COND_KEYS();
909 CLOSE_ARRAY() <<
910 "\n";
912 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
913 COND_SPACES();
914 CLOSE_ARRAY() <<
915 "\n";
918 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
919 KEY_OFFSETS();
920 CLOSE_ARRAY() <<
921 "\n";
923 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
924 KEYS();
925 CLOSE_ARRAY() <<
926 "\n";
928 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
929 SINGLE_LENS();
930 CLOSE_ARRAY() <<
931 "\n";
933 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
934 RANGE_LENS();
935 CLOSE_ARRAY() <<
936 "\n";
938 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
939 INDEX_OFFSETS();
940 CLOSE_ARRAY() <<
941 "\n";
943 if ( useIndicies ) {
944 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
945 INDICIES();
946 CLOSE_ARRAY() <<
947 "\n";
949 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
950 TRANS_TARGS_WI();
951 CLOSE_ARRAY() <<
952 "\n";
954 if ( redFsm->anyActions() ) {
955 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
956 TRANS_ACTIONS_WI();
957 CLOSE_ARRAY() <<
958 "\n";
961 else {
962 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
963 TRANS_TARGS();
964 CLOSE_ARRAY() <<
965 "\n";
967 if ( redFsm->anyActions() ) {
968 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
969 TRANS_ACTIONS();
970 CLOSE_ARRAY() <<
971 "\n";
975 if ( redFsm->anyToStateActions() ) {
976 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
977 TO_STATE_ACTIONS();
978 CLOSE_ARRAY() <<
979 "\n";
982 if ( redFsm->anyFromStateActions() ) {
983 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
984 FROM_STATE_ACTIONS();
985 CLOSE_ARRAY() <<
986 "\n";
989 if ( redFsm->anyEofActions() ) {
990 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
991 EOF_ACTIONS();
992 CLOSE_ARRAY() <<
993 "\n";
996 if ( redFsm->anyEofTrans() ) {
997 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex+1), ET() );
998 EOF_TRANS();
999 CLOSE_ARRAY() <<
1000 "\n";
1003 STATE_IDS();
1007 Local Variables:
1008 mode: c++
1009 indent-tabs-mode: 1
1010 c-file-style: "bsd"
1011 End: