-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCLexer.hx
414 lines (371 loc) · 10.9 KB
/
CLexer.hx
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package tools.generator;
import lm.LineColumn;
import haxe.macro.Expr;
using haxe.macro.ExprTools;
private enum TPExprDef {
EEof( id : String );
ESrc( id : String );
EMax( i : Int );
EName( name : String );
EAssign( name : String, value : Expr );
ERuleSet( r : RuleCaseGroup );
EToken( name : String, ?file : String);
}
private typedef TPExpr = {
texpr : TPExprDef,
pos : Position,
}
#if macro
private class LexParser{}
#else
@:rule({
start : [begin, cenum],
left : ["|"],
left : ["+"],
}) private class LexParser implements lm.SLR<Lexer> {
public var lexer : Lexer;
function mp( t1 : lm.Lexer.Position, t2 : lm.Lexer.Position ) {
return lexer.mkpos(t1.pmin, t2.pmax);
}
function mk( def : ExprDef, t1, t2 ) {
return {expr : def, pos : mp(t1, t2)};
}
function mkc( c : Constant, t1, t2 ) {
return {expr : EConst(c), pos : mp(t1, t2)};
}
function mk_tpexpr( def : TPExprDef, t1, t2 ) {
return {texpr : def, pos : mp(t1, t2)};
}
public function new( lex : Lexer ) {
this.lexer = lex;
this.stream = @:privateAccess new lm.Stream(lex);
}
// start
var begin : Array<TPExpr> = switch(s) {
case [a = list, Eof] :
a;
default:
var t = stream.peek(0);
if (t.term == Eof)
return [];
throw new Error('Unexpected "' + stream.str(t) + '"', mp(t, t));
}
var list : Array<TPExpr> = switch(s) {
case [a = list, e = tpexpr] : a.push(e); a;
case [e = tpexpr] : [e];
}
var tpexpr : TPExpr = switch(s) {
case [DName, "(", CIdent(n), ")"] : mk_tpexpr(EName (n ), T1, T4);
case [DToken, "(", CIdent(n), ")"] : mk_tpexpr(EToken(n ), T1, T4);
case [DToken, "(", CIdent(n), ",", CString(f), ")"] : mk_tpexpr(EToken(n, f), T1, T5);
case [DEoF, "(", CIdent(c), ")"] : mk_tpexpr(EEof(c), T1, T4);
case [DSrc, "(", CIdent(c), ")"] : mk_tpexpr(ESrc(c), T1, T4);
case [DMax, "(", CInt(i), ")"] : mk_tpexpr(EMax(i), T1, T4);
case [KLet, CIdent(c), "=", e = expr] : mk_tpexpr(EAssign(c, e), T1, T4);
case [KLet, CIdent(c), "=", KFun, r = cases] : r.name = c; mk_tpexpr(ERuleSet(r), T1, T5);
}
var expr : Expr = switch(s) {
case [CIdent(name), "(", e = expr , ")"] :
switch (name) {
case "Opt", "Plus", "Star":
default:
throw new Error("Duplicated null matching: _", mp(T1, T1));
}
var fname = mkc(CIdent(name), T1, T1);
mk(ECall(fname, [e]), T1, T4);
case [e1 = expr, "+", e2 = expr] :
mk(EBinop(OpAdd, e1, e2), T1, T3);
case [e1 = expr, "|", e2 = expr] :
mk(EBinop(OpOr, e1, e2), T1, T3);
case ["(", e = expr , ")"]:
mk(EParenthesis(e), T1, T3);
case [CString(s)] :
mkc(CString(s), T1, T1);
case [CIdent(i)] :
mkc(CIdent(i), T1, T1);
}
var cases : RuleCaseGroup = switch(s) {
case [c = cases, r = line]:
if (is_nullcase(r.pattern)) {
if (c.unmatch != null)
throw new Error("Duplicated null matching: _", c.unmatch.pattern.pos);
c.unmatch = r;
} else {
c.rules.push(r);
}
c;
case [r = line]:
if (is_nullcase(r.pattern)) {
{ name : "", rules : [ ], unmatch : r }
} else {
{ name : "", rules : [r], unmatch : null }
}
}
var line : RuleCase = switch(s) {
case [OpOr, pat = expr, OpArrow]:
stream.junk(stream.rest);
{ pattern : pat, action : this.lexer.copyAction(T3.pmax) }
}
//// parse c enum
var cenum : Expr = switch(s) {
case ["{", e = enum_item, "}"]:
e;
default:
var t = stream.peek(0);
throw new Error("UnExpected: " + stream.str(t) , mp(t, t));
}
var enum_item : Expr = switch(s) {
case [e1 = enum_item, ",", e2 = enum_item]:
switch(e1.expr) {
case EBlock(a):
a.push(e2);
mk(EBlock(a), T1, T3);
case _:
mk(EBlock([e1, e2]), T1, T3);
}
case [e = enum_item, ","]:
e;
case [CIdent(id) , "=", CInt(n)]:
var num = mk(EConst(CInt("" + n)), T3, T3);
mk(EMeta({"name" : id, pos : mp(T1, T1)}, num), T1, T3);
case [CIdent(id)]:
mk(EMeta({"name" : id, pos : mp(T1, T1)}, null), T1, T1);
}
// custom extract function
@:rule(CInt) inline function __s1( s : String ) return Std.parseInt(s);
@:rule(CIdent) inline function __s2( s : String ) return s;
@:rule(CString) function __s3( input, t ) {
var s = this.lexer.parray.get(t.pmin + 1);
if (s == null)
throw new Error("CLexer __s3() TODO", mp(t, t));
return s;
}
public function is_nullcase(e) {
return switch(e.expr) {
case EConst(CIdent("_")) : true;
default: false;
}
}
}
#end
/*
* build lex for c language
*/
class CLexer {
static public var tokenjsons = new Map<String, TokenJson>(); // used for CSLR
public var dargs : DArgs;
public var lexer : Lexer;
public var parser : LexParser;
public var tplvar : TplVars;
public var path : haxe.io.Path;
public function new( mt : Template, args : DArgs ) {
this.dargs = args;
this.path = new haxe.io.Path(args.lexeme);
this.tplvar = new TplVars(this.path.file);
this.lexer = new Lexer(args.lexeme);
this.parser = new LexParser(this.lexer);
try {
build(mt);
} catch ( e : Error ) {
fatalError(e.message, e.pos);
}
}
function build( mt : Template ) {
this.lexer.skipBegin();
var pstart = lexer.pmax;
var groups = [];
var varmap = new Map<String, Expr>();
// parse file
var cenums : {name : String, ?file : String} = null;
var aexpr = this.parser.begin();
for (e in aexpr) {
switch(e.texpr) {
case EEof(id):
tplvar.eof = id;
case ESrc(id):
tplvar.utf8 = id == "UTF8";
case EMax(i):
tplvar.stride = (i | 0x7f) + 1; // 128|256
case EToken(name, file):
cenums = {name : name, file : file};
case EAssign(name, value):
varmap.set(name, value);
case ERuleSet(r):
groups.push(r);
case EName(name):
tplvar.prefix = name;
}
}
if (tplvar.eof == null)
throw new Error("%EOF() is required.", lexer.mkpos(pstart, pstart));
if (groups.length == 0)
return;
generateToken(cenums, groups); // for CSLR
var paterns = toPattens(groups, varmap);
var lexe = new lm.LexEngine(paterns, tplvar.stride - 1);
// checking
ExprHelps.lexChecking(lexe, groups);
// update data to tplvar
var index = 0;
for (g in groups) {
for (r in g.rules) {
tplvar.cases.push({ index : index++, action : finalizeActoin(r.action)});
}
}
if (groups[0].unmatch != null) {
tplvar.epsilon = finalizeActoin(groups[0].unmatch.action);
} else {
tplvar.epsilon = lexUnMatching();
}
// get extra case and update tplvar.cases
var extra = ExprHelps.lexUnMatchedActions(lexe, groups);
for (c in extra) {
tplvar.cases.push({ index : index++, action : finalizeActoin(c.expr) });
}
tplvar.update(lexe);
for (i in 0...lexe.entrys.length) {
var name = groups[i].name;
tplvar.entrys.push({name : name, uname : name.toUpperCase(), begin : lexe.entrys[i].begin});
}
// write
var text = mt.execute(tplvar);
// path
var fullpath = dargs.normalizePath(this.path);
var out = sys.io.File.write(fullpath);
out.writeString(tokenPreDefines(tplvar.utf8));
out.writeString(lexer.header);
out.writeString(text);
out.writeString(lexer.footer);
out.close();
Sys.print("> " + fullpath + "\n");
}
function toPattens( groups : Array<RuleCaseGroup>, varmap : Map<String, Expr> ) {
var cset = [new lm.Charset.Char(0, tplvar.stride - 1)];
var ret = [];
var index = 0;
for (g in groups) {
var a = [];
for (r in g.rules) {
if (parser.is_nullcase(r.pattern))
throw new Error("TODO", r.pattern.pos);
a.push(ExprHelps.parsePaterns(r.pattern, varmap, cset));
}
ret.push(a);
}
return ret;
}
function generateToken( cenum : {name : String, ?file : String}, groups : Array<RuleCaseGroup> ) {
if (cenum == null)
return;
var prevstate : {lines : LineCounter, parray : lm.PosArray} = null;
var lex = this.lexer;
var par = this.parser;
if (string_has(cenum.file)) {
lex = new Lexer(cenum.file);
par = new LexParser(lex);
} else {
// save states to reuse current file
prevstate = {
lines : this.lexer.lines,
parray : this.lexer.parray,
}
lex.lines = new LineCounter(lex.lines.owner);
lex.parray = new lm.PosArray();
}
var terms = new haxe.DynamicAccess<{value : Int, pmin : Int, pmax : Int}>();
var i = 0;
var source = lex.input;
var len = source.length;
inline function char(i) return source.readByte(i);
while (i < len) {
var c = char(i++);
if (c == "e".code && char(i) == "n".code // "enum"
&& char(i + 1) == "u".code && char(i + 2) == "m".code) {
@:privateAccess lex.pmax = i + 3;
var t = lex.token();
if (!(t == CIdent && lex.current == cenum.name))
continue;
var expr = par.cenum();
readCEnumToken(expr, terms);
break;
} else if (c == "\n".code) {
lex.lines.add(i);
}
}
// reflect
function loop(e) {
switch (e.expr) {
case EBlock(a):
for (e in a)
loop(e);
case EMeta({name : name}, e):
default:
}
}
var reflect = new haxe.DynamicAccess<String>();
for (g in groups) {
for (r in g.rules) {
switch(r.action.expr) {
case EBlock(a) if (a.length > 0):
var stsm = ExprHelps.expectCString(a[a.length - 1]);
if (!terms.exists(stsm))
continue;
switch (r.pattern.expr) {
case EConst(CString(s)):
var spat = lm.Utils.unescape(s);
reflect.set(spat, stsm);
default:
}
default:
}
}
}
reflect.remove('"');
reflect.remove("'");
var json : TokenJson = {
file : lex.lines.owner,
stype : "enum " + cenum.name,
terms : terms,
reflect : reflect,
eof : this.tplvar.eof,
utf8 : this.tplvar.utf8, // the input(source) format
}
// TODO: save to file?
tokenjsons.set(dargs.lexeme, json);
// restore
if (prevstate != null) {
this.lexer.lines = prevstate.lines;
this.lexer.parray = prevstate.parray;
}
}
function readCEnumToken( e : Expr, out : haxe.DynamicAccess<{value : Int, pmin : Int, pmax : Int}> ) {
var terms = new Map<String, Int>();
var acc = 0;
function loop(e) {
if (e == null)
return;
switch(e.expr) {
case EBlock(a):
for (e in a)
loop(e);
case EMeta({name : name}, v):
var value = acc++;
if (v != null) {
value = ExprHelps.expectCInt(v);
acc = value + 1;
}
out.set(name, {value : value, pmin : e.pos.min, pmax : e.pos.max});
default:
}
}
loop(e);
}
function fatalError( msg : String, pos : Position ) : Dynamic {
var lfcount = lexer.lines;
var lmin = lfcount.get(pos.min);
var spos = '${lmin.line}: characters ${lmin.column}-${pos.max - lmin.base + 1}';
Sys.println(lfcount.owner + ":" + spos + " : " + msg);
Sys.exit( -1);
return null;
}
}