-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLEX.c
303 lines (274 loc) · 8.78 KB
/
LEX.c
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "LEX.h"
#include <unistd.h>
#include "01.h"
#include "BUF.h"
#include "FILE.h"
#include "NEST.h"
#include "PRO.h"
#define LEX_TEMPL_ACTION 0
#define LEX_TEMPL_ENUM 1
#define LEX_TEMPL_FN 2
#define LEX_TEMPL_ACT 3
#define LEX_TEMPL_PUBACTNL 4
#define LEX_TEMPL_ACTNL 5
#define LEX_TEMPL_FILE 6
#define LEX_TEMPL_L 7
const u8c *LEX_TEMPL[LEX_TEMPL_LANG_LEN][LEX_TEMPL_LEN][2] = {
{
$u8str("action $mod${act}0 { mark0[$mod$act] = p - text[0]; }\n"
"action $mod${act}1 {\n"
" tok[0] = text[0] + mark0[$mod$act];\n"
" tok[1] = p;\n"
" o = ${mod}on$act(tok, state); \n"
" if (o!=OK) {\n"
" fbreak;\n"
" }\n"
"}\n"),
$u8str("\t$mod$act = ${mod}enum+$actno,\n"),
$u8str("ok64 ${mod}on$act ($$cu8c tok, ${mod}state* state);\n"),
$u8str("$mod$act = ( "),
$u8str(" ) >$mod${act}0 %$mod${act}1;\n"),
$u8str(" ); # no $act callback\n"),
$u8str("#include \"abc/INT.h\"\n"
"#include \"abc/PRO.h\"\n"
"#include \"$mod.h\"\n"
"\n"
"// action indices for the parser\n"
"#define ${mod}enum 0\n"
"enum {\n"
"$ENUM"
"};\n"
"\n"
"// user functions (callbacks) for the parser\n"
"$FN\n"
"\n"
"\n"
"%%{\n"
"\n"
"machine $mod;\n"
"\n"
"alphtype unsigned char;\n"
"\n"
"# ragel actions\n"
"$ACTIONS"
"\n"
"# ragel grammar rules\n"
"$RULES"
"\n"
"main := ${mod}Root;\n"
"\n"
"}%%\n"
"\n"
"%%write data;\n"
"\n"
"// the public API function\n"
"pro(${mod}lexer, ${mod}state* state) {\n"
"\n"
" a$$dup(u8c, text, state->text);\n"
" sane($$ok(text));\n"
"\n"
" int cs = 0;\n"
" u8c *p = (u8c*) text[0];\n"
" u8c *pe = (u8c*) text[1];\n"
" u8c *eof = pe;\n"
" u64 mark0[64] = {};\n"
" ok64 o = OK;\n"
"\n"
" $$u8c tok = {p, p};\n"
"\n"
" %% write init;\n"
" %% write exec;\n"
"\n"
" state->text[0] = p;\n"
" if (p!=text[1] || cs < ${mod}_first_final || o!=OK) {\n"
" return ${mod}fail;\n"
" }\n"
" return o;\n"
"}\n"),
$u8str("c"),
},
{
$u8str("action $mod${act}0 { mark0[$mod$act] = p; }\n"
"action $mod${act}1 {\n"
" err = ${mod}on$act(data[mark0[$mod$act] : p], state); \n"
" if err!=nil {\n"
" fbreak;\n"
" }\n"
"}\n"),
$u8str(" $mod$act = ${mod}enum+$actno\n"),
$u8str("// func ${mod}on$act (tok []byte, state *${mod}state) error\n"),
$u8str("$mod$act = ( "),
$u8str(" ) >$mod${act}0 %$mod${act}1;\n"),
$u8str(" ); # no $act callback\n"),
$u8str("package main\n"
"import \"errors\""
"\n"
"// action indices for the parser\n"
"\n"
"const ("
"${mod}enum = 0\n"
"$ENUM"
")\n"
"\n"
"// user functions (callbacks) for the parser\n"
"$FN\n"
"\n"
"\n"
"%%{\n"
"\n"
"machine $mod;\n"
"\n"
"alphtype byte;\n"
"\n"
"# ragel actions\n"
"$ACTIONS"
"\n"
"# ragel grammar rules\n"
"$RULES"
"\n"
"main := ${mod}Root;\n"
"\n"
"}%%\n"
"\n"
"%%write data;\n"
"\n"
"// the public API function\n"
"func ${mod}lexer (state *${mod}state) (err error) {\n"
"\n"
" data := state.text\n"
" var mark0 [64]int\n"
" cs, p, pe, eof := 0, 0, len(data), len(data)\n"
"\n"
" %% write init;\n"
" %% write exec;\n"
"\n"
" if (p!=len(data) || cs < ${mod}_first_final) {\n"
" state.text = state.text[p:];\n"
" return errors.New(\"${mod} bad syntax\")\n"
" }\n"
" return nil;\n"
"}\n"),
$u8str("go"),
},
};
con ok64 LEX$ACTIONS = 0x1c5d849d30a;
con ok64 LEX$ENUM = 0x59e5ce;
con ok64 LEX$FN = 0x5cf;
con ok64 LEX$RULES = 0x1c39579b;
con ok64 LEX$mod = 0x28cf1;
con ok64 LEX$act = 0x389e5;
con ok64 LEX$actno = 0x33cb89e5;
ok64 LEXonName($cu8c tok, LEXstate *state) {
ok64 o = $u8feed(NESTidle(state->ct), state->mod);
if (o == OK) o = $u8feed(NESTidle(state->ct), tok);
return o;
}
ok64 LEXonOp($cu8c tok, LEXstate *state) {
return $u8feed(NESTidle(state->ct), tok);
}
ok64 LEXonClass($cu8c tok, LEXstate *state) {
return $u8feed(NESTidle(state->ct), tok);
}
ok64 LEXonRange($cu8c tok, LEXstate *state) {
return $u8feed(NESTidle(state->ct), tok);
}
ok64 LEXonString($cu8c tok, LEXstate *state) {
return $u8feed(NESTidle(state->ct), tok);
}
ok64 LEXonQString($cu8c tok, LEXstate *state) {
return $u8feed(NESTidle(state->ct), tok);
}
ok64 LEXonSpace($cu8c tok, LEXstate *state) {
// return $u8feed(NESTidle(state->ct), tok);
return $u8feed1(NESTidle(state->ct), ' ');
}
ok64 LEXonEntity($cu8c tok, LEXstate *state) { return OK; }
ok64 LEXonExpr($cu8c tok, LEXstate *state) { return OK; }
ok64 LEXonRep($cu8c tok, LEXstate *state) { return OK; }
ok64 LEXonEq($cu8c tok, LEXstate *state) {
sane($ok(tok) && state != nil);
u8c$ tmpl = LEX_TEMPL[state->lang][LEX_TEMPL_ACT];
try(NESTsplice, state->ct, LEX$RULES);
then try(NESTfeed, state->ct, tmpl);
done;
}
pro(LEXonRuleName, $cu8c tok, LEXstate *state) {
sane($ok(tok) && state != nil);
$set(state->cur, tok);
state->ruleno++;
if (**tok < 'A' || **tok > 'Z') done;
u8B ct = (u8B)state->ct;
u8c$ tmpl = LEX_TEMPL[state->lang][LEX_TEMPL_ACTION];
call(NESTsplice, ct, LEX$ACTIONS);
call(NESTfeed, ct, tmpl);
u8c$ enmtmpl = LEX_TEMPL[state->lang][LEX_TEMPL_ENUM];
call(NESTsplice, ct, LEX$ENUM);
call(NESTfeed, ct, enmtmpl);
call(NESTsplice, ct, LEX$actno);
call(u64decfeed, NESTidle(ct), state->ruleno);
u8c$ fntmpl = LEX_TEMPL[state->lang][LEX_TEMPL_FN];
call(NESTsplice, ct, LEX$FN);
call(NESTfeed, ct, fntmpl);
done;
}
ok64 LEXonLine($cu8c tok, LEXstate *state) {
sane($ok(tok) && state != nil);
u8B ct = (u8B)state->ct;
u8c$ cur = state->cur;
u8c$ fntmpl;
if (**cur >= 'A' && **cur <= 'Z') {
fntmpl = LEX_TEMPL[state->lang][LEX_TEMPL_PUBACTNL];
} else {
fntmpl = LEX_TEMPL[state->lang][LEX_TEMPL_ACTNL];
}
try(NESTfeed, ct, fntmpl);
then try(NESTspliceany, ct, LEX$act);
then try($u8feed, NESTidle(ct), cur);
done;
}
ok64 LEXonRoot($cu8c tok, LEXstate *state) {
sane($ok(tok) && state != nil);
u8B ct = (u8B)state->ct;
try(NESTspliceall, ct, LEX$mod);
then try($u8feed, NESTidle(ct), state->mod);
done;
}
pro(lex2rl, $u8c mod, $u8c lang) {
sane($ok(mod));
aBcpad(u8, name, KB);
aBcpad(u8, lex, KB << 8);
$u8c $namet = $u8str("$s.lex");
$feedf(nameidle, $namet, mod);
int fd;
call(FILEopen, &fd, namedata, O_RDONLY);
call(FILEdrainall, lexidle, fd);
call(FILEclose, &fd);
aBcpad(u8, ct, MB);
NESTreset(ctbuf);
int nlang = 0;
if (!$empty(lang)) {
while (nlang < LEX_TEMPL_LANG_LEN) {
if ($eq(lang, LEX_TEMPL[nlang][LEX_TEMPL_L])) break;
++nlang;
}
if (nlang == LEX_TEMPL_LANG_LEN) fail(badarg);
}
LEXstate state = {
.lang = nlang,
.ct = (u8B)ctbuf,
.mod = mod,
};
$mv(state.text, lexdata);
call(NESTfeed, ctbuf, LEX_TEMPL[nlang][LEX_TEMPL_FILE]);
aBcpad(u8, rl, MB);
call(LEXlexer, &state);
call(NESTrender, rlidle, ctbuf);
aBcpad(u8, rlname, KB);
$u8c $rnamet = $u8str("$s.$s.rl");
$feedf(rlnameidle, $rnamet, mod, LEX_TEMPL[nlang][LEX_TEMPL_L]);
int rfd;
call(FILEcreate, &rfd, rlnamedata);
call(FILEfeedall, rfd, rldata);
call(FILEclose, &rfd);
done;
}