-
Notifications
You must be signed in to change notification settings - Fork 9
/
quasi-jessie.js.ts
376 lines (305 loc) · 14.2 KB
/
quasi-jessie.js.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
// Subsets of JavaScript, starting from the grammar as defined at
// http://www.ecma-international.org/ecma-262/9.0/#sec-grammar-summary
// See https://github.com/Agoric/Jessie/blob/master/README.md
// for documentation of the Jessie grammar defined here.
/// <reference path="peg.d.ts"/>
// Safe Modules are ones that can be imported without
// insulating their symbols.
const isSafeModule = (moduleName: string) => {
switch (moduleName) {
case '@agoric/jessie': {
return true;
}
default: {
return false;
}
}
};
type TerminatedBody = [any[][], any[]];
const terminatedBlock = (manyBodies: TerminatedBody[]) => {
const stmts = manyBodies.reduce<any[][]>((prior, body) => {
const [bs, t] = body;
bs.forEach(b => prior.push(b));
prior.push(t);
return prior;
}, []);
return ['block', stmts];
};
const makeJessie = (peg: IPegTag<IParserTag<any>>, justinPeg: IPegTag<IParserTag<any>>) => {
const {FAIL, SKIP} = justinPeg;
const jessieTag = justinPeg`
# Override rather than inherit start production.
# Only module syntax is permitted.
start <- _WS moduleBody _EOF ${b => (..._a: any[]) => ['module', b]};
# A.1 Lexical Grammar
# For proposed eventual send expressions
# Tildot followed by non-digit.
LATER <- "~." ~[0-9] _WS;
# insulate is reserved by Jessica.
RESERVED_WORD <- super.RESERVED_WORD / ( "insulate" _WSN );
# A.2 Expressions
# Jessie primaryExpr does not include "this", ClassExpression,
# GeneratorExpression, AsyncFunctionExpression,
# AsyncGenerarorExpression, or RegularExpressionLiteral.
primaryExpr <-
super.primaryExpr
/ functionExpr;
propDef <-
methodDef
/ super.propDef;
purePropDef <-
methodDef
/ super.purePropDef;
# Recognize pre-increment/decrement.
prePre <-
(PLUSPLUS / MINUSMINUS) ${op => `pre:${op}`}
/ super.prePre;
# Extend to recognize proposed eventual get syntax,
# as well as computed indices and postfix increment/decrement.
memberPostOp <-
super.memberPostOp
/ LEFT_BRACKET assignExpr RIGHT_BRACKET ${(_, e, _2) => ['index', e]}
/ LATER LEFT_BRACKET assignExpr RIGHT_BRACKET ${(_, _2, e, _3) => ['indexLater', e]}
/ LATER IDENT_NAME ${(_, id) => ['getLater', id]};
# Extend to recognize proposed eventual send syntax.
# We distinguish b~.foo(x) from calling b~.foo by a post-parsing pass
callPostOp <-
super.callPostOp
/ LATER args ${(_, args) => ['callLater', args]};
postOp <- (PLUSPLUS / MINUSMINUS) _WS;
# to be extended
assignExpr <-
arrowFunc
/ functionExpr
/ lValue postOp ${(lv, op) => [op, lv]}
/ lValue (EQUALS / assignOp) assignExpr ${(lv, op, rv) => [op, lv, rv]}
/ super.assignExpr
/ primaryExpr;
# An expression without side-effects.
pureExpr <-
arrowFunc
/ super.pureExpr;
# In Jessie, an lValue is only a variable, a computed index-named
# property (an array element), or a statically string-named
# property.
# We allow assignment to statically string-named fields, since it
# is useful during initialization and prevented thereafter by
# mandatory tamper-proofing.
# to be overridden or extended
lValue <-
primaryExpr LEFT_BRACKET indexExpr RIGHT_BRACKET ${(pe, _, e, _2) => ['index', pe, e]}
/ primaryExpr LATER LEFT_BRACKET indexExpr RIGHT_BRACKET ${(pe, _, _2, e, _3) => ['indexLater', pe, e]}
/ primaryExpr DOT IDENT_NAME ${(pe, _, id) => ['get', pe, id]}
/ primaryExpr LATER IDENT_NAME ${(pe, _, id) => ['getLater', pe, id]}
/ useVar;
assignOp <-
("*=" / "/=" / "%=" / "+=" / "-="
/ "<<=" / ">>=" / ">>>="
/ "&=" / "^=" / "|="
/ "**=") _WS;
# A.3 Statements
# to be extended.
# The exprStatement production must go last, so PEG's prioritized
# choice will interpret {} as a block rather than an expression.
statement <-
block
/ IF LEFT_PAREN expr RIGHT_PAREN arm ELSE elseArm ${(_, _2, c, _3, t, _4, e) => ['if', c, t, e]}
/ IF LEFT_PAREN expr RIGHT_PAREN arm ${(_, _2, c, _3, t) => ['if', c, t]}
/ breakableStatement
/ terminator
/ IDENT COLON statement ${(label, _, stat) => ['label', label, stat]}
/ TRY block catcher finalizer ${(_, b, c, f) => ['try', b, c, f]}
/ TRY block catcher ${(_, b, c) => ['try', b, c]}
/ TRY block finalizer ${(_, b, f) => ['try', b, undefined, f]}
/ DEBUGGER SEMI ${(_, _2) => ['debugger']}
/ exprStatement;
# to be overridden. In Jessie, only blocks are accepted as arms
# of flow-of-control statements.
arm <- block;
# Allows for
# if (...) {} else if (...) {} else if (...) {};
elseArm <-
arm
/ IF LEFT_PAREN expr RIGHT_PAREN arm ELSE elseArm ${(_, _2, c, _3, t, _4, e) => ['if', c, t, e]}
/ IF LEFT_PAREN expr RIGHT_PAREN arm ${(_, _2, c, _3, t) => ['if', c, t]};
breakableStatement <-
FOR LEFT_PAREN declOp forOfBinding OF expr RIGHT_PAREN arm
${(_, _2, o, d, _3, e, _4, b) => ['forOf', o, d, e, b]}
/ FOR LEFT_PAREN declaration expr? SEMI expr? RIGHT_PAREN arm ${(_, _2, d, c, _3, i, _4, b) => ['for', d, c, i, b]}
/ WHILE LEFT_PAREN expr RIGHT_PAREN arm ${(_, _2, c, _3, b) => ['while', c, b]}
/ SWITCH LEFT_PAREN expr RIGHT_PAREN LEFT_BRACE clause* RIGHT_BRACE
${(_, _2, e, _3, _4, bs, _5) => ['switch', e, bs]};
# Each case clause must end in a terminating statement.
terminator <-
"continue" _NO_NEWLINE IDENT SEMI ${(_, label, _3) => ['continue', label]}
/ "continue" _WS SEMI ${(_, _2) => ['continue']}
/ "break" _NO_NEWLINE IDENT SEMI ${(_, label, _2) => ['break', label]}
/ "break" _WS SEMI ${(_, _2) => ['break']}
/ "return" _NO_NEWLINE expr SEMI ${(_, e, _2) => ['return', e]}
/ "return" _WS SEMI ${(_, _2) => ['return']}
/ "throw" _NO_NEWLINE expr SEMI ${(_, e, _3) => ['throw', e]};
block <- LEFT_BRACE body RIGHT_BRACE ${(_, b, _2) => ['block', b]};
body <- statementItem*;
# declaration must come first, so that PEG will prioritize
# function declarations over exprStatement.
statementItem <-
declaration
/ statement;
# No "class" declaration.
# No generator, async, or async iterator function.
declaration <-
declOp binding ** _COMMA SEMI ${(op, decls, _) => [op, decls]}
/ functionDecl;
declOp <- ("const" / "let") _WSN;
forOfBinding <- bindingPattern / defVar;
binding <-
bindingPattern EQUALS assignExpr ${(p, _, e) => ['bind', p, e]}
/ defVar EQUALS assignExpr ${(p, _, e) => ['bind', p, e]}
/ defVar;
bindingPattern <-
LEFT_BRACKET elementParam ** _COMMA RIGHT_BRACKET ${(_, ps, _2) => ['matchArray', ps]}
/ LEFT_BRACE propParam ** _COMMA RIGHT_BRACE ${(_, ps, _2) => ['matchRecord', ps]};
pattern <-
bindingPattern
/ defVar
/ undefined ${n => ['matchUndefined']}
/ dataLiteral ${n => ['matchData', JSON.parse(n)]}
/ HOLE ${h => ['patternHole', h]};
# to be overridden
elementParam <- param;
param <-
ELLIPSIS pattern ${(_, p) => ['rest', p]}
/ defVar EQUALS assignExpr ${(v, _, e) => ['optional', v, e]}
/ pattern;
propParam <-
ELLIPSIS pattern ${(_, p) => ['restObj', p]}
/ propName COLON pattern ${(k, _, p) => ['matchProp', k, p]}
/ defVar EQUALS assignExpr ${(id, _, e) => ['optionalProp', id[1], id, e]}
/ defVar ${id => ['matchProp', id[1], id]};
# Use PEG prioritized choice.
# TODO emit diagnostic for failure cases.
exprStatement <-
~cantStartExprStatement expr SEMI ${(e, _2) => e};
cantStartExprStatement <-
("{" / "function" / "async" _NO_NEWLINE "function"
/ "class" / "let" / "[") _WSN;
# to be overridden
terminatedBody <- ((~terminator statementItem)* terminator)+ ${(tb) => terminatedBlock(tb)};
clause <-
caseLabel+ LEFT_BRACE terminatedBody RIGHT_BRACE ${(cs, _, b, _2) => ['clause', cs, b]};
caseLabel <-
CASE expr COLON ${(_, e) => ['case', e]}
/ DEFAULT _WS COLON ${(_, _2) => ['default']};
catcher <- CATCH LEFT_PAREN pattern RIGHT_PAREN block ${(_, _2, p, _3, b) => ['catch', p, b]};
finalizer <- FINALLY block ${(_, b) => ['finally', b]};
# A.4 Functions and Classes
functionDecl <-
FUNCTION defVar LEFT_PAREN param ** _COMMA RIGHT_PAREN block
${(_, n, _2, p, _3, b) => ['functionDecl', n, p, b]};
functionExpr <-
FUNCTION defVar? LEFT_PAREN param ** _COMMA RIGHT_PAREN block
${(_, n, _2, p, _3, b) => ['functionExpr', n[0], p, b]};
# The assignExpr form must come after the block form, to make proper use
# of PEG prioritized choice.
arrowFunc <-
arrowParams _NO_NEWLINE ARROW block ${(ps, _2, b) => ['arrow', ps, b]}
/ arrowParams _NO_NEWLINE ARROW assignExpr ${(ps, _2, e) => ['lambda', ps, e]};
arrowParams <-
IDENT ${id => [['def', id]]}
/ LEFT_PAREN param ** _COMMA RIGHT_PAREN ${(_, ps, _2) => ps};
# to be extended
methodDef <-
method
/ GET propName LEFT_PAREN RIGHT_PAREN block ${(_, n, _2, _3, b) => ['getter', n, [], b]}
/ SET propName LEFT_PAREN param RIGHT_PAREN block ${(_, n, _2, p, _3, b) => ['setter', n, [p], b]};
method <-
propName LEFT_PAREN param ** _COMMA RIGHT_PAREN block ${(n, _, p, _2, b) => ['method', n, p, b]};
# A.5 Scripts and Modules
moduleBody <- moduleItem*;
moduleItem <-
SEMI ${_ => SKIP}
/ importDecl
/ exportDecl
/ moduleDeclaration;
useImport <- IDENT ${id => ['use', id]};
defImport <- IDENT ${id => ['def', id]};
moduleDeclaration <-
"const" _WSN moduleBinding ** _COMMA SEMI ${(op, decls) => [op, decls]};
# A properly hardened expression without side-effects.
hardenedExpr <-
dataLiteral ${d => ['data', JSON.parse(d)]}
/ undefined
/ "harden" _WS LEFT_PAREN (pureExpr / useImport) RIGHT_PAREN ${(fname, _2, expr, _3) =>
['call', ['use', fname], [expr]]}
/ useVar;
# Jessie modules only allow hardened module-level bindings.
moduleBinding <-
bindingPattern EQUALS hardenedExpr ${(p, _, e) => ['bind', p, e]}
/ defVar EQUALS hardenedExpr ${(p, _, e) => ['bind', p, e]}
/ defVar;
importClause <-
STAR AS defImport ${(_, _2, d) => ['importBind', [['as', '*', d[1]]]]}
/ namedImports ${(n) => ['importBind', n]}
/ defImport _COMMA STAR AS defImport ${(d, _, _2, d2) => ['importBind', [['as', 'default', d[1]],
['as', '*', d2[1]]]]}
/ defImport _COMMA namedImports ${(d, n) => ['importBind', [['as', 'default', d[1]], ...n]]}
/ defImport ${(d) => ['importBind', [['as', 'default', d[1]]]]};
safeImportClause <-
safeNamedImports ${(n) => ['importBind', n]};
importSpecifier <-
IDENT_NAME AS defImport ${(i, _, d) => ['as', i, d[1]]}
/ defImport ${(d) => ['as', d[1], d[1]]};
# Safe imports don't need to be prefixed.
safeImportSpecifier <-
IDENT_NAME AS defVar ${(i, _, d) => ['as', i, d[1]]}
/ defVar ${(d) => ['as', d[1], d[1]]};
namedImports <-
LEFT_BRACE importSpecifier ** _COMMA _COMMA? RIGHT_BRACE ${(_, s, _2) => s};
safeNamedImports <-
LEFT_BRACE safeImportSpecifier ** _COMMA _COMMA? RIGHT_BRACE ${(_, s, _2) => s};
safeModule <-
STRING ${(s) => JSON.parse(s)};
importDecl <-
IMPORT importClause FROM STRING SEMI ${(_, v, _2, s, _3) => ['import', v, JSON.parse(s)]}
/ IMPORT safeImportClause FROM safeModule SEMI ${(_, v, _2, s, _3) => ['import', v, s]};
exportDecl <-
EXPORT DEFAULT exportableExpr SEMI ${(_, _2, e, _3) => ['exportDefault', e]}
/ EXPORT moduleDeclaration ${(_, d) => ['export', ...d]};
# to be extended
exportableExpr <- hardenedExpr;
# Lexical syntax
ARROW <- "=>" _WS;
AS <- "as" _WSN;
DEBUGGER <- "debugger" _WSN;
PLUSPLUS <- "++" _WSN;
MINUSMINUS <- "--" _WSN;
CASE <- "case" _WSN;
IF <- "if" _WSN;
ELSE <- "else" _WSN;
FOR <- "for" _WSN;
OF <- "of" _WSN;
WHILE <- "while" _WSN;
BREAK <- "break" _WSN;
CONTINUE <- "continue" _WSN;
SWITCH <- "switch" _WSN;
TRY <- "try" _WSN;
CATCH <- "catch" _WSN;
FINALLY <- "finally" _WSN;
GET <- "get" _WSN;
SET <- "set" _WSN;
IMPORT <- "import" _WSN;
EXPORT <- "export" _WSN;
FROM <- "from" _WSN;
FUNCTION <- "function" _WSN;
DEFAULT <- "default" _WSN;
EQUALS <- "=" _WS;
SEMI <- ";" _WS;
STAR <- "*" _WS;
`;
const jessieExprTag = peg.extends(jessieTag)`
# Jump to the expr production.
start <- _WS expr _EOF ${e => (..._a: any[]) => e};
`;
return [jessieTag, jessieExprTag];
};
export default makeJessie;