-
Notifications
You must be signed in to change notification settings - Fork 1
/
babylonPlugin.js
381 lines (321 loc) · 11.4 KB
/
babylonPlugin.js
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
//Plugin for Babel's parser (Babylon)
//TEMP
const log = console.log;
import { types as tt } from "babylon/lib/tokenizer/types";
import Parser from "babylon/lib/parser";
export default function (instance) {
instance.extend("parseExpressionStatement", function (inner) {
return function (node, expr) {
if (expr.type === "Identifier") {
if (expr.name === "context") {
return this.dci_parseContext(node);
}
else if (expr.name === "role") {
return this.dci_parseRole(node);
}
}
return inner.call(this, node, expr);
};
});
instance.extend("parseExport", function (inner) {
return function(node) {
//Copied and pasted from babylon/src/parser/statement.js
//with small modifications to allow DCI contexts declared with the `context` keyword
//to be exported (modifications indicated with 'DCI' comments).
this.next();
// export * from '...'
if (this.match(tt.star)) {
let specifier = this.startNode();
this.next();
if (this.hasPlugin("exportExtensions") && this.eatContextual("as")) {
specifier.exported = this.parseIdentifier();
node.specifiers = [this.finishNode(specifier, "ExportNamespaceSpecifier")];
this.parseExportSpecifiersMaybe(node);
this.parseExportFrom(node, true);
} else {
this.parseExportFrom(node, true);
return this.finishNode(node, "ExportAllDeclaration");
}
} else if (this.hasPlugin("exportExtensions") && this.isExportDefaultSpecifier()) {
let specifier = this.startNode();
specifier.exported = this.parseIdentifier(true);
node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
if (this.match(tt.comma) && this.lookahead().type === tt.star) {
this.expect(tt.comma);
let specifier = this.startNode();
this.expect(tt.star);
this.expectContextual("as");
specifier.exported = this.parseIdentifier();
node.specifiers.push(this.finishNode(specifier, "ExportNamespaceSpecifier"));
} else {
this.parseExportSpecifiersMaybe(node);
}
this.parseExportFrom(node, true);
} else if (this.eat(tt._default)) { // export default ...
let expr = this.startNode();
let needsSemi = false;
if (this.eat(tt._function)) {
expr = this.parseFunction(expr, true, false, false, true);
} else if (this.match(tt._class)) {
expr = this.parseClass(expr, true, true);
//DCI
//ADDED FOR DCI
} else if (this.state.value === 'context') {
this.next();
expr = this.dci_parseContext(expr);
} else {
needsSemi = true;
expr = this.parseMaybeAssign();
}
node.declaration = expr;
if (needsSemi) this.semicolon();
this.checkExport(node);
return this.finishNode(node, "ExportDefaultDeclaration");
//DCI modified
} else if (this.state.type.keyword || this.state.value === 'context' || this.shouldParseExportDeclaration()) {
//} else if (this.state.type.keyword || this.shouldParseExportDeclaration()) {
node.specifiers = [];
node.source = null;
node.declaration = this.parseExportDeclaration(node);
} else { // export { x, y as z } [from '...']
node.declaration = null;
node.specifiers = this.parseExportSpecifiers();
this.parseExportFrom(node);
}
this.checkExport(node);
return this.finishNode(node, "ExportNamedDeclaration");
};
});
//COPIED FROM babylon/src/plugins/flow.js
//We need this to parse role-player contracts since the flow parser plugin hasn't
//been fully initialized yet when the DCI Babylon plugin runs
//(the flow plugin is always added last.)
//
// don't consider `void` to be a keyword as then it'll use the void token type
// and set startExpr
instance.extend("isKeyword", function (inner) {
return function (name) {
if (this.state.inType && name === "void") {
return false;
} else {
return inner.call(this, name);
}
};
});
}
let pp = Parser.prototype;
// Roles
pp.dci_parseRole = function (node) {
node.id = this.parseIdentifier();
this.dci_parseRoleBody(node);
return this.finishNode(node, "RoleDeclaration");
};
//Parse role body
//Adapted from parseClassBody(),
//https://github.com/babel/babel/blob/master/packages/babylon/src/parser/statement.js
pp.dci_parseRoleBody = function (node) {
let roleBody = this.startNode();
roleBody.body = [];
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
if (this.eat(tt.semi)) {
continue;
}
let method = this.startNode();
let isGenerator = this.eat(tt.star);
let isGetSet = false;
let isAsync = false;
this.parsePropertyName(method);
let isAsyncMethod = this.hasPlugin("asyncFunctions") && !this.match(tt.parenL) && !method.computed && method.key.type === "Identifier" && method.key.name === "async";
if (isAsyncMethod) {
if (this.hasPlugin("asyncGenerators") && this.eat(tt.star)) isGenerator = true;
isAsync = true;
this.parsePropertyName(method);
}
method.kind = "method";
if (!method.computed) {
let { key } = method;
// handle get/set methods
// eg. class Foo { get bar() {} set bar() {} }
if (!isAsync && !isGenerator && key.type === "Identifier" && !this.match(tt.parenL) && (key.name === "get" || key.name === "set")) {
isGetSet = true;
method.kind = key.name;
key = this.parsePropertyName(method);
}
}
this.dci_parseRoleMethod(roleBody, method, isGenerator, isAsync);
// get methods aren't allowed to have any parameters
// set methods must have exactly 1 parameter
if (isGetSet) {
let paramCount = method.kind === "get" ? 0 : 1;
if (method.params.length !== paramCount) {
let start = method.start;
if (method.kind === "get") {
this.raise(start, "getter should have no params");
} else {
this.raise(start, "setter should have exactly one param");
}
}
}
}
//Role-Player contracts
if (this.match(tt.name) && this.state.value === 'contract') {
let id = this.parseIdentifier();
if (id.name === 'contract') {
//use flow syntax plugin to parse the contract
let contract = this.flowParseObjectType();
contract.type = 'RolePlayerContract';
node.contract = contract;
//let contract = this.startNode();
//this.expect(tt.braceL);
//while (!this.eat(tt.braceR)) {
//
//}
//this.finishNode(contract, "RolePlayerContract");
}
}
node.body = this.finishNode(roleBody, "RoleDeclarationBody");
};
pp.dci_parseRoleMethod = function (roleBody, method, isGenerator, isAsync) {
this.parseMethod(method, isGenerator, isAsync);
roleBody.body.push(this.finishNode(method, "RoleMethod"));
};
// Context declarations (context MyContext {...})
pp.dci_parseContext = function (node) {
node.id = this.parseIdentifier();
this.dci_parseContextBody(node);
return this.finishNode(node, "ContextDeclaration");
};
//Parse context body
//Adapted from parseClassBody(),
//https://github.com/babel/babel/blob/master/packages/babylon/src/parser/statement.js
pp.dci_parseContextBody = function (node) {
// context bodies are implicitly strict
let oldStrict = this.state.strict;
this.state.strict = true;
let hadConstructorCall = false;
let hadConstructor = false;
let decorators = [];
let ctxBody = this.startNode();
ctxBody.body = [];
this.expect(tt.braceL);
while (!this.eat(tt.braceR)) {
if (this.eat(tt.semi)) {
continue;
}
//TODO?
//annotations
/*
if (this.match(tt.at)) {
}
*/
let member = this.startNode();
this.parsePropertyName(member);
if (member.key.type === 'Identifier' && member.key.name === 'role') {
ctxBody.body.push(this.dci_parseRole(member));
continue;
}
let method = member;
//let isConstructorCall = false;
let isGenerator = this.eat(tt.star);
let isGetSet = false;
let isAsync = false;
if (!isGenerator && method.key.type === "Identifier" && !method.computed) {
if (this.dci_isContextProperty()) {
ctxBody.body.push(this.dci_parseContextProperty(method));
continue;
}
/*
if (this.hasPlugin("classConstructorCall") && method.key.name === "call" && this.match(tt.name) && this.state.value === "constructor") {
isConstructorCall = true;
this.parsePropertyName(method);
}
*/
}
let isAsyncMethod = this.hasPlugin("asyncFunctions") && !this.match(tt.parenL) && !method.computed && method.key.type === "Identifier" && method.key.name === "async";
if (isAsyncMethod) {
if (this.hasPlugin("asyncGenerators") && this.eat(tt.star)) isGenerator = true;
isAsync = true;
this.parsePropertyName(method);
}
method.kind = "method";
if (!method.computed) {
let { key } = method;
// handle get/set methods
// eg. class Foo { get bar() {} set bar() {} }
if (!isAsync && !isGenerator && key.type === "Identifier" && !this.match(tt.parenL) && (key.name === "get" || key.name === "set")) {
isGetSet = true;
method.kind = key.name;
key = this.parsePropertyName(method);
}
// disallow invalid constructors
let isConstructor = (key.type === "Identifier" && key.name === "constructor")
|| (key.type === "StringLiteral" && key.value === "constructor");
/*
let isConstructor = !isConstructorCall && (
(key.type === "Identifier" && key.name === "constructor") ||
(key.type === "StringLiteral" && key.value === "constructor")
);
*/
if (isConstructor) {
if (hadConstructor) this.raise(key.start, "Duplicate constructor in the same DCI context");
if (isGetSet) this.raise(key.start, "Constructor can't have get/set modifier");
if (isGenerator) this.raise(key.start, "Constructor can't be a generator");
if (isAsync) this.raise(key.start, "Constructor can't be an async function");
method.kind = "constructor";
hadConstructor = true;
}
}
/*
// convert constructor to a constructor call
if (isConstructorCall) {
if (hadConstructorCall) this.raise(method.start, "Duplicate constructor call in the same DCI context");
method.kind = "constructorCall";
hadConstructorCall = true;
}
*/
// disallow decorators on class constructors
if ((method.kind === "constructor" || method.kind === "constructorCall") && method.decorators) {
this.raise(method.start, "You can't attach decorators to a DCI context constructor");
}
this.dci_parseContextMethod(ctxBody, method, isGenerator, isAsync);
// get methods aren't allowed to have any parameters
// set methods must have exactly 1 parameter
if (isGetSet) {
let paramCount = method.kind === "get" ? 0 : 1;
if (method.params.length !== paramCount) {
let start = method.start;
if (method.kind === "get") {
this.raise(start, "getter should have no params");
} else {
this.raise(start, "setter should have exactly one param");
}
}
}
}
if (decorators.length) {
this.raise(this.state.start, "You have trailing decorators with no method");
}
node.body = this.finishNode(ctxBody, "ContextBody");
this.state.strict = oldStrict;
};
pp.dci_parseContextMethod = function (ctxBody, method, isGenerator, isAsync) {
this.parseMethod(method, isGenerator, isAsync);
ctxBody.body.push(this.finishNode(method, "ContextMethod"));
};
pp.dci_isContextProperty = function () {
return this.match(tt.colon) || this.match(tt.eq) || this.isLineTerminator();
};
pp.dci_parseContextProperty = function (node) {
if (this.match(tt.colon)) {
node.typeAnnotation = this.flowParseTypeAnnotation();
}
if (this.match(tt.eq)) {
this.next();
node.value = this.parseMaybeAssign();
} else {
node.value = null;
}
this.semicolon();
return this.finishNode(node, "ContextProperty");
};