-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
expressions.js
668 lines (603 loc) · 19 KB
/
expressions.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
"use strict";
const { tokenMatcher } = require("chevrotain");
function defineRules($, t) {
$.RULE("constantExpression", () => {
$.SUBRULE($.expression);
});
$.RULE("expression", () => {
$.OR([
{
GATE: () => this.BACKTRACK_LOOKAHEAD($.isLambdaExpression),
ALT: () => $.SUBRULE($.lambdaExpression)
},
{ ALT: () => $.SUBRULE($.ternaryExpression) }
]);
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-LambdaExpression
$.RULE("lambdaExpression", () => {
$.SUBRULE($.lambdaParameters);
$.CONSUME(t.Arrow);
$.SUBRULE($.lambdaBody);
});
$.RULE("lambdaParameters", () => {
$.OR([
{ ALT: () => $.SUBRULE($.lambdaParametersWithBraces) },
{ ALT: () => $.CONSUME(t.Identifier) }
]);
});
$.RULE("lambdaParametersWithBraces", () => {
$.CONSUME(t.LBrace);
$.OPTION(() => {
$.SUBRULE($.lambdaParameterList);
});
$.CONSUME(t.RBrace);
});
$.RULE("lambdaParameterList", () => {
$.OR([
{
GATE: () => {
const nextTokType = this.LA(1).tokenType;
const nextNextTokType = this.LA(2).tokenType;
return (
tokenMatcher(nextTokType, t.Identifier) &&
(tokenMatcher(nextNextTokType, t.RBrace) ||
tokenMatcher(nextNextTokType, t.Comma))
);
},
ALT: () => $.SUBRULE($.inferredLambdaParameterList)
},
{ ALT: () => $.SUBRULE($.explicitLambdaParameterList) }
]);
});
$.RULE("inferredLambdaParameterList", () => {
$.CONSUME(t.Identifier);
$.MANY(() => {
$.CONSUME(t.Comma);
$.CONSUME2(t.Identifier);
});
});
$.RULE("explicitLambdaParameterList", () => {
$.SUBRULE($.lambdaParameter);
$.MANY(() => {
$.CONSUME(t.Comma);
$.SUBRULE2($.lambdaParameter);
});
});
$.RULE("lambdaParameter", () => {
// TODO: performance, investigate the performance boost that could
// be gained by refactoring out the backtracking.
$.OR([
{
GATE: $.BACKTRACK($.regularLambdaParameter),
ALT: () => $.SUBRULE($.regularLambdaParameter)
},
{ ALT: () => $.SUBRULE($.variableArityParameter) }
]);
});
$.RULE("regularLambdaParameter", () => {
$.MANY(() => {
$.SUBRULE($.variableModifier);
});
$.SUBRULE($.lambdaParameterType);
$.SUBRULE($.variableDeclaratorId);
});
$.RULE("lambdaParameterType", () => {
$.OR([
{ ALT: () => $.SUBRULE($.unannType) },
{ ALT: () => $.CONSUME(t.Var) }
]);
});
$.RULE("lambdaBody", () => {
$.OR([
{ ALT: () => $.SUBRULE($.expression) },
{ ALT: () => $.SUBRULE($.block) }
]);
});
$.RULE("ternaryExpression", () => {
$.SUBRULE($.binaryExpression);
$.OPTION(() => {
$.CONSUME(t.QuestionMark);
$.SUBRULE($.expression);
$.CONSUME(t.Colon);
// TODO: in the grammar this is limited to "lambdaExpression: or "conditionalExpression"
$.SUBRULE2($.expression);
});
});
$.RULE("binaryExpression", () => {
$.SUBRULE($.unaryExpression);
$.MANY(() => {
$.OR([
{
ALT: () => {
$.CONSUME(t.Instanceof);
$.SUBRULE($.referenceType);
}
},
{
ALT: () => {
$.CONSUME(t.AssignmentOperator);
$.SUBRULE2($.expression);
}
},
// This is an example of why Java does not have a well designed grammar
// See: https://manas.tech/blog/2008/10/12/why-java-generics-dont-have-problems-with-right-shift-operator.html
// TODO: ensure the LT/GT sequences have no whitespace between each other.
{
// TODO: this is a bug in Chevrotain lookahead calculation. the "BinaryOperator" token can match "Less" or "Greater"
// as well, but because it is a **token Category** Chevrotain does not understand it need to looks two tokens ahead.
GATE: () =>
tokenMatcher($.LA(2).tokenType, t.Less) ||
tokenMatcher($.LA(2).tokenType, t.Greater),
ALT: () => {
$.OR2([
{
GATE: () => $.LA(1).startOffset + 1 === $.LA(2).startOffset,
ALT: () => {
$.CONSUME(t.Less);
$.CONSUME2(t.Less);
}
},
{
GATE: () => $.LA(1).startOffset + 1 === $.LA(2).startOffset,
ALT: () => {
$.CONSUME(t.Greater);
$.CONSUME2(t.Greater);
$.OPTION({
GATE: () => $.LA(0).startOffset + 1 === $.LA(1).startOffset,
DEF: () => $.CONSUME3(t.Greater)
});
}
}
]);
$.SUBRULE2($.unaryExpression);
}
},
{
ALT: () => {
$.CONSUME(t.BinaryOperator);
$.SUBRULE3($.unaryExpression);
}
}
]);
});
});
$.RULE("unaryExpression", () => {
$.MANY(() => {
$.CONSUME(t.UnaryPrefixOperator);
});
$.SUBRULE($.primary);
$.MANY2(() => {
$.CONSUME(t.UnarySuffixOperator);
});
});
$.RULE("unaryExpressionNotPlusMinus", () => {
$.MANY(() => {
$.CONSUME(t.UnaryPrefixOperatorNotPlusMinus);
});
$.SUBRULE($.primary);
$.MANY2(() => {
$.CONSUME(t.UnarySuffixOperator);
});
});
$.RULE("primary", () => {
$.SUBRULE($.primaryPrefix);
$.MANY(() => {
$.SUBRULE($.primarySuffix);
});
});
$.RULE("primaryPrefix", () => {
let isCastExpression = false;
if (tokenMatcher($.LA(1).tokenType, t.LBrace)) {
isCastExpression = this.BACKTRACK_LOOKAHEAD($.isCastExpression);
}
$.OR([
{ ALT: () => $.SUBRULE($.literal) },
{ ALT: () => $.CONSUME(t.This) },
{ ALT: () => $.CONSUME(t.Void) },
// should be extracted to primitive type with optional dims suffix?
{ ALT: () => $.SUBRULE($.numericType) },
{ ALT: () => $.CONSUME(t.Boolean) },
{ ALT: () => $.SUBRULE($.fqnOrRefType) },
{
GATE: () => isCastExpression,
ALT: () => $.SUBRULE($.castExpression)
},
{ ALT: () => $.SUBRULE($.parenthesisExpression) },
{ ALT: () => $.SUBRULE($.newExpression) }
]);
});
$.RULE("primarySuffix", () => {
$.OR([
{
ALT: () => {
$.CONSUME(t.Dot);
$.OR2([
{ ALT: () => $.CONSUME(t.This) },
{
ALT: () => $.SUBRULE($.unqualifiedClassInstanceCreationExpression)
},
{
ALT: () => {
$.OPTION(() => {
$.SUBRULE($.typeArguments);
});
$.CONSUME(t.Identifier);
}
}
]);
}
},
{ ALT: () => $.SUBRULE($.methodInvocationSuffix) },
{ ALT: () => $.SUBRULE($.classLiteralSuffix) },
{ ALT: () => $.SUBRULE($.arrayAccessSuffix) },
{ ALT: () => $.SUBRULE($.methodReferenceSuffix) }
]);
});
$.RULE("fqnOrRefType", () => {
$.SUBRULE($.fqnOrRefTypePart);
$.MANY2({
// ".class" is a classLiteralSuffix
GATE: () =>
// avoids ambiguity with ".this" and ".new" which are parsed as a primary suffix.
tokenMatcher(this.LA(2).tokenType, t.Class) === false &&
tokenMatcher(this.LA(2).tokenType, t.This) === false &&
tokenMatcher(this.LA(2).tokenType, t.New) === false,
DEF: () => {
$.CONSUME(t.Dot);
$.SUBRULE2($.fqnOrRefTypePart);
}
});
// in case of an arrayType
$.OPTION({
// it is not enough to check only the opening "[", we must avoid conflict with
// arrayAccessSuffix
GATE: () =>
tokenMatcher($.LA(1).tokenType, t.At) ||
tokenMatcher($.LA(2).tokenType, t.RSquare),
DEF: () => {
$.SUBRULE($.dims);
}
});
});
// TODO: validation:
// 1. "annotation" cannot be mixed with "methodTypeArguments" or "Super".
// 2. "methodTypeArguments" cannot be mixed with "classTypeArguments" or "annotation".
// 3. "Super" cannot be mixed with "classTypeArguments" or "annotation".
// 4. At most one "Super" may be used.
// 5. "Super" may be last or one before last (last may also be first if there is only a single part).
$.RULE("fqnOrRefTypePart", () => {
$.MANY(() => {
$.SUBRULE($.annotation);
});
$.OPTION({
NAME: "$methodTypeArguments",
DEF: () => {
$.SUBRULE2($.typeArguments);
}
});
$.OR([
{ ALT: () => $.CONSUME(t.Identifier) },
{ ALT: () => $.CONSUME(t.Super) }
]);
let isRefTypeInMethodRef = false;
// Performance optimization, only perform this backtracking when a '<' is found
// TODO: performance optimization evaluation: avoid doing this backtracking for every "<" encountered.
// we could do it once (using global state) per "fqnOrRefType"
// We could do it only once for
if (tokenMatcher($.LA(1).tokenType, t.Less)) {
isRefTypeInMethodRef = this.BACKTRACK_LOOKAHEAD($.isRefTypeInMethodRef);
}
$.OPTION2({
NAME: "$classTypeArguments",
// unrestricted typeArguments here would create an ambiguity with "LessThan" operator
// e.g: "var x = a < b;"
// The "<" would be parsed as the beginning of a "typeArguments"
// and we will get an error: "expecting '>' but found: ';'"
GATE: () => isRefTypeInMethodRef,
DEF: () => {
$.SUBRULE3($.typeArguments);
}
});
});
$.RULE("parenthesisExpression", () => {
$.CONSUME(t.LBrace);
$.SUBRULE($.expression);
$.CONSUME(t.RBrace);
});
$.RULE("castExpression", () => {
$.OR([
{
// TODO: performance: can avoid backtracking again here, parent rule could have this information
// when it checks isCastExpression (refactor needed)
GATE: () => this.BACKTRACK_LOOKAHEAD($.isPrimitiveCastExpression),
ALT: () => $.SUBRULE($.primitiveCastExpression)
},
{ ALT: () => $.SUBRULE($.referenceTypeCastExpression) }
]);
});
$.RULE("primitiveCastExpression", () => {
$.CONSUME(t.LBrace);
$.SUBRULE($.primitiveType);
$.CONSUME(t.RBrace);
$.SUBRULE($.unaryExpression);
});
$.RULE("referenceTypeCastExpression", () => {
$.CONSUME(t.LBrace);
$.SUBRULE($.referenceType);
$.MANY(() => {
$.SUBRULE($.additionalBound);
});
$.CONSUME(t.RBrace);
$.OR([
{
GATE: () => this.BACKTRACK_LOOKAHEAD($.isLambdaExpression),
ALT: () => $.SUBRULE($.lambdaExpression)
},
{ ALT: () => $.SUBRULE($.unaryExpressionNotPlusMinus) }
]);
});
const newExpressionTypes = {
arrayCreationExpression: 1,
unqualifiedClassInstanceCreationExpression: 2
};
$.RULE("newExpression", () => {
const type = this.BACKTRACK_LOOKAHEAD($.identifyNewExpressionType);
$.OR([
{
GATE: () => type === newExpressionTypes.arrayCreationExpression,
ALT: () => $.SUBRULE($.arrayCreationExpression)
},
{
GATE: () =>
type ===
newExpressionTypes.unqualifiedClassInstanceCreationExpression,
ALT: () => $.SUBRULE($.unqualifiedClassInstanceCreationExpression)
}
]);
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-UnqualifiedClassInstanceCreationExpression
$.RULE("unqualifiedClassInstanceCreationExpression", () => {
$.CONSUME(t.New);
$.OPTION(() => {
$.SUBRULE($.typeArguments);
});
$.SUBRULE($.classOrInterfaceTypeToInstantiate);
$.CONSUME(t.LBrace);
$.OPTION2(() => {
$.SUBRULE($.argumentList);
});
$.CONSUME(t.RBrace);
$.OPTION3(() => {
$.SUBRULE($.classBody);
});
});
$.RULE("classOrInterfaceTypeToInstantiate", () => {
$.MANY(() => {
$.SUBRULE($.annotation);
});
$.CONSUME(t.Identifier);
$.MANY2(() => {
$.CONSUME(t.Dot);
$.MANY3(() => {
$.SUBRULE2($.annotation);
});
$.CONSUME2(t.Identifier);
});
$.OPTION(() => {
$.SUBRULE($.typeArgumentsOrDiamond);
});
});
$.RULE("typeArgumentsOrDiamond", () => {
$.OR([
{ ALT: () => $.SUBRULE($.diamond) },
{ ALT: () => $.SUBRULE($.typeArguments) }
]);
});
$.RULE("diamond", () => {
$.CONSUME(t.Less);
$.CONSUME(t.Greater);
});
$.RULE("methodInvocationSuffix", () => {
$.CONSUME(t.LBrace);
$.OPTION2(() => {
$.SUBRULE($.argumentList);
});
$.CONSUME(t.RBrace);
});
$.RULE("argumentList", () => {
$.SUBRULE($.expression);
$.MANY(() => {
$.CONSUME(t.Comma);
$.SUBRULE2($.expression);
});
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.10.1
$.RULE("arrayCreationExpression", () => {
$.CONSUME(t.New);
$.OR([
{
GATE: $.BACKTRACK($.primitiveType),
ALT: () => $.SUBRULE($.primitiveType)
},
{ ALT: () => $.SUBRULE($.classOrInterfaceType) }
]);
$.OR2([
{
GATE: $.BACKTRACK($.arrayCreationDefaultInitSuffix),
ALT: () => $.SUBRULE($.arrayCreationDefaultInitSuffix)
},
{ ALT: () => $.SUBRULE($.arrayCreationExplicitInitSuffix) }
]);
});
$.RULE("arrayCreationDefaultInitSuffix", () => {
$.SUBRULE($.dimExprs);
$.OPTION(() => {
$.SUBRULE($.dims);
});
});
$.RULE("arrayCreationExplicitInitSuffix", () => {
$.SUBRULE($.dims);
$.SUBRULE($.arrayInitializer);
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-DimExprs
$.RULE("dimExprs", () => {
$.SUBRULE($.dimExpr);
$.MANY({
// The GATE is to distinguish DimExpr from Dims :
// the only difference between these two is the presence of an expression in the DimExpr
// Example: If the GATE is not present double[3][] won't be parsed as the parser will try to parse "[]"
// as a dimExpr instead of a dims
GATE: () => tokenMatcher($.LA(2).tokenType, t.RSquare) === false,
DEF: () => $.SUBRULE2($.dimExpr)
});
});
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-DimExpr
$.RULE("dimExpr", () => {
$.MANY(() => {
$.SUBRULE($.annotation);
});
$.CONSUME(t.LSquare);
$.SUBRULE($.expression);
$.CONSUME(t.RSquare);
});
$.RULE("classLiteralSuffix", () => {
$.MANY(() => {
$.CONSUME(t.LSquare);
$.CONSUME(t.RSquare);
});
$.CONSUME(t.Dot);
$.CONSUME(t.Class);
});
$.RULE("arrayAccessSuffix", () => {
$.CONSUME(t.LSquare);
$.SUBRULE($.expression);
$.CONSUME(t.RSquare);
});
$.RULE("methodReferenceSuffix", () => {
$.CONSUME(t.ColonColon);
$.OPTION(() => {
$.SUBRULE($.typeArguments);
});
$.OR([
{ ALT: () => $.CONSUME(t.Identifier) },
// TODO: a constructor method reference ("new") can only be used
// in specific contexts, but perhaps this verification is best left
// for a semantic analysis phase
{ ALT: () => $.CONSUME(t.New) }
]);
});
// backtracking lookahead logic
$.RULE("identifyNewExpressionType", () => {
$.CONSUME(t.New);
const firstTokenAfterNew = this.LA(1).tokenType;
// not an array initialization due to the prefix "TypeArguments"
if (tokenMatcher(firstTokenAfterNew, t.Less)) {
return newExpressionTypes.unqualifiedClassInstanceCreationExpression;
}
try {
$.SUBRULE($.classOrInterfaceTypeToInstantiate);
} catch (e) {
// if it is not a "classOrInterfaceTypeToInstantiate" then
// (assuming a valid input) we are looking at an "arrayCreationExpression"
return newExpressionTypes.arrayCreationExpression;
}
const firstTokenAfterClassType = this.LA(1).tokenType;
if (tokenMatcher(firstTokenAfterClassType, t.LBrace)) {
return newExpressionTypes.unqualifiedClassInstanceCreationExpression;
}
// The LBrace above is mandatory in "classInstanceCreation..." so
// it must be an "arrayCreationExp" (if the input is valid)
// TODO: upgrade the logic to return "unknown" type if at this
// point it does not match "arrayCreation" either.
// - This will provide a better error message to the user
// in case of invalid inputs
return newExpressionTypes.arrayCreationExpression;
});
// Optimized backtracking, only scan ahead until the arrow("->").
$.RULE("isLambdaExpression", () => {
// TODO: this check of next two tokens is probably redundant as the normal lookahead should take care of this.
const firstTokenType = this.LA(1).tokenType;
const secondTokenType = this.LA(2).tokenType;
// no parent lambda "x -> x * 2"
if (
tokenMatcher(firstTokenType, t.Identifier) &&
tokenMatcher(secondTokenType, t.Arrow)
) {
return true;
}
// Performance optimizations, fail fast if it is not a LBrace.
else if (tokenMatcher(firstTokenType, t.LBrace)) {
$.SUBRULE($.lambdaParametersWithBraces);
const followedByArrow = tokenMatcher(this.LA(1).tokenType, t.Arrow);
return followedByArrow;
}
return false;
});
$.RULE("isCastExpression", () => {
if (this.BACKTRACK_LOOKAHEAD($.isPrimitiveCastExpression)) {
return true;
}
return this.BACKTRACK_LOOKAHEAD($.isReferenceTypeCastExpression);
});
$.RULE("isPrimitiveCastExpression", () => {
$.CONSUME(t.LBrace);
$.SUBRULE($.primitiveType);
// No dims so this is not a reference Type
$.CONSUME(t.RBrace);
return true;
});
let firstForUnaryExpressionNotPlusMinus = undefined;
$.RULE("isReferenceTypeCastExpression", () => {
if (firstForUnaryExpressionNotPlusMinus === undefined) {
const firstUnaryExpressionNotPlusMinus = this.computeContentAssist(
"unaryExpressionNotPlusMinus",
[]
);
const nextTokTypes = firstUnaryExpressionNotPlusMinus.map(
x => x.nextTokenType
);
// uniq
firstForUnaryExpressionNotPlusMinus = nextTokTypes.filter(
(v, i, a) => a.indexOf(v) === i
);
}
$.CONSUME(t.LBrace);
$.SUBRULE($.referenceType);
$.MANY(() => {
$.SUBRULE($.additionalBound);
});
$.CONSUME(t.RBrace);
const firstTokTypeAfterRBrace = this.LA(1).tokenType;
return (
firstForUnaryExpressionNotPlusMinus.find(tokType =>
tokenMatcher(firstTokTypeAfterRBrace, tokType)
) !== undefined
);
});
$.RULE("isRefTypeInMethodRef", () => {
$.SUBRULE($.typeArguments);
// arrayType
const hasDims = $.OPTION(() => {
$.SUBRULE($.dims);
});
const firstTokTypeAfterTypeArgs = this.LA(1).tokenType;
if (tokenMatcher(firstTokTypeAfterTypeArgs, t.ColonColon)) {
return true;
}
// we must be at the end of a "referenceType" if "dims" were encountered
// So there is not point to check farther
else if (hasDims) {
return false;
}
// in the middle of a "classReferenceType"
$.OPTION2(() => {
$.CONSUME(t.Dot);
$.SUBRULE($.classOrInterfaceType);
});
const firstTokTypeAfterRefType = this.LA(1).tokenType;
return tokenMatcher(firstTokTypeAfterRefType, t.ColonColon);
});
}
module.exports = {
defineRules
};