Repository navigation
Expand file tree
/
Copy pathDefaultRubyParser.y
More file actions
1924 lines (1764 loc) · 75.4 KB
/
Copy pathDefaultRubyParser.y
File metadata and controls
1924 lines (1764 loc) · 75.4 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
%{
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2001-2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004 Charles O Nutter <headius@headius.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2007 Mirko Stocker <me@misto.ch>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.parser;
import java.io.IOException;
import org.jruby.ast.ArgsNode;
import org.jruby.ast.ArgumentNode;
import org.jruby.ast.ArrayNode;
import org.jruby.ast.AssignableNode;
import org.jruby.ast.BackRefNode;
import org.jruby.ast.BeginNode;
import org.jruby.ast.BlockAcceptingNode;
import org.jruby.ast.BlockArgNode;
import org.jruby.ast.BlockNode;
import org.jruby.ast.BlockPassNode;
import org.jruby.ast.BreakNode;
import org.jruby.ast.CaseNode;
import org.jruby.ast.ClassNode;
import org.jruby.ast.ClassVarNode;
import org.jruby.ast.Colon3Node;
import org.jruby.ast.ConstDeclNode;
import org.jruby.ast.DRegexpNode;
import org.jruby.ast.DStrNode;
import org.jruby.ast.DSymbolNode;
import org.jruby.ast.DXStrNode;
import org.jruby.ast.DefinedNode;
import org.jruby.ast.DefnNode;
import org.jruby.ast.DefsNode;
import org.jruby.ast.DotNode;
import org.jruby.ast.EnsureNode;
import org.jruby.ast.EvStrNode;
import org.jruby.ast.FCallNoArgBlockNode;
import org.jruby.ast.FCallNoArgNode;
import org.jruby.ast.FixnumNode;
import org.jruby.ast.FloatNode;
import org.jruby.ast.ForNode;
import org.jruby.ast.GlobalVarNode;
import org.jruby.ast.HashNode;
import org.jruby.ast.IfNode;
import org.jruby.ast.InstVarNode;
import org.jruby.ast.IterNode;
import org.jruby.ast.ListNode;
import org.jruby.ast.LiteralNode;
import org.jruby.ast.ModuleNode;
import org.jruby.ast.MultipleAsgnNode;
import org.jruby.ast.NewlineNode;
import org.jruby.ast.NextNode;
import org.jruby.ast.NilImplicitNode;
import org.jruby.ast.NilNode;
import org.jruby.ast.Node;
import org.jruby.ast.NotNode;
import org.jruby.ast.OpAsgnAndNode;
import org.jruby.ast.OpAsgnNode;
import org.jruby.ast.OpAsgnOrNode;
import org.jruby.ast.PostExeNode;
import org.jruby.ast.PreExeNode;
import org.jruby.ast.RedoNode;
import org.jruby.ast.RegexpNode;
import org.jruby.ast.RescueBodyNode;
import org.jruby.ast.RescueNode;
import org.jruby.ast.RestArgNode;
import org.jruby.ast.RetryNode;
import org.jruby.ast.ReturnNode;
import org.jruby.ast.SClassNode;
import org.jruby.ast.SValueNode;
import org.jruby.ast.SelfNode;
import org.jruby.ast.SplatNode;
import org.jruby.ast.StarNode;
import org.jruby.ast.StrNode;
import org.jruby.ast.SymbolNode;
import org.jruby.ast.ToAryNode;
import org.jruby.ast.TypedArgumentNode;
import org.jruby.ast.UnnamedRestArgNode;
import org.jruby.ast.UntilNode;
import org.jruby.ast.VAliasNode;
import org.jruby.ast.WhileNode;
import org.jruby.ast.XStrNode;
import org.jruby.ast.YieldNode;
import org.jruby.ast.ZArrayNode;
import org.jruby.ast.ZSuperNode;
import org.jruby.ast.ZYieldNode;
import org.jruby.ast.ZeroArgNode;
import org.jruby.ast.types.ILiteralNode;
import org.jruby.common.IRubyWarnings;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.lexer.yacc.ISourcePositionHolder;
import org.jruby.lexer.yacc.LexerSource;
import org.jruby.lexer.yacc.RubyYaccLexer;
import org.jruby.lexer.yacc.RubyYaccLexer.LexState;
import org.jruby.lexer.yacc.StrTerm;
import org.jruby.lexer.yacc.SyntaxException;
import org.jruby.lexer.yacc.SyntaxException.PID;
import org.jruby.lexer.yacc.Token;
import org.jruby.util.ByteList;
import org.jruby.util.RegexpOptions;
public class DefaultRubyParser implements RubyParser {
protected ParserSupport support;
protected RubyYaccLexer lexer;
public DefaultRubyParser() {
this(new ParserSupport());
}
public DefaultRubyParser(ParserSupport support) {
this.support = support;
lexer = new RubyYaccLexer();
lexer.setParserSupport(support);
support.setLexer(lexer);
}
public void setWarnings(IRubyWarnings warnings) {
support.setWarnings(warnings);
lexer.setWarnings(warnings);
}
%}
%token <Token> kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF
kUNLESS kTHEN kELSIF kELSE kCASE kWHEN kWHILE kUNTIL kFOR kBREAK kNEXT
kREDO kRETRY kIN kDO kDO_COND kDO_BLOCK kRETURN kYIELD kSUPER kSELF kNIL
kTRUE kFALSE kAND kOR kNOT kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD
kRESCUE_MOD kALIAS kDEFINED klBEGIN klEND k__LINE__ k__FILE__
k__ENCODING__ kDO_LAMBDA
%token <Token> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tCHAR
%type <Token> variable
%type <Token> sym symbol operation operation2 operation3 cname fname op
%type <Token> dot_or_colon restarg_mark blkarg_mark
%token <Token> tUPLUS /* unary+ */
%token <Token> tUMINUS /* unary- */
%token <Token> tUMINUS_NUM /* unary- */
%token <Token> tPOW /* ** */
%token <Token> tCMP /* <=> */
%token <Token> tEQ /* == */
%token <Token> tEQQ /* === */
%token <Token> tNEQ /* != */
%token <Token> tGEQ /* >= */
%token <Token> tLEQ /* <= */
%token <Token> tANDOP tOROP /* && and || */
%token <Token> tMATCH tNMATCH /* =~ and !~ */
%token <Token> tDOT /* Is just '.' in ruby and not a token */
%token <Token> tDOT2 tDOT3 /* .. and ... */
%token <Token> tAREF tASET /* [] and []= */
%token <Token> tLSHFT tRSHFT /* << and >> */
%token <Token> tCOLON2 /* :: */
%token <Token> tCOLON3 /* :: at EXPR_BEG */
%token <Token> tOP_ASGN /* +=, -= etc. */
%token <Token> tASSOC /* => */
%token <Token> tLPAREN /* ( */
%token <Token> tLPAREN2 /* ( Is just '(' in ruby and not a token */
%token <Token> tRPAREN /* ) */
%token <Token> tLPAREN_ARG /* ( */
%token <Token> tLBRACK /* [ */
%token <Token> tRBRACK /* ] */
%token <Token> tLBRACE /* { */
%token <Token> tLBRACE_ARG /* { */
%token <Token> tSTAR /* * */
%token <Token> tSTAR2 /* * Is just '*' in ruby and not a token */
%token <Token> tAMPER /* & */
%token <Token> tAMPER2 /* & Is just '&' in ruby and not a token */
%token <Token> tTILDE /* ` is just '`' in ruby and not a token */
%token <Token> tPERCENT /* % is just '%' in ruby and not a token */
%token <Token> tDIVIDE /* / is just '/' in ruby and not a token */
%token <Token> tPLUS /* + is just '+' in ruby and not a token */
%token <Token> tMINUS /* - is just '-' in ruby and not a token */
%token <Token> tLT /* < is just '<' in ruby and not a token */
%token <Token> tGT /* > is just '>' in ruby and not a token */
%token <Token> tPIPE /* | is just '|' in ruby and not a token */
%token <Token> tBANG /* ! is just '!' in ruby and not a token */
%token <Token> tCARET /* ^ is just '^' in ruby and not a token */
%token <Token> tLCURLY /* { is just '{' in ruby and not a token */
%token <Token> tRCURLY /* } is just '}' in ruby and not a token */
%token <Token> tBACK_REF2 /* { is just '`' in ruby and not a token */
%token <Token> tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
%token <Token> tSTRING_DBEG tSTRING_DVAR tSTRING_END
%token <Token> tLAMBDA tLAMBEG
%token <Node> tNTH_REF tBACK_REF tSTRING_CONTENT tINTEGER
%token <FloatNode> tFLOAT
%token <RegexpNode> tREGEXP_END
%type <Node> singleton strings string string1 xstring regexp
%type <Node> string_contents xstring_contents string_content method_call
%type <Node> words qwords word literal numeric dsym cpath command_call
%type <Node> compstmt bodystmt stmts stmt expr arg primary command
%type <Node> expr_value primary_value opt_else cases if_tail exc_var
%type <Node> call_args call_args2 open_args opt_ensure paren_args superclass
%type <Node> command_args var_ref opt_paren_args block_call block_command
%type <Node> f_arglist f_args f_opt undef_list string_dvar backref
%type <Node> mrhs mlhs_item mlhs_node arg_value case_body exc_list aref_args
%type <Node> block_var opt_block_var lhs none
%type <ListNode> qword_list word_list f_arg f_optarg
%type <ListNode> args mlhs_head assocs assoc assoc_list
%type <Node> when_args
%type <BlockPassNode> opt_block_arg block_arg none_block_pass
%type <BlockArgNode> opt_f_block_arg f_block_arg
%type <IterNode> brace_block do_block cmd_brace_block
%type <MultipleAsgnNode> mlhs mlhs_basic mlhs_entry
%type <RescueBodyNode> opt_rescue
%type <AssignableNode> var_lhs
%type <LiteralNode> fsym
%type <Node> fitem
%type <Node> for_var
%type <ListNode> block_par
%type <ArgumentNode> f_norm_arg
%type <RestArgNode> f_rest_arg
//%type <Token> rparen rbracket reswords f_bad_arg
/*
* precedence table
*/
%nonassoc tLOWEST
%nonassoc tLBRACE_ARG
%nonassoc kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD
%left kOR kAND
%right kNOT
%nonassoc kDEFINED
%right '=' tOP_ASGN
%left kRESCUE_MOD
%right '?' ':'
%nonassoc tDOT2 tDOT3
%left tOROP
%left tANDOP
%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
%left tGT tGEQ tLT tLEQ
%left tPIPE tCARET
%left tAMPER2
%left tLSHFT tRSHFT
%left tPLUS tMINUS
%left tSTAR2 tDIVIDE tPERCENT
%right tUMINUS_NUM tUMINUS
%right tPOW
%right tBANG tTILDE tUPLUS
%%
program : {
lexer.setState(LexState.EXPR_BEG);
support.initTopLocalVariables();
} compstmt {
if ($2 != null) {
/* last expression should not be void */
if ($2 instanceof BlockNode) {
support.checkUselessStatement($<BlockNode>2.getLast());
} else {
support.checkUselessStatement($2);
}
}
support.getResult().setAST(support.addRootNode($2, support.getPosition($2)));
}
bodystmt : compstmt opt_rescue opt_else opt_ensure {
Node node = $1;
if ($2 != null) {
node = new RescueNode(support.getPosition($1), $1, $2, $3);
} else if ($3 != null) {
support.warn(ID.ELSE_WITHOUT_RESCUE, support.getPosition($1), "else without rescue is useless");
node = support.appendToBlock($1, $3);
}
if ($4 != null) {
if (node == null) node = NilImplicitNode.NIL;
node = new EnsureNode(support.getPosition($1), node, $4);
}
$$ = node;
}
compstmt : stmts opt_terms {
if ($1 instanceof BlockNode) {
support.checkUselessStatements($<BlockNode>1);
}
$$ = $1;
}
stmts : none
| stmt {
$$ = support.newline_node($1, support.getPosition($1));
}
| stmts terms stmt {
$$ = support.appendToBlock($1, support.newline_node($3, support.getPosition($3)));
}
| error stmt {
$$ = $2;
}
stmt : kALIAS fitem {
lexer.setState(LexState.EXPR_FNAME);
} fitem {
$$ = support.newAlias($1.getPosition(), $2, $4);
}
| kALIAS tGVAR tGVAR {
$$ = new VAliasNode($1.getPosition(), (String) $2.getValue(), (String) $3.getValue());
}
| kALIAS tGVAR tBACK_REF {
$$ = new VAliasNode($1.getPosition(), (String) $2.getValue(), "$" + $<BackRefNode>3.getType()); // XXX
}
| kALIAS tGVAR tNTH_REF {
support.yyerror("can't make alias for the number variables");
}
| kUNDEF undef_list {
$$ = $2;
}
| stmt kIF_MOD expr_value {
$$ = new IfNode(support.getPosition($1), support.getConditionNode($3), $1, null);
}
| stmt kUNLESS_MOD expr_value {
$$ = new IfNode(support.getPosition($1), support.getConditionNode($3), null, $1);
}
| stmt kWHILE_MOD expr_value {
if ($1 != null && $1 instanceof BeginNode) {
$$ = new WhileNode(support.getPosition($1), support.getConditionNode($3), $<BeginNode>1.getBodyNode(), false);
} else {
$$ = new WhileNode(support.getPosition($1), support.getConditionNode($3), $1, true);
}
}
| stmt kUNTIL_MOD expr_value {
if ($1 != null && $1 instanceof BeginNode) {
$$ = new UntilNode(support.getPosition($1), support.getConditionNode($3), $<BeginNode>1.getBodyNode(), false);
} else {
$$ = new UntilNode(support.getPosition($1), support.getConditionNode($3), $1, true);
}
}
| stmt kRESCUE_MOD stmt {
Node body = $3 == null ? NilImplicitNode.NIL : $3;
$$ = new RescueNode(support.getPosition($1), $1, new RescueBodyNode(support.getPosition($1), null, body, null), null);
}
| klBEGIN {
if (support.isInDef() || support.isInSingle()) {
support.yyerror("BEGIN in method");
}
support.pushLocalScope();
} tLCURLY compstmt tRCURLY {
support.getResult().addBeginNode(new PreExeNode($1.getPosition(), support.getCurrentScope(), $4));
support.popCurrentScope();
$$ = null; //XXX 0;
}
| klEND tLCURLY compstmt tRCURLY {
if (support.isInDef() || support.isInSingle()) {
support.warn(ID.END_IN_METHOD, $1.getPosition(), "END in method; use at_exit");
}
$$ = new PostExeNode($1.getPosition(), $3);
}
| lhs '=' command_call {
$$ = support.node_assign($1, $3);
}
| mlhs '=' command_call {
support.checkExpression($3);
if ($1.getHeadNode() != null) {
$1.setValueNode(new ToAryNode(support.getPosition($1), $3));
} else {
$1.setValueNode(support.newArrayNode(support.getPosition($1), $3));
}
$$ = $1;
}
| var_lhs tOP_ASGN command_call {
support.checkExpression($3);
String asgnOp = (String) $2.getValue();
if (asgnOp.equals("||")) {
$1.setValueNode($3);
$$ = new OpAsgnOrNode(support.getPosition($1), support.gettable2($1), $1);
} else if (asgnOp.equals("&&")) {
$1.setValueNode($3);
$$ = new OpAsgnAndNode(support.getPosition($1), support.gettable2($1), $1);
} else {
$1.setValueNode(support.getOperatorCallNode(support.gettable2($1), asgnOp, $3));
$1.setPosition(support.getPosition($1));
$$ = $1;
}
}
| primary_value '[' aref_args tRBRACK tOP_ASGN command_call {
support.checkExpression($6);
$$ = support.new_opElementAsgnNode(support.getPosition($1), $1, (String) $5.getValue(), $3, $6);
}
| primary_value tDOT tIDENTIFIER tOP_ASGN command_call {
support.checkExpression($5);
$$ = new OpAsgnNode(support.getPosition($1), $1, $5, (String) $3.getValue(), (String) $4.getValue());
}
| primary_value tDOT tCONSTANT tOP_ASGN command_call {
support.checkExpression($5);
$$ = new OpAsgnNode(support.getPosition($1), $1, $5, (String) $3.getValue(), (String) $4.getValue());
}
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call {
support.checkExpression($5);
$$ = new OpAsgnNode(support.getPosition($1), $1, $5, (String) $3.getValue(), (String) $4.getValue());
}
| backref tOP_ASGN command_call {
support.backrefAssignError($1);
}
| lhs '=' mrhs {
$$ = support.node_assign($1, support.newSValueNode(support.getPosition($1), $3));
}
| mlhs '=' arg_value {
if ($1.getHeadNode() != null) {
$1.setValueNode(new ToAryNode(support.getPosition($1), $3));
} else {
$1.setValueNode(support.newArrayNode(support.getPosition($1), $3));
}
$$ = $1;
}
| mlhs '=' mrhs {
$<AssignableNode>1.setValueNode($3);
$$ = $1;
$1.setPosition(support.getPosition($1));
}
| expr
// Node:expr *CURRENT* all but arg so far
expr : command_call
| expr kAND expr {
$$ = support.newAndNode($2.getPosition(), $1, $3);
}
| expr kOR expr {
$$ = support.newOrNode($2.getPosition(), $1, $3);
}
| kNOT expr {
$$ = new NotNode($1.getPosition(), support.getConditionNode($2));
}
| tBANG command_call {
$$ = new NotNode($1.getPosition(), support.getConditionNode($2));
}
| arg
expr_value : expr {
support.checkExpression($1);
}
// Node:command - call with or with block on end [!null]
command_call : command
| block_command
| kRETURN call_args {
$$ = new ReturnNode($1.getPosition(), support.ret_args($2, $1.getPosition()));
}
| kBREAK call_args {
$$ = new BreakNode($1.getPosition(), support.ret_args($2, $1.getPosition()));
}
| kNEXT call_args {
$$ = new NextNode($1.getPosition(), support.ret_args($2, $1.getPosition()));
}
// Node:block_command - A call with a block (foo.bar {...}, foo::bar {...}, bar {...}) [!null]
block_command : block_call
| block_call tDOT operation2 command_args {
$$ = support.new_call($1, $3, $4, null);
}
| block_call tCOLON2 operation2 command_args {
$$ = support.new_call($1, $3, $4, null);
}
// :brace_block - [!null]
cmd_brace_block : tLBRACE_ARG {
support.pushBlockScope();
} opt_block_var compstmt tRCURLY {
$$ = support.new_iter(support.getPosition($1), $3, support.getCurrentScope(), $4);
support.popCurrentScope();
}
// Node:command - fcall/call/yield/super [!null]
command : operation command_args %prec tLOWEST {
$$ = support.new_fcall($1, $2, null);
}
| operation command_args cmd_brace_block {
$$ = support.new_fcall($1, $2, $3);
}
| primary_value tDOT operation2 command_args %prec tLOWEST {
$$ = support.new_call($1, $3, $4, null);
}
| primary_value tDOT operation2 command_args cmd_brace_block {
$$ = support.new_call($1, $3, $4, $5);
}
| primary_value tCOLON2 operation2 command_args %prec tLOWEST {
$$ = support.new_call($1, $3, $4, null);
}
| primary_value tCOLON2 operation2 command_args cmd_brace_block {
$$ = support.new_call($1, $3, $4, $5);
}
| kSUPER command_args {
$$ = support.new_super($2, $1); // .setPosFrom($2);
}
| kYIELD command_args {
$$ = support.new_yield($1.getPosition(), $2);
}
// MultipleAssigNode:mlhs - [!null]
mlhs : mlhs_basic
| tLPAREN mlhs_entry tRPAREN {
$$ = $2;
}
// MultipleAssignNode:mlhs_entry - mlhs w or w/o parens [!null]
mlhs_entry : mlhs_basic
| tLPAREN mlhs_entry tRPAREN {
$$ = new MultipleAsgnNode($1.getPosition(), support.newArrayNode($1.getPosition(), $2), null);
}
// MultipleAssignNode:mlhs_basic - multiple left hand side (basic because used in multiple context) [!null]
mlhs_basic : mlhs_head {
$$ = new MultipleAsgnNode(support.getPosition($1), $1, null);
}
| mlhs_head mlhs_item {
//mirko: check
$$ = new MultipleAsgnNode(support.getPosition($<Node>1), $1.add($2), null);
}
| mlhs_head tSTAR mlhs_node {
$$ = new MultipleAsgnNode(support.getPosition($1), $1, $3);
}
| mlhs_head tSTAR {
$$ = new MultipleAsgnNode(support.getPosition($1), $1, new StarNode(lexer.getPosition()));
}
| tSTAR mlhs_node {
$$ = new MultipleAsgnNode($1.getPosition(), null, $2);
}
| tSTAR {
$$ = new MultipleAsgnNode($1.getPosition(), null, new StarNode(lexer.getPosition()));
}
mlhs_item : mlhs_node
| tLPAREN mlhs_entry tRPAREN {
$$ = $2;
}
// [!null]
mlhs_head : mlhs_item ',' {
$$ = support.newArrayNode($1.getPosition(), $1);
}
| mlhs_head mlhs_item ',' {
$$ = $1.add($2);
}
mlhs_node : variable {
$$ = support.assignable($1, NilImplicitNode.NIL);
}
| primary_value '[' aref_args tRBRACK {
$$ = support.aryset($1, $3);
}
| primary_value tDOT tIDENTIFIER {
$$ = support.attrset($1, (String) $3.getValue());
}
| primary_value tCOLON2 tIDENTIFIER {
$$ = support.attrset($1, (String) $3.getValue());
}
| primary_value tDOT tCONSTANT {
$$ = support.attrset($1, (String) $3.getValue());
}
| primary_value tCOLON2 tCONSTANT {
if (support.isInDef() || support.isInSingle()) {
support.yyerror("dynamic constant assignment");
}
ISourcePosition position = support.getPosition($1);
$$ = new ConstDeclNode(position, null, support.new_colon2(position, $1, (String) $3.getValue()), NilImplicitNode.NIL);
}
| tCOLON3 tCONSTANT {
if (support.isInDef() || support.isInSingle()) {
support.yyerror("dynamic constant assignment");
}
ISourcePosition position = support.getPosition($1);
$$ = new ConstDeclNode(position, null, support.new_colon3(position, (String) $2.getValue()), NilImplicitNode.NIL);
}
| backref {
support.backrefAssignError($1);
}
// Node:lhs - left hand side of an assignment [!null]
lhs : variable {
$$ = support.assignable($1, NilImplicitNode.NIL);
}
| primary_value '[' aref_args tRBRACK {
$$ = support.aryset($1, $3);
}
| primary_value tDOT tIDENTIFIER {
$$ = support.attrset($1, (String) $3.getValue());
}
| primary_value tCOLON2 tIDENTIFIER {
$$ = support.attrset($1, (String) $3.getValue());
}
| primary_value tDOT tCONSTANT {
$$ = support.attrset($1, (String) $3.getValue());
}
| primary_value tCOLON2 tCONSTANT {
if (support.isInDef() || support.isInSingle()) {
support.yyerror("dynamic constant assignment");
}
ISourcePosition position = support.getPosition($1);
$$ = new ConstDeclNode(position, null, support.new_colon2(position, $1, (String) $3.getValue()), NilImplicitNode.NIL);
}
| tCOLON3 tCONSTANT {
if (support.isInDef() || support.isInSingle()) {
support.yyerror("dynamic constant assignment");
}
ISourcePosition position = support.getPosition($1);
$$ = new ConstDeclNode(position, null, support.new_colon3(position, (String) $2.getValue()), NilImplicitNode.NIL);
}
| backref {
support.backrefAssignError($1);
}
cname : tIDENTIFIER {
support.yyerror("class/module name must be CONSTANT");
}
| tCONSTANT
cpath : tCOLON3 cname {
$$ = support.new_colon3($1.getPosition(), (String) $2.getValue());
}
| cname {
$$ = support.new_colon2($1.getPosition(), null, (String) $1.getValue());
}
| primary_value tCOLON2 cname {
$$ = support.new_colon2($1.getPosition(), $1, (String) $3.getValue());
}
// Token:fname - A function name [!null]
fname : tIDENTIFIER | tCONSTANT | tFID
| op {
lexer.setState(LexState.EXPR_END);
$$ = $1;
}
// FIXME: reswords is really Keyword which is not a Token...This should bomb
| reswords {
lexer.setState(LexState.EXPR_END);
$$ = $<>1;
}
// LiteralNode:fsym
fsym : fname {
$$ = new LiteralNode($1);
}
| symbol {
$$ = new LiteralNode($1);
}
// Node:fitem
fitem : fsym {
$$ = $1;
}
| dsym {
$$ = $1;
}
undef_list : fitem {
$$ = support.newUndef($1.getPosition(), $1);
}
| undef_list ',' {
lexer.setState(LexState.EXPR_FNAME);
} fitem {
$$ = support.appendToBlock($1, support.newUndef($1.getPosition(), $4));
}
// Token:op - inline operations [!null]
op : tPIPE | tCARET | tAMPER2 | tCMP | tEQ | tEQQ | tMATCH | tGT
| tGEQ | tLT | tLEQ | tLSHFT | tRSHFT | tPLUS | tMINUS | tSTAR2
| tSTAR | tDIVIDE | tPERCENT | tPOW | tTILDE | tUPLUS | tUMINUS
| tAREF | tASET | tBACK_REF2
// Keyword:reswords - reserved words [!null]
reswords : k__LINE__ | k__FILE__ | klBEGIN | klEND
| kALIAS | kAND | kBEGIN | kBREAK | kCASE | kCLASS | kDEF
| kDEFINED | kDO | kELSE | kELSIF | kEND | kENSURE | kFALSE
| kFOR | kIN | kMODULE | kNEXT | kNIL | kNOT
| kOR | kREDO | kRESCUE | kRETRY | kRETURN | kSELF | kSUPER
| kTHEN | kTRUE | kUNDEF | kWHEN | kYIELD
| kIF_MOD | kUNLESS_MOD | kWHILE_MOD | kUNTIL_MOD | kRESCUE_MOD
arg : lhs '=' arg {
$$ = support.node_assign($1, $3);
// FIXME: Consider fixing node_assign itself rather than single case
$<Node>$.setPosition(support.getPosition($1));
}
| lhs '=' arg kRESCUE_MOD arg {
ISourcePosition position = $4.getPosition();
Node body = $5 == null ? NilImplicitNode.NIL : $5;
$$ = support.node_assign($1, new RescueNode(position, $3, new RescueBodyNode(position, null, body, null), null));
}
| var_lhs tOP_ASGN arg {
support.checkExpression($3);
String asgnOp = (String) $2.getValue();
if (asgnOp.equals("||")) {
$1.setValueNode($3);
$$ = new OpAsgnOrNode(support.getPosition($1), support.gettable2($1), $1);
} else if (asgnOp.equals("&&")) {
$1.setValueNode($3);
$$ = new OpAsgnAndNode(support.getPosition($1), support.gettable2($1), $1);
} else {
$1.setValueNode(support.getOperatorCallNode(support.gettable2($1), asgnOp, $3));
$1.setPosition(support.getPosition($1));
$$ = $1;
}
}
| primary_value '[' aref_args tRBRACK tOP_ASGN arg {
support.checkExpression($6);
$$ = support.new_opElementAsgnNode(support.getPosition($1), $1, (String) $5.getValue(), $3, $6);
}
| primary_value tDOT tIDENTIFIER tOP_ASGN arg {
support.checkExpression($5);
$$ = new OpAsgnNode(support.getPosition($1), $1, $5, (String) $3.getValue(), (String) $4.getValue());
}
| primary_value tDOT tCONSTANT tOP_ASGN arg {
support.checkExpression($5);
$$ = new OpAsgnNode(support.getPosition($1), $1, $5, (String) $3.getValue(), (String) $4.getValue());
}
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg {
support.checkExpression($5);
$$ = new OpAsgnNode(support.getPosition($1), $1, $5, (String) $3.getValue(), (String) $4.getValue());
}
| primary_value tCOLON2 tCONSTANT tOP_ASGN arg {
support.yyerror("constant re-assignment");
}
| tCOLON3 tCONSTANT tOP_ASGN arg {
support.yyerror("constant re-assignment");
}
| backref tOP_ASGN arg {
support.backrefAssignError($1);
}
| arg tDOT2 arg {
support.checkExpression($1);
support.checkExpression($3);
boolean isLiteral = $1 instanceof FixnumNode && $3 instanceof FixnumNode;
$$ = new DotNode(support.getPosition($1), $1, $3, false, isLiteral);
}
| arg tDOT3 arg {
support.checkExpression($1);
support.checkExpression($3);
boolean isLiteral = $1 instanceof FixnumNode && $3 instanceof FixnumNode;
$$ = new DotNode(support.getPosition($1), $1, $3, true, isLiteral);
}
| arg tPLUS arg {
$$ = support.getOperatorCallNode($1, "+", $3, lexer.getPosition());
}
| arg tMINUS arg {
$$ = support.getOperatorCallNode($1, "-", $3, lexer.getPosition());
}
| arg tSTAR2 arg {
$$ = support.getOperatorCallNode($1, "*", $3, lexer.getPosition());
}
| arg tDIVIDE arg {
$$ = support.getOperatorCallNode($1, "/", $3, lexer.getPosition());
}
| arg tPERCENT arg {
$$ = support.getOperatorCallNode($1, "%", $3, lexer.getPosition());
}
| arg tPOW arg {
$$ = support.getOperatorCallNode($1, "**", $3, lexer.getPosition());
}
| tUMINUS_NUM tINTEGER tPOW arg {
$$ = support.getOperatorCallNode(support.getOperatorCallNode($2, "**", $4, lexer.getPosition()), "-@");
}
| tUMINUS_NUM tFLOAT tPOW arg {
$$ = support.getOperatorCallNode(support.getOperatorCallNode($2, "**", $4, lexer.getPosition()), "-@");
}
| tUPLUS arg {
if (support.isLiteral($2)) {
$$ = $2;
} else {
$$ = support.getOperatorCallNode($2, "+@");
}
}
| tUMINUS arg {
$$ = support.getOperatorCallNode($2, "-@");
}
| arg tPIPE arg {
$$ = support.getOperatorCallNode($1, "|", $3, lexer.getPosition());
}
| arg tCARET arg {
$$ = support.getOperatorCallNode($1, "^", $3, lexer.getPosition());
}
| arg tAMPER2 arg {
$$ = support.getOperatorCallNode($1, "&", $3, lexer.getPosition());
}
| arg tCMP arg {
$$ = support.getOperatorCallNode($1, "<=>", $3, lexer.getPosition());
}
| arg tGT arg {
$$ = support.getOperatorCallNode($1, ">", $3, lexer.getPosition());
}
| arg tGEQ arg {
$$ = support.getOperatorCallNode($1, ">=", $3, lexer.getPosition());
}
| arg tLT arg {
$$ = support.getOperatorCallNode($1, "<", $3, lexer.getPosition());
}
| arg tLEQ arg {
$$ = support.getOperatorCallNode($1, "<=", $3, lexer.getPosition());
}
| arg tEQ arg {
$$ = support.getOperatorCallNode($1, "==", $3, lexer.getPosition());
}
| arg tEQQ arg {
$$ = support.getOperatorCallNode($1, "===", $3, lexer.getPosition());
}
| arg tNEQ arg {
$$ = new NotNode(support.getPosition($1), support.getOperatorCallNode($1, "==", $3, lexer.getPosition()));
}
| arg tMATCH arg {
$$ = support.getMatchNode($1, $3);
}
| arg tNMATCH arg {
$$ = new NotNode(support.getPosition($1), support.getMatchNode($1, $3));
}
| tBANG arg {
$$ = new NotNode(support.getPosition($1), support.getConditionNode($2));
}
| tTILDE arg {
$$ = support.getOperatorCallNode($2, "~");
}
| arg tLSHFT arg {
$$ = support.getOperatorCallNode($1, "<<", $3, lexer.getPosition());
}
| arg tRSHFT arg {
$$ = support.getOperatorCallNode($1, ">>", $3, lexer.getPosition());
}
| arg tANDOP arg {
$$ = support.newAndNode($2.getPosition(), $1, $3);
}
| arg tOROP arg {
$$ = support.newOrNode($2.getPosition(), $1, $3);
}
| kDEFINED opt_nl arg {
$$ = new DefinedNode($1.getPosition(), $3);
}
| arg '?' arg ':' arg {
$$ = new IfNode(support.getPosition($1), support.getConditionNode($1), $3, $5);
}
| primary {
$$ = $1;
}
arg_value : arg {
support.checkExpression($1);
$$ = $1;
}
aref_args : none
| command opt_nl {
$$ = support.newArrayNode(support.getPosition($1), $1);
}
| args trailer {
$$ = $1;
}
| args ',' tSTAR arg_value opt_nl {
$$ = support.arg_concat(support.getPosition($1), $1, $4);
}
| assocs trailer {
ISourcePosition pos = $1.getPosition();
$$ = support.newArrayNode(pos, new HashNode(pos, $1));
}
| tSTAR arg_value opt_nl {
$$ = new NewlineNode($1.getPosition(), support.newSplatNode($1.getPosition(), $2));
}
paren_args : tLPAREN2 none tRPAREN {
$$ = new ArrayNode($1.getPosition());
}
| tLPAREN2 call_args opt_nl tRPAREN {
$$ = $2;
$<Node>$.setPosition($1.getPosition());
}
| tLPAREN2 block_call opt_nl tRPAREN {
$$ = support.newArrayNode($1.getPosition(), $2);
}
| tLPAREN2 args ',' block_call opt_nl tRPAREN {
$$ = $2.add($4);
}
opt_paren_args: none | paren_args
// Node:call_args - Arguments for a function call
call_args : command {
$$ = support.newArrayNode(support.getPosition($1), $1);
}
| args opt_block_arg {
$$ = support.arg_blk_pass($1, $2);
}
| args ',' tSTAR arg_value opt_block_arg {
$$ = support.arg_concat(support.getPosition($1), $1, $4);
$$ = support.arg_blk_pass($<Node>$, $5);
}
| assocs opt_block_arg {
ISourcePosition pos = $1.getPosition();
$$ = support.newArrayNode(pos, new HashNode(pos, $1));
$$ = support.arg_blk_pass((Node)$$, $2);
}
| assocs ',' tSTAR arg_value opt_block_arg {
ISourcePosition pos = $1.getPosition();
$$ = support.arg_concat(pos, support.newArrayNode(pos, new HashNode(pos, $1)), $4);
$$ = support.arg_blk_pass((Node)$$, $5);
}
| args ',' assocs opt_block_arg {
$$ = $1.add(new HashNode(lexer.getPosition(), $3));
$$ = support.arg_blk_pass((Node)$$, $4);
}
| args ',' assocs ',' tSTAR arg opt_block_arg {
support.checkExpression($6);
$$ = support.arg_concat(support.getPosition($1), $1.add(new HashNode(lexer.getPosition(), $3)), $6);
$$ = support.arg_blk_pass((Node)$$, $7);
}
| tSTAR arg_value opt_block_arg {
$$ = support.arg_blk_pass(support.newSplatNode($1.getPosition(), $2), $3);
}
| block_arg {
}
call_args2 : arg_value ',' args opt_block_arg {
$$ = support.arg_blk_pass(support.newArrayNode(support.getPosition($1), $1).addAll($3), $4);
}
| arg_value ',' block_arg {
$$ = support.arg_blk_pass(support.newArrayNode(support.getPosition($1), $1), $3);
}
| arg_value ',' tSTAR arg_value opt_block_arg {
$$ = support.arg_concat(support.getPosition($1), support.newArrayNode(support.getPosition($1), $1), $4);
$$ = support.arg_blk_pass((Node)$$, $5);
}
| arg_value ',' args ',' tSTAR arg_value opt_block_arg {
$$ = support.arg_concat(support.getPosition($1), support.newArrayNode(support.getPosition($1), $1).addAll(new HashNode(lexer.getPosition(), $3)), $6);
$$ = support.arg_blk_pass((Node)$$, $7);
}
| assocs opt_block_arg {
ISourcePosition pos = $1.getPosition();
$$ = support.newArrayNode(pos, new HashNode(pos, $1));
$$ = support.arg_blk_pass((Node)$$, $2);
}
| assocs ',' tSTAR arg_value opt_block_arg {
ISourcePosition pos = $1.getPosition();
$$ = support.arg_concat(pos, support.newArrayNode(pos, new HashNode(pos, $1)), $4);
$$ = support.arg_blk_pass((Node)$$, $5);
}
| arg_value ',' assocs opt_block_arg {
$$ = support.newArrayNode(support.getPosition($1), $1).add(new HashNode(lexer.getPosition(), $3));
$$ = support.arg_blk_pass((Node)$$, $4);
}
| arg_value ',' args ',' assocs opt_block_arg {
$$ = support.newArrayNode(support.getPosition($1), $1).addAll($3).add(new HashNode(lexer.getPosition(), $5));
$$ = support.arg_blk_pass((Node)$$, $6);
}
| arg_value ',' assocs ',' tSTAR arg_value opt_block_arg {
$$ = support.arg_concat(support.getPosition($1), support.newArrayNode(support.getPosition($1), $1).add(new HashNode(lexer.getPosition(), $3)), $6);
$$ = support.arg_blk_pass((Node)$$, $7);
}
| arg_value ',' args ',' assocs ',' tSTAR arg_value opt_block_arg {
$$ = support.arg_concat(support.getPosition($1), support.newArrayNode(support.getPosition($1), $1).addAll($3).add(new HashNode(lexer.getPosition(), $5)), $8);
$$ = support.arg_blk_pass((Node)$$, $9);
}
| tSTAR arg_value opt_block_arg {
$$ = support.arg_blk_pass(support.newSplatNode($1.getPosition(), $2), $3);
}