/
ruby.tree.g
902 lines (842 loc) · 30 KB
/
ruby.tree.g
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
header {
package com.xruby.compiler.parser;
import com.xruby.compiler.codedom.*;
}
/**
* Copyright 2005-2007 Xue Yong Zhi
* Distributed under the BSD License
*/
class RubyTreeParser extends TreeParser;
options {
importVocab = Ruby;
defaultErrorHandler = false;
}
{
private String filename_ = null;
protected int currentLineNumber = 0;
public Program parse(AST t, String filename) throws RecognitionException {
filename_ = filename;
Program p = program(t);
if (null == p) {
return new Program(null);
} else {
return p;
}
}
}
program
returns [Program p]
{
p = null;
CompoundStatement cs = null;
}
: (cs=compoundStatement {p = new Program(cs);}
)?
;
compoundStatement
returns [CompoundStatement cs]
{
Statement s = null;
cs = new CompoundStatement();
}
: #(COMPSTMT
(s=statement {cs.addStatement(s);}
)+
)
;
statement
returns [Statement s]
: s=expressionStatement
| s=alias
| s=undef
| s=multipleAssignment
| s=beginBlock
| s=endBlock
;
beginBlock
returns [BEGINBlock b]
{
CompoundStatement cs=null;
}
: "BEGIN" LCURLY_BLOCK (cs=compoundStatement)? RCURLY
{
b = new BEGINBlock(cs);
}
;
endBlock
returns [ENDBlock b]
{
CompoundStatement cs=null;
}
: "END" LCURLY_BLOCK (cs=compoundStatement)? RCURLY
{
b = new ENDBlock(cs);
}
;
expressionStatement
returns[ExpressionStatement s]
{
Expression e = null;
}
: e=expression
{
s = new ExpressionStatement(e);
}
;
alias
returns[AliasStatement s]
{
String newname = null;
String oldname = null;
}
: #("alias" newname=aliasParameter oldname=aliasParameter) {s = new AliasStatement(newname, oldname);}
;
aliasParameter
returns[String s]
{SymbolExpression sym = null;}
: gvar:GLOBAL_VARIABLE {s = gvar.getText();}
| id:IDENTIFIER (assign1:ASSIGN_WITH_NO_LEADING_SPACE)? {s = id.getText(); if (null != assign1) {s += "=";}}
| function:FUNCTION (assign2:ASSIGN_WITH_NO_LEADING_SPACE)? {s = function.getText(); if (null != assign2) {s += "=";}}
| constant:CONSTANT (assign3:ASSIGN_WITH_NO_LEADING_SPACE)? {s = constant.getText(); if (null != assign3) {s += "=";}}
| sym=symbol {s = sym.getValue();}
| s=operator
| s=keyword
;
undef
returns[UndefStatement s]
{String name = null;}
: #("undef" {s = new UndefStatement();} (name=undefParameter {s.add(name);})+)
;
undefParameter
returns[String s]
{SymbolExpression sym = null;}
: func:FUNCTION {s = func.getText();}
| id:IDENTIFIER {s = id.getText();}
| sym=symbol {s = sym.getValue();}
;
multipleAssignment
returns[MultipleAssignmentStatement s]
{
Expression e = null;
}
: #( MULTIPLE_ASSIGN {s = new MultipleAssignmentStatement(false);}
multipleAssignmentBody[s]
)
| #( MULTIPLE_ASSIGN_WITH_EXTRA_COMMA {s = new MultipleAssignmentStatement(true);}
multipleAssignmentBody[s]
)
;
protected
multipleAssignmentBody[MultipleAssignmentStatement s]
{
Expression e = null;
}
: (e=lhs {s.addLhs(e);e=null;})*
(REST_ARG_PREFIX (e=expression)? {s.setAsteriskLhs(e);} )?
(
#(MRHS
(e=expression {s.addRhs(e);})*
(REST_ARG_PREFIX e=expression {s.setAsteriskRhs(e);})?
)
)?
;
lhs
returns[Expression e]
: e=expression
| e=nestedLhs
;
nestedLhs
returns[NestedVariableExpression e]
{
Expression exp = null;
}
: #( NESTED_LHS {e = new NestedVariableExpression();}
(exp=expression {e.addLhs(exp);})*
(REST_ARG_PREFIX exp=expression {e.setAsteriskLhs(exp);})?
)
;
expression
returns [Expression e]
{
e = null;
Expression condition = null;
Expression left = null;
Expression right = null;
MethodCallArguments args = null;
ReturnArguments return_args = null;
CompoundStatement cs = null;
String method_name = null;
int lineNumber = (null == _t) ? 0 : _t.getLine();
boolean startANewLine = false;
if (lineNumber != currentLineNumber) {
currentLineNumber = lineNumber;
startANewLine = true;
}
}
: (#("and" left=expression right=expression) {e = new AndOrBinaryOperatorExpression("&&", left, right);}
| #("or" left=expression right=expression) {e = new AndOrBinaryOperatorExpression("||", left, right);}
| #("not" left=expression) {e = new UnaryOperatorExpression("!", left);}
| #(QUESTION condition=expression left=expression right=expression)
{e = new IfExpression(condition, left, right);}
| #(ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, right);}
| #(PLUS_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("+", left, right));}
| #(MINUS_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("-", left, right));}
| #(STAR_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("*", left, right));}
| #(DIV_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("/", left, right));}
| #(MOD_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("%", left, right));}
| #(POWER_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("**", left, right));}
| #(BAND_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("&", left, right));}
| #(BXOR_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("^", left, right));}
| #(BOR_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("|", left, right));}
| #(LEFT_SHIFT_ASSIGN left=expression right=expression) {e =AssignmentOperatorExpression.create(left, new BinaryOperatorExpression("<<", left, right));}
| #(RIGHT_SHIFT_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new BinaryOperatorExpression(">>", left, right));}
| #(LOGICAL_AND_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new AndOrBinaryOperatorExpression("&&", left, right));}
| #(LOGICAL_OR_ASSIGN left=expression right=expression) {e = AssignmentOperatorExpression.create(left, new AndOrBinaryOperatorExpression("||", left, right));}
| #(INCLUSIVE_RANGE left=expression right=expression) {e = new RangeOperatorExpression("..", left, right);}
| #(EXCLUSIVE_RANGE left=expression right=expression) {e = new RangeOperatorExpression("...", left, right);}
| #(LOGICAL_OR left=expression right=expression) {e = new AndOrBinaryOperatorExpression("||", left, right);}
| #(LOGICAL_AND left=expression right=expression) {e = new AndOrBinaryOperatorExpression("&&", left, right);}
| #(COMPARE left=expression right=expression) {e = new BinaryOperatorExpression("<=>", left, right);}
| #(EQUAL left=expression right=expression) {e = new BinaryOperatorExpression("==", left, right);}
| #(CASE_EQUAL left=expression right=expression) {e = new BinaryOperatorExpression("===", left, right);}
| #(NOT_EQUAL left=expression right=expression) {e = new BinaryOperatorExpression("!=", left, right);}
| #(MATCH left=expression right=expression) {e = new BinaryOperatorExpression("=~", left, right);}
| #(NOT_MATCH left=expression right=expression) {e = new BinaryOperatorExpression("!~", left, right);}
| #(LESS_THAN left=expression right=expression) {e = new BinaryOperatorExpression("<", left, right);}
| #(GREATER_THAN left=expression right=expression) {e = new BinaryOperatorExpression(">", left, right);}
| #(LESS_OR_EQUAL left=expression right=expression) {e = new BinaryOperatorExpression("<=", left, right);}
| #(GREATER_OR_EQUAL left=expression right=expression) {e = new BinaryOperatorExpression(">=", left, right);}
| #(BXOR left=expression right=expression) {e = new BinaryOperatorExpression("^", left, right);}
| #(BOR left=expression right=expression) {e = new BinaryOperatorExpression("|", left, right);}
| #(BAND left=expression right=expression) {e = new BinaryOperatorExpression("&", left, right);}
| #(LEFT_SHIFT left=expression right=expression) {e = new BinaryOperatorExpression("<<", left, right);}
| #(RIGHT_SHIFT left=expression right=expression) {e = new BinaryOperatorExpression(">>", left, right);}
| #(PLUS left=expression right=expression) {e = new BinaryOperatorExpression("+", left, right);}
| #(MINUS left=expression right=expression) {e = new BinaryOperatorExpression("-", left, right);}
| #(STAR left=expression right=expression) {e = new BinaryOperatorExpression("*", left, right);}
| #(DIV left=expression right=expression) {e = new BinaryOperatorExpression("/", left, right);}
| #(MOD left=expression right=expression) {e = new BinaryOperatorExpression("%", left, right);}
| #(POWER left=expression right=expression) {e = new BinaryOperatorExpression("**", left, right);}
| #(UNARY_PLUS left=expression) {e = new UnaryOperatorExpression("+@", left);}
| #(UNARY_MINUS left=expression) {e = new UnaryOperatorExpression("-@", left);}
| BNOT left=expression {e = new UnaryOperatorExpression("~", left);}
| NOT left=expression {e = new UnaryOperatorExpression("!", left);}
| #(DOT left=expression (right=callExpression|method_name=methodCallName))
{
if (right != null) {
MethodCallExpression mc = (MethodCallExpression)right;
e = new MethodCallExpression(left, mc.getName(), mc.getArguments(), mc.getBlock());
} else {
e = new MethodCallExpression(left, method_name, null, null);
}
}
| e=callExpression
| #(LBRACK_ARRAY_ACCESS left=expression args=elements_as_arguments) {e = new MethodCallExpression(left, "[]", args, null);}
| #(COLON2 left=expression (right=callExpression|constant:CONSTANT|function:FUNCTION))
{ if (null != right) {
MethodCallExpression mc = (MethodCallExpression)right;
e = new MethodCallExpression(left, mc.getName(), mc.getArguments(), mc.getBlock());
} else if (null != constant) {
e = new Colon2Expression(left, constant.getText());
} else {
e = new MethodCallExpression(left, function.getText(), null, null);
}
}
| e=primaryExpression
| e=methodDefinition
| e=moduleDefinition
| e=classDefinition
| e=ifExpression
| e=whileExpression
| e=forInExpression
| e=unlessExpression
| e=caseExpression
| e=exceptionHandlingExpression
| e=arrayExpression
| e=hashExpression
| e=symbol
| #("return" (return_args=return_arguments)?) {e = new ReturnExpression(return_args);}
| #("break" (return_args=return_arguments)?) {e = new BreakExpression(return_args);}
| #("next" (return_args=return_arguments)?) {e = new NextExpression(return_args);}
| #(LPAREN cs=compoundStatement) {e = new ParenthesisExpression(cs);}
| #(LPAREN_WITH_NO_LEADING_SPACE cs=compoundStatement) {e = new ParenthesisExpression(cs);}
| "redo" {e = new RedoExpression();}
| "retry" {e = new RetryExpression();}
)
{
e.setNewLine(startANewLine);
if(e.getPosition() < 1) {
e.setPosition(currentLineNumber);
} // To fix the linenumber problem for forInExpression
}
;
callExpression
returns [Expression e]
{
String method_name = null;
String method_name2 = null;
MethodCallArguments args = null;
Block block = null;
Expression left = null;
Expression right = null;
}
: #( CALL
( method_name=methodCallName
|yield:"yield"
|sup:"super"
|defined:"defined?"
|#(DOT left=expression (right=callExpression|method_name2=methodCallName))
|#(COLON2 left=expression (right=callExpression|method_name2=methodCallName))
)
(args=arguments)?
(block=codeBlock)?
)
{
if (null != yield) {
if (null != block) {
throw new RecognitionException("block can not be passed into yield");
}
e = new YieldExpression(args);
} else if (null != sup) {
e = new SuperExpression(args, block);
} else if (null != defined) {
if (null != block) {
throw new RecognitionException("block can not be passed into defined?");
}
e = new DefinedExpression(args);
} else if (null != method_name) {
e = new MethodCallExpression(null, method_name, args, block);
} else if (right != null) {
MethodCallExpression mc = (MethodCallExpression)right;
e = new MethodCallExpression(left, mc.getName(), mc.getArguments(), mc.getBlock());
} else {
e = new MethodCallExpression(left, method_name2, args, block);
}
}
;
arguments
returns [MethodCallArguments args]
{
args = new MethodCallArguments();
Expression e = null;
}
: #(ARG
(e = expression {args.addArgument(e);})*
(e = implicitHash {args.addArgument(e);})?
(REST_ARG_PREFIX e = expression {args.setAsteriskArgument(e);})?
(BLOCK_ARG_PREFIX e = expression {args.setBlockArgument(e);})?
)
;
implicitHash
returns [HashExpression e]
{
Expression key = null;
Expression value = null;
e = new HashExpression();
}
: (
#( ASSOC
key=expression
(value=expression)?
{e.addElement(key, value);value=null;}
)
)+
;
return_arguments
returns [ReturnArguments args]
{
args = new ReturnArguments();
Expression e = null;
}
: #(ARG
(e = expression {args.addArgument(e);})*
(REST_ARG_PREFIX e = expression {args.setAsteriskArgument(e);})?
)
;
elements_as_arguments
returns [MethodCallArguments args]
{
args = new MethodCallArguments();
Expression e = null;
}
: (e = expression {args.addArgument(e);})*
(REST_ARG_PREFIX e = expression {args.setAsteriskArgument(e);})?
;
primaryExpression
returns [Expression e]
: integer:INTEGER {e = new IntegerExpression(integer.getText(), 10);}
| hex:HEX {e = new IntegerExpression(hex.getText(), 16);}
| binary:BINARY {e = new IntegerExpression(binary.getText(), 2);}
| octal:OCTAL {e = new IntegerExpression(octal.getText(), 8);}
| float_:FLOAT {e = new FloatExpression(float_.getText());}
| ascii:ASCII_VALUE {e = new AsciiValueExpression(ascii.getText());}
| command_output:COMMAND_OUTPUT {e = new CommandOutputExpression(command_output.getText());}
| heredoc:HERE_DOC_CONTENT {e = new StringExpression(heredoc.getText(), true);}
| regex:REGEX {e = new RegexpExpression(regex.getText());}
| warray:W_ARRAY {e = new WArrayExpression(warray.getText());}
| e=local_variable
| constant:CONSTANT {e = new CurrentNamespaceConstantExpression(constant.getText());}
| LEADING_COLON2 constant2:CONSTANT {e = new Colon2Expression(null, constant2.getText());}
| gvar:GLOBAL_VARIABLE {e = new GlobalVariableExpression(gvar.getText());}
| "true" {e = new TrueExpression();}
| "false" {e = new FalseExpression();}
| "self" {e = new SelfExpression();}
| "nil" {e = new NilExpression();}
| "super" {e = new SuperExpression(null, null);}
| class_variable:CLASS_VARIABLE {e = new ClassVariableExpression(class_variable.getText());}
| instance_variable:INSTANCE_VARIABLE {e = new InstanceVariableExpression(instance_variable.getText());}
| "__FILE__" {e = new StringExpression((null != filename_) ? filename_ : "-", false);}
| line:"__LINE__" {e = new IntegerExpression(line.getLine());}
| e=string
| e=regexWithExpressionSubstituation
| e=commandOutputWithExpressionSubstituation
| e=heredocWithExpressionSubstituation
;
string
returns[StringGroupExpression e]
{
e = new StringGroupExpression();
StringExpressionWithExpressionSubstitution s = null;
}
: #(STRING
(double_quote_string:DOUBLE_QUOTE_STRING {e.appendString(double_quote_string.getText(), true);}
|single_quote_string:SINGLE_QUOTE_STRING {e.appendString(single_quote_string.getText(), false);}
|s=stringWithExpressionSubstituation {e.addStringWithExpressionSubstituation(s);}
)+
)
;
protected
stringWithExpressionSubstituation
returns [StringExpressionWithExpressionSubstitution e]
: #( b:STRING_BEFORE_EXPRESSION_SUBSTITUTION {e = new StringExpressionWithExpressionSubstitution(b.getText());}
(expression_substituation[e])?
(m:STRING_BETWEEN_EXPRESSION_SUBSTITUTION {e.addString(m.getText());}
(expression_substituation[e])?
)*
a:STRING_AFTER_EXPRESSION_SUBSTITUTION {e.addString(a.getText());}
)
;
protected
heredocWithExpressionSubstituation
returns [StringExpressionWithExpressionSubstitution e]
: #( b:HERE_DOC_BEFORE_EXPRESSION_SUBSTITUTION {e = new StringExpressionWithExpressionSubstitution(b.getText());}
(expression_substituation[e])?
(m:HERE_DOC_BETWEEN_EXPRESSION_SUBSTITUTION {e.addString(m.getText());}
(expression_substituation[e])?
)*
a:HERE_DOC_AFTER_EXPRESSION_SUBSTITUTION {e.addString(a.getText());}
)
;
regexWithExpressionSubstituation
returns [RegexpExpressionWithExpressionSubstitution e]
: #( b:REGEX_BEFORE_EXPRESSION_SUBSTITUTION {e = new RegexpExpressionWithExpressionSubstitution(b.getText());}
(expression_substituation[e])?
(m:STRING_BETWEEN_EXPRESSION_SUBSTITUTION {e.addString(m.getText());}
(expression_substituation[e])?
)*
a:STRING_AFTER_EXPRESSION_SUBSTITUTION {e.addLastString(a.getText());}
)
;
commandOutputWithExpressionSubstituation
returns [CommandOutputExpressionWithExpressionSubstitution e]
: #( b:COMMAND_OUTPUT_BEFORE_EXPRESSION_SUBSTITUTION {e = new CommandOutputExpressionWithExpressionSubstitution(b.getText());}
(expression_substituation[e])?
(m:STRING_BETWEEN_EXPRESSION_SUBSTITUTION {e.addString(m.getText());}
(expression_substituation[e])?
)*
a:STRING_AFTER_EXPRESSION_SUBSTITUTION {e.addString(a.getText());}
)
;
expression_substituation[ExpressionWithExpressionSubstitution e]
{
CompoundStatement cs = null;
}
: ( cs=compoundStatement {e.addCompoundStatement(cs);}
| g:GLOBAL_VARIABLE {e.addGlobalVariable(g.getText());}
| i:INSTANCE_VARIABLE {e.addInstanceVariable(i.getText());}
| c:CLASS_VARIABLE {e.addClassVariable(c.getText());}
)
;
symbol
returns [SymbolExpression e]
{
String s = null;
StringExpressionWithExpressionSubstitution swes = null;
}
: #(SYMBOL
(id:IDENTIFIER {e= new SymbolExpression(id.getText());}
|f:FUNCTION {e= new SymbolExpression(f.getText());}
|constant:CONSTANT {e= new SymbolExpression(constant.getText());}
|g:GLOBAL_VARIABLE {e= new SymbolExpression(g.getText());}
|i:INSTANCE_VARIABLE {e= new SymbolExpression(i.getText());}
|c:CLASS_VARIABLE {e= new SymbolExpression(c.getText());}
|u:UNARY_PLUS_MINUS_METHOD_NAME {e= new SymbolExpression(u.getText());}
|ds:DOUBLE_QUOTE_STRING {e= new SymbolExpression(ds.getText());}
|ss:SINGLE_QUOTE_STRING {e= new SymbolExpression(ss.getText());}
|s=keyword {e = new SymbolExpression(s);}
|s=operator {e = new SymbolExpression(s);}
|swes=stringWithExpressionSubstituation {e = new SymbolExpression(swes);}
)
)
;
methodCallName
returns [String s]
: constant:CONSTANT {s = constant.getText();}
| id:IDENTIFIER {s = id.getText();}
| function:FUNCTION {s = function.getText();}
| unary:UNARY_PLUS_MINUS_METHOD_NAME {s = unary.getText();}
| empty_array:EMPTY_ARRAY {s = empty_array.getText();}
;
local_variable
returns [LocalVariableExpression e]
: id:IDENTIFIER {e = new LocalVariableExpression(id.getText(), false);}
| function:FUNCTION {e = new LocalVariableExpression(function.getText(), true);}
;
arrayExpression
returns [ArrayExpression e]
{
Expression element = null;
}
: #( LBRACK {e = new ArrayExpression();}
(element=expression {e.addElement(element);})*
(REST_ARG_PREFIX element=expression {e.setAsteriskElement(element);})?
)
| EMPTY_ARRAY {e = new ArrayExpression();}
;
hashExpression
returns [HashExpression e]
{
Expression key = null;
Expression value = null;
}
: #( LCURLY_HASH {e = new HashExpression();}
(key=expression (ASSOC value=expression)? {e.addElement(key, value);value=null;})*
)
;
ifExpression
returns [IfExpression e]
{
Expression condition = null;
CompoundStatement body = null;
}
: #( "if" condition=expression (body=compoundStatement)? {e = new IfExpression(condition, body);condition=null;body=null;}
("elsif" (condition=expression)? (body=compoundStatement)? {e.addElsif(condition, body);condition=null;body=null;})*
("else" (body=compoundStatement)? {e.addElse(body);})?
)
;
whileExpression
returns [WhileExpression e]
{
Expression condition = null;
CompoundStatement body = null;
}
: #( "while" condition=expression (body=compoundStatement)? {e = new WhileExpression(condition, body, false, false);}
)
| #( "until" condition=expression (body=compoundStatement)? {e = new WhileExpression(condition, body, true, false);}
)
| #( WHILE_MODIFIER condition=expression (body=compoundStatement)? {e = new WhileExpression(condition, body, false, true);}
)
| #( UNTIL_MODIFIER condition=expression (body=compoundStatement)? {e = new WhileExpression(condition, body, true, true);}
)
;
unlessExpression
returns [UnlessExpression e]
{
Expression condition = null;
CompoundStatement body = null;
CompoundStatement else_body = null;
}
: #( "unless" condition=expression (body=compoundStatement)?
("else" (else_body=compoundStatement)?)?
{e = new UnlessExpression(condition, body, else_body);}
)
;
caseExpression
returns [CaseExpression e]
{
Expression condition = null;
CompoundStatement body = null;
When w = null;
} : #( "case" (condition=expression)? {e=null;} {e = new CaseExpression(condition);}
(w=when {e.addWhen(w);})*
("else" (body=compoundStatement)? {e.addElse(body);})?
)
;
when
returns [When w]
{
Expression e = null;
CompoundStatement body = null;
}
: "when" {w = new When();}
#(MRHS
(e=expression {w.addCondition(e);})*
(REST_ARG_PREFIX e=expression {w.setAsteriskCondition(e);})?
)
(body=compoundStatement)? {w.setBody(body);}
;
exceptionHandlingExpression
returns [ExceptionHandlingExpression e]
{
String name = null;
BodyStatement body = null;
}
: #("begin"
(body=bodyStatement)?
)
{
e = new ExceptionHandlingExpression(body);
}
;
classDefinition
returns [ClassDefinitionExpression e]
{
Expression scope = null;
String name = null;
Expression exp = null;
Expression super_class = null;
BodyStatement body = null;
}
: #("class"
(LEADING_COLON2 {scope = new CurrentNamespaceConstantExpression("Object");})?
(name=className (LESS_THAN super_class=expression)?
|#(COLON2 scope=expression name=className) (LESS_THAN super_class=expression)?
|LEFT_SHIFT exp=expression
)
(body=bodyStatement)?
)
{
if (name != null) {
e = new ClassDefinitionExpression(scope, name, super_class, body);
} else {
e = new ClassDefinitionExpression(exp, super_class, body);
}
}
;
moduleDefinition
returns [ModuleDefinitionExpression e]
{
String name = null;
Expression scope = null;
BodyStatement body = null;
}
: #("module"
(LEADING_COLON2 {scope = new CurrentNamespaceConstantExpression("Object");})?
(name=moduleName
|#(COLON2 scope=expression name=moduleName)
)
(body=bodyStatement)?
)
{
e = new ModuleDefinitionExpression(scope, name, body);
}
;
methodDefinition
returns [MethodDefinitionExpression e]
{
String name = null;
BodyStatement body = null;
Expression exp = null;
int lineNumber = _t.getLine();
}
: #("def"
(name=methodName {e = new MethodDefinitionExpression(name);}
|#(SINGLETON_METHOD exp=expression (DOT|COLON2) name=methodName {e = new MethodDefinitionExpression(name, exp);})
)
( {id=null;func=null;exp=null;}
(id:IDENTIFIER|func:FUNCTION)
((ASSIGN|ASSIGN_WITH_NO_LEADING_SPACE) exp=expression)?
{e.addParameter((null != id) ? id.getText() : func.getText(), exp);}
)*
(
REST_ARG_PREFIX (id2:IDENTIFIER|func2:FUNCTION)?
{e.setAsteriskParameter((null != id2) ? id2.getText() : ((null != func2) ? func2.getText() : null));}
)?
(
BLOCK_ARG_PREFIX (id3:IDENTIFIER|func3:FUNCTION)
{e.setBlockParameter((null != id3) ? id3.getText() : func3.getText());}
)?
(body=bodyStatement {e.setBody(body);})?
)
{
e.setPosition(lineNumber);
}
;
forInExpression
returns [ForInExpression e]
{
// TODO: Here's a line number issue, after forInExpression is called. Line number is added to one
// So, the final line number is the line next to it.
Expression exp = null;
Block b = null;
CompoundStatement cs = null;
ParameterVariableExpression var = null;
int lineNumber = _t.getLine();
}
: #( "for" {b = new Block();}
( #(BLOCK_ARG
((var=local_variable|var=nestedLhs) {b.addParameter(var, null);})+
)
| #(BLOCK_ARG_WITH_EXTRA_COMMA {{b.setHasExtraComma();}}
((var=local_variable|var=nestedLhs) {b.addParameter(var, null);})+
)
)
"in"
exp=expression
(cs=compoundStatement {b.setBody(cs);})?
{e = new ForInExpression(exp, b);}
)
{
e.setPosition(lineNumber);
}
;
codeBlock
returns [Block b]
{
CompoundStatement cs = null;
Expression default_value = null;
ParameterVariableExpression var = null;
}
: #( BLOCK {b = new Block();}
((BOR|LOGICAL_OR) {b.setShouldValidateArgumentLength();})?
(
#(BLOCK_ARG
( (var=local_variable|var=nestedLhs)
((ASSIGN|ASSIGN_WITH_NO_LEADING_SPACE) default_value=expression)?
{b.addParameter(var, default_value);}
)*
( REST_ARG_PREFIX {b.setAsterisk();}
(
var=local_variable {b.setAsteriskParameter(var);}
)?
)?
)
| #(BLOCK_ARG_WITH_EXTRA_COMMA {{b.setHasExtraComma();}}
( (var=local_variable|var=nestedLhs)
((ASSIGN|ASSIGN_WITH_NO_LEADING_SPACE) default_value=expression)?
{b.addParameter(var, default_value);}
)*
( REST_ARG_PREFIX
(
var=local_variable {b.setAsteriskParameter(var);}
)?
)?
)
)?
(cs=compoundStatement {b.setBody(cs);})?
)
;
className
returns [String s]
: id:IDENTIFIER {s = id.getText();}
| function:FUNCTION {s = function.getText();}
| constant:CONSTANT {s = constant.getText();}
;
moduleName
returns [String s]
: id:IDENTIFIER {s = id.getText();}
| function:FUNCTION {s = function.getText();}
| constant:CONSTANT {s = constant.getText();}
;
methodName
returns [String s]
: id:IDENTIFIER (assign1:ASSIGN_WITH_NO_LEADING_SPACE)? {s = id.getText(); if (null != assign1) {s += "=";}}
| function:FUNCTION (assign2:ASSIGN_WITH_NO_LEADING_SPACE)? {s = function.getText(); if (null != assign2) {s += "=";}}
| constant:CONSTANT {s = constant.getText();}
| unary:UNARY_PLUS_MINUS_METHOD_NAME {s = unary.getText();}
| s=keyword
| s=operator
;
operator
returns [String s]
: LEFT_SHIFT {s = "<<";}
| RIGHT_SHIFT {s = ">>";}
| EQUAL {s = "==";}
| CASE_EQUAL {s = "===";}
| GREATER_THAN {s = ">";}
| GREATER_OR_EQUAL {s = ">=";}
| LESS_THAN {s = "<";}
| LESS_OR_EQUAL {s = "<=";}
| PLUS {s = "+";}
| MINUS {s = "-";}
| STAR {s = "*";}
| DIV {s = "/";}
| MOD {s = "%";}
| POWER {s = "**";}
| BAND {s = "&";}
| BOR {s = "|";}
| BXOR {s = "^";}
| (EMPTY_ARRAY |EMPTY_ARRAY_ACCESS) {s = "[]";} (options{greedy=true;}:ASSIGN_WITH_NO_LEADING_SPACE {s = "[]=";})?
| MATCH {s = "=~";}
| COMPARE {s = "<=>";}
| BNOT {s = "~";}
| SINGLE_QUOTE {s = "`";}
;
keyword
returns [String s]
: "and" {s = "and";}
| "def" {s = "def";}
| "end" {s = "end";}
| "in" {s = "in";}
| "or" {s = "or";}
| "unless" {s = "unless";}
| "begin" {s = "begin";}
| "defined?" {s = "defined?";}
| "ensure" {s = "ensure";}
| "module" {s = "module";}
| "redo" {s = "redo";}
| "super" {s = "super";}
| "until" {s = "until";}
| "BEGIN" {s = "BEGIN";}
| "break" {s = "break";}
| "do" {s = "do";}
| "next" {s = "next";}
| "rescue" {s = "rescue";}
| "then" {s = "then";}
| "when" {s = "when";}
| "END" {s = "END";}
| "case" {s = "case";}
| "else" {s = "else";}
| "for" {s = "for";}
| "retry" {s = "retry";}
| "while" {s = "while";}
| "alias" {s = "alias";}
| "class" {s = "class";}
| "elsif" {s = "elsif";}
| "if" {s = "if";}
| "not" {s = "not";}
| "return" {s = "return";}
| "undef" {s = "undef";}
| "yield" {s = "yield";}
| "true" {s = "true";}
| "false" {s = "false";}
| "self" {s = "self";}
| "nil" {s = "nil";}
;
bodyStatement
returns [BodyStatement bs]
{
ExceptionList el;
CompoundStatement cs = null;
CompoundStatement rescue_cs = null;
CompoundStatement else_cs = null;
CompoundStatement ensure_cs = null;
}
: #( BODY
(cs=compoundStatement)? {bs = new BodyStatement(cs);}
("rescue" el=exceptionList (rescue_cs=compoundStatement)? {bs.addRescue(el, rescue_cs);})*
("else" (else_cs=compoundStatement)? {bs.addElse(else_cs);})?
("ensure" (ensure_cs=compoundStatement)? {bs.addEnsure(ensure_cs);})?
)
;
//TODO
exceptionList
returns [ExceptionList el]
{
Expression e;
LocalVariableExpression v;
}
: {el = new ExceptionList();}
(e=expression {el.addArgument(e);})*
(ASSOC v=local_variable {el.addExceptionVariable(v);})?
;