Some more cleanup. Put a pointer to CodeGenData in ParseData and eliminate codeGenMap...
[external/ragel.git] / ragel / cdcodegen.h
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
3  *            2004 Erich Ocean <eric.ocean@ampede.com>
4  *            2005 Alan West <alan@alanz.com>
5  */
6
7 /*  This file is part of Ragel.
8  *
9  *  Ragel is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  * 
14  *  Ragel is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  * 
19  *  You should have received a copy of the GNU General Public License
20  *  along with Ragel; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
22  */
23
24 #ifndef _CDCODEGEN_H
25 #define _CDCODEGEN_H
26
27 #include <iostream>
28 #include <string>
29 #include <stdio.h>
30 #include "common.h"
31 #include "gendata.h"
32
33 using std::string;
34 using std::ostream;
35
36 /* Integer array line length. */
37 #define IALL 8
38
39 /* Forwards. */
40 struct RedFsmAp;
41 struct RedStateAp;
42 struct CodeGenData;
43 struct GenAction;
44 struct NameInst;
45 struct GenInlineItem;
46 struct GenInlineList;
47 struct RedAction;
48 struct LongestMatch;
49 struct LongestMatchPart;
50
51 inline string itoa( int i )
52 {
53         char buf[16];
54         sprintf( buf, "%i", i );
55         return buf;
56 }
57
58 /*
59  * class FsmCodeGen
60  */
61 class FsmCodeGen : public CodeGenData
62 {
63 public:
64         FsmCodeGen( ostream &out );
65         virtual ~FsmCodeGen() {}
66
67         virtual void finishRagelDef();
68         virtual void writeInit();
69         virtual void writeStart();
70         virtual void writeFirstFinal();
71         virtual void writeError();
72
73 protected:
74         string FSM_NAME();
75         string START_STATE_ID();
76         ostream &ACTIONS_ARRAY();
77         string GET_WIDE_KEY();
78         string GET_WIDE_KEY( RedStateAp *state );
79         string TABS( int level );
80         string KEY( Key key );
81         string LDIR_PATH( char *path );
82         void ACTION( ostream &ret, GenAction *action, int targState, 
83                         bool inFinish, bool csForced );
84         void CONDITION( ostream &ret, GenAction *condition );
85         string ALPH_TYPE();
86         string WIDE_ALPH_TYPE();
87         string ARRAY_TYPE( unsigned long maxVal );
88
89         virtual string ARR_OFF( string ptr, string offset ) = 0;
90         virtual string CAST( string type ) = 0;
91         virtual string UINT() = 0;
92         virtual string NULL_ITEM() = 0;
93         virtual string POINTER() = 0;
94         virtual string GET_KEY();
95         virtual ostream &SWITCH_DEFAULT() = 0;
96
97         string P();
98         string PE();
99         string EOFV();
100
101         string ACCESS();
102         string CS();
103         string STACK();
104         string TOP();
105         string TOKSTART();
106         string TOKEND();
107         string ACT();
108
109         string DATA_PREFIX();
110         string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
111         string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
112         string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
113         string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
114         string I() { return "_" + DATA_PREFIX() + "indicies"; }
115         string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
116         string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
117         string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
118         string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
119         string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
120         string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
121         string A() { return "_" + DATA_PREFIX() + "actions"; }
122         string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
123         string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
124         string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
125         string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
126         string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
127         string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
128         string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
129         string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
130         string START() { return DATA_PREFIX() + "start"; }
131         string ERROR() { return DATA_PREFIX() + "error"; }
132         string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
133         string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
134
135         void INLINE_LIST( ostream &ret, GenInlineList *inlineList, 
136                         int targState, bool inFinish, bool csForced );
137         virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
138         virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
139         virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
140         virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
141         virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
142         virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, 
143                         int targState, bool inFinish ) = 0;
144         virtual void RET( ostream &ret, bool inFinish ) = 0;
145         virtual void BREAK( ostream &ret, int targState, bool csForced ) = 0;
146         virtual void CURS( ostream &ret, bool inFinish ) = 0;
147         virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
148         void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
149         void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, 
150                         int inFinish, bool csForced );
151         void SET_ACT( ostream &ret, GenInlineItem *item );
152         void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
153         void INIT_ACT( ostream &ret, GenInlineItem *item );
154         void SET_TOKSTART( ostream &ret, GenInlineItem *item );
155         void SET_TOKEND( ostream &ret, GenInlineItem *item );
156         void GET_TOKEND( ostream &ret, GenInlineItem *item );
157         void SUB_ACTION( ostream &ret, GenInlineItem *item, 
158                         int targState, bool inFinish, bool csForced );
159         void STATE_IDS();
160
161         string ERROR_STATE();
162         string FIRST_FINAL_STATE();
163
164         virtual string PTR_CONST() = 0;
165         virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
166         virtual ostream &CLOSE_ARRAY() = 0;
167         virtual ostream &STATIC_VAR( string type, string name ) = 0;
168
169         virtual string CTRL_FLOW() = 0;
170
171         ostream &source_warning(const InputLoc &loc);
172         ostream &source_error(const InputLoc &loc);
173
174         unsigned int arrayTypeSize( unsigned long maxVal );
175
176         bool outLabelUsed;
177         bool testEofUsed;
178         bool againLabelUsed;
179         bool useIndicies;
180
181         void genLineDirective( ostream &out );
182
183 public:
184         /* Determine if we should use indicies. */
185         virtual void calcIndexSize() {}
186 };
187
188 class CCodeGen : virtual public FsmCodeGen
189 {
190 public:
191         CCodeGen( ostream &out ) : FsmCodeGen(out) {}
192
193         virtual string NULL_ITEM();
194         virtual string POINTER();
195         virtual ostream &SWITCH_DEFAULT();
196         virtual ostream &OPEN_ARRAY( string type, string name );
197         virtual ostream &CLOSE_ARRAY();
198         virtual ostream &STATIC_VAR( string type, string name );
199         virtual string ARR_OFF( string ptr, string offset );
200         virtual string CAST( string type );
201         virtual string UINT();
202         virtual string PTR_CONST();
203         virtual string CTRL_FLOW();
204
205         virtual void writeExports();
206 };
207
208 class DCodeGen : virtual public FsmCodeGen
209 {
210 public:
211         DCodeGen( ostream &out ) : FsmCodeGen(out) {}
212
213         virtual string NULL_ITEM();
214         virtual string POINTER();
215         virtual ostream &SWITCH_DEFAULT();
216         virtual ostream &OPEN_ARRAY( string type, string name );
217         virtual ostream &CLOSE_ARRAY();
218         virtual ostream &STATIC_VAR( string type, string name );
219         virtual string ARR_OFF( string ptr, string offset );
220         virtual string CAST( string type );
221         virtual string UINT();
222         virtual string PTR_CONST();
223         virtual string CTRL_FLOW();
224
225         virtual void writeExports();
226 };
227
228 #endif