Added tests for exports and entry points. For machine exports, added "ex_" in
[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] )\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         STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
894
895         if ( writeFirstFinal )
896                 STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
897
898         if ( writeErr )
899                 STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
900         
901         out << "\n";
902
903         if ( entryPointNames.length() > 0 ) {
904                 for ( EntryNameVect::Iter en = entryPointNames; en.lte(); en++ ) {
905                         STATIC_VAR( "int", DATA_PREFIX() + "en_" + *en ) << 
906                                         " = " << entryPointIds[en.pos()] << ";\n";
907                 }
908                 out << "\n";
909         }
910 }
911
912 void JavaTabCodeGen::writeExec()
913 {
914         out <<
915                 "       {\n"
916                 "       int _klen";
917
918         if ( redFsm->anyRegCurStateRef() )
919                 out << ", _ps";
920
921         out << 
922                 ";\n"
923                 "       int _trans;\n";
924
925         if ( redFsm->anyConditions() )
926                 out << "        int _widec;\n";
927
928         if ( redFsm->anyToStateActions() || redFsm->anyRegActions() || 
929                         redFsm->anyFromStateActions() )
930         {
931                 out << 
932                         "       int _acts;\n"
933                         "       int _nacts;\n";
934         }
935
936         out <<
937                 "       int _keys;\n"
938                 "\n";
939
940         if ( hasEnd )
941                 out << "        if ( " << P() << " != " << PE() << " ) {\n";
942
943         out << "        _resume: while ( true ) {\n";
944
945         out << "        _again: do {\n";
946
947         if ( redFsm->errState != 0 ) {
948                 out << 
949                         "       if ( " << CS() << " == " << redFsm->errState->id << " )\n"
950                         "               break _resume;\n";
951         }
952
953         if ( redFsm->anyFromStateActions() ) {
954                 out <<
955                         "       _acts = " << FSA() << "[" << CS() << "]" << ";\n"
956                         "       _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
957                         "       while ( _nacts-- > 0 ) {\n"
958                         "               switch ( " << A() << "[_acts++] ) {\n";
959                         FROM_STATE_ACTION_SWITCH() <<
960                         "               }\n"
961                         "       }\n"
962                         "\n";
963         }
964
965         if ( redFsm->anyConditions() )
966                 COND_TRANSLATE();
967
968         LOCATE_TRANS();
969
970         if ( redFsm->anyRegCurStateRef() )
971                 out << "        _ps = " << CS() << ";\n";
972
973         if ( useIndicies )
974                 out << "        _trans = " << I() << "[_trans];\n";
975
976         out <<
977                 "       " << CS() << " = " << TT() << "[_trans];\n"
978                 "\n";
979
980         if ( redFsm->anyRegActions() ) {
981                 out <<
982                         "       if ( " << TA() << "[_trans] == 0 )\n"
983                         "               break _again;\n"
984                         "\n"
985                         "       _acts = " <<  TA() << "[_trans]" << ";\n"
986                         "       _nacts = " << CAST("int") << " " <<  A() << "[_acts++];\n"
987                         "       while ( _nacts-- > 0 )\n        {\n"
988                         "               switch ( " << A() << "[_acts++] )\n"
989                         "               {\n";
990                         ACTION_SWITCH() <<
991                         "               }\n"
992                         "       }\n"
993                         "\n";
994         }
995
996         /* Again loop, functions as again label. */
997         out << "        } while (false);\n";
998
999         if ( redFsm->anyToStateActions() ) {
1000                 out <<
1001                         "       _acts = " << TSA() << "[" << CS() << "]" << ";\n"
1002                         "       _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
1003                         "       while ( _nacts-- > 0 ) {\n"
1004                         "               switch ( " << A() << "[_acts++] ) {\n";
1005                         TO_STATE_ACTION_SWITCH() <<
1006                         "               }\n"
1007                         "       }\n"
1008                         "\n";
1009         }
1010
1011         if ( hasEnd ) {
1012                 out << 
1013                         "       if ( ++" << P() << " == " << PE() << " )\n"
1014                         "               break _resume;\n";
1015         }
1016         else {
1017                 out << 
1018                         "       " << P() << " += 1;\n";
1019         }
1020
1021         /* Close the resume loop. */
1022         out << "        }\n";
1023
1024         /* The if guarding on empty string. */
1025         if ( hasEnd )
1026                 out << "        }\n";
1027
1028         /* The execute block. */
1029         out << "        }\n";
1030 }
1031
1032 void JavaTabCodeGen::writeEOF()
1033 {
1034         if ( redFsm->anyEofActions() ) {
1035                 out <<
1036                         "       int _acts = " << EA() << "[" << CS() << "]" << ";\n"
1037                         "       int _nacts = " << CAST("int") << " " << A() << "[_acts++];\n"
1038                         "       while ( _nacts-- > 0 ) {\n"
1039                         "               switch ( " << A() << "[_acts++] ) {\n";
1040                         EOF_ACTION_SWITCH() <<
1041                         "               }\n"
1042                         "       }\n"
1043                         "\n";
1044         }
1045 }
1046
1047 std::ostream &JavaTabCodeGen::OPEN_ARRAY( string type, string name )
1048 {
1049         array_type = type;
1050         array_name = name;
1051         item_count = 0;
1052         div_count = 1;
1053
1054         out << 
1055                 "private static void init_" << name << "_0( " << type << "[] r )\n"
1056                 "{\n\t";
1057
1058         return out;
1059 }
1060
1061 std::ostream &JavaTabCodeGen::ARRAY_ITEM( string item, bool last )
1062 {
1063         out << "r[" << item_count << "]=" << item << "; ";
1064
1065         item_count += 1;
1066         
1067         if ( !last ) {
1068                 if ( item_count % SAIIC == 0 ) {
1069                         out << "\n}\n\n";
1070                         out << "private static void init_" << array_name << "_" << div_count << 
1071                                         "( " << array_type << "[] r )\n{\n\t";
1072                         div_count += 1;
1073                 }
1074                 else if ( item_count % IALL == 0 )
1075                         out << "\n\t";
1076         }
1077
1078         return out;
1079 }
1080
1081 std::ostream &JavaTabCodeGen::CLOSE_ARRAY()
1082 {
1083         out << "\n}\n\n";
1084
1085         out << 
1086                 "private static " << array_type << "[] create_" << array_name << "( )\n"
1087                 "{\n"
1088                 "       " << array_type << "[] r = new " << array_type << "[" << item_count << "];\n";
1089
1090         for ( int i = 0; i < div_count; i++ )
1091                 out << "        init_" << array_name << "_" << i << "( r );\n";
1092
1093         out <<
1094                 "       return r;\n"
1095                 "}\n"
1096                 "\n";
1097
1098         out << 
1099                 "private static final " << array_type << " " << array_name << 
1100                                 "[] = create_" << array_name << "();\n\n";
1101
1102         return out;
1103 }
1104
1105
1106 std::ostream &JavaTabCodeGen::STATIC_VAR( string type, string name )
1107 {
1108         out << "static final " << type << " " << name;
1109         return out;
1110 }
1111
1112 string JavaTabCodeGen::ARR_OFF( string ptr, string offset )
1113 {
1114         return ptr + " + " + offset;
1115 }
1116
1117 string JavaTabCodeGen::CAST( string type )
1118 {
1119         return "(" + type + ")";
1120 }
1121
1122 string JavaTabCodeGen::NULL_ITEM()
1123 {
1124         /* In java we use integers instead of pointers. */
1125         return "-1";
1126 }
1127
1128 string JavaTabCodeGen::GET_KEY()
1129 {
1130         ostringstream ret;
1131         if ( getKeyExpr != 0 ) { 
1132                 /* Emit the user supplied method of retrieving the key. */
1133                 ret << "(";
1134                 INLINE_LIST( ret, getKeyExpr, 0, false );
1135                 ret << ")";
1136         }
1137         else {
1138                 /* Expression for retrieving the key, use simple dereference. */
1139                 ret << "data[" << P() << "]";
1140         }
1141         return ret.str();
1142 }
1143
1144 string JavaTabCodeGen::CTRL_FLOW()
1145 {
1146         return "if (true) ";
1147 }
1148
1149 unsigned int JavaTabCodeGen::arrayTypeSize( unsigned long maxVal )
1150 {
1151         long long maxValLL = (long long) maxVal;
1152         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
1153         assert( arrayType != 0 );
1154         return arrayType->size;
1155 }
1156
1157 string JavaTabCodeGen::ARRAY_TYPE( unsigned long maxVal )
1158 {
1159         long long maxValLL = (long long) maxVal;
1160         HostType *arrayType = keyOps->typeSubsumes( maxValLL );
1161         assert( arrayType != 0 );
1162
1163         string ret = arrayType->data1;
1164         if ( arrayType->data2 != 0 ) {
1165                 ret += " ";
1166                 ret += arrayType->data2;
1167         }
1168         return ret;
1169 }
1170
1171
1172 /* Write out the fsm name. */
1173 string JavaTabCodeGen::FSM_NAME()
1174 {
1175         return fsmName;
1176 }
1177
1178 /* Emit the offset of the start state as a decimal integer. */
1179 string JavaTabCodeGen::START_STATE_ID()
1180 {
1181         ostringstream ret;
1182         ret << redFsm->startState->id;
1183         return ret.str();
1184 };
1185
1186 /* Write out the array of actions. */
1187 std::ostream &JavaTabCodeGen::ACTIONS_ARRAY()
1188 {
1189         ARRAY_ITEM( INT(0), false );
1190         for ( ActionTableMap::Iter act = redFsm->actionMap; act.lte(); act++ ) {
1191                 /* Write out the length, which will never be the last character. */
1192                 ARRAY_ITEM( INT(act->key.length()), false );
1193
1194                 for ( ActionTable::Iter item = act->key; item.lte(); item++ )
1195                         ARRAY_ITEM( INT(item->value->actionId), (act.last() && item.last()) );
1196         }
1197         return out;
1198 }
1199
1200
1201 string JavaTabCodeGen::CS()
1202 {
1203         ostringstream ret;
1204         if ( curStateExpr != 0 ) { 
1205                 /* Emit the user supplied method of retrieving the key. */
1206                 ret << "(";
1207                 INLINE_LIST( ret, curStateExpr, 0, false );
1208                 ret << ")";
1209         }
1210         else {
1211                 /* Expression for retrieving the key, use simple dereference. */
1212                 ret << ACCESS() << "cs";
1213         }
1214         return ret.str();
1215 }
1216
1217 string JavaTabCodeGen::ACCESS()
1218 {
1219         ostringstream ret;
1220         if ( accessExpr != 0 )
1221                 INLINE_LIST( ret, accessExpr, 0, false );
1222         return ret.str();
1223 }
1224
1225 string JavaTabCodeGen::GET_WIDE_KEY()
1226 {
1227         if ( redFsm->anyConditions() ) 
1228                 return "_widec";
1229         else
1230                 return GET_KEY();
1231 }
1232
1233 string JavaTabCodeGen::GET_WIDE_KEY( RedStateAp *state )
1234 {
1235         if ( state->stateCondList.length() > 0 )
1236                 return "_widec";
1237         else
1238                 return GET_KEY();
1239 }
1240
1241 /* Write out level number of tabs. Makes the nested binary search nice
1242  * looking. */
1243 string JavaTabCodeGen::TABS( int level )
1244 {
1245         string result;
1246         while ( level-- > 0 )
1247                 result += "\t";
1248         return result;
1249 }
1250
1251 string JavaTabCodeGen::KEY( Key key )
1252 {
1253         ostringstream ret;
1254         if ( keyOps->isSigned || !hostLang->explicitUnsigned )
1255                 ret << key.getVal();
1256         else
1257                 ret << (unsigned long) key.getVal();
1258         return ret.str();
1259 }
1260
1261 string JavaTabCodeGen::INT( int i )
1262 {
1263         ostringstream ret;
1264         ret << i;
1265         return ret.str();
1266 }
1267
1268 void JavaTabCodeGen::LM_SWITCH( ostream &ret, InlineItem *item, 
1269                 int targState, int inFinish )
1270 {
1271         ret << 
1272                 "       switch( " << ACT() << " ) {\n";
1273
1274         /* If the switch handles error then we also forced the error state. It
1275          * will exist. */
1276         if ( item->handlesError ) {
1277                 ret << "        case 0: " << TOKEND() << " = " << TOKSTART() << "; ";
1278                 GOTO( ret, redFsm->errState->id, inFinish );
1279                 ret << "\n";
1280         }
1281
1282         for ( InlineList::Iter lma = *item->children; lma.lte(); lma++ ) {
1283                 /* Write the case label, the action and the case break. */
1284                 ret << "        case " << lma->lmId << ":\n";
1285
1286                 /* Write the block and close it off. */
1287                 ret << "        {";
1288                 INLINE_LIST( ret, lma->children, targState, inFinish );
1289                 ret << "}\n";
1290
1291                 ret << "        break;\n";
1292         }
1293         /* Default required for D code. */
1294         ret << 
1295                 "       default: break;\n"
1296                 "       }\n"
1297                 "\t";
1298 }
1299
1300 void JavaTabCodeGen::SET_ACT( ostream &ret, InlineItem *item )
1301 {
1302         ret << ACT() << " = " << item->lmId << ";";
1303 }
1304
1305 void JavaTabCodeGen::SET_TOKEND( ostream &ret, InlineItem *item )
1306 {
1307         /* The tokend action sets tokend. */
1308         ret << TOKEND() << " = " << P();
1309         if ( item->offset != 0 ) 
1310                 out << "+" << item->offset;
1311         out << ";";
1312 }
1313
1314 void JavaTabCodeGen::GET_TOKEND( ostream &ret, InlineItem *item )
1315 {
1316         ret << TOKEND();
1317 }
1318
1319 void JavaTabCodeGen::INIT_TOKSTART( ostream &ret, InlineItem *item )
1320 {
1321         ret << TOKSTART() << " = " << NULL_ITEM() << ";";
1322 }
1323
1324 void JavaTabCodeGen::INIT_ACT( ostream &ret, InlineItem *item )
1325 {
1326         ret << ACT() << " = 0;";
1327 }
1328
1329 void JavaTabCodeGen::SET_TOKSTART( ostream &ret, InlineItem *item )
1330 {
1331         ret << TOKSTART() << " = " << P() << ";";
1332 }
1333
1334 void JavaTabCodeGen::SUB_ACTION( ostream &ret, InlineItem *item, 
1335                 int targState, bool inFinish )
1336 {
1337         if ( item->children->length() > 0 ) {
1338                 /* Write the block and close it off. */
1339                 ret << "{";
1340                 INLINE_LIST( ret, item->children, targState, inFinish );
1341                 ret << "}";
1342         }
1343 }
1344
1345 void JavaTabCodeGen::ACTION( ostream &ret, Action *action, int targState, bool inFinish )
1346 {
1347         /* Write the preprocessor line info for going into the source file. */
1348         lineDirective( ret, sourceFileName, action->loc.line );
1349
1350         /* Write the block and close it off. */
1351         ret << "\t{";
1352         INLINE_LIST( ret, action->inlineList, targState, inFinish );
1353         ret << "}\n";
1354 }
1355
1356 void JavaTabCodeGen::CONDITION( ostream &ret, Action *condition )
1357 {
1358         ret << "\n";
1359         lineDirective( ret, sourceFileName, condition->loc.line );
1360         INLINE_LIST( ret, condition->inlineList, 0, false );
1361 }
1362
1363 string JavaTabCodeGen::ERROR_STATE()
1364 {
1365         ostringstream ret;
1366         if ( redFsm->errState != 0 )
1367                 ret << redFsm->errState->id;
1368         else
1369                 ret << "-1";
1370         return ret.str();
1371 }
1372
1373 string JavaTabCodeGen::FIRST_FINAL_STATE()
1374 {
1375         ostringstream ret;
1376         if ( redFsm->firstFinState != 0 )
1377                 ret << redFsm->firstFinState->id;
1378         else
1379                 ret << redFsm->nextStateId;
1380         return ret.str();
1381 }
1382
1383 void JavaTabCodeGen::writeInit()
1384 {
1385         out << "        {\n";
1386         out << "\t" << CS() << " = " << START() << ";\n";
1387         
1388         /* If there are any calls, then the stack top needs initialization. */
1389         if ( redFsm->anyActionCalls() || redFsm->anyActionRets() )
1390                 out << "\t" << TOP() << " = 0;\n";
1391
1392         if ( hasLongestMatch ) {
1393                 out << 
1394                         "       " << TOKSTART() << " = " << NULL_ITEM() << ";\n"
1395                         "       " << TOKEND() << " = " << NULL_ITEM() << ";\n"
1396                         "       " << ACT() << " = 0;\n";
1397         }
1398         out << "        }\n";
1399 }
1400
1401 void JavaTabCodeGen::finishRagelDef()
1402 {
1403         /* The frontend will do this for us, but it may be a good idea to force it
1404          * if the intermediate file is edited. */
1405         redFsm->sortByStateId();
1406
1407         /* Choose default transitions and the single transition. */
1408         redFsm->chooseDefaultSpan();
1409                 
1410         /* Maybe do flat expand, otherwise choose single. */
1411         redFsm->chooseSingle();
1412
1413         /* If any errors have occured in the input file then don't write anything. */
1414         if ( gblErrorCount > 0 )
1415                 return;
1416         
1417         /* Anlayze Machine will find the final action reference counts, among
1418          * other things. We will use these in reporting the usage
1419          * of fsm directives in action code. */
1420         analyzeMachine();
1421
1422         /* Determine if we should use indicies. */
1423         calcIndexSize();
1424 }
1425
1426 ostream &JavaTabCodeGen::source_warning( const InputLoc &loc )
1427 {
1428         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": warning: ";
1429         return cerr;
1430 }
1431
1432 ostream &JavaTabCodeGen::source_error( const InputLoc &loc )
1433 {
1434         gblErrorCount += 1;
1435         assert( sourceFileName != 0 );
1436         cerr << sourceFileName << ":" << loc.line << ":" << loc.col << ": ";
1437         return cerr;
1438 }
1439
1440