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