-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathquasi-justin.js.ts
296 lines (238 loc) · 9.54 KB
/
quasi-justin.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
// Subsets of JavaScript, starting from the grammar as defined at
// http://www.ecma-international.org/ecma-262/9.0/#sec-grammar-summary
// Justin is the safe JavaScript expression language, a potentially
// pure terminating superset of JSON and subset of Jessie, that relieves
// many of the pain points of using JSON as a data format:
// * unquoted indentifier property names.
// * comments.
// * multi-line strings (via template literals).
// * undefined.
// * includes all floating point values: NaN, Infinity, -Infinity
// * will include BigInt once available.
// Justin also includes most pure JavaScript expressions. Justin does not
// include function expressions or variable or function
// definitions. However, it does include free variable uses and
// function calls; so the purity and termination of Justin depends on
// the endowments provided for these free variable bindings.
// Justin is defined to be extended into the Jessie grammar, which is
// defined to be extended into the JavaScript grammar.
// See https://github.com/Agoric/Jessie/blob/master/README.md
// for documentation of the Jessie grammar.
// Justin is defined to be extended into the Chainmail grammar, to
// provide its expression language in a JS-like style. Chainmail
// expressions need to be pure and should be terminating.
/// <reference path="peg.d.ts"/>
import {qunpack} from './quasi-utils.js';
const binary = (left: PegExpr, rights: any[]) => {
return rights.reduce<PegExpr>((prev, [op, right]) => [op, prev, right], left);
};
const transformSingleQuote = (s: string) => {
let i = 0, qs = '';
while (i < s.length) {
const c = s.slice(i, i + 1);
if (c === '\\') {
// Skip one char.
qs += s.slice(i, i + 2);
i += 2;
} else if (c === '"') {
// Quote it.
qs += '\\"';
i ++;
} else {
// Add it directly.
qs += c;
i ++;
}
}
return `"${qs}"`;
};
const makeJustin = (peg: IPegTag<IParserTag<any>>) => {
const {SKIP} = peg;
return peg`
# to be overridden or inherited
start <- _WS assignExpr _EOF ${v => (..._a: any[]) => v};
# A.1 Lexical Grammar
DOT <- "." _WS;
ELLIPSIS <- "..." _WS;
LEFT_PAREN <- "(" _WS;
PLUS <- "+" _WS;
QUESTION <- "?" _WS;
RIGHT_PAREN <- ")" _WS;
STARSTAR <- "**" _WS;
# Define Javascript-style comments.
_WS <- super._WS (EOL_COMMENT / MULTILINE_COMMENT)? ${_ => SKIP};
EOL_COMMENT <- "//" (~[\n\r] .)* _WS;
MULTILINE_COMMENT <- "/*" (~"*/" .)* "*/" _WS;
# Add single-quoted strings.
STRING <-
super.STRING
/ "'" < (~"'" character)* > "'" _WS ${s => transformSingleQuote(s)};
# Only match if whitespace doesn't contain newline
_NO_NEWLINE <- ~IDENT [ \t]* ${_ => SKIP};
IDENT_NAME <- ~(HIDDEN_PFX / "__proto__" _WSN) (IDENT / RESERVED_WORD);
IDENT <-
~(HIDDEN_PFX / IMPORT_PFX / RESERVED_WORD)
< [$A-Za-z_] [$A-Za-z0-9_]* > _WSN;
HIDDEN_PFX <- "$h_";
IMPORT_PFX <- "$i_";
# Omit "async", "arguments", "eval", "get", and "set" from IDENT
# in Justin even though ES2017 considers them in IDENT.
RESERVED_WORD <-
(KEYWORD / RESERVED_KEYWORD / FUTURE_RESERVED_WORD
/ "null" / "false" / "true"
/ "async" / "arguments" / "eval" / "get" / "set") _WSN;
KEYWORD <-
("break"
/ "case" / "catch" / "const" / "continue"
/ "debugger" / "default"
/ "else" / "export"
/ "finally" / "for" / "function"
/ "if" / "import"
/ "return"
/ "switch"
/ "throw" / "try" / "typeof"
/ "void"
/ "while") _WSN;
# Unused by Justin but enumerated here, in order to omit them
# from the IDENT token.
RESERVED_KEYWORD <-
("class"
/ "delete" / "do"
/ "extends"
/ "instanceof"
/ "in"
/ "new"
/ "super"
/ "this"
/ "var"
/ "with"
/ "yield") _WSN;
FUTURE_RESERVED_WORD <-
("await" / "enum"
/ "implements" / "package" / "protected"
/ "interface" / "private" / "public") _WSN;
# Quasiliterals aka template literals
QUASI_CHAR <- "\\" . / ~"\`" .;
QUASI_ALL <- "\`" < (~"\${" QUASI_CHAR)* > "\`" _WS;
QUASI_HEAD <- "\`" < (~"\${" QUASI_CHAR)* > "\${" _WS;
QUASI_MID <- "}" < (~"\${" QUASI_CHAR)* > "\${" _WS;
QUASI_TAIL <- "}" < (~"\${" QUASI_CHAR)* > "\`" _WS;
# A.2 Expressions
undefined <-
"undefined" _WSN ${_ => ['undefined']};
dataStructure <-
undefined
/ super.dataStructure;
# Optional trailing commas.
record <-
super.record
/ LEFT_BRACE propDef ** _COMMA _COMMA? RIGHT_BRACE ${(_, ps, _2) => ['record', ps]};
array <-
super.array
/ LEFT_BRACKET element ** _COMMA _COMMA? RIGHT_BRACKET ${(_, es, _2) => ['array', es]};
useVar <- IDENT ${id => ['use', id]};
# Justin does not contain variable definitions, only uses. However,
# multiple languages that extend Justin will contain defining
# occurrences of variable names, so we put the defVar production
# here.
defVar <- IDENT ${id => ['def', id]};
primaryExpr <-
super.primaryExpr
/ quasiExpr
/ LEFT_PAREN expr RIGHT_PAREN ${(_, e, _2) => e}
/ useVar;
pureExpr <-
super.pureExpr
/ LEFT_PAREN pureExpr RIGHT_PAREN ${(_, e, _2) => e}
/ useVar;
element <-
super.element
/ ELLIPSIS assignExpr ${(_, e) => ['spread', e]};
propDef <-
super.propDef
/ useVar ${u => ['prop', u[1], u]}
/ ELLIPSIS assignExpr ${(_, e) => ['spreadObj', e]};
purePropDef <-
super.purePropDef
/ useVar ${u => ['prop', u[1], u]}
/ ELLIPSIS assignExpr ${(_, e) => ['spreadObj', e]};
# No computed property name
propName <-
super.propName
/ IDENT_NAME
/ NUMBER;
quasiExpr <-
QUASI_ALL ${q => ['quasi', [q]]}
/ QUASI_HEAD expr ** QUASI_MID QUASI_TAIL ${(h, ms, t) => ['quasi', qunpack(h, ms, t)]};
# to be extended
memberPostOp <-
LEFT_BRACKET indexExpr RIGHT_BRACKET ${(_, e, _3) => ['index', e]}
/ DOT IDENT_NAME ${(_, id) => ['get', id]}
/ quasiExpr ${q => ['tag', q]};
# to be extended
callPostOp <-
memberPostOp
/ args ${args => ['call', args]};
# Because Justin and Jessie have no "new" or "super", they don't need
# to distinguish callExpr from memberExpr. So justin omits memberExpr
# and newExpr. Instead, in Justin, callExpr jumps directly to
# primaryExpr and updateExpr jumps directly to callExpr.
# to be overridden.
callExpr <- primaryExpr callPostOp* ${binary};
# To be overridden rather than inherited.
# Introduced to impose a non-JS restriction
# Restrict index access to number-names, including
# floating point, NaN, Infinity, and -Infinity.
indexExpr <-
NUMBER ${n => ['data', JSON.parse(n)]}
/ PLUS unaryExpr ${(_, e) => [`pre:+`, e]};
args <- LEFT_PAREN arg ** _COMMA RIGHT_PAREN ${(_, args, _2) => args};
arg <-
assignExpr
/ ELLIPSIS assignExpr ${(_, e) => ['spread', e]};
# to be overridden
updateExpr <- callExpr;
unaryExpr <-
preOp unaryExpr ${(op, e) => [op, e]}
/ updateExpr;
# to be extended
# No prefix or postfix "++" or "--".
# No "delete".
preOp <- (("void" / "typeof") _WSN / prePre);
prePre <- ("+" / "-" / "~" / "!") _WS ${op => `pre:${op}`};
# Different communities will think -x**y parses in different ways,
# so the EcmaScript grammar forces parens to disambiguate.
powExpr <-
updateExpr STARSTAR powExpr ${(x, op, y) => [op, x, y]}
/ unaryExpr;
multExpr <- powExpr (multOp powExpr)* ${binary};
addExpr <- multExpr (addOp multExpr)* ${binary};
shiftExpr <- addExpr (shiftOp addExpr)* ${binary};
# Non-standard, to be overridden
# In C-like languages, the precedence and associativity of the
# relational, equality, and bitwise operators is surprising, and
# therefore hazardous. Here, none of these associate with the
# others, forcing parens to disambiguate.
eagerExpr <- shiftExpr (eagerOp shiftExpr)? ${binary};
andThenExpr <- eagerExpr (andThenOp eagerExpr)* ${binary};
orElseExpr <- andThenExpr (orElseOp andThenExpr)* ${binary};
multOp <- ("*" / "/" / "%") _WS;
addOp <- ("+" / "-") _WS;
shiftOp <- ("<<" / ">>>" / ">>") _WS;
relOp <- ("<=" / "<" / ">=" / ">") _WS;
eqOp <- ("===" / "!==") _WS;
bitOp <- ("&" / "^" / "|") _WS;
eagerOp <- relOp / eqOp / bitOp;
andThenOp <- "&&" _WS;
orElseOp <- "||" _WS;
condExpr <-
orElseExpr QUESTION assignExpr COLON assignExpr ${(c, _, t, _2, e) => ['cond', c, t, e]}
/ orElseExpr;
# override, to be extended
assignExpr <- condExpr;
# The comma expression is not in Jessie because we
# opt to pass only insulated expressions as the this-binding.
expr <- assignExpr;
`;
};
export default makeJustin;