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 #ifndef _JAVACODEGEN_H
24 #define _JAVACODEGEN_H
25 
26 #include <iostream>
27 #include <string>
28 #include <stdio.h>
29 #include "common.h"
30 #include "gendata.h"
31 
32 using std::string;
33 using std::ostream;
34 
35 /*
36  * JavaTabCodeGen
37  */
38 struct JavaTabCodeGen : public CodeGenData
39 {
JavaTabCodeGenJavaTabCodeGen40 	JavaTabCodeGen( ostream &out ) :
41 		CodeGenData(out) {}
42 
43 	std::ostream &TO_STATE_ACTION_SWITCH();
44 	std::ostream &FROM_STATE_ACTION_SWITCH();
45 	std::ostream &EOF_ACTION_SWITCH();
46 	std::ostream &ACTION_SWITCH();
47 
48 	std::ostream &COND_KEYS();
49 	std::ostream &COND_SPACES();
50 	std::ostream &KEYS();
51 	std::ostream &INDICIES();
52 	std::ostream &COND_OFFSETS();
53 	std::ostream &KEY_OFFSETS();
54 	std::ostream &INDEX_OFFSETS();
55 	std::ostream &COND_LENS();
56 	std::ostream &SINGLE_LENS();
57 	std::ostream &RANGE_LENS();
58 	std::ostream &TO_STATE_ACTIONS();
59 	std::ostream &FROM_STATE_ACTIONS();
60 	std::ostream &EOF_ACTIONS();
61 	std::ostream &EOF_TRANS();
62 	std::ostream &TRANS_TARGS();
63 	std::ostream &TRANS_ACTIONS();
64 	std::ostream &TRANS_TARGS_WI();
65 	std::ostream &TRANS_ACTIONS_WI();
66 
67 	void BREAK( ostream &ret, int targState );
68 	void GOTO( ostream &ret, int gotoDest, bool inFinish );
69 	void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish );
70 	void CALL( ostream &ret, int callDest, int targState, bool inFinish );
71 	void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
72 	void RET( ostream &ret, bool inFinish );
73 
74 	void COND_TRANSLATE();
75 	void LOCATE_TRANS();
76 
77 	virtual void writeExec();
78 	virtual void writeData();
79 	virtual void writeInit();
80 	virtual void writeExports();
81 	virtual void writeStart();
82 	virtual void writeFirstFinal();
83 	virtual void writeError();
84 	virtual void finishRagelDef();
85 
86 	void NEXT( ostream &ret, int nextDest, bool inFinish );
87 	void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish );
88 
89 	int TO_STATE_ACTION( RedStateAp *state );
90 	int FROM_STATE_ACTION( RedStateAp *state );
91 	int EOF_ACTION( RedStateAp *state );
92 	int TRANS_ACTION( RedTransAp *trans );
93 
94 	/* Determine if we should use indicies. */
95 	void calcIndexSize();
96 
97 private:
98 	string array_type;
99 	string array_name;
100 	int item_count;
101 	int div_count;
102 
103 public:
104 
105 	virtual string NULL_ITEM();
106 	virtual ostream &OPEN_ARRAY( string type, string name );
107 	virtual ostream &ARRAY_ITEM( string item, bool last );
108 	virtual ostream &CLOSE_ARRAY();
109 	virtual ostream &STATIC_VAR( string type, string name );
110 	virtual string ARR_OFF( string ptr, string offset );
111 	virtual string CAST( string type );
112 	virtual string GET_KEY();
113 	virtual string CTRL_FLOW();
114 
115 	string FSM_NAME();
116 	string START_STATE_ID();
117 	ostream &ACTIONS_ARRAY();
118 	string GET_WIDE_KEY();
119 	string GET_WIDE_KEY( RedStateAp *state );
120 	string TABS( int level );
121 	string KEY( Key key );
122 	string INT( int i );
123 	void ACTION( ostream &ret, GenAction *action, int targState, bool inFinish );
124 	void CONDITION( ostream &ret, GenAction *condition );
125 	string ALPH_TYPE();
126 	string WIDE_ALPH_TYPE();
127 	string ARRAY_TYPE( unsigned long maxVal );
128 
129 	string ACCESS();
130 
131 	string P();
132 	string PE();
133 	string vEOF();
134 
135 	string vCS();
136 	string STACK();
137 	string TOP();
138 	string TOKSTART();
139 	string TOKEND();
140 	string ACT();
141 	string DATA();
142 
143 	string DATA_PREFIX();
PMJavaTabCodeGen144 	string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
CJavaTabCodeGen145 	string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
CKJavaTabCodeGen146 	string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
KJavaTabCodeGen147 	string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
IJavaTabCodeGen148 	string I() { return "_" + DATA_PREFIX() + "indicies"; }
COJavaTabCodeGen149 	string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
KOJavaTabCodeGen150 	string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
IOJavaTabCodeGen151 	string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
CLJavaTabCodeGen152 	string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
SLJavaTabCodeGen153 	string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
RLJavaTabCodeGen154 	string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
AJavaTabCodeGen155 	string A() { return "_" + DATA_PREFIX() + "actions"; }
TAJavaTabCodeGen156 	string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
TTJavaTabCodeGen157 	string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
TSAJavaTabCodeGen158 	string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
FSAJavaTabCodeGen159 	string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
EAJavaTabCodeGen160 	string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
ETJavaTabCodeGen161 	string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
SPJavaTabCodeGen162 	string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
CSPJavaTabCodeGen163 	string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
STARTJavaTabCodeGen164 	string START() { return DATA_PREFIX() + "start"; }
ERRORJavaTabCodeGen165 	string ERROR() { return DATA_PREFIX() + "error"; }
FIRST_FINALJavaTabCodeGen166 	string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
CTXDATAJavaTabCodeGen167 	string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
168 
169 	void INLINE_LIST( ostream &ret, GenInlineList *inlineList, int targState, bool inFinish );
170 	void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
171 	void EXECTE( ostream &ret, GenInlineItem *item, int targState, int inFinish );
172 	void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, int inFinish );
173 	void SET_ACT( ostream &ret, GenInlineItem *item );
174 	void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
175 	void INIT_ACT( ostream &ret, GenInlineItem *item );
176 	void SET_TOKSTART( ostream &ret, GenInlineItem *item );
177 	void SET_TOKEND( ostream &ret, GenInlineItem *item );
178 	void GET_TOKEND( ostream &ret, GenInlineItem *item );
179 	void SUB_ACTION( ostream &ret, GenInlineItem *item,
180 			int targState, bool inFinish );
181 
182 	string ERROR_STATE();
183 	string FIRST_FINAL_STATE();
184 
185 	ostream &source_warning(const InputLoc &loc);
186 	ostream &source_error(const InputLoc &loc);
187 
188 	unsigned int arrayTypeSize( unsigned long maxVal );
189 
190 	bool outLabelUsed;
191 	bool againLabelUsed;
192 	bool useIndicies;
193 
194 	void genLineDirective( ostream &out );
195 };
196 
197 #endif
198