Use portable types in the C/C++ code generator
[ragel-jkt.git] / ragel / mlcodegen.h
blob3df047b91715f74171ab3286d95bbd67106902c6
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 */
7 /* This file is part of Ragel.
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.
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.
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
24 #ifndef _MLCODEGEN_H
25 #define _MLCODEGEN_H
27 #include <iostream>
28 #include <string>
29 #include <stdio.h>
30 #include "common.h"
31 #include "gendata.h"
33 using std::string;
34 using std::ostream;
36 /* Forwards. */
38 struct RedFsmAp;
39 struct RedStateAp;
40 struct CodeGenData;
41 struct GenAction;
42 struct NameInst;
43 struct GenInlineItem;
44 struct GenInlineList;
45 struct RedAction;
46 struct LongestMatch;
47 struct LongestMatchPart;
50 /* Integer array line length. */
51 #define IALL 8
53 //string itoa( int i );
56 * class OCamlCodeGen
58 class OCamlCodeGen : public CodeGenData
60 public:
61 OCamlCodeGen( ostream &out );
62 virtual ~OCamlCodeGen() {}
64 virtual void finishRagelDef();
65 virtual void writeInit();
66 virtual void writeStart();
67 virtual void writeFirstFinal();
68 virtual void writeError();
70 protected:
71 string data_prefix;
73 string FSM_NAME();
74 string START_STATE_ID();
75 ostream &ACTIONS_ARRAY();
76 string GET_WIDE_KEY();
77 string GET_WIDE_KEY( RedStateAp *state );
78 string TABS( int level );
79 string KEY( Key key );
80 string ALPHA_KEY( Key key );
81 string LDIR_PATH( char *path );
82 void ACTION( ostream &ret, GenAction *action, int targState, bool inFinish );
83 void CONDITION( ostream &ret, GenAction *condition );
84 string ALPH_TYPE();
85 string WIDE_ALPH_TYPE();
86 string ARRAY_TYPE( unsigned long maxVal );
87 string ARRAY_TYPE( unsigned long maxVal, bool forceSigned );
89 virtual string ARR_OFF( string ptr, string offset );
90 virtual string CAST( string type );
91 virtual string UINT();
92 virtual string NULL_ITEM();
93 virtual string POINTER();
94 virtual string GET_KEY();
95 virtual ostream &SWITCH_DEFAULT();
97 string P();
98 string PE();
99 string vEOF();
101 // string ACCESS();
102 string vCS();
103 string STACK();
104 string TOP();
105 string TOKSTART();
106 string TOKEND();
107 string ACT();
109 // ++x
110 string PRE_INCR(string);
111 string PRE_DECR(string);
113 // x++
114 string POST_INCR(string);
115 string POST_DECR(string);
117 string DATA_PREFIX();
118 string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
119 string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
120 string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
121 string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
122 string I() { return "_" + DATA_PREFIX() + "indicies"; }
123 string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
124 string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
125 string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
126 string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
127 string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
128 string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
129 string A() { return "_" + DATA_PREFIX() + "actions"; }
130 string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
131 string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
132 string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
133 string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
134 string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
135 string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
136 string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
137 string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
138 string START() { return DATA_PREFIX() + "start"; }
139 string ERROR() { return DATA_PREFIX() + "error"; }
140 string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
141 string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
143 void INLINE_LIST( ostream &ret, GenInlineList *inlineList, int targState, bool inFinish );
144 virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
145 virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
146 virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
147 virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
148 virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
149 virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem,
150 int targState, bool inFinish ) = 0;
151 virtual void RET( ostream &ret, bool inFinish ) = 0;
152 virtual void BREAK( ostream &ret, int targState ) = 0;
153 virtual void CURS( ostream &ret, bool inFinish ) = 0;
154 virtual void TARGS( ostream &ret, bool inFinish, int targState ) = 0;
155 void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
156 void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, int inFinish );
157 void SET_ACT( ostream &ret, GenInlineItem *item );
158 void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
159 void INIT_ACT( ostream &ret, GenInlineItem *item );
160 void SET_TOKSTART( ostream &ret, GenInlineItem *item );
161 void SET_TOKEND( ostream &ret, GenInlineItem *item );
162 void GET_TOKEND( ostream &ret, GenInlineItem *item );
163 void SUB_ACTION( ostream &ret, GenInlineItem *item,
164 int targState, bool inFinish );
165 void STATE_IDS();
167 string ERROR_STATE();
168 string FIRST_FINAL_STATE();
170 virtual string PTR_CONST();
171 virtual ostream &OPEN_ARRAY( string type, string name );
172 virtual ostream &CLOSE_ARRAY();
173 virtual ostream &STATIC_VAR( string type, string name );
175 virtual string CTRL_FLOW();
177 // toplevel phrase separator
178 string TOP_SEP();
179 // array elements separator
180 string ARR_SEP();
181 // access array
182 string AT(const string& array, const string& index);
184 string make_access(char const* name, GenInlineList* x, bool prefix);
186 ostream &source_warning(const InputLoc &loc);
187 ostream &source_error(const InputLoc &loc);
189 unsigned int arrayTypeSize( unsigned long maxVal );
191 bool outLabelUsed;
192 bool testEofUsed;
193 bool againLabelUsed;
194 bool useIndicies;
196 public:
197 /* Determine if we should use indicies. */
198 virtual void calcIndexSize() {}
200 void genLineDirective( ostream &out );
203 #define MAX(a, b) (a > b ? a : b)
205 #endif