-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.ts
425 lines (366 loc) · 13.3 KB
/
compile.ts
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
415
416
417
418
419
420
421
422
423
424
425
import * as t from '@babel/types';
import * as g from "./transform";
const libGlobal = '$';
const primGlobal = '$';
const astGlobal = '$ast';
export const generateTsAst = (node: g.Grammar): t.File => {
const imports: t.Statement[] = [
emitNsImport(libGlobal, '@tonstudio/parser-runtime'),
];
const definedRules = new Set<string>();
const isDefined = (name: string) => definedRules.has(name);
const stmts: t.Statement[] = [];
const ast: t.Statement[] = [];
const names: string[] = [];
for (const rule of node.rules) {
const { expr, type, name } = compileRule(rule)({ isDefined });
stmts.push(expr);
ast.push(type);
if (name) names.push(name);
definedRules.add(rule.name);
}
return t.file(t.program([
...imports,
t.exportNamedDeclaration(t.tsModuleDeclaration(
t.identifier(astGlobal),
t.tsModuleBlock(ast),
)),
...stmts,
]));
};
type Context = {
isDefined: (name: string) => boolean;
}
type Compiler<T> = (ctx: Context) => T;
const compileRule = ({ name, formals, body, display }: g.Rule): Compiler<{
name: string | undefined,
expr: t.Statement,
type: t.Statement,
}> => ctx => {
const nameCode = formals.length === 0
? withType(t.identifier(name), emitParserType(name, []))
: t.identifier(name);
const { expr: exprCode, type: typeCode } = compileExpr(body)(ctx);
const bodyCode = compileFormals(name, formals, compileDisplay(display, exprCode));
return {
name: formals.length > 0 ? undefined : name,
expr: t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(nameCode, bodyCode)
])),
type: t.exportNamedDeclaration(t.tsTypeAliasDeclaration(
t.identifier(name),
formals.length > 0
? t.tsTypeParameterDeclaration(
formals.map(formal => t.tsTypeParameter(null, null, formal))
)
: null,
typeCode,
))
};
};
const compileDisplay = (display: g.Rule["display"], expr: t.Expression): t.Expression => {
if (!display) {
return expr;
}
const body = display.map(char => compileChar(char)).join('');
const value = JSON.parse(`"${body}"`);
return emitCall('named', [t.stringLiteral(value), expr]);
};
const compileFormals = (name: string, formals: readonly string[], bodyCode: t.Expression): t.Expression => {
if (formals.length === 0) {
return bodyCode;
}
const functionParams = formals.map(formal => {
return withType(t.identifier(formal), emitParserType(formal, [], true));
});
const arrow = t.arrowFunctionExpression(functionParams, bodyCode);
arrow.typeParameters = t.tsTypeParameterDeclaration(formals.map(formal => {
return t.tsTypeParameter(null, null, formal);
}));
arrow.returnType = t.tsTypeAnnotation(emitParserType(name, formals))
return arrow;
};
type ExprWithType = {
readonly expr: t.Expression,
readonly type: t.TSType,
}
const ewt = (expr: t.Expression, type: t.TSType): ExprWithType => ({ expr, type });
const compileExpr = (node: g.Expr): Compiler<ExprWithType> => ctx => {
switch (node.$) {
case 'Call':
return compileCall(node)(ctx);
case 'Field':
return compileField(node)(ctx);
case 'Located':
return compileLocated(node)(ctx);
case 'Pure':
return compilePure(node)(ctx);
case 'Ap':
return compileAp(node)(ctx);
case 'Eps':
return compileEps(node)(ctx);
case 'LookNeg':
return compileLookNeg(node)(ctx);
case 'LookPos':
return compileLookPos(node)(ctx);
case 'Optional':
return compileOptional(node)(ctx);
case 'Plus':
return compilePlus(node)(ctx);
case 'Star':
return compileStar(node)(ctx);
case 'Stringify':
return compileStringify(node)(ctx);
case 'Lex':
return compileLex(node)(ctx);
case 'Alt':
return compileAlt(node)(ctx);
case 'Class':
return compileClass(node)(ctx);
case 'Terminal':
return compileTerminal(node)(ctx);
case 'Any':
return compileAny(node)(ctx);
}
};
const compileField = (node: g.Field): Compiler<ExprWithType> => ctx => {
const left = compileExpr(node.left)(ctx);
const right = compileExpr(node.right)(ctx);
const body = emitCall('field', [left.expr, t.stringLiteral(node.key), right.expr]);
if (!t.isTSTypeLiteral(right.type)) {
throw new Error('Bug! Field can only extend object');
}
const newProp = t.tsPropertySignature(
t.identifier(node.key),
t.tsTypeAnnotation(left.type),
);
newProp.readonly = true;
const type = t.tsTypeLiteral([
newProp,
...right.type.members,
]);
return ewt(body, type);
};
const compileLocated = (node: g.Located): Compiler<ExprWithType> => (ctx) => {
const child = compileExpr(node.child)(ctx);
const body = emitCall('loc', [child.expr]);
const type = t.tsTypeReference(
t.tsQualifiedName(t.identifier(libGlobal), t.identifier('Located')),
t.tsTypeParameterInstantiation([child.type]),
);
return ewt(body, type);
};
const compilePure = ({ value }: g.Pure): Compiler<ExprWithType> => _ctx => {
return ewt(
emitCall('pure', [t.stringLiteral(value)]),
t.tsLiteralType(t.stringLiteral(value)),
);
};
const compileEps = (_node: g.Eps): Compiler<ExprWithType> => _ctx => {
return ewt(emitPrim('eps'), t.tsTypeLiteral([]));
};
const modeToName = {
l: 'left',
r: 'right',
} as const;
const compileAp = (node: g.Ap): Compiler<ExprWithType> => ctx => {
const left = compileExpr(node.left)(ctx);
const right = compileExpr(node.right)(ctx);
return ewt(
emitCall(modeToName[node.mode], [left.expr, right.expr]),
node.mode === 'l' ? left.type : right.type
);
};
const compileAlt = (node: g.Alt): Compiler<ExprWithType> => ctx => {
const left = compileExpr(node.left)(ctx);
const right = compileExpr(node.right)(ctx);
return ewt(emitCall('alt', [left.expr, right.expr]), t.tsUnionType([left.type, right.type]));
};
const compileAny = (_node: g.Any): Compiler<ExprWithType> => _ctx => {
return ewt(emitPrim('any'), t.tsStringKeyword());
};
const compileTerminal = (node: g.Terminal): Compiler<ExprWithType> => ctx => {
const body = node.value.map(char => compileChar(char)).join('');
const value = JSON.parse(`"${body}"`);
const wrapped = t.stringLiteral(value);
return ewt(emitCall('str', [wrapped]), t.tsLiteralType(t.stringLiteral(value)));
};
const compileClass = ({ negated, seqs }: g.Class): Compiler<ExprWithType> => ctx => {
const prefix = negated ? "^" : "";
const children = seqs.map(seq => compileSeq(seq));
const body = children.map(child => child.expr).join('');
const types = children.map(child => child.type);
const expectables = t.arrayExpression(children.map(child => child.exp));
return ewt(
emitCall(
'regex',
[
t.stringLiteral(prefix + body),
negated
? emitCall('negateExps', [expectables])
: expectables,
],
[t.tsUnionType(types)]
),
t.tsUnionType(types),
);
};
const compileStringify = (node: g.Stringify): Compiler<ExprWithType> => ctx => {
const { expr } = compileExpr(node.expr)(ctx);
return ewt(emitCall('stry', [expr]), t.tsStringKeyword());
};
const compileLex = (node: g.Lex): Compiler<ExprWithType> => ctx => {
const { expr, type } = compileExpr(node.expr)(ctx);
return ewt(emitCall('lex', [expr]), type);
};
const compilePlus = (node: g.Plus): Compiler<ExprWithType> => ctx => {
const { expr, type } = compileExpr(node.expr)(ctx);
const array = t.tsTypeOperator(t.tsArrayType(type))
array.operator = 'readonly';
return ewt(emitCall('plus', [expr]), array);
};
const compileStar = (node: g.Star): Compiler<ExprWithType> => ctx => {
const { expr, type } = compileExpr(node.expr)(ctx);
const array = t.tsTypeOperator(t.tsArrayType(type))
array.operator = 'readonly';
return ewt(emitCall('star', [expr]), array);
};
const compileOptional = (node: g.Optional): Compiler<ExprWithType> => ctx => {
const { expr, type } = compileExpr(node.expr)(ctx);
return ewt(emitCall('opt', [expr]), t.tsUnionType([type, t.tsUndefinedKeyword()]));
};
const compileLookPos = (node: g.LookPos): Compiler<ExprWithType> => ctx => {
const { expr, type } = compileExpr(node.expr)(ctx);
return ewt(emitCall('lookPos', [expr]), type);
};
const compileLookNeg = (node: g.LookNeg): Compiler<ExprWithType> => ctx => {
const { expr } = compileExpr(node.expr)(ctx);
return ewt(emitCall('lookNeg', [expr]), t.tsUndefinedKeyword());
};
const compileCall = (node: g.Call): Compiler<ExprWithType> => ctx => {
const wrapInRef = (name: string, expr: t.Expression) => {
if (ctx.isDefined(name)) {
return expr;
} else {
return t.callExpression(emitPrim('lazy'), [t.arrowFunctionExpression([], expr)]);
}
};
const params = node.params.map(param => compileExpr(param)(ctx));
const body = node.params.length > 0
? t.callExpression(
t.identifier(node.name),
params.map(param => param.expr),
)
: t.identifier(node.name);
const type = t.tsTypeReference(
t.identifier(node.name),
node.params.length > 0
? t.tsTypeParameterInstantiation(
params.map(param => param.type)
)
: null
);
return ewt(wrapInRef(node.name, body), type);
};
type TypedString = { expr: string, type: t.TSType, exp: t.Expression };
const compileChar = (node: g.Escape | g.Special | g.Char): string => {
switch (node.$) {
case 'Char':
return node.value;
default:
return compileEscape(node).expr;
}
};
const compileSeq = (node: g.Group | g.ClassChar | g.SpecialClass | g.Escape): TypedString => {
switch (node.$) {
case 'Group':
const from = compileSeq(node.from).expr;
const to = compileSeq(node.to).expr;
return {
expr: from + '-' + to,
type: t.tsStringKeyword(),
exp: emitCall('ExpRange', [t.stringLiteral(from), t.stringLiteral(to)])
};
case 'ClassChar':
return {
expr: node.value === '\\' || node.value === '\]'
? `\\${node.value}`
: node.value,
type: t.tsLiteralType(t.stringLiteral(node.value)),
exp: emitCall('ExpString', [t.stringLiteral(node.value)])
};
default:
return compileEscape(node);
}
};
const compileEscape = (node: g.Escape | g.Special | g.SpecialClass): TypedString => {
let expr = compileEscapeToString(node);
const str = `"${expr}"`;
try {
const parsed = JSON.parse(str);
return {
expr,
type: t.tsLiteralType(t.stringLiteral(parsed)),
exp: emitCall('ExpString', [t.stringLiteral(parsed)])
};
} catch (e) {
return {
expr,
type: t.tsStringKeyword(),
exp: emitCall('ExpString', [t.stringLiteral(str)])
};
}
};
const compileEscapeToString = (node: g.Escape | g.Special | g.SpecialClass): string => {
switch (node.$) {
case 'Ascii':
return `\\x${node.value}`;
case 'Short':
return `\\u${node.value}`;
case 'Long':
return `\\u{${node.value}}`;
case 'Named':
return `\\${node.value}`;
case 'Special':
case 'SpecialClass':
return `\\${node.value}`;
}
};
const emitCall = (name: string, args: t.Expression[], params?: t.TSType[]): t.Expression => {
const result = t.callExpression(emitPrim(name), args);
if (params && params.length > 0) {
result.typeParameters = t.tsTypeParameterInstantiation(params);
}
return result;
};
const emitPrim = (name: string): t.Expression => {
return t.memberExpression(t.identifier(primGlobal), t.identifier(name));
};
const emitParserType = (name: string, formals: readonly string[], notQualified: boolean = false): t.TSType => {
const typeParams = formals.length === 0
? null
: t.tsTypeParameterInstantiation(formals.map(formal => {
return t.tsTypeReference(t.identifier(formal));
}));
return t.tsTypeReference(
t.tsQualifiedName(t.identifier(libGlobal), t.identifier('Parser')),
t.tsTypeParameterInstantiation([
t.tsTypeReference(
notQualified
? t.identifier(name)
: t.tsQualifiedName(t.identifier(astGlobal), t.identifier(name)),
typeParams,
)
])
);
};
const emitNsImport = (name: string, from: string): t.ImportDeclaration => {
return t.importDeclaration(
[t.importNamespaceSpecifier(t.identifier(name))],
t.stringLiteral(from),
);
};
const withType = <U extends t.Identifier>(value: U, type: t.TSType): U => {
value.typeAnnotation = t.tsTypeAnnotation(type);
return value;
};