forked from DrJavaAtRice/drjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.jj
More file actions
3616 lines (3365 loc) · 106 KB
/
grammar.jj
File metadata and controls
3616 lines (3365 loc) · 106 KB
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Java -*- */
// Parser options
//
options {
JAVA_UNICODE_ESCAPE = true;
STATIC = false;
JDK_VERSION = "1.5";
}
PARSER_BEGIN(Parser)
package koala.dynamicjava.parser.impl;
import java.util.*;
import java.io.File;
import edu.rice.cs.plt.tuple.Option;
import edu.rice.cs.plt.tuple.Pair;
import edu.rice.cs.plt.collect.ConsList;
import edu.rice.cs.plt.collect.CollectUtil;
import koala.dynamicjava.parser.wrapper.*;
import koala.dynamicjava.tree.*;
import koala.dynamicjava.tree.ModifierSet.Modifier;
import koala.dynamicjava.tree.visitor.*;
import koala.dynamicjava.util.*;
import koala.dynamicjava.tree.tiger.*;
import edu.rice.cs.dynamicjava.Options;
/**
* This class represents a (interpreted) Java 1.1 language parser
* adapted for 1.5 language extensions.
*/
public class Parser {
/** The name of the file currently interpreted; null by default */
private File file = null;
/** DynamicJava options */
private Options opt = Options.DEFAULT;
/**
* A flag to use in semantic lookaheads. Declared here as a workaround because
* lookahead can't access local variables or production parameters.
*/
private boolean lookaheadFlag;
/** Hack to pass the fact that a formal parameter was varargs back to the method/constructor production. */
boolean lastFormalParameterIsVarArgs = false;
/**
* The message reader
*/
private LocalizedMessageReader reader =
new LocalizedMessageReader("koala.dynamicjava.parser.resources.messages");
/** Sets the current file */
public void setFile(File f) { file = f; }
public void setOptions(Options o) { opt = o; }
private SourceInfo _range(Token first, Token last) {
return SourceInfo.range(file, first.beginLine, first.beginColumn, last.endLine, last.endColumn);
}
private SourceInfo _range(Token first, SourceInfo.Wrapper last) {
return SourceInfo.prepend(first.beginLine, first.beginColumn, last);
}
private SourceInfo _range(SourceInfo.Wrapper first, Token last) {
return SourceInfo.extend(first, last.beginLine, last.beginColumn);
}
private SourceInfo _range(Token first, SourceInfo last) {
return SourceInfo.prepend(first.beginLine, first.beginColumn, last);
}
private SourceInfo _range(SourceInfo first, Token last) {
return SourceInfo.extend(first, last.beginLine, last.beginColumn);
}
private SourceInfo _range(SourceInfo.Wrapper first, SourceInfo.Wrapper last) {
return SourceInfo.span(first, last);
}
private SourceInfo _range(SourceInfo first, SourceInfo last) {
return SourceInfo.span(first, last);
}
/**
* Throws a parse exception with the given message at the current (last consumed) token
* @param message - the message to be thrown
*/
private <T> T _throwParseException(String message) throws ParseException {
ParseException pe = new ParseException(message);
pe.currentToken = token;
throw pe;
}
/**
* Throws a parse exception with the given message at the current token, if the given
* parse exception was one auto generated by the parser (not one of ours), but not if the
* message is an <EOF> message, which is passed up to allow continuation of typing
* @param pe - the previous parse exception thrown
* @param message - the message to be thrown
*/
private <T> T _throwParseException(ParseException pe, String message) throws ParseException {
if (pe.expectedTokenSequences == null) { // has custom message
message = pe.getMessage();
}
else {
edu.rice.cs.plt.debug.DebugUtil.debug.log("Raw parse exception", pe);
}
if(pe.getMessage().indexOf("<EOF>\"") != -1) {
message = "Encountered Unexpected \"<EOF>\"";
}
return this.<T>_throwParseException(message);
}
public static enum DeclType { TOP, CLASS_MEMBER, INTERFACE_MEMBER, LOCAL, REPL };
/**
* Creates a field declaration
*/
private FieldDeclaration createFieldDeclaration(ModifierSet mods,
TypeName typ,
Token name,
Expression exp,
int dim) {
SourceInfo si = (exp == null) ? _range(mods, name) : _range(mods, exp);
// If the field is an array, create an array type node
if (dim > 0) {
typ = new ArrayTypeName(typ, dim, false, typ.getSourceInfo());
}
return new FieldDeclaration(mods, typ, name.image, exp, si);
}
/**
* Creates a variable declaration
*/
private VariableDeclaration createVariableDeclaration(ModifierSet mods,
TypeName typ,
Token name,
Expression exp,
int dim) {
SourceInfo si = (exp == null) ? _range(mods, name) : _range(mods, exp);
// If the variable contains an array, create an array type node
if (dim > 0) {
typ = new ArrayTypeName(typ, dim, false, typ.getSourceInfo());
}
return new VariableDeclaration(mods, typ, name.image, exp, si);
}
private void checkModifiers(ModifierSet mods, ModifierSet.Modifier... allowed) throws ParseException {
Set<ModifierSet.Modifier> copy = EnumSet.copyOf(mods.getFlags());
for (ModifierSet.Modifier m : allowed) { copy.remove(m); }
if (!copy.isEmpty()) {
ModifierSet.Modifier badMod = copy.iterator().next();
_throwParseException("Modifier " + badMod.getName() + " is not allowed here");
}
}
/** A hierarchy of helper classes for building primary expressions. */
abstract class Primary implements SourceInfo.Wrapper {
private final SourceInfo _si;
protected Primary(SourceInfo si) { _si = si; }
public SourceInfo getSourceInfo() { return _si; }
public Expression asExpression() throws ParseException {
return _throwParseException("Incomplete expression");
}
public Primary dotId(Option<List<TypeName>> targs, Token id) throws ParseException {
if (targs.isNone()) { return _throwParseException("Unexpected identifier"); }
else { return _throwParseException("Unexpected type arguments and identifier"); }
}
public Primary dotThis(Option<List<TypeName>> targs, Token t) throws ParseException {
if (targs.isNone()) { return _throwParseException("Unexpected 'this' keyword"); }
else { return _throwParseException("Unexpected type arguments and 'this' keyword"); }
}
public Primary dotSuper(Option<List<TypeName>> targs, Token t) throws ParseException {
if (targs.isNone()) { return _throwParseException("Unexpected 'super' keyword"); }
else { return _throwParseException("Unexpected type arguments and 'super' keyword"); }
}
public Primary dotClass(Token t) throws ParseException {
return _throwParseException("Illegal class literal");
}
public Primary dotNew(Option<List<TypeName>> targs, Token id) throws ParseException {
if (targs.isNone()) { return _throwParseException("Unexpected 'new' keyword"); }
else { return _throwParseException("Unexpected 'new' keyword and type arguments"); }
}
public Primary withArrayDim(Token last) throws ParseException {
return _throwParseException("Unexpected empty array brackets");
}
public Primary withArrayAccess(Expression exp, Token last) throws ParseException {
return _throwParseException("Unexpected array access");
}
public Primary withTypeArgs(List<TypeName> targs, Token last) throws ParseException {
return _throwParseException("Unexpected type arguments");
}
public Primary withArgs(List<Expression> args, Token last) throws ParseException {
return _throwParseException("Unexpected argument list");
}
public Primary withClassBody(List<Node> members, Token last) throws ParseException {
return _throwParseException("Unexpected class body");
}
}
/** A primary that can be parsed as a complete expression and act as a method receiver, etc. */
abstract class CompletedPrimary extends Primary {
protected CompletedPrimary(SourceInfo si) { super(si); }
@Override public abstract Expression asExpression() throws ParseException;
@Override public Primary dotId(Option<List<TypeName>> targs, Token id) throws ParseException {
if (targs.isNone()) { return new ObjectFieldAccessPrimary(asExpression(), id); }
else { return new PartialObjectMethodCallPrimary(asExpression(), targs, id); }
}
@Override public Primary dotSuper(Option<List<TypeName>> targs, Token t) throws ParseException {
return new PartialSuperConstructorCallPrimary(asExpression(), targs, t);
}
@Override public Primary dotNew(Option<List<TypeName>> targs, Token id) throws ParseException {
return new PartialInnerAllocationPrimary(asExpression(), targs, id);
}
@Override public Primary withArrayAccess(Expression exp, Token last) throws ParseException {
return new ExpressionPrimary(new ArrayAccess(asExpression(), exp, _range(this, last)));
}
}
/** A primary that can *only* be parsed as the given expression, regardless of what follows. */
class ExpressionPrimary extends CompletedPrimary {
private Expression _exp;
public ExpressionPrimary(Expression exp) { super(exp.getSourceInfo()); _exp = exp; }
public Expression asExpression() { return _exp; }
}
/** Like an ExpressionPrimary, but constructor calls can't be followed by additional dots, etc. */
class ConstructorCallPrimary extends Primary {
private ConstructorCall _call;
public ConstructorCallPrimary(ConstructorCall call) { super(call.getSourceInfo()); _call = call; }
@Override public Expression asExpression() { return _call; }
}
/** An ExpressionPrimary, but doesn't allow array accesses. */
class ArrayAllocationPrimary extends ExpressionPrimary {
public ArrayAllocationPrimary(ArrayAllocation alloc) { super(alloc); }
public Primary withArrayAccess(Expression exp, Token last) throws ParseException {
return _throwParseException("Unexpected array access");
}
}
/** A dotted sequence of names. */
class AmbiguousNamePrimary extends CompletedPrimary {
private final ConsList.Nonempty<IdentifierToken> _reversed;
public AmbiguousNamePrimary(Token id) {
this(ConsList.<IdentifierToken>singleton(new TreeToken(id, file)), _range(id, id));
}
private AmbiguousNamePrimary(ConsList.Nonempty<IdentifierToken> reversed, SourceInfo si) {
super(si);
_reversed = reversed;
}
private LinkedList<IdentifierToken> ids() { return CollectUtil.makeLinkedList(ConsList.reverse(_reversed)); }
public Expression asExpression() {
return new AmbiguousName(ids(), getSourceInfo());
}
@Override public Primary dotId(Option<List<TypeName>> targs, Token id) {
if (targs.isNone()) {
return new AmbiguousNamePrimary(ConsList.cons(new TreeToken(id, file), _reversed), _range(this, id));
}
else { return new PartialObjectMethodCallPrimary(asExpression(), targs, id); }
}
@Override public Primary dotThis(Option<List<TypeName>> targs, Token t) throws ParseException {
if (targs.isNone()) {
Option<String> cn = Option.some(TreeUtilities.listToName(ids()));
return new ExpressionPrimary(new ThisExpression(cn, _range(this, t)));
}
else { return _throwParseException("Unexpected type arguments"); }
}
@Override public Primary dotSuper(Option<List<TypeName>> targs, Token t) throws ParseException {
if (targs.isNone()) { return new PartialSuperPrimary(ids(), t); }
else { return super.dotSuper(targs, t); }
}
@Override public Primary dotClass(Token t) {
return new ExpressionPrimary(new TypeExpression(new ReferenceTypeName(ids(), getSourceInfo()), _range(this, t)));
}
@Override public Primary withArrayDim(Token last) {
TypeName elt = new ReferenceTypeName(ids(), getSourceInfo());
return new ArrayTypeNamePrimary(new ArrayTypeName(elt, 1, false, _range(this, last)));
}
@Override public Primary withTypeArgs(List<TypeName> targs, Token last) {
return new GenericReferenceTypeNamePrimary(_reversed, targs, _range(this, last));
}
@Override public Primary withArgs(List<Expression> args, Token last) {
SourceInfo si = _range(this, last);
ConsList<? extends IdentifierToken> revObjIds = _reversed.rest();
if (revObjIds.isEmpty()) {
return new ExpressionPrimary(new SimpleMethodCall(_reversed.first().image(), args, si));
}
else {
LinkedList<IdentifierToken> objIds = CollectUtil.makeLinkedList(ConsList.reverse(revObjIds));
AmbiguousName obj = new AmbiguousName(objIds, _range(objIds.getFirst(), objIds.getLast()));
return new ExpressionPrimary(new ObjectMethodCall(obj, _reversed.first().image(), args, si));
}
}
}
/** A dotted name with at least one type argument. Can be followed by additional identifiers and type arguments. */
class GenericReferenceTypeNamePrimary extends Primary {
protected final ConsList.Nonempty<IdentifierToken> _reversed;
protected final ConsList.Nonempty<List<TypeName>> _targsReversed;
public GenericReferenceTypeNamePrimary(ConsList.Nonempty<IdentifierToken> reversed, List<TypeName> targs,
SourceInfo si) {
super(si);
_reversed = reversed;
ConsList<List<TypeName>> noArgs = ConsList.empty();
int noArgCount = _reversed.size() - 1;
for (int i = 0; i < noArgCount; i++) { noArgs = ConsList.cons(new LinkedList<TypeName>(), noArgs); }
_targsReversed = ConsList.cons(targs, noArgs);
}
private GenericReferenceTypeNamePrimary(ConsList.Nonempty<IdentifierToken> reversed,
ConsList.Nonempty<List<TypeName>> targsReversed, SourceInfo si) {
super(si);
_reversed = reversed;
_targsReversed = targsReversed;
}
protected GenericReferenceTypeName asTypeName() {
List<IdentifierToken> ids = CollectUtil.makeLinkedList(ConsList.reverse(_reversed));
List<List<? extends TypeName>> targs =
CollectUtil.<List<? extends TypeName>>makeLinkedList(ConsList.reverse(_targsReversed));
return new GenericReferenceTypeName(ids, targs, getSourceInfo());
}
@Override public Primary dotId(Option<List<TypeName>> targs, Token id) {
if (targs.isNone()) { return new IdGenericReferenceTypeNamePrimary(this, id); }
else { return new PartialStaticMethodCallPrimary(asTypeName(), targs, id); }
}
@Override public Primary dotClass(Token t) {
return new ExpressionPrimary(new TypeExpression(asTypeName(), _range(this, t)));
}
@Override public Primary withArrayDim(Token last) {
return new ArrayTypeNamePrimary(new ArrayTypeName(asTypeName(), 1, false, _range(this, last)));
}
}
/** A GenericReferenceTypeNamePrimary that ends with an id. */
class IdGenericReferenceTypeNamePrimary extends GenericReferenceTypeNamePrimary {
private final GenericReferenceTypeNamePrimary _prev;
public IdGenericReferenceTypeNamePrimary(GenericReferenceTypeNamePrimary prev, Token id) {
super(ConsList.cons(new TreeToken(id, file), prev._reversed),
ConsList.cons(new LinkedList<TypeName>(), prev._targsReversed),
_range(prev, id));
_prev = prev;
}
@Override public Primary withTypeArgs(List<TypeName> targs, Token last) {
ConsList.Nonempty<List<TypeName>> newTargs = ConsList.cons(targs, _targsReversed.rest());
return new GenericReferenceTypeNamePrimary(_reversed, newTargs, _range(this, last));
}
@Override public Primary withArgs(List<Expression> args, Token last) {
return new ExpressionPrimary(new StaticMethodCall(_prev.asTypeName(), _reversed.first().image(),
args, _range(this, last)));
}
}
/** An array type name. */
class ArrayTypeNamePrimary extends Primary {
private final ArrayTypeName _type;
public ArrayTypeNamePrimary(ArrayTypeName type) { super(type.getSourceInfo()); _type = type; }
@Override public Primary dotClass(Token t) {
return new ExpressionPrimary(new TypeExpression(_type, _range(this, t)));
}
@Override public Primary withArrayDim(Token last) {
return new ArrayTypeNamePrimary(new ArrayTypeName(_type, 1, false, _range(this, last)));
}
}
/** A primitive type, which may be part of a class literal or an array type. */
class PrimitiveTypeNamePrimary extends Primary {
private final PrimitiveTypeName _type;
public PrimitiveTypeNamePrimary(PrimitiveTypeName type) { super(type.getSourceInfo()); _type = type; }
@Override public Primary dotClass(Token t) {
return new ExpressionPrimary(new TypeExpression(_type, _range(this, t)));
}
@Override public Primary withArrayDim(Token last) {
return new ArrayTypeNamePrimary(new ArrayTypeName(_type, 1, false, _range(this, last)));
}
}
/** A "void" keyword, which may be part of a class literal. */
class VoidTypeNamePrimary extends Primary {
private final VoidTypeName _type;
public VoidTypeNamePrimary(VoidTypeName type) { super(type.getSourceInfo()); _type = type; }
@Override public Primary dotClass(Token t) {
return new ExpressionPrimary(new TypeExpression(_type, _range(this, t)));
}
}
/** Optional type arguments followed by a method name. */
class PartialSimpleMethodCallPrimary extends Primary {
private final Option<List<TypeName>> _targs;
private final String _name;
public PartialSimpleMethodCallPrimary(Option<List<TypeName>> targs, Token id, SourceInfo si) {
super(si);
_targs = targs;
_name = id.image;
}
@Override public Primary withArgs(List<Expression> args, Token last) throws ParseException {
SourceInfo si = _range(this, last);
return new ExpressionPrimary(new SimpleMethodCall(_targs, _name, args, si));
}
}
/** An expression followed by a dot and a name. May be followed by method arguments. */
class ObjectFieldAccessPrimary extends CompletedPrimary {
private final Expression _obj;
private final String _name;
public ObjectFieldAccessPrimary(Expression obj, Token id) {
super(_range(obj, id));
_obj = obj;
_name = id.image;
}
public Expression asExpression() { return new ObjectFieldAccess(_obj, _name, getSourceInfo()); }
@Override public Primary withArgs(List<Expression> args, Token last) {
return new ExpressionPrimary(new ObjectMethodCall(_obj, _name, args, _range(this, last)));
}
}
/** An expression followed by a dot, optional type arguments, and a method name. */
class PartialObjectMethodCallPrimary extends Primary {
private final Expression _obj;
private final Option<List<TypeName>> _targs;
private final String _name;
public PartialObjectMethodCallPrimary(Expression obj, Option<List<TypeName>> targs, Token id) {
super(_range(obj, id));
_obj = obj;
_targs = targs;
_name = id.image;
}
@Override public Primary withArgs(List<Expression> args, Token last) {
SourceInfo si = _range(this, last);
return new ExpressionPrimary(new ObjectMethodCall(_obj, _targs, _name, args, si));
}
}
/** A type followed by a dot, optional type arguments, and a method name. */
class PartialStaticMethodCallPrimary extends Primary {
private final TypeName _type;
private final Option<List<TypeName>> _targs;
private final String _name;
PartialStaticMethodCallPrimary(TypeName type, Option<List<TypeName>> targs, Token id) {
super(_range(type, id));
_type = type;
_targs = targs;
_name = id.image;
}
@Override public Primary withArgs(List<Expression> args, Token last) {
SourceInfo si = _range(this, last);
return new ExpressionPrimary(new StaticMethodCall(_type, _targs, _name, args, si));
}
}
/** The "this" keyword. May be a constructor call. */
class ThisExpressionPrimary extends CompletedPrimary {
public ThisExpressionPrimary(Token t) { super(_range(t, t)); }
public Expression asExpression() {
return new ThisExpression(Option.<String>none(), getSourceInfo());
}
@Override public Primary withArgs(List<Expression> args, Token last) {
return new ConstructorCallPrimary(new ConstructorCall(null, args, false, _range(this, last)));
}
}
/** A "this" keyword prefixed by optional type arguments. */
class PartialThisConstructorCallPrimary extends Primary {
private final Option<List<TypeName>> _targs;
public PartialThisConstructorCallPrimary(Option<List<TypeName>> targs, SourceInfo si) {
super(si);
_targs = targs;
}
@Override public Primary withArgs(List<Expression> args, Token last) throws ParseException {
if (_targs.isSome()) {
return _throwParseException("Parameterized this constructor calls are not yet supported");
}
return new ConstructorCallPrimary(new ConstructorCall(null, args, false, _range(this, last)));
}
}
/** The "super" keyword, optionally prefixed by a dotted name. May be a constructor call or a field/method access. */
class PartialSuperPrimary extends Primary {
private final LinkedList<IdentifierToken> _ids; // may be null
public PartialSuperPrimary(Token t) { super(_range(t, t)); _ids = null; }
public PartialSuperPrimary(LinkedList<IdentifierToken> ids, Token t) {
super(_range(ids.getFirst(), t));
_ids = ids;
}
@Override public Primary dotId(Option<List<TypeName>> targs, Token id) {
Option<String> cn = (_ids == null) ? Option.<String>none() : Option.some(TreeUtilities.listToName(_ids));
if (targs.isNone()) { return new SuperFieldAccessPrimary(cn, id, _range(this, id)); }
else { return new PartialSuperMethodCallPrimary(cn, targs, id, _range(this, id)); }
}
@Override public Primary withArgs(List<Expression> args, Token last) {
Expression outer = null;
if (_ids != null) { outer = new AmbiguousName(_ids, _range(_ids.getFirst(), _ids.getLast())); }
return new ConstructorCallPrimary(new ConstructorCall(outer, args, true, _range(this, last)));
}
}
/** A "super" keyword prefixed by optional type arguments. Optionally prefixed by an outer expression. */
class PartialSuperConstructorCallPrimary extends Primary {
private final Expression _outer; // may be null
private final Option<List<TypeName>> _targs;
public PartialSuperConstructorCallPrimary(Option<List<TypeName>> targs, SourceInfo si) {
super(si);
_outer = null;
_targs = targs;
}
public PartialSuperConstructorCallPrimary(Expression outer, Option<List<TypeName>> targs, Token last) {
super(_range(outer, last));
_outer = outer;
_targs = targs;
}
@Override public Primary withArgs(List<Expression> args, Token last) throws ParseException {
if (_targs.isSome()) {
return _throwParseException("Parameterized super constructor calls are not yet supported");
}
return new ConstructorCallPrimary(new ConstructorCall(_outer, args, true, _range(this, last)));
}
}
/**
* A "super" keyword (optionally prefixed by a name), followed by a dot and a name. May be followed by
* method arguments.
*/
class SuperFieldAccessPrimary extends CompletedPrimary {
private final Option<String> _className;
private final String _name;
public SuperFieldAccessPrimary(Option<String> className, Token id, SourceInfo si) {
super(si);
_className = className;
_name = id.image;
}
public Expression asExpression() throws ParseException {
return new SuperFieldAccess(_className, _name, getSourceInfo());
}
@Override public Primary withArgs(List<Expression> args, Token last) throws ParseException {
return new ExpressionPrimary(new SuperMethodCall(_className, _name, args, _range(this, last)));
}
}
/**
* A "super" keyword (optionally prefixed by a name), followed by a dot, optional type arguments, and
* a method name.
*/
class PartialSuperMethodCallPrimary extends Primary {
private final Option<String> _className;
private final Option<List<TypeName>> _targs;
private final String _name;
PartialSuperMethodCallPrimary(Option<String> className, Option<List<TypeName>> targs, Token id,
SourceInfo si) {
super(si);
_className = className;
_targs = targs;
_name = id.image;
}
@Override public Primary withArgs(List<Expression> args, Token last) throws ParseException {
SourceInfo si = _range(this, last);
return new ExpressionPrimary(new SuperMethodCall(_className, _targs, _name, args, si));
}
}
/** A "new" keyword followed by optional type arguments and a type. */
class PartialSimpleAllocationPrimary extends Primary {
private final Option<List<TypeName>> _targs;
private final ReferenceTypeName _type;
public PartialSimpleAllocationPrimary(Option<List<TypeName>> targs, ReferenceTypeName type, SourceInfo si) {
super(si);
_targs = targs;
_type = type;
}
@Override public Primary withArgs(List<Expression> args, Token last) {
return new SimpleAllocationPrimary(_targs, _type, args, _range(this, last));
}
}
/**
* A "new" keyword followed by optional type arguments, a type, and constructor arguments. May
* be an anonymous allocation if followed by a class body.
*/
class SimpleAllocationPrimary extends CompletedPrimary {
private final Option<List<TypeName>> _targs;
private final ReferenceTypeName _type;
private final List<Expression> _args;
public SimpleAllocationPrimary(Option<List<TypeName>> targs, ReferenceTypeName type, List<Expression> args,
SourceInfo si) {
super(si);
_targs = targs;
_type = type;
_args = args;
}
public Expression asExpression() {
return new SimpleAllocation(_targs, _type, _args, getSourceInfo());
}
@Override public Primary withClassBody(List<Node> members, Token last) {
SourceInfo si = _range(this, last);
return new ExpressionPrimary(new AnonymousAllocation(_targs, _type, _args, members, si));
}
}
/** An expression followed by a dot, "new", optional type arguments, and a class name. */
class PartialInnerAllocationPrimary extends Primary {
private final Expression _outer;
private final Option<List<TypeName>> _targs;
private final String _name;
public PartialInnerAllocationPrimary(Expression outer, Option<List<TypeName>> targs, Token id) {
super(_range(outer, id));
_outer = outer;
_targs = targs;
_name = id.image;
}
@Override public Primary withTypeArgs(List<TypeName> args, Token last) {
return new PartialGenericInnerAllocationPrimary(_outer, _targs, _name, args, last);
}
@Override public Primary withArgs(List<Expression> args, Token last) {
return new InnerAllocationPrimary(_outer, _targs, _name, Option.<List<TypeName>>none(), args, last);
}
}
/** An expression followed by a dot, "new", optional type arguments, a class name, and class type arguments. */
class PartialGenericInnerAllocationPrimary extends Primary {
private final Expression _outer;
private final Option<List<TypeName>> _targs;
private final String _name;
private final List<TypeName> _ctargs;
public PartialGenericInnerAllocationPrimary(Expression outer, Option<List<TypeName>> targs, String name,
List<TypeName> ctargs, Token last) {
super(_range(outer, last));
_outer = outer;
_targs = targs;
_name = name;
_ctargs = ctargs;
}
@Override public Primary withArgs(List<Expression> args, Token last) {
return new InnerAllocationPrimary(_outer, _targs, _name, Option.some(_ctargs), args, last);
}
}
/**
* An expresion followed by a dot, "new", optional type arguments, a class name, optional class type arguments,
* and constructor arguments. May be an anonymous allocation if followed by a class body.
*/
class InnerAllocationPrimary extends CompletedPrimary {
private final Expression _outer;
private final Option<List<TypeName>> _targs;
private final String _name;
private final Option<List<TypeName>> _ctargs;
private final List<Expression> _args;
public InnerAllocationPrimary(Expression outer, Option<List<TypeName>> targs, String name,
Option<List<TypeName>> ctargs, List<Expression> args, Token last) {
super(_range(outer, last));
_outer = outer;
_targs = targs;
_name = name;
_ctargs = ctargs;
_args = args;
}
public Expression asExpression() {
return new InnerAllocation(_outer, _targs, _name, _ctargs, _args, getSourceInfo());
}
@Override public Primary withClassBody(List<Node> members, Token last) {
SourceInfo si = _range(this, last);
return new ExpressionPrimary(new AnonymousInnerAllocation(_outer, _targs, _name, _ctargs, _args, members, si));
}
}
}
PARSER_END(Parser)
/*
* The lexical grammar
*/
/* WHITE SPACE */
SKIP :
{
" "
| "\t"
| "\n"
| "\r"
| "\f"
}
/* COMMENTS */
SKIP :
{
"//" : IN_SINGLE_LINE_COMMENT
|
"#" : IN_SINGLE_LINE_COMMENT
|
<"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
|
"/*" : IN_MULTI_LINE_COMMENT
}
<IN_SINGLE_LINE_COMMENT>
SKIP :
{
<SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : DEFAULT
}
<IN_FORMAL_COMMENT>
SKIP :
{
<FORMAL_COMMENT: "*/" > : DEFAULT
}
<IN_MULTI_LINE_COMMENT>
SKIP :
{
<MULTI_LINE_COMMENT: "*/" > : DEFAULT
}
<IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
SKIP :
{
< ~[] >
}
/* RESERVED WORDS AND LITERALS */
TOKEN :
{
< ABSTRACT: "abstract" >
| < ASSERT: "assert" >
| < BOOLEAN: "boolean" >
| < BREAK: "break" >
| < BYTE: "byte" >
| < CASE: "case" >
| < CATCH: "catch" >
| < CHAR: "char" >
| < CLASS: "class" >
| < CONST: "const" >
| < CONTINUE: "continue" >
| < _DEFAULT: "default" >
| < DO: "do" >
| < DOUBLE: "double" >
| < ELSE: "else" >
| < ENUM: "enum" >
| < EXTENDS: "extends" >
| < FALSE: "false" >
| < FINAL: "final" >
| < FINALLY: "finally" >
| < FLOAT: "float" >
| < FOR: "for" >
| < GOTO: "goto" >
| < IF: "if" >
| < IMPLEMENTS: "implements" >
| < IMPORT: "import" >
| < INSTANCEOF: "instanceof" >
| < INT: "int" >
| < INTERFACE: "interface" >
| < LONG: "long" >
| < NATIVE: "native" >
| < NEW: "new" >
| < NULL: "null" >
| < PACKAGE: "package">
| < PRIVATE: "private" >
| < PROTECTED: "protected" >
| < PUBLIC: "public" >
| < RETURN: "return" >
| < SHORT: "short" >
| < STATIC: "static" >
| < STRICTFP: "strictfp" >
| < SUPER: "super" >
| < SWITCH: "switch" >
| < SYNCHRONIZED: "synchronized" >
| < THIS: "this" >
| < THROW: "throw" >
| < THROWS: "throws" >
| < TRANSIENT: "transient" >
| < TRUE: "true" >
| < TRY: "try" >
| < VOID: "void" >
| < VOLATILE: "volatile" >
| < WHILE: "while" >
}
/* LITERALS */
TOKEN :
{
< INTEGER_LITERAL:
<DECIMAL_LITERAL>
| <HEX_LITERAL>
| <OCTAL_LITERAL>
>
|
< LONG_LITERAL:
<DECIMAL_LITERAL> ["l","L"]
| <HEX_LITERAL> ["l","L"]
| <OCTAL_LITERAL> ["l","L"]
>
|
< #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
|
< #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
|
< #OCTAL_LITERAL: "0" (["0"-"7"])* >
|
< FLOAT_LITERAL:
(["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? ["f","F"]
| "." (["0"-"9"])+ (<EXPONENT>)? ["f","F"]
| (["0"-"9"])+ <EXPONENT> ["f","F"]
| (["0"-"9"])+ (<EXPONENT>)? ["f","F"]
>
|
< DOUBLE_LITERAL:
(["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["d","D"])?
| "." (["0"-"9"])+ (<EXPONENT>)? (["d","D"])?
| (["0"-"9"])+ <EXPONENT> (["d","D"])?
| (["0"-"9"])+ (<EXPONENT>)? ["d","D"]
>
|
< #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
|
< CHARACTER_LITERAL:
"'"
( (~["'","\\","\n","\r"])
| ("\\"
( ["n","t","b","r","f","\\","'","\""]
| ["0"-"7"] ( ["0"-"7"] )?
| ["0"-"3"] ["0"-"7"] ["0"-"7"]
)
)
)
"'"
>
|
< STRING_LITERAL:
"\""
( (~["\"","\\","\n","\r"])
| ("\\"
( ["n","t","b","r","f","\\","'","\""]
| ["0"-"7"] ( ["0"-"7"] )?
| ["0"-"3"] ["0"-"7"] ["0"-"7"]
)
)
)*
"\""
>
}
/* IDENTIFIERS */
TOKEN :
{
< IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
|
< #LETTER:
[
"\u0024",
"\u0041"-"\u005a",
"\u005f",
"\u0061"-"\u007a",
"\u00c0"-"\u00d6",
"\u00d8"-"\u00f6",
"\u00f8"-"\u00ff",
"\u0100"-"\u1fff",
"\u3040"-"\u318f",
"\u3300"-"\u337f",
"\u3400"-"\u3d2d",
"\u4e00"-"\u9fff",
"\uf900"-"\ufaff"
]
>
|
< #DIGIT:
[
"\u0030"-"\u0039",
"\u0660"-"\u0669",
"\u06f0"-"\u06f9",
"\u0966"-"\u096f",
"\u09e6"-"\u09ef",
"\u0a66"-"\u0a6f",
"\u0ae6"-"\u0aef",
"\u0b66"-"\u0b6f",
"\u0be7"-"\u0bef",
"\u0c66"-"\u0c6f",
"\u0ce6"-"\u0cef",
"\u0d66"-"\u0d6f",
"\u0e50"-"\u0e59",
"\u0ed0"-"\u0ed9",
"\u1040"-"\u1049"
]
>
}
/* SEPARATORS */
TOKEN :
{
< LPAREN: "(" >
| < RPAREN: ")" >
| < LBRACE: "{" >
| < RBRACE: "}" >
| < LBRACKET: "[" >
| < RBRACKET: "]" >
| < SEMICOLON: ";" >
| < COMMA: "," >
| < DOT: "." >
}
/* OPERATORS */
TOKEN :
{
< ASSIGN: "=" >
| < GREATER_THAN: ">" >
| < LESS: "<" >
| < BANG: "!" >
| < TILDE: "~" >
| < HOOK: "?" >
| < COLON: ":" >
| < EQUAL: "==" >
| < LESS_OR_EQUAL: "<=" >
| < GREATER_OR_EQUAL: ">=" >
| < NOT_EQUAL: "!=" >
| < CONDITIONAL_OR: "||" >
| < CONDITIONAL_AND: "&&" >
| < INCREMENT: "++" >
| < DECREMENT: "--" >
| < PLUS: "+" >
| < MINUS: "-" >
| < STAR: "*" >
| < SLASH: "/" >
| < BITWISE_AND: "&" >
| < BITWISE_OR: "|" >
| < XOR: "^" >
| < REMAINDER: "%" >
| < LEFT_SHIFT: "<<" >
/* | < RIGHT_SIGNED_SHIFT: ">>" >
| < RIGHT_UNSIGNED_SHIFT: ">>>" > */
| < PLUS_ASSIGN: "+=" >
| < MINUS_ASSIGN: "-=" >
| < STAR_ASSIGN: "*=" >
| < SLASH_ASSIGN: "/=" >
| < AND_ASSIGN: "&=" >
| < OR_ASSIGN: "|=" >
| < XOR_ASSIGN: "^=" >
| < REMAINDER_ASSIGN: "%=" >
| < LEFT_SHIFT_ASSIGN: "<<=" >
| < RIGHT_SIGNED_SHIFT_ASSIGN: ">>=" >
| < RIGHT_UNSIGNED_SHIFTASSIGN: ">>>=" >
| < VAR_ARGS: "...">
}
TOKEN : /* OPERATORS, from Generics Preprocessor */
{
/* < ASSIGN: "=" >
| < GT: ">" >
| < LT: "<" >
| < BANG: "!" >
| < TILDE: "~" >
| < HOOK: "?" >
| < COLON: ":" >
| < EQ: "==" >
| < LE: "<=" >
| < GE: ">=" >
| < NE: "!=" >
| < SC_OR: "||" >
| < SC_AND: "&&" >
| < INCR: "++" >
| < DECR: "--" >
| < PLUS: "+" >