-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.y
5661 lines (5200 loc) · 105 KB
/
parse.y
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
/**********************************************************************
parse.y -
$Author$
$Date$
created at: Fri May 28 18:02:42 JST 1993
Copyright (C) 1993-2002 Yukihiro Matsumoto
**********************************************************************/
%{
#define YYDEBUG 1
#include "ruby.h"
#include "env.h"
#include "intern.h"
#include "node.h"
#include "st.h"
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#define yyparse ruby_yyparse
#define yylex ruby_yylex
#define yyerror ruby_yyerror
#define yylval ruby_yylval
#define yychar ruby_yychar
#define yydebug ruby_yydebug
#define ID_SCOPE_SHIFT 3
#define ID_SCOPE_MASK 0x07
#define ID_LOCAL 0x01
#define ID_INSTANCE 0x02
#define ID_GLOBAL 0x03
#define ID_ATTRSET 0x04
#define ID_CONST 0x05
#define ID_CLASS 0x06
#define ID_JUNK 0x07
#define ID_INTERNAL ID_JUNK
#define is_notop_id(id) ((id)>LAST_TOKEN)
#define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
#define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL)
#define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE)
#define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET)
#define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
#define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
NODE *ruby_eval_tree_begin = 0;
NODE *ruby_eval_tree = 0;
char *ruby_sourcefile; /* current source file */
int ruby_sourceline; /* current line no. */
static int yylex();
static int yyerror();
static enum lex_state {
EXPR_BEG, /* ignore newline, +/- is a sign. */
EXPR_END, /* newline significant, +/- is a operator. */
EXPR_ARG, /* newline significant, +/- is a operator. */
EXPR_CMDARG, /* newline significant, +/- is a operator. */
EXPR_ENDARG, /* newline significant, +/- is a operator. */
EXPR_MID, /* newline significant, +/- is a operator. */
EXPR_FNAME, /* ignore newline, no reserved words. */
EXPR_DOT, /* right after `.' or `::', no reserved words. */
EXPR_CLASS, /* immediate after `class', no here document. */
} lex_state;
static NODE *lex_strterm;
static int lex_strnest;
#ifdef HAVE_LONG_LONG
typedef unsigned LONG_LONG stack_type;
#else
typedef unsigned long stack_type;
#endif
static stack_type cond_stack = 0;
#define COND_PUSH(n) (cond_stack = (cond_stack<<1)|((n)&1))
#define COND_POP() (cond_stack >>= 1)
#define COND_LEXPOP() do {\
int last = COND_P();\
cond_stack >>= 1;\
if (last) cond_stack |= 1;\
} while (0)
#define COND_P() (cond_stack&1)
static stack_type cmdarg_stack = 0;
#define CMDARG_PUSH(n) (cmdarg_stack = (cmdarg_stack<<1)|((n)&1))
#define CMDARG_POP() (cmdarg_stack >>= 1)
#define CMDARG_LEXPOP() do {\
int last = CMDARG_P();\
cmdarg_stack >>= 1;\
if (last) cmdarg_stack |= 1;\
} while (0)
#define CMDARG_P() (cmdarg_stack&1)
static int class_nest = 0;
static int in_single = 0;
static int in_def = 0;
static int compile_for_eval = 0;
static ID cur_mid = 0;
static int quoted_term;
#define quoted_term_char ((unsigned char)quoted_term)
#define WHEN_QUOTED_TERM(x) ((quoted_term >= 0) && (x))
#define QUOTED_TERM_P(c) WHEN_QUOTED_TERM((c) == quoted_term_char)
static NODE *cond();
static NODE *logop();
static NODE *newline_node();
static void fixpos();
static int value_expr();
static void void_expr();
static void void_stmts();
static NODE *block_append();
static NODE *list_append();
static NODE *list_concat();
static NODE *arg_concat();
static NODE *arg_prepend();
static NODE *literal_concat();
static NODE *literal_append();
static NODE *call_op();
static int in_defined = 0;
static NODE *ret_args();
static NODE *arg_blk_pass();
static NODE *new_call();
static NODE *new_fcall();
static NODE *new_super();
static NODE *gettable();
static NODE *assignable();
static NODE *aryset();
static NODE *attrset();
static void rb_backref_error();
static NODE *node_assign();
static NODE *match_gen();
static void local_push();
static void local_pop();
static int local_append();
static int local_cnt();
static int local_id();
static ID *local_tbl();
static ID internal_id();
static struct RVarmap *dyna_push();
static void dyna_pop();
static int dyna_in_block();
static void top_local_init();
static void top_local_setup();
#define RE_OPTION_ONCE 0x80
#define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
#define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
#define nd_func u1.id
#define nd_term u2.id
#define nd_paren u3.id
%}
%union {
NODE *node;
VALUE val;
ID id;
int num;
struct RVarmap *vars;
}
%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__
%token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR
%token <val> tINTEGER tFLOAT tSTRING_CONTENT
%token <node> tNTH_REF tBACK_REF
%token <num> tREGEXP_END
%type <node> singleton strings string string1 xstring regexp
%type <node> string_contents xstring_contents string_content
%type <node> words qwords word_list qword_list word
%type <val> literal numeric
%type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call
%type <node> expr_value arg_value primary_value
%type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
%type <node> args when_args call_args call_args2 open_args paren_args opt_paren_args
%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
%type <node> mrhs mrhs_basic superclass block_call block_command
%type <node> f_arglist f_args f_optarg f_opt f_block_arg opt_f_block_arg
%type <node> assoc_list assocs assoc undef_list backref string_dvar
%type <node> block_var opt_block_var brace_block do_block lhs none
%type <node> mlhs mlhs_head mlhs_basic mlhs_entry mlhs_item mlhs_node
%type <id> fitem variable sym symbol operation operation2 operation3
%type <id> cname fname op f_rest_arg
%type <num> f_norm_arg f_arg term_push
%token tUPLUS /* unary+ */
%token tUMINUS /* unary- */
%token tPOW /* ** */
%token tCMP /* <=> */
%token tEQ /* == */
%token tEQQ /* === */
%token tNEQ /* != */
%token tGEQ /* >= */
%token tLEQ /* <= */
%token tANDOP tOROP /* && and || */
%token tMATCH tNMATCH /* =~ and !~ */
%token tDOT2 tDOT3 /* .. and ... */
%token tAREF tASET /* [] and []= */
%token tLSHFT tRSHFT /* << and >> */
%token tCOLON2 /* :: */
%token tCOLON3 /* :: at EXPR_BEG */
%token <id> tOP_ASGN /* +=, -= etc. */
%token tASSOC /* => */
%token tLPAREN /* ( */
%token tLPAREN_ARG /* ( */
%token tRPAREN /* ) */
%token tLBRACK /* [ */
%token tLBRACE /* { */
%token tLBRACE_ARG /* { */
%token tSTAR /* * */
%token tAMPER /* & */
%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
%token tSTRING_DBEG tSTRING_DVAR tSTRING_END
/*
* precedence table
*/
%left kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD kRESCUE_MOD
%left kOR kAND
%right kNOT
%nonassoc kDEFINED
%right '=' tOP_ASGN
%right '?' ':'
%nonassoc tDOT2 tDOT3
%left tOROP
%left tANDOP
%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
%left '>' tGEQ '<' tLEQ
%left '|' '^'
%left '&'
%left tLSHFT tRSHFT
%left '+' '-'
%left '*' '/' '%'
%right '!' '~' tUPLUS tUMINUS
%right tPOW
%token LAST_TOKEN
%%
program : {
lex_state = EXPR_BEG;
top_local_init();
if ((VALUE)ruby_class == rb_cObject) class_nest = 0;
else class_nest = 1;
}
compstmt
{
if ($2 && !compile_for_eval) {
/* last expression should not be void */
if (nd_type($2) != NODE_BLOCK) void_expr($2);
else {
NODE *node = $2;
while (node->nd_next) {
node = node->nd_next;
}
void_expr(node->nd_head);
}
}
ruby_eval_tree = block_append(ruby_eval_tree, $2);
top_local_setup();
class_nest = 0;
}
;
bodystmt : compstmt
opt_rescue
opt_else
opt_ensure
{
$$ = $1;
if ($2) {
$$ = NEW_RESCUE($1, $2, $3);
}
else if ($3) {
rb_warn("else without rescue is useless");
$$ = block_append($$, $3);
}
if ($4) {
$$ = NEW_ENSURE($$, $4);
}
fixpos($$, $1);
}
;
compstmt : stmts opt_terms
{
void_stmts($1);
$$ = $1;
}
;
stmts : none
| stmt
{
$$ = newline_node($1);
}
| stmts terms stmt
{
$$ = block_append($1, newline_node($3));
}
| error stmt
{
$$ = $2;
}
;
stmt : kALIAS fitem {lex_state = EXPR_FNAME;} fitem
{
$$ = NEW_ALIAS($2, $4);
}
| kALIAS tGVAR tGVAR
{
$$ = NEW_VALIAS($2, $3);
}
| kALIAS tGVAR tBACK_REF
{
char buf[3];
sprintf(buf, "$%c", $3->nd_nth);
$$ = NEW_VALIAS($2, rb_intern(buf));
}
| kALIAS tGVAR tNTH_REF
{
yyerror("can't make alias for the number variables");
$$ = 0;
}
| kUNDEF undef_list
{
$$ = $2;
}
| stmt kIF_MOD expr_value
{
$$ = NEW_IF(cond($3), $1, 0);
fixpos($$, $3);
}
| stmt kUNLESS_MOD expr_value
{
$$ = NEW_UNLESS(cond($3), $1, 0);
fixpos($$, $3);
}
| stmt kWHILE_MOD expr_value
{
if ($1 && nd_type($1) == NODE_BEGIN) {
$$ = NEW_WHILE(cond($3), $1->nd_body, 0);
}
else {
$$ = NEW_WHILE(cond($3), $1, 1);
}
}
| stmt kUNTIL_MOD expr_value
{
if ($1 && nd_type($1) == NODE_BEGIN) {
$$ = NEW_UNTIL(cond($3), $1->nd_body, 0);
}
else {
$$ = NEW_UNTIL(cond($3), $1, 1);
}
}
| stmt kRESCUE_MOD stmt
{
$$ = NEW_RESCUE($1, NEW_RESBODY(0,$3,0), 0);
}
| klBEGIN
{
if (in_def || in_single) {
yyerror("BEGIN in method");
}
local_push(0);
}
'{' compstmt '}'
{
ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
NEW_PREEXE($4));
local_pop();
$$ = 0;
}
| klEND '{' compstmt '}'
{
if (compile_for_eval && (in_def || in_single)) {
yyerror("END in method; use at_exit");
}
$$ = NEW_ITER(0, NEW_POSTEXE(), $3);
}
| lhs '=' command_call
{
$$ = node_assign($1, $3);
}
| mlhs '=' command_call
{
value_expr($3);
$1->nd_value = $3;
$$ = $1;
}
| var_lhs tOP_ASGN command_call
{
value_expr($3);
if ($1) {
ID vid = $1->nd_vid;
if ($2 == tOROP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_instance_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
$$->nd_value = call_op(gettable(vid),$2,1,$3);
}
}
else {
$$ = 0;
}
}
| primary_value '[' aref_args ']' tOP_ASGN command_call
{
NODE *args;
value_expr($6);
args = NEW_LIST($6);
$3 = list_append($3, NEW_NIL());
list_concat(args, $3);
if ($5 == tOROP) {
$5 = 0;
}
else if ($5 == tANDOP) {
$5 = 1;
}
$$ = NEW_OP_ASGN1($1, $5, args);
fixpos($$, $1);
}
| primary_value '.' tIDENTIFIER tOP_ASGN command_call
{
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
}
| primary_value '.' tCONSTANT tOP_ASGN command_call
{
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
}
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
{
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
}
| backref tOP_ASGN command_call
{
rb_backref_error($1);
$$ = 0;
}
| lhs '=' mrhs_basic
{
$$ = node_assign($1, NEW_REXPAND($3));
}
| mlhs '=' mrhs
{
$1->nd_value = $3;
$$ = $1;
}
| expr
;
expr : kRETURN call_args
{
if (!compile_for_eval && !in_def && !in_single)
yyerror("return appeared outside of method");
$$ = NEW_RETURN(ret_args($2));
}
| kBREAK call_args
{
$$ = NEW_BREAK(ret_args($2));
}
| kNEXT call_args
{
$$ = NEW_NEXT(ret_args($2));
}
| command_call
| expr kAND expr
{
$$ = logop(NODE_AND, $1, $3);
}
| expr kOR expr
{
$$ = logop(NODE_OR, $1, $3);
}
| kNOT expr
{
$$ = NEW_NOT(cond($2));
}
| '!' command_call
{
$$ = NEW_NOT(cond($2));
}
| arg
;
expr_value : expr
{
value_expr($$);
$$ = $1;
}
;
command_call : command
| block_command
;
block_command : block_call
| block_call '.' operation2 command_args
{
$$ = new_call($1, $3, $4);
}
| block_call tCOLON2 operation2 command_args
{
$$ = new_call($1, $3, $4);
}
;
command : operation command_args
{
$$ = new_fcall($1, $2);
fixpos($$, $2);
}
| primary_value '.' operation2 command_args
{
$$ = new_call($1, $3, $4);
fixpos($$, $1);
}
| primary_value tCOLON2 operation2 command_args
{
$$ = new_call($1, $3, $4);
fixpos($$, $1);
}
| kSUPER command_args
{
$$ = new_super($2);
fixpos($$, $2);
}
| kYIELD command_args
{
$$ = NEW_YIELD(ret_args($2));
fixpos($$, $2);
}
;
mlhs : mlhs_basic
| tLPAREN mlhs_entry ')'
{
$$ = $2;
}
;
mlhs_entry : mlhs_basic
| tLPAREN mlhs_entry ')'
{
$$ = NEW_MASGN(NEW_LIST($2), 0);
}
;
mlhs_basic : mlhs_head
{
$$ = NEW_MASGN($1, 0);
}
| mlhs_head mlhs_item
{
$$ = NEW_MASGN(list_append($1,$2), 0);
}
| mlhs_head tSTAR mlhs_node
{
$$ = NEW_MASGN($1, $3);
}
| mlhs_head tSTAR
{
$$ = NEW_MASGN($1, -1);
}
| tSTAR mlhs_node
{
$$ = NEW_MASGN(0, $2);
}
| tSTAR
{
$$ = NEW_MASGN(0, -1);
}
;
mlhs_item : mlhs_node
| tLPAREN mlhs_entry ')'
{
$$ = $2;
}
;
mlhs_head : mlhs_item ','
{
$$ = NEW_LIST($1);
}
| mlhs_head mlhs_item ','
{
$$ = list_append($1, $2);
}
;
mlhs_node : variable
{
$$ = assignable($1, 0);
}
| primary_value '[' aref_args ']'
{
$$ = aryset($1, $3);
}
| primary_value '.' tIDENTIFIER
{
$$ = attrset($1, $3);
}
| primary_value tCOLON2 tIDENTIFIER
{
$$ = attrset($1, $3);
}
| primary_value '.' tCONSTANT
{
$$ = attrset($1, $3);
}
| backref
{
rb_backref_error($1);
$$ = 0;
}
;
lhs : variable
{
$$ = assignable($1, 0);
}
| primary_value '[' aref_args ']'
{
$$ = aryset($1, $3);
}
| primary_value '.' tIDENTIFIER
{
$$ = attrset($1, $3);
}
| primary_value tCOLON2 tIDENTIFIER
{
$$ = attrset($1, $3);
}
| primary_value '.' tCONSTANT
{
$$ = attrset($1, $3);
}
| backref
{
rb_backref_error($1);
$$ = 0;
}
;
cname : tIDENTIFIER
{
yyerror("class/module name must be CONSTANT");
}
| tCONSTANT
;
fname : tIDENTIFIER
| tCONSTANT
| tFID
| op
{
lex_state = EXPR_END;
$$ = $1;
}
| reswords
{
lex_state = EXPR_END;
$$ = $<id>1;
}
;
fitem : fname
| symbol
;
undef_list : fitem
{
$$ = NEW_UNDEF($1);
}
| undef_list ',' {lex_state = EXPR_FNAME;} fitem
{
$$ = block_append($1, NEW_UNDEF($4));
}
;
op : '|' { $$ = '|'; }
| '^' { $$ = '^'; }
| '&' { $$ = '&'; }
| tCMP { $$ = tCMP; }
| tEQ { $$ = tEQ; }
| tEQQ { $$ = tEQQ; }
| tMATCH { $$ = tMATCH; }
| '>' { $$ = '>'; }
| tGEQ { $$ = tGEQ; }
| '<' { $$ = '<'; }
| tLEQ { $$ = tLEQ; }
| tLSHFT { $$ = tLSHFT; }
| tRSHFT { $$ = tRSHFT; }
| '+' { $$ = '+'; }
| '-' { $$ = '-'; }
| '*' { $$ = '*'; }
| tSTAR { $$ = '*'; }
| '/' { $$ = '/'; }
| '%' { $$ = '%'; }
| tPOW { $$ = tPOW; }
| '~' { $$ = '~'; }
| tUPLUS { $$ = tUPLUS; }
| tUMINUS { $$ = tUMINUS; }
| tAREF { $$ = tAREF; }
| tASET { $$ = tASET; }
| '`' { $$ = '`'; }
;
reswords : k__LINE__ | k__FILE__ | klBEGIN | klEND
| kALIAS | kAND | kBEGIN | kBREAK | kCASE | kCLASS | kDEF
| kDEFINED | kDO | kELSE | kELSIF | kEND | kENSURE | kFALSE
| kFOR | kIF_MOD | kIN | kMODULE | kNEXT | kNIL | kNOT
| kOR | kREDO | kRESCUE | kRETRY | kRETURN | kSELF | kSUPER
| kTHEN | kTRUE | kUNDEF | kUNLESS_MOD | kUNTIL_MOD | kWHEN
| kWHILE_MOD | kYIELD | kRESCUE_MOD
;
arg : lhs '=' arg
{
$$ = node_assign($1, $3);
}
| var_lhs tOP_ASGN arg
{
value_expr($3);
if ($1) {
ID vid = $1->nd_vid;
if ($2 == tOROP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_instance_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
$$->nd_value = call_op(gettable(vid),$2,1,$3);
}
}
else {
$$ = 0;
}
}
| primary_value '[' aref_args ']' tOP_ASGN arg
{
NODE *args;
value_expr($6);
args = NEW_LIST($6);
$3 = list_append($3, NEW_NIL());
list_concat(args, $3);
if ($5 == tOROP) {
$5 = 0;
}
else if ($5 == tANDOP) {
$5 = 1;
}
$$ = NEW_OP_ASGN1($1, $5, args);
fixpos($$, $1);
}
| primary_value '.' tIDENTIFIER tOP_ASGN arg
{
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
}
| primary_value '.' tCONSTANT tOP_ASGN arg
{
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
}
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
{
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
}
| backref tOP_ASGN arg
{
rb_backref_error($1);
$$ = 0;
}
| arg tDOT2 arg
{
value_expr($1);
value_expr($3);
$$ = NEW_DOT2($1, $3);
}
| arg tDOT3 arg
{
value_expr($1);
value_expr($3);
$$ = NEW_DOT3($1, $3);
}
| arg '+' arg
{
$$ = call_op($1, '+', 1, $3);
}
| arg '-' arg
{
$$ = call_op($1, '-', 1, $3);
}
| arg '*' arg
{
$$ = call_op($1, '*', 1, $3);
}
| arg '/' arg
{
$$ = call_op($1, '/', 1, $3);
}
| arg '%' arg
{
$$ = call_op($1, '%', 1, $3);
}
| arg tPOW arg
{
$$ = call_op($1, tPOW, 1, $3);
}
| tUPLUS arg
{
if ($2 && nd_type($2) == NODE_LIT) {
$$ = $2;
}
else {
$$ = call_op($2, tUPLUS, 0, 0);
}
}
| tUMINUS arg
{
if ($2 && nd_type($2) == NODE_LIT && FIXNUM_P($2->nd_lit)) {
long i = FIX2LONG($2->nd_lit);
$2->nd_lit = LONG2NUM(-i);
$$ = $2;
}
else {
$$ = call_op($2, tUMINUS, 0, 0);
}
}
| arg '|' arg
{
$$ = call_op($1, '|', 1, $3);
}
| arg '^' arg
{
$$ = call_op($1, '^', 1, $3);
}
| arg '&' arg
{
$$ = call_op($1, '&', 1, $3);
}
| arg tCMP arg
{
$$ = call_op($1, tCMP, 1, $3);
}
| arg '>' arg
{
$$ = call_op($1, '>', 1, $3);
}
| arg tGEQ arg
{
$$ = call_op($1, tGEQ, 1, $3);
}
| arg '<' arg
{
$$ = call_op($1, '<', 1, $3);
}
| arg tLEQ arg
{
$$ = call_op($1, tLEQ, 1, $3);
}
| arg tEQ arg
{
$$ = call_op($1, tEQ, 1, $3);
}
| arg tEQQ arg