summaryrefslogtreecommitdiff
path: root/ragel/tables.h
blob: 258f869ec0b40f39cf3f217d680b9c7e8869b1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright 2018 Adrian Thurston <thurston@colm.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _TABLES_H
#define _TABLES_H

#include <iostream>
#include "codegen.h"

struct Tables
:
	public CodeGen
{
	Tables( const CodeGenArgs &args ) 
	:
		CodeGen( args ),

		pa( "_pa" ),
		klen( "_klen" ),
		ckeys( "_ckeys" ),
		cekeys( "_cekeys" ),
		trans( "_trans" ),
		cond( "_cond" ),
		keys( "_keys" ),
		acts( "_acts" ),
		nacts( "_nacts" ),
		inds( "_inds" ),

		cont( "_cont" ),
		nfa_repeat( "_nfa_repeat" ),
		nfa_test( "_nfa_test" ),
		ps( "_ps" ),
		nbreak( "_nbreak" ),
		have( "__have" ),
		ic( "_ic" ),

		_out("_out"),
		_pop("_pop"),
		_test_eof( "_test_eof" ),
		_resume( "_resume" ),
		_match_cond( "_match_cond" ),
		_again( "_again" ),
		_match( "_match" ),
		_eof_goto( "_eof_goto" ),

		actions(          "actions",             *this ),
		transKeys(        "trans_keys",          *this ),
		charClass(        "char_class",          *this ),
		flatIndexOffset(  "index_offsets",       *this ),
		indices(          "indices",             *this ),
		indexDefaults(    "index_defaults",      *this ),
		transCondSpaces(  "trans_cond_spaces",   *this ),
		transOffsets(     "trans_offsets",       *this ),
		condTargs(        "cond_targs",          *this ),
		condActions(      "cond_actions",        *this ),
		toStateActions(   "to_state_actions",    *this ),
		fromStateActions( "from_state_actions",  *this ),
		eofCondSpaces(    "eof_cond_spaces",     *this ),
		eofCondKeyOffs(   "eof_cond_key_offs",   *this ),
		eofCondKeyLens(   "eof_cond_key_lens",   *this ),
		eofCondKeys(      "eof_cond_keys",       *this ),
		eofActions(       "eof_actions",         *this ),
		eofTrans(         "eof_trans",           *this ),

		keyOffsets(         "key_offsets",           *this ),
		singleLens(         "single_lengths",        *this ),
		rangeLens(          "range_lengths",         *this ),
		indexOffsets(       "index_offsets",         *this ),
		transCondSpacesWi(  "trans_cond_spaces_wi",  *this ),
		transOffsetsWi(     "trans_offsets_wi",      *this ),
		transLengthsWi(     "trans_lengths_wi",      *this ),
		transLengths(       "trans_lengths",         *this ),
		condKeys(           "cond_keys",             *this )
	{}

	Variable pa;
	Variable klen;
	Variable ckeys;
	Variable cekeys;
	Variable trans;
	Variable cond;
	Variable keys;
	Variable acts;
	Variable nacts;
	Variable inds;
	Variable cont;
	Variable nfa_repeat;
	Variable nfa_test;
	Variable ps;
	Variable nbreak;
	Variable have;
	Variable ic;

	GotoLabel _out;
	GotoLabel _pop;
	GotoLabel _test_eof;
	GotoLabel _resume;
	GotoLabel _match_cond;
	GotoLabel _again;
	GotoLabel _match;
	GotoLabel _eof_goto;

	TableArray actions;
	TableArray transKeys;
	TableArray charClass;
	TableArray flatIndexOffset;
	TableArray indices;
	TableArray indexDefaults;
	TableArray transCondSpaces;
	TableArray transOffsets;
	TableArray condTargs;
	TableArray condActions;
	TableArray toStateActions;
	TableArray fromStateActions;
	TableArray eofCondSpaces;
	TableArray eofCondKeyOffs;
	TableArray eofCondKeyLens;
	TableArray eofCondKeys;
	TableArray eofActions;
	TableArray eofTrans;

	TableArray keyOffsets;
	TableArray singleLens;
	TableArray rangeLens;
	TableArray indexOffsets;
	TableArray transCondSpacesWi;
	TableArray transOffsetsWi;
	TableArray transLengthsWi;
	TableArray transLengths;
	TableArray condKeys;

	int errCondOffset;

	virtual void TO_STATE_ACTION( RedStateAp *state ) = 0;
	virtual void FROM_STATE_ACTION( RedStateAp *state ) = 0;
	virtual void EOF_ACTION( RedStateAp *state ) = 0;
	virtual void COND_ACTION( RedCondPair *cond ) = 0;

	virtual void NFA_PUSH_ACTION( RedNfaTarg *targ ) = 0;
	virtual void NFA_POP_TEST( RedNfaTarg *targ ) = 0;
	virtual void NFA_FROM_STATE_ACTION_EXEC() = 0;

	virtual void FROM_STATE_ACTIONS() = 0;
	virtual void REG_ACTIONS( std::string cond ) = 0;
	virtual void TO_STATE_ACTIONS() = 0;
	virtual void EOF_ACTIONS() = 0;

	void CURS( ostream &ret, bool inFinish );
	void TARGS( ostream &ret, bool inFinish, int targState );
	void NEXT( ostream &ret, int nextDest, bool inFinish );
	void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish );
	void EOF_TRANS();
	void COND_EXEC( std::string expr );
};

struct TabGoto
:
	public virtual Tables
{
	TabGoto( const CodeGenArgs &args )
	:
		Tables( args )
	{}

	void CONTROL_JUMP( ostream &ret, bool inFinish );

	void GOTO( ostream &ret, int gotoDest, bool inFinish );
	void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish );
	void CALL( ostream &ret, int callDest, int targState, bool inFinish );
	void NCALL( ostream &ret, int callDest, int targState, bool inFinish );
	void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
	void NCALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
	void RET( ostream &ret, bool inFinish );
	void NRET( ostream &ret, bool inFinish );
	void BREAK( ostream &ret, int targState, bool csForced );
	void NBREAK( ostream &ret, int targState, bool csForced );

	void NFA_POP() {}

	void writeExec();
};

struct TabBreak
:
	public virtual Tables
{
	TabBreak( const CodeGenArgs &args )
	:
		Tables( args ),
		loopLabels( args.loopLabels )
	{}

	void CONTROL_JUMP( ostream &ret, bool inFinish );

	void GOTO( ostream &ret, int gotoDest, bool inFinish );
	void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish );
	void CALL( ostream &ret, int callDest, int targState, bool inFinish );
	void NCALL( ostream &ret, int callDest, int targState, bool inFinish );
	void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
	void NCALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
	void RET( ostream &ret, bool inFinish );
	void NRET( ostream &ret, bool inFinish );
	void BREAK( ostream &ret, int targState, bool csForced );
	void NBREAK( ostream &ret, int targState, bool csForced );

	void NFA_POP() {}

	void writeExec();

	bool loopLabels;
	std::string BREAK( GotoLabel &label );
	std::string CONTINUE( GotoLabel &label );
	std::string BREAK_LABEL( GotoLabel &label );
};

struct TabVar
:
	public virtual Tables
{
	TabVar( const CodeGenArgs &args )
	:
		Tables( args )
	{}

	void GOTO( ostream &ret, int gotoDest, bool inFinish );
	void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish );
	void CALL( ostream &ret, int callDest, int targState, bool inFinish );
	void NCALL( ostream &ret, int callDest, int targState, bool inFinish );
	void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
	void NCALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish );
	void RET( ostream &ret, bool inFinish );
	void NRET( ostream &ret, bool inFinish );
	void BREAK( ostream &ret, int targState, bool csForced );
	void NBREAK( ostream &ret, int targState, bool csForced );

	void NFA_POP() {}

	std::string BREAK( GotoLabel &label );
	std::string CONTINUE( GotoLabel &label );
	std::string BREAK_LABEL( GotoLabel &label );

	void writeExec();
};


#endif