Split dotfile generation out into its own executable.
[external/ragel.git] / rlgen-cd / fsmcodegen.h
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
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 _FSMCODEGEN_H
25 #define _FSMCODEGEN_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 Action;
44 struct NameInst;
45 struct InlineItem;
46 struct InlineList;
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 writeOutInit();
69
70 protected:
71         string FSM_NAME();
72         string START_STATE_ID();
73         ostream &ACTIONS_ARRAY();
74         string GET_WIDE_KEY();
75         string GET_WIDE_KEY( RedStateAp *state );
76         string TABS( int level );
77         string KEY( Key key );
78         string LDIR_PATH( char *path );
79         void ACTION( ostream &ret, Action *action, int targState, bool inFinish );
80         void CONDITION( ostream &ret, Action *condition );
81         string ALPH_TYPE();
82         string WIDE_ALPH_TYPE();
83         string ARRAY_TYPE( unsigned long maxVal );
84
85         virtual string ARR_OFF( string ptr, string offset ) = 0;
86         virtual string CAST( string type ) = 0;
87         virtual string UINT() = 0;
88         virtual string NULL_ITEM() = 0;
89         virtual string POINTER() = 0;
90         virtual string GET_KEY();
91         virtual ostream &SWITCH_DEFAULT() = 0;
92
93         string P() { return "p"; }
94         string PE() { return "pe"; }
95
96         string ACCESS();
97         string CS();
98         string STACK() { return ACCESS() + "stack"; }
99         string TOP() { return ACCESS() + "top"; }
100         string TOKSTART() { return ACCESS() + "tokstart"; }
101         string TOKEND() { return ACCESS() + "tokend"; }
102         string ACT() { return ACCESS() + "act"; }
103
104         string DATA_PREFIX();
105         string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
106         string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
107         string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
108         string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
109         string I() { return "_" + DATA_PREFIX() + "indicies"; }
110         string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
111         string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
112         string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
113         string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
114         string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
115         string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
116         string A() { return "_" + DATA_PREFIX() + "actions"; }
117         string TA() { return "_" + DATA_PREFIX() + "trans_actions_wi"; }
118         string TT() { return "_" + DATA_PREFIX() + "trans_targs_wi"; }
119         string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
120         string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
121         string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
122         string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
123         string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
124         string START() { return DATA_PREFIX() + "start"; }
125         string ERROR() { return DATA_PREFIX() + "error"; }
126         string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
127         string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
128
129         void INLINE_LIST( ostream &ret, InlineList *inlineList, int targState, bool inFinish );
130         virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
131         virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
132         virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
133         virtual void GOTO_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
134         virtual void NEXT_EXPR( ostream &ret, InlineItem *ilItem, bool inFinish ) = 0;
135         virtual void CALL_EXPR( ostream &ret, InlineItem *ilItem, 
136                         int targState, bool inFinish ) = 0;
137         virtual void RET( ostream &ret, bool inFinish ) = 0;
138         virtual void BREAK( ostream &ret, int targState ) = 0;
139         virtual void CURS( ostream &ret, bool inFinish ) = 0;
140         virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
141         void EXEC( ostream &ret, InlineItem *item, int targState, int inFinish );
142         void EXECTE( ostream &ret, InlineItem *item, int targState, int inFinish );
143         void LM_SWITCH( ostream &ret, InlineItem *item, int targState, int inFinish );
144         void SET_ACT( ostream &ret, InlineItem *item );
145         void INIT_TOKSTART( ostream &ret, InlineItem *item );
146         void INIT_ACT( ostream &ret, InlineItem *item );
147         void SET_TOKSTART( ostream &ret, InlineItem *item );
148         void SET_TOKEND( ostream &ret, InlineItem *item );
149         void GET_TOKEND( ostream &ret, InlineItem *item );
150         void SUB_ACTION( ostream &ret, InlineItem *item, 
151                         int targState, bool inFinish );
152
153         string ERROR_STATE();
154         string FIRST_FINAL_STATE();
155
156         virtual string PTR_CONST() = 0;
157         virtual ostream &OPEN_ARRAY( string type, string name ) = 0;
158         virtual ostream &CLOSE_ARRAY() = 0;
159         virtual ostream &STATIC_VAR( string type, string name ) = 0;
160
161         virtual string CTRL_FLOW() = 0;
162
163         ostream &source_warning(const InputLoc &loc);
164         ostream &source_error(const InputLoc &loc);
165
166         unsigned int arrayTypeSize( unsigned long maxVal );
167
168         bool outLabelUsed;
169         bool againLabelUsed;
170         bool useIndicies;
171
172 public:
173         /* Determine if we should use indicies. */
174         virtual void calcIndexSize() {}
175 };
176
177 class CCodeGen : virtual public FsmCodeGen
178 {
179 public:
180         CCodeGen( ostream &out ) : FsmCodeGen(out) {}
181
182         virtual string NULL_ITEM();
183         virtual string POINTER();
184         virtual ostream &SWITCH_DEFAULT();
185         virtual ostream &OPEN_ARRAY( string type, string name );
186         virtual ostream &CLOSE_ARRAY();
187         virtual ostream &STATIC_VAR( string type, string name );
188         virtual string ARR_OFF( string ptr, string offset );
189         virtual string CAST( string type );
190         virtual string UINT();
191         virtual string PTR_CONST();
192         virtual string CTRL_FLOW();
193 };
194
195 class DCodeGen : virtual public FsmCodeGen
196 {
197 public:
198         DCodeGen( ostream &out ) : FsmCodeGen(out) {}
199
200         virtual string NULL_ITEM();
201         virtual string POINTER();
202         virtual ostream &SWITCH_DEFAULT();
203         virtual ostream &OPEN_ARRAY( string type, string name );
204         virtual ostream &CLOSE_ARRAY();
205         virtual ostream &STATIC_VAR( string type, string name );
206         virtual string ARR_OFF( string ptr, string offset );
207         virtual string CAST( string type );
208         virtual string UINT();
209         virtual string PTR_CONST();
210         virtual string CTRL_FLOW();
211 };
212
213 #endif /* _FSMCODEGEN_H */