Fixed bugs in the binary searching for condition keys in both the Ruby and Java
[external/ragel.git] / rlgen-java / javacodegen.cpp
1 /*
2  *  Copyright 2006-2007 Adrian Thurston <thurston@cs.queensu.ca>
3  *            2007 Colin Fleming <colin.fleming@caverock.com>
4  */
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
23 #include "rlgen-java.h"
24 #include "javacodegen.h"
25 #include "redfsm.h"
26 #include "gendata.h"
27 #include <iomanip>
28 #include <sstream>
29
30 /* Integer array line length. */
31 #define IALL 8
32
33 /* Static array initialization item count 
34  * (should be multiple of IALL). */
35 #define SAIIC 8192
36
37 using std::ostream;
38 using std::ostringstream;
39 using std::string;
40 using std::cerr;
41 using std::endl;
42
43 void lineDirective( ostream &out, char *fileName, int line )
44 {
45         /* Write the preprocessor line info for to the input file. */
46         out << "// line " << line  << " \"";
47         for ( char *pc = fileName; *pc != 0; pc++ ) {
48                 if ( *pc == '\\' )
49                         out << "\\\\";
50                 else
51                         out << *pc;
52         }
53         out << "\"\n";
54 }
55
56 void genLineDirective( ostream &out )
57 {
58         std::streambuf *sbuf = out.rdbuf();
59         output_filter *filter = static_cast<output_filter*>(sbuf);
60         lineDirective( out, filter->fileName, filter->line + 1 );
61 }
62
63 void JavaTabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
64 {
65         ret << "{" << CS() << " = " << gotoDest << "; " << 
66                         CTRL_FLOW() << "break _again;}";
67 }
68
69 void JavaTabCodeGen::GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
70 {
71         ret << "{" << CS() << " = (";
72         INLINE_LIST( ret, ilItem->children, 0, inFinish );
73         ret << "); " << CTRL_FLOW() << "break _again;}";
74 }
75
76 void JavaTabCodeGen::CALL( ostream &ret, int callDest, int targState, bool inFinish )
77 {
78         ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = " << 
79                         callDest << "; " << CTRL_FLOW() << "break _again;}";
80 }
81
82 void JavaTabCodeGen::CALL_EXPR( ostream &ret, InlineItem *ilItem, int targState, bool inFinish )
83 {
84         ret << "{" << STACK() << "[" << TOP() << "++] = " << CS() << "; " << CS() << " = (";
85         INLINE_LIST( ret, ilItem->children, targState, inFinish );
86         ret << "); " << CTRL_FLOW() << "break _again;}";
87 }
88
89 void JavaTabCodeGen::RET( ostream &ret, bool inFinish )
90 {
91         ret << "{" << CS() << " = " << STACK() << "[--" << TOP() 
92                         << "]; " << CTRL_FLOW() << "break _again;}";
93 }
94
95 void JavaTabCodeGen::BREAK( ostream &ret, int targState )
96 {
97         ret << CTRL_FLOW() << "break _resume;";
98 }
99
100 void JavaTabCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
101 {
102         ret << CS() << " = " << nextDest << ";";
103 }
104
105 void JavaTabCodeGen::NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish )
106 {
107         ret << CS() << " = (";
108         INLINE_LIST( ret, ilItem->children, 0, inFinish );
109         ret << ");";
110 }
111
112 void JavaTabCodeGen::EXEC( ostream &ret, InlineItem *item, int targState, int inFinish )
113 {
114         /* The parser gives fexec two children. The double brackets are for D
115          * code. If the inline list is a single word it will get interpreted as a
116          * C-style cast by the D compiler. */
117         ret << "{" << P() << " = ((";
118         INLINE_LIST( ret, item->children, targState, inFinish );
119         ret << "))-1;}";
120 }
121
122 void JavaTabCodeGen::EXECTE( ostream &ret, InlineItem *item, int targState, int inFinish )
123 {
124         /* Tokend version of exec. */
125
126         /* The parser gives fexec two children. The double brackets are for D
127          * code. If the inline list is a single word it will get interpreted as a
128          * C-style cast by the D compiler. */
129         ret << "{" << TOKEND() << " = ((";
130         INLINE_LIST( ret, item->children, targState, inFinish );
131         ret << "));}";
132 }
133
134 /* Write out an inline tree structure. Walks the list and possibly calls out
135  * to virtual functions than handle language specific items in the tree. */
136 void JavaTabCodeGen::INLINE_LIST( ostream &ret, InlineList *inlineList, 
137                 int targState, bool inFinish )
138 {
139         for ( InlineList::Iter item = *inlineList; item.lte(); item++ ) {
140                 switch ( item->type ) {
141                 case InlineItem::Text:
142                         ret << item->data;
143                         break;
144                 case InlineItem::Goto:
145                         GOTO( ret, item->targState->id, inFinish );
146                         break;
147                 case InlineItem::Call:
148                         CALL( ret, item->targState->id, targState, inFinish );
149                         break;
150                 case InlineItem::Next:
151                         NEXT( ret, item->targState->id, inFinish );
152                         break;
153                 case InlineItem::Ret:
154                         RET( ret, inFinish );
155                         break;
156                 case InlineItem::PChar:
157                         ret << P();
158                         break;
159                 case InlineItem::Char:
160                         ret << GET_KEY();
161                         break;
162                 case InlineItem::Hold:
163                         ret << P() << "--;";
164                         break;
165                 case InlineItem::Exec:
166                         EXEC( ret, item, targState, inFinish );
167                         break;
168                 case InlineItem::HoldTE:
169                         ret << TOKEND() << "--;";
170                         break;
171                 case InlineItem::ExecTE:
172                         EXECTE( ret, item, targState, inFinish );
173                         break;
174                 case InlineItem::Curs:
175                         ret << "(_ps)";
176                         break;
177                 case InlineItem::Targs:
178                         ret << "(" << CS() << ")";
179                         break;
180                 case InlineItem::Entry:
181                         ret << item->targState->id;
182                         break;
183                 case InlineItem::GotoExpr:
184                         GOTO_EXPR( ret, item, inFinish );
185                         break;
186                 case InlineItem::CallExpr:
187                         CALL_EXPR( ret, item, targState, inFinish );
188                         break;
189                 case InlineItem::NextExpr:
190                         NEXT_EXPR( ret, item, inFinish );
191                         break;
192                 case InlineItem::LmSwitch:
193                         LM_SWITCH( ret, item, targState, inFinish );
194                         break;
195                 case InlineItem::LmSetActId:
196                         SET_ACT( ret, item );
197                         break;
198                 case InlineItem::LmSetTokEnd:
199                         SET_TOKEND( ret, item );
200                         break;
201                 case InlineItem::LmGetTokEnd:
202                         GET_TOKEND( ret, item );
203                         break;
204                 case InlineItem::LmInitTokStart:
205                         INIT_TOKSTART( ret, item );
206                         break;
207                 case InlineItem::LmInitAct:
208                         INIT_ACT( ret, item );
209                         break;
210                 case InlineItem::LmSetTokStart:
211                         SET_TOKSTART( ret, item );
212                         break;
213                 case InlineItem::SubAction:
214                         SUB_ACTION( ret, item, targState, inFinish );
215                         break;
216                 case InlineItem::Break:
217                         BREAK( ret, targState );
218                         break;
219                 }
220         }
221 }
222
223 string JavaTabCodeGen::DATA_PREFIX()
224 {
225         if ( dataPrefix )
226                 return FSM_NAME() + "_";
227         return "";
228 }
229
230 /* Emit the alphabet data type. */
231 string JavaTabCodeGen::ALPH_TYPE()
232 {
233         string ret = keyOps->alphType->data1;
234         if ( keyOps->alphType->data2 != 0 ) {
235                 ret += " ";
236                 ret += + keyOps->alphType->data2;
237         }
238         return ret;
239 }
240
241 /* Emit the alphabet data type. */
242 string JavaTabCodeGen::WIDE_ALPH_TYPE()
243 {
244         string ret;
245         if ( redFsm->maxKey <= keyOps->maxKey )
246                 ret = ALPH_TYPE();
247         else {
248                 long long maxKeyVal = redFsm->maxKey.getLongLong();
249                 HostType *wideType = keyOps->typeSubsumes( keyOps->isSigned, maxKeyVal );
250                 assert( wideType != 0 );
251
252                 ret = wideType->data1;
253                 if ( wideType->data2 != 0 ) {
254                         ret += " ";
255                         ret += wideType->data2;
256                 }
257         }
258         return ret;
259 }
260
261
262
263 void JavaTabCodeGen::COND_TRANSLATE()
264 {
265         out << 
266                 "       _widec = " << GET_KEY() << ";\n"
267                 "       _keys = " << CO() << "[" << CS() << "]*2\n;"
268                 "       _klen = " << CL() << "[" << CS() << "];\n"
269                 "       if ( _klen > 0 ) {\n"
270                 "               int _lower = _keys\n;"
271                 "               int _mid;\n"
272                 "               int _upper = _keys + (_klen<<1) - 2;\n"
273                 "               while (true) {\n"
274                 "                       if ( _upper < _lower )\n"
275                 "                               break;\n"
276                 "\n"
277                 "                       _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
278                 "                       if ( " << GET_WIDE_KEY() << " < " << CK() << "[_mid] )\n"
279                 "                               _upper = _mid - 2;\n"
280                 "                       else if ( " << GET_WIDE_KEY() << " > " << CK() << "[_mid+1] )\n"
281                 "                               _lower = _mid + 2;\n"
282                 "                       else {\n"
283                 "                               switch ( " << C() << "[" << CO() << "[" << CS() << "]"
284                                                         " + ((_mid - _keys)>>1)] ) {\n"
285                 ;
286
287         for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
288                 CondSpace *condSpace = csi;
289                 out << "        case " << condSpace->condSpaceId << ": {\n";
290                 out << TABS(2) << "_widec = " << KEY(condSpace->baseKey) << 
291                                 " + (" << GET_KEY() << " - " << KEY(keyOps->minKey) << ");\n";
292
293                 for ( CondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
294                         out << TABS(2) << "if ( ";
295                         CONDITION( out, *csi );
296                         Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
297                         out << " ) _widec += " << condValOffset << ";\n";
298                 }
299
300                 out << 
301                         "               break;\n"
302                         "       }\n";
303         }
304
305         out << 
306                 "                               }\n"
307                 "                               break;\n"
308                 "                       }\n"
309                 "               }\n"
310                 "       }\n"
311                 "\n";
312 }
313
314
315 void JavaTabCodeGen::LOCATE_TRANS()
316 {
317         out <<
318                 "       _match: do {\n"
319                 "       _keys = " << KO() << "[" << CS() << "]" << ";\n"
320                 "       _trans = " << IO() << "[" << CS() << "];\n"
321                 "       _klen = " << SL() << "[" << CS() << "];\n"
322                 "       if ( _klen > 0 ) {\n"
323                 "               int _lower = _keys;\n"
324                 "               int _mid;\n"
325                 "               int _upper = _keys + _klen - 1;\n"
326                 "               while (true) {\n"
327                 "                       if ( _upper < _lower )\n"
328                 "                               break;\n"
329                 "\n"
330                 "                       _mid = _lower + ((_upper-_lower) >> 1);\n"
331                 "                       if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
332                 "                               _upper = _mid - 1;\n"
333                 "                       else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid] )\n"
334                 "                               _lower = _mid + 1;\n"
335                 "                       else {\n"
336                 "                               _trans += (_mid - _keys);\n"
337                 "                               break _match;\n"
338                 "                       }\n"
339                 "               }\n"
340                 "               _keys += _klen;\n"
341                 "               _trans += _klen;\n"
342                 "       }\n"
343                 "\n"
344                 "       _klen = " << RL() << "[" << CS() << "];\n"
345                 "       if ( _klen > 0 ) {\n"
346                 "               int _lower = _keys;\n"
347                 "               int _mid;\n"
348                 "               int _upper = _keys + (_klen<<1) - 2;\n"
349                 "               while (true) {\n"
350                 "                       if ( _upper < _lower )\n"
351                 "                               break;\n"
352                 "\n"
353                 "                       _mid = _lower + (((_upper-_lower) >> 1) & ~1);\n"
354                 "                       if ( " << GET_WIDE_KEY() << " < " << K() << "[_mid] )\n"
355                 "                               _upper = _mid - 2;\n"
356                 "                       else if ( " << GET_WIDE_KEY() << " > " << K() << "[_mid+1] )\n"
357                 "                               _lower = _mid + 2;\n"
358                 "                       else {\n"
359                 "                               _trans += ((_mid - _keys)>>1);\n"
360                 "                               break _match;\n"
361                 "                       }\n"
362                 "               }\n"
363                 "               _trans += _klen;\n"
364                 "       }\n"
365                 "       } while (false);\n"
366                 "\n";
367 }
368
369 /* Determine if we should use indicies or not. */
370 void JavaTabCodeGen::calcIndexSize()
371 {
372         int sizeWithInds = 0, sizeWithoutInds = 0;
373
374         /* Calculate cost of using with indicies. */
375         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
376                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
377                                 (st->defTrans == 0 ? 0 : 1);
378                 sizeWithInds += arrayTypeSize(redFsm->maxIndex) * totalIndex;
379         }
380         sizeWithInds += arrayTypeSize(redFsm->maxState) * redFsm->transSet.length();
381         if ( redFsm->anyActions() )
382                 sizeWithInds += arrayTypeSize(redFsm->maxActionLoc) * redFsm->transSet.length();
383
384         /* Calculate the cost of not using indicies. */
385         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
386                 int totalIndex = st->outSingle.length() + st->outRange.length() + 
387                                 (st->defTrans == 0 ? 0 : 1);
388                 sizeWithoutInds += arrayTypeSize(redFsm->maxState) * totalIndex;
389                 if ( redFsm->anyActions() )
390                         sizeWithoutInds += arrayTypeSize(redFsm->maxActionLoc) * totalIndex;
391         }
392
393         /* If using indicies reduces the size, use them. */
394         useIndicies = sizeWithInds < sizeWithoutInds;
395 }
396
397 int JavaTabCodeGen::TO_STATE_ACTION( RedStateAp *state )
398 {
399         int act = 0;
400         if ( state->toStateAction != 0 )
401                 act = state->toStateAction->location+1;
402         return act;
403 }
404
405 int JavaTabCodeGen::FROM_STATE_ACTION( RedStateAp *state )
406 {
407         int act = 0;
408         if ( state->fromStateAction != 0 )
409                 act = state->fromStateAction->location+1;
410         return act;
411 }
412
413 int JavaTabCodeGen::EOF_ACTION( RedStateAp *state )
414 {
415         int act = 0;
416         if ( state->eofAction != 0 )
417                 act = state->eofAction->location+1;
418         return act;
419 }
420
421
422 int JavaTabCodeGen::TRANS_ACTION( RedTransAp *trans )
423 {
424         /* If there are actions, emit them. Otherwise emit zero. */
425         int act = 0;
426         if ( trans->action != 0 )
427                 act = trans->action->location+1;
428         return act;
429 }
430
431 std::ostream &JavaTabCodeGen::TO_STATE_ACTION_SWITCH()
432 {
433         /* Walk the list of functions, printing the cases. */
434         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
435                 /* Write out referenced actions. */
436                 if ( act->numToStateRefs > 0 ) {
437                         /* Write the case label, the action and the case break. */
438                         out << "\tcase " << act->actionId << ":\n";
439                         ACTION( out, act, 0, false );
440                         out << "\tbreak;\n";
441                 }
442         }
443
444         genLineDirective( out );
445         return out;
446 }
447
448 std::ostream &JavaTabCodeGen::FROM_STATE_ACTION_SWITCH()
449 {
450         /* Walk the list of functions, printing the cases. */
451         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
452                 /* Write out referenced actions. */
453                 if ( act->numFromStateRefs > 0 ) {
454                         /* Write the case label, the action and the case break. */
455                         out << "\tcase " << act->actionId << ":\n";
456                         ACTION( out, act, 0, false );
457                         out << "\tbreak;\n";
458                 }
459         }
460
461         genLineDirective( out );
462         return out;
463 }
464
465 std::ostream &JavaTabCodeGen::EOF_ACTION_SWITCH()
466 {
467         /* Walk the list of functions, printing the cases. */
468         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
469                 /* Write out referenced actions. */
470                 if ( act->numEofRefs > 0 ) {
471                         /* Write the case label, the action and the case break. */
472                         out << "\tcase " << act->actionId << ":\n";
473                         ACTION( out, act, 0, true );
474                         out << "\tbreak;\n";
475                 }
476         }
477
478         genLineDirective( out );
479         return out;
480 }
481
482
483 std::ostream &JavaTabCodeGen::ACTION_SWITCH()
484 {
485         /* Walk the list of functions, printing the cases. */
486         for ( ActionList::Iter act = actionList; act.lte(); act++ ) {
487                 /* Write out referenced actions. */
488                 if ( act->numTransRefs > 0 ) {
489                         /* Write the case label, the action and the case break. */
490                         out << "\tcase " << act->actionId << ":\n";
491                         ACTION( out, act, 0, false );
492                         out << "\tbreak;\n";
493                 }
494         }
495
496         genLineDirective( out );
497         return out;
498 }
499
500 std::ostream &JavaTabCodeGen::COND_OFFSETS()
501 {
502         int curKeyOffset = 0;
503         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
504                 /* Write the key offset. */
505                 ARRAY_ITEM( INT(curKeyOffset), st.last() );
506
507                 /* Move the key offset ahead. */
508                 curKeyOffset += st->stateCondList.length();
509         }
510         return out;
511 }
512
513 std::ostream &JavaTabCodeGen::KEY_OFFSETS()
514 {
515         int curKeyOffset = 0;
516         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
517                 /* Write the key offset. */
518                 ARRAY_ITEM( INT(curKeyOffset), st.last() );
519
520                 /* Move the key offset ahead. */
521                 curKeyOffset += st->outSingle.length() + st->outRange.length()*2;
522         }
523         return out;
524 }
525
526
527 std::ostream &JavaTabCodeGen::INDEX_OFFSETS()
528 {
529         int curIndOffset = 0;
530         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
531                 /* Write the index offset. */
532                 ARRAY_ITEM( INT(curIndOffset), st.last() );
533
534                 /* Move the index offset ahead. */
535                 curIndOffset += st->outSingle.length() + st->outRange.length();
536                 if ( st->defTrans != 0 )
537                         curIndOffset += 1;
538         }
539         return out;
540 }
541
542 std::ostream &JavaTabCodeGen::COND_LENS()
543 {
544         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
545                 /* Write singles length. */
546                 ARRAY_ITEM( INT(st->stateCondList.length()), st.last() );
547         }
548         return out;
549 }
550
551
552 std::ostream &JavaTabCodeGen::SINGLE_LENS()
553 {
554         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
555                 /* Write singles length. */
556                 ARRAY_ITEM( INT(st->outSingle.length()), st.last() );
557         }
558         return out;
559 }
560
561 std::ostream &JavaTabCodeGen::RANGE_LENS()
562 {
563         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
564                 /* Emit length of range index. */
565                 ARRAY_ITEM( INT(st->outRange.length()), st.last() );
566         }
567         return out;
568 }
569
570 std::ostream &JavaTabCodeGen::TO_STATE_ACTIONS()
571 {
572         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
573                 /* Write any eof action. */
574                 ARRAY_ITEM( INT(TO_STATE_ACTION(st)), st.last() );
575         }
576         return out;
577 }
578
579 std::ostream &JavaTabCodeGen::FROM_STATE_ACTIONS()
580 {
581         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
582                 /* Write any eof action. */
583                 ARRAY_ITEM( INT(FROM_STATE_ACTION(st)), st.last() );
584         }
585         return out;
586 }
587
588 std::ostream &JavaTabCodeGen::EOF_ACTIONS()
589 {
590         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
591                 /* Write any eof action. */
592                 ARRAY_ITEM( INT(EOF_ACTION(st)), st.last() );
593         }
594         return out;
595 }
596
597 std::ostream &JavaTabCodeGen::COND_KEYS()
598 {
599         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
600                 /* Loop the state's transitions. */
601                 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
602                         /* Lower key. */
603                         ARRAY_ITEM( KEY( sc->lowKey ), false );
604                         ARRAY_ITEM( KEY( sc->highKey ), false );
605                 }
606         }
607
608         /* Output one last number so we don't have to figure out when the last
609          * entry is and avoid writing a comma. */
610         ARRAY_ITEM( INT(0), true );
611         return out;
612 }
613
614 std::ostream &JavaTabCodeGen::COND_SPACES()
615 {
616         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
617                 /* Loop the state's transitions. */
618                 for ( StateCondList::Iter sc = st->stateCondList; sc.lte(); sc++ ) {
619                         /* Cond Space id. */
620                         ARRAY_ITEM( KEY( sc->condSpace->condSpaceId ), false );
621                 }
622         }
623
624         /* Output one last number so we don't have to figure out when the last
625          * entry is and avoid writing a comma. */
626         ARRAY_ITEM( INT(0), true );
627         return out;
628 }
629
630 std::ostream &JavaTabCodeGen::KEYS()
631 {
632         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
633                 /* Loop the singles. */
634                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
635                         ARRAY_ITEM( KEY( stel->lowKey ), false );
636                 }
637
638                 /* Loop the state's transitions. */
639                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
640                         /* Lower key. */
641                         ARRAY_ITEM( KEY( rtel->lowKey ), false );
642
643                         /* Upper key. */
644                         ARRAY_ITEM( KEY( rtel->highKey ), false );
645                 }
646         }
647
648         /* Output one last number so we don't have to figure out when the last
649          * entry is and avoid writing a comma. */
650         ARRAY_ITEM( INT(0), true );
651         return out;
652 }
653
654 std::ostream &JavaTabCodeGen::INDICIES()
655 {
656         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
657                 /* Walk the singles. */
658                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
659                         ARRAY_ITEM( KEY( stel->value->id ), false );
660                 }
661
662                 /* Walk the ranges. */
663                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
664                         ARRAY_ITEM( KEY( rtel->value->id ), false );
665                 }
666
667                 /* The state's default index goes next. */
668                 if ( st->defTrans != 0 ) {
669                         ARRAY_ITEM( KEY( st->defTrans->id ), false );
670                 }
671         }
672
673         /* Output one last number so we don't have to figure out when the last
674          * entry is and avoid writing a comma. */
675         ARRAY_ITEM( INT(0), true );
676         return out;
677 }
678
679 std::ostream &JavaTabCodeGen::TRANS_TARGS()
680 {
681         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
682                 /* Walk the singles. */
683                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
684                         RedTransAp *trans = stel->value;
685                         ARRAY_ITEM( KEY( trans->targ->id ), false );
686                 }
687
688                 /* Walk the ranges. */
689                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
690                         RedTransAp *trans = rtel->value;
691                         ARRAY_ITEM( KEY( trans->targ->id ), false );
692                 }
693
694                 /* The state's default target state. */
695                 if ( st->defTrans != 0 ) {
696                         RedTransAp *trans = st->defTrans;
697                         ARRAY_ITEM( KEY( trans->targ->id ), false );
698                 }
699         }
700
701         /* Output one last number so we don't have to figure out when the last
702          * entry is and avoid writing a comma. */
703         ARRAY_ITEM( INT(0), true );
704         return out;
705 }
706
707
708 std::ostream &JavaTabCodeGen::TRANS_ACTIONS()
709 {
710         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
711                 /* Walk the singles. */
712                 for ( RedTransList::Iter stel = st->outSingle; stel.lte(); stel++ ) {
713                         RedTransAp *trans = stel->value;
714                         ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
715                 }
716
717                 /* Walk the ranges. */
718                 for ( RedTransList::Iter rtel = st->outRange; rtel.lte(); rtel++ ) {
719                         RedTransAp *trans = rtel->value;
720                         ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
721                 }
722
723                 /* The state's default index goes next. */
724                 if ( st->defTrans != 0 ) {
725                         RedTransAp *trans = st->defTrans;
726                         ARRAY_ITEM( INT(TRANS_ACTION( trans )), false );
727                 }
728         }
729
730         /* Output one last number so we don't have to figure out when the last
731          * entry is and avoid writing a comma. */
732         ARRAY_ITEM( INT(0), true );
733         return out;
734 }
735
736 std::ostream &JavaTabCodeGen::TRANS_TARGS_WI()
737 {
738         /* Transitions must be written ordered by their id. */
739         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
740         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
741                 transPtrs[trans->id] = trans;
742
743         /* Keep a count of the num of items in the array written. */
744         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
745                 /* Write out the target state. */
746                 RedTransAp *trans = transPtrs[t];
747                 ARRAY_ITEM( INT(trans->targ->id), ( t >= redFsm->transSet.length()-1 ) );
748         }
749         delete[] transPtrs;
750         return out;
751 }
752
753
754 std::ostream &JavaTabCodeGen::TRANS_ACTIONS_WI()
755 {
756         /* Transitions must be written ordered by their id. */
757         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
758         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
759                 transPtrs[trans->id] = trans;
760
761         /* Keep a count of the num of items in the array written. */
762         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
763                 /* Write the function for the transition. */
764                 RedTransAp *trans = transPtrs[t];
765                 ARRAY_ITEM( INT(TRANS_ACTION( trans )), ( t >= redFsm->transSet.length()-1 ) );
766         }
767         delete[] transPtrs;
768         return out;
769 }
770
771 void JavaTabCodeGen::writeExports()
772 {
773         if ( exportList.length() > 0 ) {
774                 for ( ExportList::Iter ex = exportList; ex.lte(); ex++ ) {
775                         STATIC_VAR( ALPH_TYPE(), DATA_PREFIX() + "ex_" + ex->name ) 
776                                         << " = " << KEY(ex->key) << ";\n";
777                 }
778                 out << "\n";
779         }
780 }
781
782 void JavaTabCodeGen::writeData()
783 {
784         /* If there are any transtion functions then output the array. If there
785          * are none, don't bother emitting an empty array that won't be used. */
786         if ( redFsm->anyActions() ) {
787                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
788                 ACTIONS_ARRAY();
789                 CLOSE_ARRAY() <<
790                 "\n";
791         }
792
793         if ( redFsm->anyConditions() ) {
794                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondOffset), CO() );
795                 COND_OFFSETS();
796                 CLOSE_ARRAY() <<
797                 "\n";
798
799                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondLen), CL() );
800                 COND_LENS();
801                 CLOSE_ARRAY() <<
802                 "\n";
803
804                 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
805                 COND_KEYS();
806                 CLOSE_ARRAY() <<
807                 "\n";
808
809                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpaceId), C() );
810                 COND_SPACES();
811                 CLOSE_ARRAY() <<
812                 "\n";
813         }
814
815         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxKeyOffset), KO() );
816         KEY_OFFSETS();
817         CLOSE_ARRAY() <<
818         "\n";
819
820         OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
821         KEYS();
822         CLOSE_ARRAY() <<
823         "\n";
824
825         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSingleLen), SL() );
826         SINGLE_LENS();
827         CLOSE_ARRAY() <<
828         "\n";
829
830         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxRangeLen), RL() );
831         RANGE_LENS();
832         CLOSE_ARRAY() <<
833         "\n";
834
835         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset), IO() );
836         INDEX_OFFSETS();
837         CLOSE_ARRAY() <<
838         "\n";
839
840         if ( useIndicies ) {
841                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
842                 INDICIES();
843                 CLOSE_ARRAY() <<
844                 "\n";
845
846                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
847                 TRANS_TARGS_WI();
848                 CLOSE_ARRAY() <<
849                 "\n";
850
851                 if ( redFsm->anyActions() ) {
852                         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
853                         TRANS_ACTIONS_WI();
854                         CLOSE_ARRAY() <<
855                         "\n";
856                 }
857         }
858         else {
859                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
860                 TRANS_TARGS();
861                 CLOSE_ARRAY() <<
862                 "\n";
863
864                 if ( redFsm->anyActions() ) {
865                         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
866                         TRANS_ACTIONS();
867                         CLOSE_ARRAY() <<
868                         "\n";
869                 }
870         }
871
872         if ( redFsm->anyToStateActions() ) {
873                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
874                 TO_STATE_ACTIONS();
875                 CLOSE_ARRAY() <<
876                 "\n";
877         }
878
879         if ( redFsm->anyFromStateActions() ) {
880                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
881                 FROM_STATE_ACTIONS();
882                 CLOSE_ARRAY() <<
883                 "\n";
884         }
885
886         if ( redFsm->anyEofActions() ) {
887                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
888                 EOF_ACTIONS();
889                 CLOSE_ARRAY() <<
890                 "\n";
891         }
892
893         if ( redFsm->startState != 0 )
894                 STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
895
896         if ( writeFirstFinal )
897                 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
898
899         if ( writeErr )
900                 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
901         
902         out << "\n";
903
904         if ( entryPointNames.length() > 0 ) {
905                 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
906                         STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) << 
907                                         " = " << entryPointIds[en.pos()] << ";\n";
908                 }
909                 out << "\n";
910         }
911 }
912
913 void JavaTabCodeGen::writeExec()
914 {
915         out <<
916                 "       {\n"
917                 "       int _klen";
918
919         if ( redFsm->anyRegCurStateRef() )
920                 out << ", _ps";
921
922         out << 
923                 ";\n"
924                 "       int _trans;\n";
925
926         if ( redFsm->anyConditions() )
927                 out << "        int _widec;\n";
928
929         if ( redFsm->anyToStateActions() || redFsm->anyRegActions() || 
930                         redFsm->anyFromStateActions() )
931         {
932                 out << 
933                         "       int _acts;\n"
934                         "       int _nacts;\n";
935         }
936
937         out <<
938                 "       int _keys;\n"
939                 "\n";
940
941         if ( hasEnd )
942                 out << "        if ( " << P() << " != " << PE() << " ) {\n";
943
944         out << "        _resume: while ( true ) {\n";
945
946         out << "        _again: do {\n";
947
948         if ( redFsm->errState != 0 ) {
949                 out << 
950                         "       if ( " << CS() << " == " << redFsm->errState->id << " )\n"
951                         "               break _resume;\n";
952         }
953
954         if ( redFsm->anyFromStateActions() ) {
955                 out <<
956                         "       _acts = " << FSA() << "[" << CS() << "]" << ";\n"
957                         "       _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
958                         "       while ( _nacts-- > 0 ) {\n"
959                         "               switch ( " << A() << "[_acts++] ) {\n";
960                         FROM_STATE_ACTION_SWITCH() <<
961                         "               }\n"
962                         "       }\n"
963                         "\n";
964         }
965
966         if ( redFsm->anyConditions() )
967                 COND_TRANSLATE();
968
969         LOCATE_TRANS();
970
971         if ( redFsm->anyRegCurStateRef() )
972                 out << "        _ps = " << CS() << ";\n";
973
974         if ( useIndicies )
975                 out << "        _trans = " << I() << "[_trans];\n";
976
977         out <<
978                 "       " << CS() << " = " << TT() << "[_trans];\n"
979                 "\n";
980
981         if ( redFsm->anyRegActions() ) {
982                 out <<
983                         "       if ( " << TA() << "[_trans] == 0 )\n"
984                         "               break _again;\n"
985                         "\n"
986                         "       _acts = " <<  TA() << "[_trans]" << ";\n"
987                         "       _nacts = " << CAST("int") << " " <<  A() << "[_acts++];\n"
988                         "       while ( _nacts-- > 0 )\n        {\n"
989                         "               switch ( " << A() << "[_acts++] )\n"
990                         "               {\n";
991                         ACTION_SWITCH() <<
992                         "               }\n"
993                         "       }\n"
994                         "\n";
995         }
996
997         /* Again loop, functions as again label. */
998         out << "        } while (false);\n";
999
1000         if ( redFsm->anyToStateActions() ) {
1001                 out <<
1002                         "       _acts = " << TSA() << "[" << CS() << "]" << ";\n"
1003                         "       _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
1004                         "       while ( _nacts-- > 0 ) {\n"
1005                         "               switch ( " << A() << "[_acts++] ) {\n";
1006                         TO_STATE_ACTION_SWITCH() <<
1007                         "               }\n"
1008                         "       }\n"
1009                         "\n";
1010         }
1011
1012         if ( hasEnd ) {
1013                 out << 
1014                         "       if ( ++" << P() << " == " << PE() << " )\n"
1015                         "               break _resume;\n";
1016         }
1017         else {
1018                 out << 
1019                         "       " << P() << " += 1;\n";
1020         }
1021
1022         /* Close the resume loop. */
1023         out << "        }\n";
1024
1025         /* The if guarding on empty string. */
1026         if ( hasEnd )
1027                 out << "        }\n";
1028
1029         /* The execute block. */
1030         out << "        }\n";
1031 }
1032
1033 void JavaTabCodeGen::writeEOF()
1034 {
1035         if ( redFsm->anyEofActions() ) {
1036                 out <<
1037                         "       int _acts = " << EA() << "[" << CS() << "]" << ";\n"
1038                         "       int _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
1039                         "       while ( _nacts-- > 0 ) {\n"
1040                         "               switch ( " << A() << "[_acts++] ) {\n";
1041                         EOF_ACTION_SWITCH() <<
1042                         "               }\n"
1043                         "       }\n"
1044                         "\n";
1045         }
1046 }
1047
1048 std::ostream &JavaTabCodeGen::OPEN_ARRAY( string type, string name )
1049 {
1050         array_type = type;
1051         array_name = name;
1052         item_count = 0;
1053         div_count = 1;
1054
1055         out << 
1056                 "private static void init_" << name << "_0( " << type << "[] r )\n"
1057                 "{\n\t";
1058
1059         return out;
1060 }
1061
1062 std::ostream &JavaTabCodeGen::ARRAY_ITEM( string item, bool last )
1063 {
1064         out << "r[" << item_count << "]=" << item << "; ";
1065
1066         item_count += 1;
1067         
1068         if ( !last ) {
1069                 if ( item_count % SAIIC == 0 ) {
1070                         out << "\n}\n\n";
1071                         out << "private static void init_" << array_name << "_" << div_count << 
1072                                         "( " << array_type << "[] r )\n{\n\t";
1073                         div_count += 1;
1074                 }
1075                 else if ( item_count % IALL == 0 )
1076                         out << "\n\t";
1077         }
1078
1079         return out;
1080 }
1081
1082 std::ostream &JavaTabCodeGen::CLOSE_ARRAY()
1083 {
1084         out << "\n}\n\n";
1085
1086         out << 
1087                 "private static " << array_type << "[] create_" << array_name << "( )\n"
1088                 "{\n"
1089                 "       " << array_type << "[] r = new " << array_type << "[" << item_count << "];\n";
1090
1091         for ( int i = 0; i < div_count; i++ )
1092                 out << "        init_" << array_name << "_" << i << "( r );\n";
1093
1094         out <<
1095                 "       return r;\n"
1096                 "}\n"
1097                 "\n";
1098
1099         out << 
1100                 "private static final " << array_type << " " << array_name << 
1101                                 "[] = create_" << array_name << "();\n\n";
1102
1103         return out;
1104 }
1105
1106
1107 std::ostream &JavaTabCodeGen::STATIC_VAR( string type, string name )
1108 {
1109         out << "static final " << type << " " << name;
1110         return out;
1111 }
1112
1113 string JavaTabCodeGen::ARR_OFF( string ptr, string offset )
1114 {
1115         return ptr + " + " + offset;
1116 }
1117
1118 string JavaTabCodeGen::CAST( string type )
1119 {
1120         return "(" + type + ")";
1121 }
1122
1123 string JavaTabCodeGen::NULL_ITEM()
1124 {
1125         /* In java we use integers instead of pointers. */
1126         return "-1";
1127 }
1128
1129 string JavaTabCodeGen::GET_KEY()
1130 {
1131         ostringstream ret;
1132         if ( getKeyExpr != 0 ) { 
1133                 /* Emit the user supplied method of retrieving the key. */
1134                 ret << "(";
1135                 INLINE_LIST( ret, getKeyExpr, 0, false );
1136                 ret << ")";
1137         }
1138         else {
1139                 /* Expression for retrieving the key, use simple dereference. */
1140                 ret << "data[" << P() << "]";
1141         }
1142         return ret.str();
1143 }
1144
1145 string JavaTabCodeGen::CTRL_FLOW()
1146 {
1147         return "if (true) ";
1148 }
1149
1150 unsigned int JavaTabCodeGen::arrayTypeSize( unsigned long maxVal )
1151 {
1152         long long maxValLL = (long long) maxVal;
1153         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
1154         assert( arrayType != 0 );
1155         return arrayType->size;
1156 }
1157
1158 string JavaTabCodeGen::ARRAY_TYPE( unsigned long maxVal )
1159 {
1160         long long maxValLL = (long long) maxVal;
1161         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
1162         assert( arrayType != 0 );
1163
1164         string ret = arrayType->data1;
1165         if ( arrayType->data2 != 0 ) {
1166                 ret += " ";
1167                 ret += arrayType->data2;
1168         }
1169         return ret;
1170 }
1171
1172
1173 /* Write out the fsm name. */
1174 string JavaTabCodeGen::FSM_NAME()
1175 {
1176         return fsmName;
1177 }
1178
1179 /* Emit the offset of the start state as a decimal integer. */
1180 string JavaTabCodeGen::START_STATE_ID()
1181 {
1182         ostringstream ret;
1183         ret << redFsm->startState->id;
1184         return ret.str();
1185 };
1186
1187 /* Write out the array of actions. */
1188 std::ostream &JavaTabCodeGen::ACTIONS_ARRAY()
1189 {
1190         ARRAY_ITEM( INT(0), false );
1191         for ( ActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
1192                 /* Write out the length, which will never be the last character. */
1193                 ARRAY_ITEM( INT(act->key.length()), false );
1194
1195                 for ( ActionTable::Iter item = act->key; item.lte(); item++ )
1196                         ARRAY_ITEM( INT(item->value->actionId), (act.last() && item.last()) );
1197         }
1198         return out;
1199 }
1200
1201
1202 string JavaTabCodeGen::CS()
1203 {
1204         ostringstream ret;
1205         if ( curStateExpr != 0 ) { 
1206                 /* Emit the user supplied method of retrieving the key. */
1207                 ret << "(";
1208                 INLINE_LIST( ret, curStateExpr, 0, false );
1209                 ret << ")";
1210         }
1211         else {
1212                 /* Expression for retrieving the key, use simple dereference. */
1213                 ret << ACCESS() << "cs";
1214         }
1215         return ret.str();
1216 }
1217
1218 string JavaTabCodeGen::ACCESS()
1219 {
1220         ostringstream ret;
1221         if ( accessExpr != 0 )
1222                 INLINE_LIST( ret, accessExpr, 0, false );
1223         return ret.str();
1224 }
1225
1226 string JavaTabCodeGen::GET_WIDE_KEY()
1227 {
1228         if ( redFsm->anyConditions() ) 
1229                 return "_widec";
1230         else
1231                 return GET_KEY();
1232 }
1233
1234 string JavaTabCodeGen::GET_WIDE_KEY( RedStateAp *state )
1235 {
1236         if ( state->stateCondList.length() > 0 )
1237                 return "_widec";
1238         else
1239                 return GET_KEY();
1240 }
1241
1242 /* Write out level number of tabs. Makes the nested binary search nice
1243  * looking. */
1244 string JavaTabCodeGen::TABS( int level )
1245 {
1246         string result;
1247         while ( level-- > 0 )
1248                 result += "\t";
1249         return result;
1250 }
1251
1252 string JavaTabCodeGen::KEY( Key key )
1253 {
1254         ostringstream ret;
1255         if ( keyOps->isSigned || !hostLang->explicitUnsigned )
1256                 ret << key.getVal();
1257         else
1258                 ret << (unsigned long) key.getVal();
1259         return ret.str();
1260 }
1261
1262 string JavaTabCodeGen::INT( int i )
1263 {
1264         ostringstream ret;
1265         ret << i;
1266         return ret.str();
1267 }
1268
1269 void JavaTabCodeGen::LM_SWITCH( ostream &ret, InlineItem *item, 
1270                 int targState, int inFinish )
1271 {
1272         ret << 
1273                 "       switch( " << ACT() << " ) {\n";
1274
1275         /* If the switch handles error then we also forced the error state. It
1276          * will exist. */
1277         if ( item->handlesError ) {
1278                 ret << "        case 0: " << TOKEND() << " = " << TOKSTART() << "; ";
1279                 GOTO( ret, redFsm->errState->id, inFinish );
1280                 ret << "\n";
1281         }
1282
1283         for ( InlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
1284                 /* Write the case label, the action and the case break. */
1285                 ret << "        case " << lma->lmId << ":\n";
1286
1287                 /* Write the block and close it off. */
1288                 ret << "        {";
1289                 INLINE_LIST( ret, lma->children, targState, inFinish );
1290                 ret << "}\n";
1291
1292                 ret << "        break;\n";
1293         }
1294         /* Default required for D code. */
1295         ret << 
1296                 "       default: break;\n"
1297                 "       }\n"
1298                 "\t";
1299 }
1300
1301 void JavaTabCodeGen::SET_ACT( ostream &ret, InlineItem *item )
1302 {
1303         ret << ACT() << " = " << item->lmId << ";";
1304 }
1305
1306 void JavaTabCodeGen::SET_TOKEND( ostream &ret, InlineItem *item )
1307 {
1308         /* The tokend action sets tokend. */
1309         ret << TOKEND() << " = " << P();
1310         if ( item->offset != 0 ) 
1311                 out << "+" << item->offset;
1312         out << ";";
1313 }
1314
1315 void JavaTabCodeGen::GET_TOKEND( ostream &ret, InlineItem *item )
1316 {
1317         ret << TOKEND();
1318 }
1319
1320 void JavaTabCodeGen::INIT_TOKSTART( ostream &ret, InlineItem *item )
1321 {
1322         ret << TOKSTART() << " = " << NULL_ITEM() << ";";
1323 }
1324
1325 void JavaTabCodeGen::INIT_ACT( ostream &ret, InlineItem *item )
1326 {
1327         ret << ACT() << " = 0;";
1328 }
1329
1330 void JavaTabCodeGen::SET_TOKSTART( ostream &ret, InlineItem *item )
1331 {
1332         ret << TOKSTART() << " = " << P() << ";";
1333 }
1334
1335 void JavaTabCodeGen::SUB_ACTION( ostream &ret, InlineItem *item, 
1336                 int targState, bool inFinish )
1337 {
1338         if ( item->children->length() > 0 ) {
1339                 /* Write the block and close it off. */
1340                 ret << "{";
1341                 INLINE_LIST( ret, item->children, targState, inFinish );
1342                 ret << "}";
1343         }
1344 }
1345
1346 void JavaTabCodeGen::ACTION( ostream &ret, Action *action, int targState, bool inFinish )
1347 {
1348         /* Write the preprocessor line info for going into the source file. */
1349         lineDirective( ret, sourceFileName, action->loc.line );
1350
1351         /* Write the block and close it off. */
1352         ret << "\t{";
1353         INLINE_LIST( ret, action->inlineList, targState, inFinish );
1354         ret << "}\n";
1355 }
1356
1357 void JavaTabCodeGen::CONDITION( ostream &ret, Action *condition )
1358 {
1359         ret << "\n";
1360         lineDirective( ret, sourceFileName, condition->loc.line );
1361         INLINE_LIST( ret, condition->inlineList, 0, false );
1362 }
1363
1364 string JavaTabCodeGen::ERROR_STATE()
1365 {
1366         ostringstream ret;
1367         if ( redFsm->errState != 0 )
1368                 ret << redFsm->errState->id;
1369         else
1370                 ret << "-1";
1371         return ret.str();
1372 }
1373
1374 string JavaTabCodeGen::FIRST_FINAL_STATE()
1375 {
1376         ostringstream ret;
1377         if ( redFsm->firstFinState != 0 )
1378                 ret << redFsm->firstFinState->id;
1379         else
1380                 ret << redFsm->nextStateId;
1381         return ret.str();
1382 }
1383
1384 void JavaTabCodeGen::writeInit()
1385 {
1386         out << "        {\n";
1387
1388         if ( writeCS )
1389                 out << "\t" << CS() << " = " << START() << ";\n";
1390         
1391         /* If there are any calls, then the stack top needs initialization. */
1392         if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
1393                 out << "\t" << TOP() << " = 0;\n";
1394
1395         if ( hasLongestMatch ) {
1396                 out << 
1397                         "       " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
1398                         "       " << TOKEND() << " = " << NULL_ITEM() << ";\n"
1399                         "       " << ACT() << " = 0;\n";
1400         }
1401         out << "        }\n";
1402 }
1403
1404 void JavaTabCodeGen::finishRagelDef()
1405 {
1406         /* The frontend will do this for us, but it may be a good idea to force it
1407          * if the intermediate file is edited. */
1408         redFsm->sortByStateId();
1409
1410         /* Choose default transitions and the single transition. */
1411         redFsm->chooseDefaultSpan();
1412                 
1413         /* Maybe do flat expand, otherwise choose single. */
1414         redFsm->chooseSingle();
1415
1416         /* If any errors have occured in the input file then don't write anything. */
1417         if ( gblErrorCount > 0 )
1418                 return;
1419         
1420         /* Anlayze Machine will find the final action reference counts, among
1421          * other things. We will use these in reporting the usage
1422          * of fsm directives in action code. */
1423         analyzeMachine();
1424
1425         /* Determine if we should use indicies. */
1426         calcIndexSize();
1427 }
1428
1429 ostream &JavaTabCodeGen::source_warning( const InputLoc &loc )
1430 {
1431         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
1432         return cerr;
1433 }
1434
1435 ostream &JavaTabCodeGen::source_error( const InputLoc &loc )
1436 {
1437         gblErrorCount += 1;
1438         assert( sourceFileName != 0 );
1439         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
1440         return cerr;
1441 }
1442
1443