parser 2.7.1.1 → 2.7.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb7c9797808fcd8920ef34ccce6dae7f416d0fe3084a863fa3fec7fa30b086fc
4
- data.tar.gz: 828a937688a35e277c7d435197ca2d023458de366a8e94fbaa67bdd6d3a04145
3
+ metadata.gz: e95a182ac672d1917e517628161ab896f34d4518df71430e0b349998817308e0
4
+ data.tar.gz: 24175a8fa5f50956127930f697ba4f09ae0561c359d9fea4ea19883c8c7c072a
5
5
  SHA512:
6
- metadata.gz: 679bc103f3d8b2663a1ec2de6c3acac41c90c1d738b40bf4b5ed945b8e3f51e22359caa0789e55009f3bd2c45ac832d00182db9ba9be1f43b18e6fe0e0508278
7
- data.tar.gz: b4f4d29766e23ca406644fb578e2134f28a9983d765d4aa45117dcb5a9ee012f27c2b8b65eccf11b7e3818b1543a62cc0483aa510a51352bc2260ad0fd650acd
6
+ metadata.gz: 95e17dd645e2240c9b3f6d7972a03d2c0df0305548920643c9d930ed3fa8b6aee8da77211b11d80a4d3f651aa75c1c2d43e51daa46aca163bd292ba91e9bcc0e
7
+ data.tar.gz: e9b989147352537fb1460a71585ace1165f5bca6d3baa01fde8f83a5b951c305bb37bab9ac93b019c70b728e6152ecd178f07935c335aab5e0f3a8ee4caf83a8
data/.gitignore CHANGED
@@ -29,5 +29,6 @@ lib/parser/ruby24.rb
29
29
  lib/parser/ruby25.rb
30
30
  lib/parser/ruby26.rb
31
31
  lib/parser/ruby27.rb
32
+ lib/parser/ruby28.rb
32
33
  lib/parser/macruby.rb
33
34
  lib/parser/rubymotion.rb
@@ -1,9 +1,19 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
- Not released (2020-04-15)
4
+ Not released (2020-04-30)
5
5
  -------------------------
6
6
 
7
+ Features implemented:
8
+ * ruby28.y: endless method definition (#676) (Vladimir Dementyev)
9
+ * ruby28.y: branch parser (#677) (Vladimir Dementyev)
10
+
11
+ Bugs fixed:
12
+ * ruby27.y: reject invalid lvar in pattern matching (#680) (Vladimir Dementyev)
13
+
14
+ v2.7.1.1 (2020-04-15)
15
+ ---------------------
16
+
7
17
  Features implemented:
8
18
  * Add Source::Range#eql? and hash (#675) (Marc-André Lafortune)
9
19
  * Source::TreeRewriter: Add #merge, #merge! and #empty? (#674) (Marc-André Lafortune)
data/Rakefile CHANGED
@@ -32,6 +32,7 @@ GENERATED_FILES = %w(lib/parser/lexer.rb
32
32
  lib/parser/ruby25.rb
33
33
  lib/parser/ruby26.rb
34
34
  lib/parser/ruby27.rb
35
+ lib/parser/ruby28.rb
35
36
  lib/parser/macruby.rb
36
37
  lib/parser/rubymotion.rb)
37
38
 
@@ -637,7 +637,7 @@ Format:
637
637
  (masgn (mlhs (ivasgn :@a) (cvasgn :@@b)) (array (splat (lvar :c))))
638
638
  "@a, @@b = *c"
639
639
 
640
- (masgn (mlhs (mlhs (lvasgn :a) (lvasgn :b)) (lvasgn :c)) (lvar :d))
640
+ (masgn (mlhs (lvasgn :a) (mlhs (lvasgn :b)) (lvasgn :c)) (lvar :d))
641
641
  "a, (b, c) = d"
642
642
 
643
643
  (masgn (mlhs (send (self) :a=) (send (self) :[]= (int 1))) (lvar :a))
@@ -802,6 +802,33 @@ Format:
802
802
  ~~~~~~~~~~~~~~~~~ expression
803
803
  ~~~
804
804
 
805
+ ### "Endless" method
806
+
807
+ Format:
808
+
809
+ ~~~
810
+ (def_e :foo (args) (int 42))
811
+ "def foo() = 42"
812
+ ~~~ keyword
813
+ ~~~ name
814
+ ^ assignment
815
+ ~~~~~~~~~~~~~~ expression
816
+ ~~~
817
+
818
+
819
+ ### "Endless" singleton method
820
+
821
+ Format:
822
+
823
+ ~~~
824
+ (defs_e (self) :foo (args) (int 42))
825
+ "def self.foo() = 42"
826
+ ~~~ keyword
827
+ ~~~ name
828
+ ^ assignment
829
+ ~~~~~~~~~~~~~~~~~~~ expression
830
+ ~~~
831
+
805
832
  ### Undefinition
806
833
 
807
834
  Format:
@@ -46,6 +46,7 @@ module Parser
46
46
  require 'parser/source/map/variable'
47
47
  require 'parser/source/map/keyword'
48
48
  require 'parser/source/map/definition'
49
+ require 'parser/source/map/endless_definition'
49
50
  require 'parser/source/map/send'
50
51
  require 'parser/source/map/index'
51
52
  require 'parser/source/map/condition'
@@ -10,3 +10,4 @@ require 'parser/ruby24'
10
10
  require 'parser/ruby25'
11
11
  require 'parser/ruby26'
12
12
  require 'parser/ruby27'
13
+ require 'parser/ruby28'
@@ -159,6 +159,8 @@ module Parser
159
159
  ])
160
160
  end
161
161
 
162
+ alias on_def_e on_def
163
+
162
164
  def on_defs(node)
163
165
  definee_node, name, args_node, body_node = *node
164
166
 
@@ -168,6 +170,8 @@ module Parser
168
170
  ])
169
171
  end
170
172
 
173
+ alias on_defs_e on_defs
174
+
171
175
  alias on_undef process_regular_node
172
176
  alias on_alias process_regular_node
173
177
 
@@ -652,19 +652,28 @@ module Parser
652
652
  definition_map(def_t, nil, name_t, end_t))
653
653
  end
654
654
 
655
+ def def_endless_method(def_t, name_t, args,
656
+ assignment_t, body)
657
+ n(:def_e, [ value(name_t).to_sym, args, body ],
658
+ endless_definition_map(def_t, nil, name_t, assignment_t, body))
659
+ end
660
+
655
661
  def def_singleton(def_t, definee, dot_t,
656
662
  name_t, args,
657
663
  body, end_t)
658
- case definee.type
659
- when :int, :str, :dstr, :sym, :dsym,
660
- :regexp, :array, :hash
664
+ return unless validate_definee(definee)
661
665
 
662
- diagnostic :error, :singleton_literal, nil, definee.loc.expression
666
+ n(:defs, [ definee, value(name_t).to_sym, args, body ],
667
+ definition_map(def_t, dot_t, name_t, end_t))
668
+ end
663
669
 
664
- else
665
- n(:defs, [ definee, value(name_t).to_sym, args, body ],
666
- definition_map(def_t, dot_t, name_t, end_t))
667
- end
670
+ def def_endless_singleton(def_t, definee, dot_t,
671
+ name_t, args,
672
+ assignment_t, body)
673
+ return unless validate_definee(definee)
674
+
675
+ n(:defs_e, [ definee, value(name_t).to_sym, args, body ],
676
+ endless_definition_map(def_t, dot_t, name_t, assignment_t, body))
668
677
  end
669
678
 
670
679
  def undef_method(undef_t, names)
@@ -1239,8 +1248,10 @@ module Parser
1239
1248
 
1240
1249
  def match_var(name_t)
1241
1250
  name = value(name_t).to_sym
1251
+ name_l = loc(name_t)
1242
1252
 
1243
- check_duplicate_pattern_variable(name, loc(name_t))
1253
+ check_lvar_name(name, name_l)
1254
+ check_duplicate_pattern_variable(name, name_l)
1244
1255
  @parser.static_env.declare(name)
1245
1256
 
1246
1257
  n(:match_var, [ name ],
@@ -1253,6 +1264,7 @@ module Parser
1253
1264
  expr_l = loc(name_t)
1254
1265
  name_l = expr_l.adjust(end_pos: -1)
1255
1266
 
1267
+ check_lvar_name(name, name_l)
1256
1268
  check_duplicate_pattern_variable(name, name_l)
1257
1269
  @parser.static_env.declare(name)
1258
1270
 
@@ -1293,6 +1305,9 @@ module Parser
1293
1305
  Source::Map::Variable.new(name_l, expr_l))
1294
1306
  when :begin
1295
1307
  match_hash_var_from_str(begin_t, string.children, end_t)
1308
+ else
1309
+ # we only can get here if there is an interpolation, e.g., ``in "#{ a }":`
1310
+ diagnostic :error, :pm_interp_in_var_name, nil, loc(begin_t).join(loc(end_t))
1296
1311
  end
1297
1312
  end
1298
1313
 
@@ -1737,6 +1752,14 @@ module Parser
1737
1752
  loc(end_t))
1738
1753
  end
1739
1754
 
1755
+ def endless_definition_map(keyword_t, operator_t, name_t, assignment_t, body_e)
1756
+ body_l = body_e.loc.expression
1757
+
1758
+ Source::Map::EndlessDefinition.new(loc(keyword_t),
1759
+ loc(operator_t), loc(name_t),
1760
+ loc(assignment_t), body_l)
1761
+ end
1762
+
1740
1763
  def send_map(receiver_e, dot_t, selector_t, begin_t=nil, args=[], end_t=nil)
1741
1764
  if receiver_e
1742
1765
  begin_l = receiver_e.loc.expression
@@ -1979,6 +2002,18 @@ module Parser
1979
2002
  @parser.send :yyerror
1980
2003
  end
1981
2004
  end
2005
+
2006
+ def validate_definee(definee)
2007
+ case definee.type
2008
+ when :int, :str, :dstr, :sym, :dsym,
2009
+ :regexp, :array, :hash
2010
+
2011
+ diagnostic :error, :singleton_literal, nil, definee.loc.expression
2012
+ false
2013
+ else
2014
+ true
2015
+ end
2016
+ end
1982
2017
  end
1983
2018
 
1984
2019
  end
@@ -83,6 +83,15 @@ module Parser
83
83
  require 'parser/ruby27'
84
84
  CurrentRuby = Ruby27
85
85
 
86
+ when /^2\.8\./
87
+ current_version = '2.8.0-dev'
88
+ if RUBY_VERSION != current_version
89
+ warn_syntax_deviation 'parser/ruby28', current_version
90
+ end
91
+
92
+ require 'parser/ruby28'
93
+ CurrentRuby = Ruby28
94
+
86
95
  else # :nocov:
87
96
  # Keep this in sync with released Ruby.
88
97
  warn_syntax_deviation 'parser/ruby27', '2.7.x'
@@ -16,7 +16,7 @@ module Parser
16
16
  op_asgn and_asgn ensure rescue arg_expr
17
17
  or_asgn back_ref nth_ref
18
18
  match_with_lvasgn match_current_line
19
- module class sclass def defs undef alias args
19
+ module class sclass def defs def_e defs_e undef alias args
20
20
  cbase arg optarg restarg blockarg block_pass kwarg kwoptarg
21
21
  kwrestarg kwnilarg send csend super zsuper yield block
22
22
  and not or if when case while until while_post
@@ -0,0 +1,2974 @@
1
+ class Parser::Ruby28
2
+
3
+ token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS
4
+ kTHEN kELSIF kELSE kCASE kWHEN kWHILE kUNTIL kFOR kBREAK kNEXT
5
+ kREDO kRETRY kIN kDO kDO_COND kDO_BLOCK kDO_LAMBDA kRETURN kYIELD kSUPER
6
+ kSELF kNIL kTRUE kFALSE kAND kOR kNOT kIF_MOD kUNLESS_MOD kWHILE_MOD
7
+ kUNTIL_MOD kRESCUE_MOD kALIAS kDEFINED klBEGIN klEND k__LINE__
8
+ k__FILE__ k__ENCODING__ tIDENTIFIER tFID tGVAR tIVAR tCONSTANT
9
+ tLABEL tCVAR tNTH_REF tBACK_REF tSTRING_CONTENT tINTEGER tFLOAT
10
+ tUPLUS tUMINUS tUNARY_NUM tPOW tCMP tEQ tEQQ tNEQ
11
+ tGEQ tLEQ tANDOP tOROP tMATCH tNMATCH tDOT tDOT2 tDOT3 tAREF
12
+ tASET tLSHFT tRSHFT tCOLON2 tCOLON3 tOP_ASGN tASSOC tLPAREN
13
+ tLPAREN2 tRPAREN tLPAREN_ARG tLBRACK tLBRACK2 tRBRACK tLBRACE
14
+ tLBRACE_ARG tSTAR tSTAR2 tAMPER tAMPER2 tTILDE tPERCENT tDIVIDE
15
+ tDSTAR tPLUS tMINUS tLT tGT tPIPE tBANG tCARET tLCURLY tRCURLY
16
+ tBACK_REF2 tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tREGEXP_OPT
17
+ tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG tSTRING_DBEG
18
+ tSTRING_DVAR tSTRING_END tSTRING_DEND tSTRING tSYMBOL
19
+ tNL tEH tCOLON tCOMMA tSPACE tSEMI tLAMBDA tLAMBEG tCHARACTER
20
+ tRATIONAL tIMAGINARY tLABEL_END tANDDOT tBDOT2 tBDOT3
21
+
22
+ prechigh
23
+ right tBANG tTILDE tUPLUS
24
+ right tPOW
25
+ right tUNARY_NUM tUMINUS
26
+ left tSTAR2 tDIVIDE tPERCENT
27
+ left tPLUS tMINUS
28
+ left tLSHFT tRSHFT
29
+ left tAMPER2
30
+ left tPIPE tCARET
31
+ left tGT tGEQ tLT tLEQ
32
+ nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
33
+ left tANDOP
34
+ left tOROP
35
+ nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
36
+ right tEH tCOLON
37
+ left kRESCUE_MOD
38
+ right tEQL tOP_ASGN
39
+ nonassoc kDEFINED
40
+ right kNOT
41
+ left kOR kAND
42
+ nonassoc kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD kIN
43
+ nonassoc tLBRACE_ARG
44
+ nonassoc tLOWEST
45
+ preclow
46
+
47
+ rule
48
+
49
+ program: top_compstmt
50
+
51
+ top_compstmt: top_stmts opt_terms
52
+ {
53
+ result = @builder.compstmt(val[0])
54
+ }
55
+
56
+ top_stmts: # nothing
57
+ {
58
+ result = []
59
+ }
60
+ | top_stmt
61
+ {
62
+ result = [ val[0] ]
63
+ }
64
+ | top_stmts terms top_stmt
65
+ {
66
+ result = val[0] << val[2]
67
+ }
68
+ | error top_stmt
69
+ {
70
+ result = [ val[1] ]
71
+ }
72
+
73
+ top_stmt: stmt
74
+ | klBEGIN begin_block
75
+ {
76
+ result = @builder.preexe(val[0], *val[1])
77
+ }
78
+
79
+ begin_block: tLCURLY top_compstmt tRCURLY
80
+ {
81
+ result = val
82
+ }
83
+
84
+ bodystmt: compstmt opt_rescue opt_else opt_ensure
85
+ {
86
+ rescue_bodies = val[1]
87
+ else_t, else_ = val[2]
88
+ ensure_t, ensure_ = val[3]
89
+
90
+ if rescue_bodies.empty? && !else_t.nil?
91
+ diagnostic :error, :useless_else, nil, else_t
92
+ end
93
+
94
+ result = @builder.begin_body(val[0],
95
+ rescue_bodies,
96
+ else_t, else_,
97
+ ensure_t, ensure_)
98
+ }
99
+
100
+ compstmt: stmts opt_terms
101
+ {
102
+ result = @builder.compstmt(val[0])
103
+ }
104
+
105
+ stmts: # nothing
106
+ {
107
+ result = []
108
+ }
109
+ | stmt_or_begin
110
+ {
111
+ result = [ val[0] ]
112
+ }
113
+ | stmts terms stmt_or_begin
114
+ {
115
+ result = val[0] << val[2]
116
+ }
117
+ | error stmt
118
+ {
119
+ result = [ val[1] ]
120
+ }
121
+
122
+ stmt_or_begin: stmt
123
+ | klBEGIN begin_block
124
+ {
125
+ diagnostic :error, :begin_in_method, nil, val[0]
126
+ }
127
+
128
+ stmt: kALIAS fitem
129
+ {
130
+ @lexer.state = :expr_fname
131
+ }
132
+ fitem
133
+ {
134
+ result = @builder.alias(val[0], val[1], val[3])
135
+ }
136
+ | kALIAS tGVAR tGVAR
137
+ {
138
+ result = @builder.alias(val[0],
139
+ @builder.gvar(val[1]),
140
+ @builder.gvar(val[2]))
141
+ }
142
+ | kALIAS tGVAR tBACK_REF
143
+ {
144
+ result = @builder.alias(val[0],
145
+ @builder.gvar(val[1]),
146
+ @builder.back_ref(val[2]))
147
+ }
148
+ | kALIAS tGVAR tNTH_REF
149
+ {
150
+ diagnostic :error, :nth_ref_alias, nil, val[2]
151
+ }
152
+ | kUNDEF undef_list
153
+ {
154
+ result = @builder.undef_method(val[0], val[1])
155
+ }
156
+ | stmt kIF_MOD expr_value
157
+ {
158
+ result = @builder.condition_mod(val[0], nil,
159
+ val[1], val[2])
160
+ }
161
+ | stmt kUNLESS_MOD expr_value
162
+ {
163
+ result = @builder.condition_mod(nil, val[0],
164
+ val[1], val[2])
165
+ }
166
+ | stmt kWHILE_MOD expr_value
167
+ {
168
+ result = @builder.loop_mod(:while, val[0], val[1], val[2])
169
+ }
170
+ | stmt kUNTIL_MOD expr_value
171
+ {
172
+ result = @builder.loop_mod(:until, val[0], val[1], val[2])
173
+ }
174
+ | stmt kRESCUE_MOD stmt
175
+ {
176
+ rescue_body = @builder.rescue_body(val[1],
177
+ nil, nil, nil,
178
+ nil, val[2])
179
+
180
+ result = @builder.begin_body(val[0], [ rescue_body ])
181
+ }
182
+ | klEND tLCURLY compstmt tRCURLY
183
+ {
184
+ result = @builder.postexe(val[0], val[1], val[2], val[3])
185
+ }
186
+ | command_asgn
187
+ | mlhs tEQL command_call
188
+ {
189
+ result = @builder.multi_assign(val[0], val[1], val[2])
190
+ }
191
+ | lhs tEQL mrhs
192
+ {
193
+ result = @builder.assign(val[0], val[1],
194
+ @builder.array(nil, val[2], nil))
195
+ }
196
+ | mlhs tEQL mrhs_arg kRESCUE_MOD stmt
197
+ {
198
+ rescue_body = @builder.rescue_body(val[3],
199
+ nil, nil, nil,
200
+ nil, val[4])
201
+ begin_body = @builder.begin_body(val[2], [ rescue_body ])
202
+
203
+ result = @builder.multi_assign(val[0], val[1], begin_body)
204
+ }
205
+ | mlhs tEQL mrhs_arg
206
+ {
207
+ result = @builder.multi_assign(val[0], val[1], val[2])
208
+ }
209
+ | expr
210
+
211
+ command_asgn: lhs tEQL command_rhs
212
+ {
213
+ result = @builder.assign(val[0], val[1], val[2])
214
+ }
215
+ | var_lhs tOP_ASGN command_rhs
216
+ {
217
+ result = @builder.op_assign(val[0], val[1], val[2])
218
+ }
219
+ | primary_value tLBRACK2 opt_call_args rbracket tOP_ASGN command_rhs
220
+ {
221
+ result = @builder.op_assign(
222
+ @builder.index(
223
+ val[0], val[1], val[2], val[3]),
224
+ val[4], val[5])
225
+ }
226
+ | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
227
+ {
228
+ result = @builder.op_assign(
229
+ @builder.call_method(
230
+ val[0], val[1], val[2]),
231
+ val[3], val[4])
232
+ }
233
+ | primary_value call_op tCONSTANT tOP_ASGN command_rhs
234
+ {
235
+ result = @builder.op_assign(
236
+ @builder.call_method(
237
+ val[0], val[1], val[2]),
238
+ val[3], val[4])
239
+ }
240
+ | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
241
+ {
242
+ const = @builder.const_op_assignable(
243
+ @builder.const_fetch(val[0], val[1], val[2]))
244
+ result = @builder.op_assign(const, val[3], val[4])
245
+ }
246
+ | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
247
+ {
248
+ result = @builder.op_assign(
249
+ @builder.call_method(
250
+ val[0], val[1], val[2]),
251
+ val[3], val[4])
252
+ }
253
+ | backref tOP_ASGN command_rhs
254
+ {
255
+ @builder.op_assign(val[0], val[1], val[2])
256
+ }
257
+
258
+ command_rhs: command_call =tOP_ASGN
259
+ | command_call kRESCUE_MOD stmt
260
+ {
261
+ rescue_body = @builder.rescue_body(val[1],
262
+ nil, nil, nil,
263
+ nil, val[2])
264
+
265
+ result = @builder.begin_body(val[0], [ rescue_body ])
266
+ }
267
+ | command_asgn
268
+
269
+ expr: command_call
270
+ | expr kAND expr
271
+ {
272
+ result = @builder.logical_op(:and, val[0], val[1], val[2])
273
+ }
274
+ | expr kOR expr
275
+ {
276
+ result = @builder.logical_op(:or, val[0], val[1], val[2])
277
+ }
278
+ | kNOT opt_nl expr
279
+ {
280
+ result = @builder.not_op(val[0], nil, val[2], nil)
281
+ }
282
+ | tBANG command_call
283
+ {
284
+ result = @builder.not_op(val[0], nil, val[1], nil)
285
+ }
286
+ | arg kIN
287
+ {
288
+ @lexer.state = :expr_beg
289
+ @lexer.command_start = false
290
+ pattern_variables.push
291
+
292
+ result = @lexer.in_kwarg
293
+ @lexer.in_kwarg = true
294
+ }
295
+ p_expr
296
+ {
297
+ @lexer.in_kwarg = val[2]
298
+ result = @builder.in_match(val[0], val[1], val[3])
299
+ }
300
+ | arg =tLBRACE_ARG
301
+
302
+ expr_value: expr
303
+
304
+ expr_value_do: {
305
+ @lexer.cond.push(true)
306
+ }
307
+ expr_value do
308
+ {
309
+ @lexer.cond.pop
310
+ result = [ val[1], val[2] ]
311
+ }
312
+
313
+ def_name: fname
314
+ {
315
+ @static_env.extend_static
316
+ @lexer.cmdarg.push(false)
317
+ @lexer.cond.push(false)
318
+ @current_arg_stack.push(nil)
319
+
320
+ result = val[0]
321
+ }
322
+
323
+ defn_head: kDEF def_name
324
+ {
325
+ @context.push(:def)
326
+
327
+ result = [ val[0], val[1] ]
328
+ }
329
+
330
+ defs_head: kDEF singleton dot_or_colon
331
+ {
332
+ @lexer.state = :expr_fname
333
+ }
334
+ def_name
335
+ {
336
+ @context.push(:defs)
337
+
338
+ result = [ val[0], val[1], val[2], val[4] ]
339
+ }
340
+
341
+
342
+ command_call: command
343
+ | block_command
344
+
345
+ block_command: block_call
346
+ | block_call dot_or_colon operation2 command_args
347
+ {
348
+ result = @builder.call_method(val[0], val[1], val[2],
349
+ nil, val[3], nil)
350
+ }
351
+
352
+ cmd_brace_block: tLBRACE_ARG
353
+ {
354
+ @context.push(:block)
355
+ }
356
+ brace_body tRCURLY
357
+ {
358
+ result = [ val[0], *val[2], val[3] ]
359
+ @context.pop
360
+ }
361
+
362
+ fcall: operation
363
+
364
+ command: fcall command_args =tLOWEST
365
+ {
366
+ result = @builder.call_method(nil, nil, val[0],
367
+ nil, val[1], nil)
368
+ }
369
+ | fcall command_args cmd_brace_block
370
+ {
371
+ method_call = @builder.call_method(nil, nil, val[0],
372
+ nil, val[1], nil)
373
+
374
+ begin_t, args, body, end_t = val[2]
375
+ result = @builder.block(method_call,
376
+ begin_t, args, body, end_t)
377
+ }
378
+ | primary_value call_op operation2 command_args =tLOWEST
379
+ {
380
+ result = @builder.call_method(val[0], val[1], val[2],
381
+ nil, val[3], nil)
382
+ }
383
+ | primary_value call_op operation2 command_args cmd_brace_block
384
+ {
385
+ method_call = @builder.call_method(val[0], val[1], val[2],
386
+ nil, val[3], nil)
387
+
388
+ begin_t, args, body, end_t = val[4]
389
+ result = @builder.block(method_call,
390
+ begin_t, args, body, end_t)
391
+ }
392
+ | primary_value tCOLON2 operation2 command_args =tLOWEST
393
+ {
394
+ result = @builder.call_method(val[0], val[1], val[2],
395
+ nil, val[3], nil)
396
+ }
397
+ | primary_value tCOLON2 operation2 command_args cmd_brace_block
398
+ {
399
+ method_call = @builder.call_method(val[0], val[1], val[2],
400
+ nil, val[3], nil)
401
+
402
+ begin_t, args, body, end_t = val[4]
403
+ result = @builder.block(method_call,
404
+ begin_t, args, body, end_t)
405
+ }
406
+ | kSUPER command_args
407
+ {
408
+ result = @builder.keyword_cmd(:super, val[0],
409
+ nil, val[1], nil)
410
+ }
411
+ | kYIELD command_args
412
+ {
413
+ result = @builder.keyword_cmd(:yield, val[0],
414
+ nil, val[1], nil)
415
+ }
416
+ | k_return call_args
417
+ {
418
+ result = @builder.keyword_cmd(:return, val[0],
419
+ nil, val[1], nil)
420
+ }
421
+ | kBREAK call_args
422
+ {
423
+ result = @builder.keyword_cmd(:break, val[0],
424
+ nil, val[1], nil)
425
+ }
426
+ | kNEXT call_args
427
+ {
428
+ result = @builder.keyword_cmd(:next, val[0],
429
+ nil, val[1], nil)
430
+ }
431
+
432
+ mlhs: mlhs_basic
433
+ {
434
+ result = @builder.multi_lhs(nil, val[0], nil)
435
+ }
436
+ | tLPAREN mlhs_inner rparen
437
+ {
438
+ result = @builder.begin(val[0], val[1], val[2])
439
+ }
440
+
441
+ mlhs_inner: mlhs_basic
442
+ {
443
+ result = @builder.multi_lhs(nil, val[0], nil)
444
+ }
445
+ | tLPAREN mlhs_inner rparen
446
+ {
447
+ result = @builder.multi_lhs(val[0], val[1], val[2])
448
+ }
449
+
450
+ mlhs_basic: mlhs_head
451
+ | mlhs_head mlhs_item
452
+ {
453
+ result = val[0].
454
+ push(val[1])
455
+ }
456
+ | mlhs_head tSTAR mlhs_node
457
+ {
458
+ result = val[0].
459
+ push(@builder.splat(val[1], val[2]))
460
+ }
461
+ | mlhs_head tSTAR mlhs_node tCOMMA mlhs_post
462
+ {
463
+ result = val[0].
464
+ push(@builder.splat(val[1], val[2])).
465
+ concat(val[4])
466
+ }
467
+ | mlhs_head tSTAR
468
+ {
469
+ result = val[0].
470
+ push(@builder.splat(val[1]))
471
+ }
472
+ | mlhs_head tSTAR tCOMMA mlhs_post
473
+ {
474
+ result = val[0].
475
+ push(@builder.splat(val[1])).
476
+ concat(val[3])
477
+ }
478
+ | tSTAR mlhs_node
479
+ {
480
+ result = [ @builder.splat(val[0], val[1]) ]
481
+ }
482
+ | tSTAR mlhs_node tCOMMA mlhs_post
483
+ {
484
+ result = [ @builder.splat(val[0], val[1]),
485
+ *val[3] ]
486
+ }
487
+ | tSTAR
488
+ {
489
+ result = [ @builder.splat(val[0]) ]
490
+ }
491
+ | tSTAR tCOMMA mlhs_post
492
+ {
493
+ result = [ @builder.splat(val[0]),
494
+ *val[2] ]
495
+ }
496
+
497
+ mlhs_item: mlhs_node
498
+ | tLPAREN mlhs_inner rparen
499
+ {
500
+ result = @builder.begin(val[0], val[1], val[2])
501
+ }
502
+
503
+ mlhs_head: mlhs_item tCOMMA
504
+ {
505
+ result = [ val[0] ]
506
+ }
507
+ | mlhs_head mlhs_item tCOMMA
508
+ {
509
+ result = val[0] << val[1]
510
+ }
511
+
512
+ mlhs_post: mlhs_item
513
+ {
514
+ result = [ val[0] ]
515
+ }
516
+ | mlhs_post tCOMMA mlhs_item
517
+ {
518
+ result = val[0] << val[2]
519
+ }
520
+
521
+ mlhs_node: user_variable
522
+ {
523
+ result = @builder.assignable(val[0])
524
+ }
525
+ | keyword_variable
526
+ {
527
+ result = @builder.assignable(val[0])
528
+ }
529
+ | primary_value tLBRACK2 opt_call_args rbracket
530
+ {
531
+ result = @builder.index_asgn(val[0], val[1], val[2], val[3])
532
+ }
533
+ | primary_value call_op tIDENTIFIER
534
+ {
535
+ if (val[1][0] == :anddot)
536
+ diagnostic :error, :csend_in_lhs_of_masgn, nil, val[1]
537
+ end
538
+
539
+ result = @builder.attr_asgn(val[0], val[1], val[2])
540
+ }
541
+ | primary_value tCOLON2 tIDENTIFIER
542
+ {
543
+ result = @builder.attr_asgn(val[0], val[1], val[2])
544
+ }
545
+ | primary_value call_op tCONSTANT
546
+ {
547
+ if (val[1][0] == :anddot)
548
+ diagnostic :error, :csend_in_lhs_of_masgn, nil, val[1]
549
+ end
550
+
551
+ result = @builder.attr_asgn(val[0], val[1], val[2])
552
+ }
553
+ | primary_value tCOLON2 tCONSTANT
554
+ {
555
+ result = @builder.assignable(
556
+ @builder.const_fetch(val[0], val[1], val[2]))
557
+ }
558
+ | tCOLON3 tCONSTANT
559
+ {
560
+ result = @builder.assignable(
561
+ @builder.const_global(val[0], val[1]))
562
+ }
563
+ | backref
564
+ {
565
+ result = @builder.assignable(val[0])
566
+ }
567
+
568
+ lhs: user_variable
569
+ {
570
+ result = @builder.assignable(val[0])
571
+ }
572
+ | keyword_variable
573
+ {
574
+ result = @builder.assignable(val[0])
575
+ }
576
+ | primary_value tLBRACK2 opt_call_args rbracket
577
+ {
578
+ result = @builder.index_asgn(val[0], val[1], val[2], val[3])
579
+ }
580
+ | primary_value call_op tIDENTIFIER
581
+ {
582
+ result = @builder.attr_asgn(val[0], val[1], val[2])
583
+ }
584
+ | primary_value tCOLON2 tIDENTIFIER
585
+ {
586
+ result = @builder.attr_asgn(val[0], val[1], val[2])
587
+ }
588
+ | primary_value call_op tCONSTANT
589
+ {
590
+ result = @builder.attr_asgn(val[0], val[1], val[2])
591
+ }
592
+ | primary_value tCOLON2 tCONSTANT
593
+ {
594
+ result = @builder.assignable(
595
+ @builder.const_fetch(val[0], val[1], val[2]))
596
+ }
597
+ | tCOLON3 tCONSTANT
598
+ {
599
+ result = @builder.assignable(
600
+ @builder.const_global(val[0], val[1]))
601
+ }
602
+ | backref
603
+ {
604
+ result = @builder.assignable(val[0])
605
+ }
606
+
607
+ cname: tIDENTIFIER
608
+ {
609
+ diagnostic :error, :module_name_const, nil, val[0]
610
+ }
611
+ | tCONSTANT
612
+
613
+ cpath: tCOLON3 cname
614
+ {
615
+ result = @builder.const_global(val[0], val[1])
616
+ }
617
+ | cname
618
+ {
619
+ result = @builder.const(val[0])
620
+ }
621
+ | primary_value tCOLON2 cname
622
+ {
623
+ result = @builder.const_fetch(val[0], val[1], val[2])
624
+ }
625
+
626
+ fname: tIDENTIFIER | tCONSTANT | tFID
627
+ | op
628
+ | reswords
629
+
630
+ fitem: fname
631
+ {
632
+ result = @builder.symbol(val[0])
633
+ }
634
+ | symbol
635
+
636
+ undef_list: fitem
637
+ {
638
+ result = [ val[0] ]
639
+ }
640
+ | undef_list tCOMMA
641
+ {
642
+ @lexer.state = :expr_fname
643
+ }
644
+ fitem
645
+ {
646
+ result = val[0] << val[3]
647
+ }
648
+
649
+ op: tPIPE | tCARET | tAMPER2 | tCMP | tEQ | tEQQ
650
+ | tMATCH | tNMATCH | tGT | tGEQ | tLT | tLEQ
651
+ | tNEQ | tLSHFT | tRSHFT | tPLUS | tMINUS | tSTAR2
652
+ | tSTAR | tDIVIDE | tPERCENT | tPOW | tBANG | tTILDE
653
+ | tUPLUS | tUMINUS | tAREF | tASET | tDSTAR | tBACK_REF2
654
+
655
+ reswords: k__LINE__ | k__FILE__ | k__ENCODING__ | klBEGIN | klEND
656
+ | kALIAS | kAND | kBEGIN | kBREAK | kCASE
657
+ | kCLASS | kDEF | kDEFINED | kDO | kELSE
658
+ | kELSIF | kEND | kENSURE | kFALSE | kFOR
659
+ | kIN | kMODULE | kNEXT | kNIL | kNOT
660
+ | kOR | kREDO | kRESCUE | kRETRY | kRETURN
661
+ | kSELF | kSUPER | kTHEN | kTRUE | kUNDEF
662
+ | kWHEN | kYIELD | kIF | kUNLESS | kWHILE
663
+ | kUNTIL
664
+
665
+ arg: lhs tEQL arg_rhs
666
+ {
667
+ result = @builder.assign(val[0], val[1], val[2])
668
+ }
669
+ | var_lhs tOP_ASGN arg_rhs
670
+ {
671
+ result = @builder.op_assign(val[0], val[1], val[2])
672
+ }
673
+ | primary_value tLBRACK2 opt_call_args rbracket tOP_ASGN arg_rhs
674
+ {
675
+ result = @builder.op_assign(
676
+ @builder.index(
677
+ val[0], val[1], val[2], val[3]),
678
+ val[4], val[5])
679
+ }
680
+ | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
681
+ {
682
+ result = @builder.op_assign(
683
+ @builder.call_method(
684
+ val[0], val[1], val[2]),
685
+ val[3], val[4])
686
+ }
687
+ | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
688
+ {
689
+ result = @builder.op_assign(
690
+ @builder.call_method(
691
+ val[0], val[1], val[2]),
692
+ val[3], val[4])
693
+ }
694
+ | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
695
+ {
696
+ result = @builder.op_assign(
697
+ @builder.call_method(
698
+ val[0], val[1], val[2]),
699
+ val[3], val[4])
700
+ }
701
+ | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
702
+ {
703
+ const = @builder.const_op_assignable(
704
+ @builder.const_fetch(val[0], val[1], val[2]))
705
+ result = @builder.op_assign(const, val[3], val[4])
706
+ }
707
+ | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
708
+ {
709
+ const = @builder.const_op_assignable(
710
+ @builder.const_global(val[0], val[1]))
711
+ result = @builder.op_assign(const, val[2], val[3])
712
+ }
713
+ | backref tOP_ASGN arg_rhs
714
+ {
715
+ result = @builder.op_assign(val[0], val[1], val[2])
716
+ }
717
+ | arg tDOT2 arg
718
+ {
719
+ result = @builder.range_inclusive(val[0], val[1], val[2])
720
+ }
721
+ | arg tDOT3 arg
722
+ {
723
+ result = @builder.range_exclusive(val[0], val[1], val[2])
724
+ }
725
+ | arg tDOT2
726
+ {
727
+ result = @builder.range_inclusive(val[0], val[1], nil)
728
+ }
729
+ | arg tDOT3
730
+ {
731
+ result = @builder.range_exclusive(val[0], val[1], nil)
732
+ }
733
+ | tBDOT2 arg
734
+ {
735
+ result = @builder.range_inclusive(nil, val[0], val[1])
736
+ }
737
+ | tBDOT3 arg
738
+ {
739
+ result = @builder.range_exclusive(nil, val[0], val[1])
740
+ }
741
+ | arg tPLUS arg
742
+ {
743
+ result = @builder.binary_op(val[0], val[1], val[2])
744
+ }
745
+ | arg tMINUS arg
746
+ {
747
+ result = @builder.binary_op(val[0], val[1], val[2])
748
+ }
749
+ | arg tSTAR2 arg
750
+ {
751
+ result = @builder.binary_op(val[0], val[1], val[2])
752
+ }
753
+ | arg tDIVIDE arg
754
+ {
755
+ result = @builder.binary_op(val[0], val[1], val[2])
756
+ }
757
+ | arg tPERCENT arg
758
+ {
759
+ result = @builder.binary_op(val[0], val[1], val[2])
760
+ }
761
+ | arg tPOW arg
762
+ {
763
+ result = @builder.binary_op(val[0], val[1], val[2])
764
+ }
765
+ | tUNARY_NUM simple_numeric tPOW arg
766
+ {
767
+ result = @builder.unary_op(val[0],
768
+ @builder.binary_op(
769
+ val[1], val[2], val[3]))
770
+ }
771
+ | tUPLUS arg
772
+ {
773
+ result = @builder.unary_op(val[0], val[1])
774
+ }
775
+ | tUMINUS arg
776
+ {
777
+ result = @builder.unary_op(val[0], val[1])
778
+ }
779
+ | arg tPIPE arg
780
+ {
781
+ result = @builder.binary_op(val[0], val[1], val[2])
782
+ }
783
+ | arg tCARET arg
784
+ {
785
+ result = @builder.binary_op(val[0], val[1], val[2])
786
+ }
787
+ | arg tAMPER2 arg
788
+ {
789
+ result = @builder.binary_op(val[0], val[1], val[2])
790
+ }
791
+ | arg tCMP arg
792
+ {
793
+ result = @builder.binary_op(val[0], val[1], val[2])
794
+ }
795
+ | rel_expr =tCMP
796
+ | arg tEQ arg
797
+ {
798
+ result = @builder.binary_op(val[0], val[1], val[2])
799
+ }
800
+ | arg tEQQ arg
801
+ {
802
+ result = @builder.binary_op(val[0], val[1], val[2])
803
+ }
804
+ | arg tNEQ arg
805
+ {
806
+ result = @builder.binary_op(val[0], val[1], val[2])
807
+ }
808
+ | arg tMATCH arg
809
+ {
810
+ result = @builder.match_op(val[0], val[1], val[2])
811
+ }
812
+ | arg tNMATCH arg
813
+ {
814
+ result = @builder.binary_op(val[0], val[1], val[2])
815
+ }
816
+ | tBANG arg
817
+ {
818
+ result = @builder.not_op(val[0], nil, val[1], nil)
819
+ }
820
+ | tTILDE arg
821
+ {
822
+ result = @builder.unary_op(val[0], val[1])
823
+ }
824
+ | arg tLSHFT arg
825
+ {
826
+ result = @builder.binary_op(val[0], val[1], val[2])
827
+ }
828
+ | arg tRSHFT arg
829
+ {
830
+ result = @builder.binary_op(val[0], val[1], val[2])
831
+ }
832
+ | arg tANDOP arg
833
+ {
834
+ result = @builder.logical_op(:and, val[0], val[1], val[2])
835
+ }
836
+ | arg tOROP arg
837
+ {
838
+ result = @builder.logical_op(:or, val[0], val[1], val[2])
839
+ }
840
+ | kDEFINED opt_nl arg
841
+ {
842
+ result = @builder.keyword_cmd(:defined?, val[0], nil, [ val[2] ], nil)
843
+ }
844
+ | arg tEH arg opt_nl tCOLON arg
845
+ {
846
+ result = @builder.ternary(val[0], val[1],
847
+ val[2], val[4], val[5])
848
+ }
849
+ | defn_head f_paren_args tEQL arg
850
+ {
851
+ result = @builder.def_endless_method(*val[0],
852
+ val[1], val[2], val[3])
853
+
854
+ @lexer.cmdarg.pop
855
+ @lexer.cond.pop
856
+ @static_env.unextend
857
+ @context.pop
858
+ @current_arg_stack.pop
859
+ }
860
+ | defs_head f_paren_args tEQL arg
861
+ {
862
+ result = @builder.def_endless_singleton(*val[0],
863
+ val[1], val[2], val[3])
864
+
865
+ @lexer.cmdarg.pop
866
+ @lexer.cond.pop
867
+ @static_env.unextend
868
+ @context.pop
869
+ @current_arg_stack.pop
870
+ }
871
+ | primary
872
+
873
+ relop: tGT | tLT | tGEQ | tLEQ
874
+
875
+ rel_expr: arg relop arg =tGT
876
+ {
877
+ result = @builder.binary_op(val[0], val[1], val[2])
878
+ }
879
+ | rel_expr relop arg =tGT
880
+ {
881
+ result = @builder.binary_op(val[0], val[1], val[2])
882
+ }
883
+
884
+ arg_value: arg
885
+
886
+ aref_args: none
887
+ | args trailer
888
+ | args tCOMMA assocs trailer
889
+ {
890
+ result = val[0] << @builder.associate(nil, val[2], nil)
891
+ }
892
+ | assocs trailer
893
+ {
894
+ result = [ @builder.associate(nil, val[0], nil) ]
895
+ }
896
+
897
+ arg_rhs: arg =tOP_ASGN
898
+ | arg kRESCUE_MOD arg
899
+ {
900
+ rescue_body = @builder.rescue_body(val[1],
901
+ nil, nil, nil,
902
+ nil, val[2])
903
+
904
+ result = @builder.begin_body(val[0], [ rescue_body ])
905
+ }
906
+
907
+ paren_args: tLPAREN2 opt_call_args rparen
908
+ {
909
+ result = val
910
+ }
911
+ | tLPAREN2 args_forward rparen
912
+ {
913
+ unless @static_env.declared_forward_args?
914
+ diagnostic :error, :unexpected_token, { :token => 'tBDOT3' } , val[1]
915
+ end
916
+
917
+ result = [val[0], [@builder.forwarded_args(val[1])], val[2]]
918
+ }
919
+
920
+ opt_paren_args: # nothing
921
+ {
922
+ result = [ nil, [], nil ]
923
+ }
924
+ | paren_args
925
+
926
+ opt_call_args: # nothing
927
+ {
928
+ result = []
929
+ }
930
+ | call_args
931
+ | args tCOMMA
932
+ | args tCOMMA assocs tCOMMA
933
+ {
934
+ result = val[0] << @builder.associate(nil, val[2], nil)
935
+ }
936
+ | assocs tCOMMA
937
+ {
938
+ result = [ @builder.associate(nil, val[0], nil) ]
939
+ }
940
+
941
+ call_args: command
942
+ {
943
+ result = [ val[0] ]
944
+ }
945
+ | args opt_block_arg
946
+ {
947
+ result = val[0].concat(val[1])
948
+ }
949
+ | assocs opt_block_arg
950
+ {
951
+ result = [ @builder.associate(nil, val[0], nil) ]
952
+ result.concat(val[1])
953
+ }
954
+ | args tCOMMA assocs opt_block_arg
955
+ {
956
+ assocs = @builder.associate(nil, val[2], nil)
957
+ result = val[0] << assocs
958
+ result.concat(val[3])
959
+ }
960
+ | block_arg
961
+ {
962
+ result = [ val[0] ]
963
+ }
964
+
965
+ command_args: {
966
+ # When branch gets invoked by RACC's lookahead
967
+ # and command args start with '[' or '('
968
+ # we need to put `true` to the cmdarg stack
969
+ # **before** `false` pushed by lexer
970
+ # m [], n
971
+ # ^
972
+ # Right here we have cmdarg [...0] because
973
+ # lexer pushed it on '['
974
+ # We need to modify cmdarg stack to [...10]
975
+ #
976
+ # For all other cases (like `m n` or `m n, []`) we simply put 1 to the stack
977
+ # and later lexer pushes corresponding bits on top of it.
978
+ last_token = @last_token[0]
979
+ lookahead = last_token == :tLBRACK || last_token == :tLPAREN_ARG
980
+
981
+ if lookahead
982
+ top = @lexer.cmdarg.pop
983
+ @lexer.cmdarg.push(true)
984
+ @lexer.cmdarg.push(top)
985
+ else
986
+ @lexer.cmdarg.push(true)
987
+ end
988
+ }
989
+ call_args
990
+ {
991
+ # call_args can be followed by tLBRACE_ARG (that does cmdarg.push(0) in the lexer)
992
+ # but the push must be done after cmdarg.pop() in the parser.
993
+ # So this code does cmdarg.pop() to pop 0 pushed by tLBRACE_ARG,
994
+ # cmdarg.pop() to pop 1 pushed by command_args,
995
+ # and cmdarg.push(0) to restore back the flag set by tLBRACE_ARG.
996
+ last_token = @last_token[0]
997
+ lookahead = last_token == :tLBRACE_ARG
998
+ if lookahead
999
+ top = @lexer.cmdarg.pop
1000
+ @lexer.cmdarg.pop
1001
+ @lexer.cmdarg.push(top)
1002
+ else
1003
+ @lexer.cmdarg.pop
1004
+ end
1005
+
1006
+ result = val[1]
1007
+ }
1008
+
1009
+ block_arg: tAMPER arg_value
1010
+ {
1011
+ result = @builder.block_pass(val[0], val[1])
1012
+ }
1013
+
1014
+ opt_block_arg: tCOMMA block_arg
1015
+ {
1016
+ result = [ val[1] ]
1017
+ }
1018
+ | # nothing
1019
+ {
1020
+ result = []
1021
+ }
1022
+
1023
+ args: arg_value
1024
+ {
1025
+ result = [ val[0] ]
1026
+ }
1027
+ | tSTAR arg_value
1028
+ {
1029
+ result = [ @builder.splat(val[0], val[1]) ]
1030
+ }
1031
+ | args tCOMMA arg_value
1032
+ {
1033
+ result = val[0] << val[2]
1034
+ }
1035
+ | args tCOMMA tSTAR arg_value
1036
+ {
1037
+ result = val[0] << @builder.splat(val[2], val[3])
1038
+ }
1039
+
1040
+ mrhs_arg: mrhs
1041
+ {
1042
+ result = @builder.array(nil, val[0], nil)
1043
+ }
1044
+ | arg_value
1045
+
1046
+ mrhs: args tCOMMA arg_value
1047
+ {
1048
+ result = val[0] << val[2]
1049
+ }
1050
+ | args tCOMMA tSTAR arg_value
1051
+ {
1052
+ result = val[0] << @builder.splat(val[2], val[3])
1053
+ }
1054
+ | tSTAR arg_value
1055
+ {
1056
+ result = [ @builder.splat(val[0], val[1]) ]
1057
+ }
1058
+
1059
+ primary: literal
1060
+ | strings
1061
+ | xstring
1062
+ | regexp
1063
+ | words
1064
+ | qwords
1065
+ | symbols
1066
+ | qsymbols
1067
+ | var_ref
1068
+ | backref
1069
+ | tFID
1070
+ {
1071
+ result = @builder.call_method(nil, nil, val[0])
1072
+ }
1073
+ | kBEGIN
1074
+ {
1075
+ @lexer.cmdarg.push(false)
1076
+ }
1077
+ bodystmt kEND
1078
+ {
1079
+ @lexer.cmdarg.pop
1080
+
1081
+ result = @builder.begin_keyword(val[0], val[2], val[3])
1082
+ }
1083
+ | tLPAREN_ARG stmt
1084
+ {
1085
+ @lexer.state = :expr_endarg
1086
+ }
1087
+ rparen
1088
+ {
1089
+ result = @builder.begin(val[0], val[1], val[3])
1090
+ }
1091
+ | tLPAREN_ARG
1092
+ {
1093
+ @lexer.state = :expr_endarg
1094
+ }
1095
+ opt_nl tRPAREN
1096
+ {
1097
+ result = @builder.begin(val[0], nil, val[3])
1098
+ }
1099
+ | tLPAREN compstmt tRPAREN
1100
+ {
1101
+ result = @builder.begin(val[0], val[1], val[2])
1102
+ }
1103
+ | primary_value tCOLON2 tCONSTANT
1104
+ {
1105
+ result = @builder.const_fetch(val[0], val[1], val[2])
1106
+ }
1107
+ | tCOLON3 tCONSTANT
1108
+ {
1109
+ result = @builder.const_global(val[0], val[1])
1110
+ }
1111
+ | tLBRACK aref_args tRBRACK
1112
+ {
1113
+ result = @builder.array(val[0], val[1], val[2])
1114
+ }
1115
+ | tLBRACE assoc_list tRCURLY
1116
+ {
1117
+ result = @builder.associate(val[0], val[1], val[2])
1118
+ }
1119
+ | k_return
1120
+ {
1121
+ result = @builder.keyword_cmd(:return, val[0])
1122
+ }
1123
+ | kYIELD tLPAREN2 call_args rparen
1124
+ {
1125
+ result = @builder.keyword_cmd(:yield, val[0], val[1], val[2], val[3])
1126
+ }
1127
+ | kYIELD tLPAREN2 rparen
1128
+ {
1129
+ result = @builder.keyword_cmd(:yield, val[0], val[1], [], val[2])
1130
+ }
1131
+ | kYIELD
1132
+ {
1133
+ result = @builder.keyword_cmd(:yield, val[0])
1134
+ }
1135
+ | kDEFINED opt_nl tLPAREN2 expr rparen
1136
+ {
1137
+ result = @builder.keyword_cmd(:defined?, val[0],
1138
+ val[2], [ val[3] ], val[4])
1139
+ }
1140
+ | kNOT tLPAREN2 expr rparen
1141
+ {
1142
+ result = @builder.not_op(val[0], val[1], val[2], val[3])
1143
+ }
1144
+ | kNOT tLPAREN2 rparen
1145
+ {
1146
+ result = @builder.not_op(val[0], val[1], nil, val[2])
1147
+ }
1148
+ | fcall brace_block
1149
+ {
1150
+ method_call = @builder.call_method(nil, nil, val[0])
1151
+
1152
+ begin_t, args, body, end_t = val[1]
1153
+ result = @builder.block(method_call,
1154
+ begin_t, args, body, end_t)
1155
+ }
1156
+ | method_call
1157
+ | method_call brace_block
1158
+ {
1159
+ begin_t, args, body, end_t = val[1]
1160
+ result = @builder.block(val[0],
1161
+ begin_t, args, body, end_t)
1162
+ }
1163
+ | tLAMBDA lambda
1164
+ {
1165
+ lambda_call = @builder.call_lambda(val[0])
1166
+
1167
+ args, (begin_t, body, end_t) = val[1]
1168
+ result = @builder.block(lambda_call,
1169
+ begin_t, args, body, end_t)
1170
+ }
1171
+ | kIF expr_value then compstmt if_tail kEND
1172
+ {
1173
+ else_t, else_ = val[4]
1174
+ result = @builder.condition(val[0], val[1], val[2],
1175
+ val[3], else_t,
1176
+ else_, val[5])
1177
+ }
1178
+ | kUNLESS expr_value then compstmt opt_else kEND
1179
+ {
1180
+ else_t, else_ = val[4]
1181
+ result = @builder.condition(val[0], val[1], val[2],
1182
+ else_, else_t,
1183
+ val[3], val[5])
1184
+ }
1185
+ | kWHILE expr_value_do compstmt kEND
1186
+ {
1187
+ result = @builder.loop(:while, val[0], *val[1], val[2], val[3])
1188
+ }
1189
+ | kUNTIL expr_value_do compstmt kEND
1190
+ {
1191
+ result = @builder.loop(:until, val[0], *val[1], val[2], val[3])
1192
+ }
1193
+ | kCASE expr_value opt_terms case_body kEND
1194
+ {
1195
+ *when_bodies, (else_t, else_body) = *val[3]
1196
+
1197
+ result = @builder.case(val[0], val[1],
1198
+ when_bodies, else_t, else_body,
1199
+ val[4])
1200
+ }
1201
+ | kCASE opt_terms case_body kEND
1202
+ {
1203
+ *when_bodies, (else_t, else_body) = *val[2]
1204
+
1205
+ result = @builder.case(val[0], nil,
1206
+ when_bodies, else_t, else_body,
1207
+ val[3])
1208
+ }
1209
+ | kCASE expr_value opt_terms p_case_body kEND
1210
+ {
1211
+ *in_bodies, (else_t, else_body) = *val[3]
1212
+
1213
+ result = @builder.case_match(val[0], val[1],
1214
+ in_bodies, else_t, else_body,
1215
+ val[4])
1216
+ }
1217
+ | kFOR for_var kIN expr_value_do compstmt kEND
1218
+ {
1219
+ result = @builder.for(val[0], val[1], val[2], *val[3], val[4], val[5])
1220
+ }
1221
+ | kCLASS cpath superclass
1222
+ {
1223
+ @static_env.extend_static
1224
+ @lexer.cmdarg.push(false)
1225
+ @lexer.cond.push(false)
1226
+ @context.push(:class)
1227
+ }
1228
+ bodystmt kEND
1229
+ {
1230
+ unless @context.class_definition_allowed?
1231
+ diagnostic :error, :class_in_def, nil, val[0]
1232
+ end
1233
+
1234
+ lt_t, superclass = val[2]
1235
+ result = @builder.def_class(val[0], val[1],
1236
+ lt_t, superclass,
1237
+ val[4], val[5])
1238
+
1239
+ @lexer.cmdarg.pop
1240
+ @lexer.cond.pop
1241
+ @static_env.unextend
1242
+ @context.pop
1243
+ }
1244
+ | kCLASS tLSHFT expr term
1245
+ {
1246
+ @static_env.extend_static
1247
+ @lexer.cmdarg.push(false)
1248
+ @lexer.cond.push(false)
1249
+ @context.push(:sclass)
1250
+ }
1251
+ bodystmt kEND
1252
+ {
1253
+ result = @builder.def_sclass(val[0], val[1], val[2],
1254
+ val[5], val[6])
1255
+
1256
+ @lexer.cmdarg.pop
1257
+ @lexer.cond.pop
1258
+ @static_env.unextend
1259
+ @context.pop
1260
+ }
1261
+ | kMODULE cpath
1262
+ {
1263
+ @static_env.extend_static
1264
+ @lexer.cmdarg.push(false)
1265
+ }
1266
+ bodystmt kEND
1267
+ {
1268
+ unless @context.module_definition_allowed?
1269
+ diagnostic :error, :module_in_def, nil, val[0]
1270
+ end
1271
+
1272
+ result = @builder.def_module(val[0], val[1],
1273
+ val[3], val[4])
1274
+
1275
+ @lexer.cmdarg.pop
1276
+ @static_env.unextend
1277
+ }
1278
+ | defn_head f_arglist bodystmt kEND
1279
+ {
1280
+ result = @builder.def_method(*val[0], val[1],
1281
+ val[2], val[3])
1282
+
1283
+ @lexer.cmdarg.pop
1284
+ @lexer.cond.pop
1285
+ @static_env.unextend
1286
+ @context.pop
1287
+ @current_arg_stack.pop
1288
+ }
1289
+ | defs_head f_arglist bodystmt kEND
1290
+ {
1291
+ result = @builder.def_singleton(*val[0], val[1],
1292
+ val[2], val[3])
1293
+
1294
+ @lexer.cmdarg.pop
1295
+ @lexer.cond.pop
1296
+ @static_env.unextend
1297
+ @context.pop
1298
+ @current_arg_stack.pop
1299
+ }
1300
+ | kBREAK
1301
+ {
1302
+ result = @builder.keyword_cmd(:break, val[0])
1303
+ }
1304
+ | kNEXT
1305
+ {
1306
+ result = @builder.keyword_cmd(:next, val[0])
1307
+ }
1308
+ | kREDO
1309
+ {
1310
+ result = @builder.keyword_cmd(:redo, val[0])
1311
+ }
1312
+ | kRETRY
1313
+ {
1314
+ result = @builder.keyword_cmd(:retry, val[0])
1315
+ }
1316
+
1317
+ primary_value: primary
1318
+
1319
+ k_return: kRETURN
1320
+ {
1321
+ if @context.in_class?
1322
+ diagnostic :error, :invalid_return, nil, val[0]
1323
+ end
1324
+ }
1325
+
1326
+ then: term
1327
+ | kTHEN
1328
+ | term kTHEN
1329
+ {
1330
+ result = val[1]
1331
+ }
1332
+
1333
+ do: term
1334
+ | kDO_COND
1335
+
1336
+ if_tail: opt_else
1337
+ | kELSIF expr_value then compstmt if_tail
1338
+ {
1339
+ else_t, else_ = val[4]
1340
+ result = [ val[0],
1341
+ @builder.condition(val[0], val[1], val[2],
1342
+ val[3], else_t,
1343
+ else_, nil),
1344
+ ]
1345
+ }
1346
+
1347
+ opt_else: none
1348
+ | kELSE compstmt
1349
+ {
1350
+ result = val
1351
+ }
1352
+
1353
+ for_var: lhs
1354
+ | mlhs
1355
+
1356
+ f_marg: f_norm_arg
1357
+ {
1358
+ result = @builder.arg(val[0])
1359
+ }
1360
+ | tLPAREN f_margs rparen
1361
+ {
1362
+ result = @builder.multi_lhs(val[0], val[1], val[2])
1363
+ }
1364
+
1365
+ f_marg_list: f_marg
1366
+ {
1367
+ result = [ val[0] ]
1368
+ }
1369
+ | f_marg_list tCOMMA f_marg
1370
+ {
1371
+ result = val[0] << val[2]
1372
+ }
1373
+
1374
+ f_margs: f_marg_list
1375
+ | f_marg_list tCOMMA f_rest_marg
1376
+ {
1377
+ result = val[0].
1378
+ push(val[2])
1379
+ }
1380
+ | f_marg_list tCOMMA f_rest_marg tCOMMA f_marg_list
1381
+ {
1382
+ result = val[0].
1383
+ push(val[2]).
1384
+ concat(val[4])
1385
+ }
1386
+ | f_rest_marg
1387
+ {
1388
+ result = [ val[0] ]
1389
+ }
1390
+ | f_rest_marg tCOMMA f_marg_list
1391
+ {
1392
+ result = [ val[0], *val[2] ]
1393
+ }
1394
+
1395
+ f_rest_marg: tSTAR f_norm_arg
1396
+ {
1397
+ result = @builder.restarg(val[0], val[1])
1398
+ }
1399
+ | tSTAR
1400
+ {
1401
+ result = @builder.restarg(val[0])
1402
+ }
1403
+
1404
+ block_args_tail: f_block_kwarg tCOMMA f_kwrest opt_f_block_arg
1405
+ {
1406
+ result = val[0].concat(val[2]).concat(val[3])
1407
+ }
1408
+ | f_block_kwarg opt_f_block_arg
1409
+ {
1410
+ result = val[0].concat(val[1])
1411
+ }
1412
+ | f_kwrest opt_f_block_arg
1413
+ {
1414
+ result = val[0].concat(val[1])
1415
+ }
1416
+ | f_no_kwarg opt_f_block_arg
1417
+ {
1418
+ result = val[0].concat(val[1])
1419
+ }
1420
+ | f_block_arg
1421
+ {
1422
+ result = [ val[0] ]
1423
+ }
1424
+
1425
+ opt_block_args_tail:
1426
+ tCOMMA block_args_tail
1427
+ {
1428
+ result = val[1]
1429
+ }
1430
+ | # nothing
1431
+ {
1432
+ result = []
1433
+ }
1434
+
1435
+ block_param: f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg opt_block_args_tail
1436
+ {
1437
+ result = val[0].
1438
+ concat(val[2]).
1439
+ concat(val[4]).
1440
+ concat(val[5])
1441
+ }
1442
+ | f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1443
+ {
1444
+ result = val[0].
1445
+ concat(val[2]).
1446
+ concat(val[4]).
1447
+ concat(val[6]).
1448
+ concat(val[7])
1449
+ }
1450
+ | f_arg tCOMMA f_block_optarg opt_block_args_tail
1451
+ {
1452
+ result = val[0].
1453
+ concat(val[2]).
1454
+ concat(val[3])
1455
+ }
1456
+ | f_arg tCOMMA f_block_optarg tCOMMA f_arg opt_block_args_tail
1457
+ {
1458
+ result = val[0].
1459
+ concat(val[2]).
1460
+ concat(val[4]).
1461
+ concat(val[5])
1462
+ }
1463
+ | f_arg tCOMMA f_rest_arg opt_block_args_tail
1464
+ {
1465
+ result = val[0].
1466
+ concat(val[2]).
1467
+ concat(val[3])
1468
+ }
1469
+ | f_arg tCOMMA
1470
+ | f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1471
+ {
1472
+ result = val[0].
1473
+ concat(val[2]).
1474
+ concat(val[4]).
1475
+ concat(val[5])
1476
+ }
1477
+ | f_arg opt_block_args_tail
1478
+ {
1479
+ if val[1].empty? && val[0].size == 1
1480
+ result = [@builder.procarg0(val[0][0])]
1481
+ else
1482
+ result = val[0].concat(val[1])
1483
+ end
1484
+ }
1485
+ | f_block_optarg tCOMMA f_rest_arg opt_block_args_tail
1486
+ {
1487
+ result = val[0].
1488
+ concat(val[2]).
1489
+ concat(val[3])
1490
+ }
1491
+ | f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_block_args_tail
1492
+ {
1493
+ result = val[0].
1494
+ concat(val[2]).
1495
+ concat(val[4]).
1496
+ concat(val[5])
1497
+ }
1498
+ | f_block_optarg opt_block_args_tail
1499
+ {
1500
+ result = val[0].
1501
+ concat(val[1])
1502
+ }
1503
+ | f_block_optarg tCOMMA f_arg opt_block_args_tail
1504
+ {
1505
+ result = val[0].
1506
+ concat(val[2]).
1507
+ concat(val[3])
1508
+ }
1509
+ | f_rest_arg opt_block_args_tail
1510
+ {
1511
+ result = val[0].
1512
+ concat(val[1])
1513
+ }
1514
+ | f_rest_arg tCOMMA f_arg opt_block_args_tail
1515
+ {
1516
+ result = val[0].
1517
+ concat(val[2]).
1518
+ concat(val[3])
1519
+ }
1520
+ | block_args_tail
1521
+
1522
+ opt_block_param: # nothing
1523
+ {
1524
+ result = @builder.args(nil, [], nil)
1525
+ }
1526
+ | block_param_def
1527
+ {
1528
+ @lexer.state = :expr_value
1529
+ }
1530
+
1531
+ block_param_def: tPIPE opt_bv_decl tPIPE
1532
+ {
1533
+ @max_numparam_stack.has_ordinary_params!
1534
+ @current_arg_stack.set(nil)
1535
+ result = @builder.args(val[0], val[1], val[2])
1536
+ }
1537
+ | tPIPE block_param opt_bv_decl tPIPE
1538
+ {
1539
+ @max_numparam_stack.has_ordinary_params!
1540
+ @current_arg_stack.set(nil)
1541
+ result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1542
+ }
1543
+
1544
+ opt_bv_decl: opt_nl
1545
+ {
1546
+ result = []
1547
+ }
1548
+ | opt_nl tSEMI bv_decls opt_nl
1549
+ {
1550
+ result = val[2]
1551
+ }
1552
+
1553
+ bv_decls: bvar
1554
+ {
1555
+ result = [ val[0] ]
1556
+ }
1557
+ | bv_decls tCOMMA bvar
1558
+ {
1559
+ result = val[0] << val[2]
1560
+ }
1561
+
1562
+ bvar: tIDENTIFIER
1563
+ {
1564
+ @static_env.declare val[0][0]
1565
+ result = @builder.shadowarg(val[0])
1566
+ }
1567
+ | f_bad_arg
1568
+
1569
+ lambda: {
1570
+ @static_env.extend_dynamic
1571
+ @max_numparam_stack.push
1572
+ @context.push(:lambda)
1573
+ }
1574
+ f_larglist
1575
+ {
1576
+ @context.pop
1577
+ @lexer.cmdarg.push(false)
1578
+ }
1579
+ lambda_body
1580
+ {
1581
+ args = @max_numparam_stack.has_numparams? ? @builder.numargs(@max_numparam_stack.top) : val[1]
1582
+ result = [ args, val[3] ]
1583
+
1584
+ @max_numparam_stack.pop
1585
+ @static_env.unextend
1586
+ @lexer.cmdarg.pop
1587
+ }
1588
+
1589
+ f_larglist: tLPAREN2 f_args opt_bv_decl tRPAREN
1590
+ {
1591
+ @max_numparam_stack.has_ordinary_params!
1592
+ result = @builder.args(val[0], val[1].concat(val[2]), val[3])
1593
+ }
1594
+ | f_args
1595
+ {
1596
+ if val[0].any?
1597
+ @max_numparam_stack.has_ordinary_params!
1598
+ end
1599
+ result = @builder.args(nil, val[0], nil)
1600
+ }
1601
+
1602
+ lambda_body: tLAMBEG
1603
+ {
1604
+ @context.push(:lambda)
1605
+ }
1606
+ compstmt tRCURLY
1607
+ {
1608
+ result = [ val[0], val[2], val[3] ]
1609
+ @context.pop
1610
+ }
1611
+ | kDO_LAMBDA
1612
+ {
1613
+ @context.push(:lambda)
1614
+ }
1615
+ bodystmt kEND
1616
+ {
1617
+ result = [ val[0], val[2], val[3] ]
1618
+ @context.pop
1619
+ }
1620
+
1621
+ do_block: kDO_BLOCK
1622
+ {
1623
+ @context.push(:block)
1624
+ }
1625
+ do_body kEND
1626
+ {
1627
+ result = [ val[0], *val[2], val[3] ]
1628
+ @context.pop
1629
+ }
1630
+
1631
+ block_call: command do_block
1632
+ {
1633
+ begin_t, block_args, body, end_t = val[1]
1634
+ result = @builder.block(val[0],
1635
+ begin_t, block_args, body, end_t)
1636
+ }
1637
+ | block_call dot_or_colon operation2 opt_paren_args
1638
+ {
1639
+ lparen_t, args, rparen_t = val[3]
1640
+ result = @builder.call_method(val[0], val[1], val[2],
1641
+ lparen_t, args, rparen_t)
1642
+ }
1643
+ | block_call dot_or_colon operation2 opt_paren_args brace_block
1644
+ {
1645
+ lparen_t, args, rparen_t = val[3]
1646
+ method_call = @builder.call_method(val[0], val[1], val[2],
1647
+ lparen_t, args, rparen_t)
1648
+
1649
+ begin_t, args, body, end_t = val[4]
1650
+ result = @builder.block(method_call,
1651
+ begin_t, args, body, end_t)
1652
+ }
1653
+ | block_call dot_or_colon operation2 command_args do_block
1654
+ {
1655
+ method_call = @builder.call_method(val[0], val[1], val[2],
1656
+ nil, val[3], nil)
1657
+
1658
+ begin_t, args, body, end_t = val[4]
1659
+ result = @builder.block(method_call,
1660
+ begin_t, args, body, end_t)
1661
+ }
1662
+
1663
+ method_call: fcall paren_args
1664
+ {
1665
+ lparen_t, args, rparen_t = val[1]
1666
+ result = @builder.call_method(nil, nil, val[0],
1667
+ lparen_t, args, rparen_t)
1668
+ }
1669
+ | primary_value call_op operation2 opt_paren_args
1670
+ {
1671
+ lparen_t, args, rparen_t = val[3]
1672
+ result = @builder.call_method(val[0], val[1], val[2],
1673
+ lparen_t, args, rparen_t)
1674
+ }
1675
+ | primary_value tCOLON2 operation2 paren_args
1676
+ {
1677
+ lparen_t, args, rparen_t = val[3]
1678
+ result = @builder.call_method(val[0], val[1], val[2],
1679
+ lparen_t, args, rparen_t)
1680
+ }
1681
+ | primary_value tCOLON2 operation3
1682
+ {
1683
+ result = @builder.call_method(val[0], val[1], val[2])
1684
+ }
1685
+ | primary_value call_op paren_args
1686
+ {
1687
+ lparen_t, args, rparen_t = val[2]
1688
+ result = @builder.call_method(val[0], val[1], nil,
1689
+ lparen_t, args, rparen_t)
1690
+ }
1691
+ | primary_value tCOLON2 paren_args
1692
+ {
1693
+ lparen_t, args, rparen_t = val[2]
1694
+ result = @builder.call_method(val[0], val[1], nil,
1695
+ lparen_t, args, rparen_t)
1696
+ }
1697
+ | kSUPER paren_args
1698
+ {
1699
+ lparen_t, args, rparen_t = val[1]
1700
+ result = @builder.keyword_cmd(:super, val[0],
1701
+ lparen_t, args, rparen_t)
1702
+ }
1703
+ | kSUPER
1704
+ {
1705
+ result = @builder.keyword_cmd(:zsuper, val[0])
1706
+ }
1707
+ | primary_value tLBRACK2 opt_call_args rbracket
1708
+ {
1709
+ result = @builder.index(val[0], val[1], val[2], val[3])
1710
+ }
1711
+
1712
+ brace_block: tLCURLY
1713
+ {
1714
+ @context.push(:block)
1715
+ }
1716
+ brace_body tRCURLY
1717
+ {
1718
+ result = [ val[0], *val[2], val[3] ]
1719
+ @context.pop
1720
+ }
1721
+ | kDO
1722
+ {
1723
+ @context.push(:block)
1724
+ }
1725
+ do_body kEND
1726
+ {
1727
+ result = [ val[0], *val[2], val[3] ]
1728
+ @context.pop
1729
+ }
1730
+
1731
+ brace_body: {
1732
+ @static_env.extend_dynamic
1733
+ @max_numparam_stack.push
1734
+ }
1735
+ opt_block_param compstmt
1736
+ {
1737
+ args = @max_numparam_stack.has_numparams? ? @builder.numargs(@max_numparam_stack.top) : val[1]
1738
+ result = [ args, val[2] ]
1739
+
1740
+ @max_numparam_stack.pop
1741
+ @static_env.unextend
1742
+ }
1743
+
1744
+ do_body: {
1745
+ @static_env.extend_dynamic
1746
+ @max_numparam_stack.push
1747
+ }
1748
+ {
1749
+ @lexer.cmdarg.push(false)
1750
+ }
1751
+ opt_block_param bodystmt
1752
+ {
1753
+ args = @max_numparam_stack.has_numparams? ? @builder.numargs(@max_numparam_stack.top) : val[2]
1754
+ result = [ args, val[3] ]
1755
+
1756
+ @max_numparam_stack.pop
1757
+ @static_env.unextend
1758
+ @lexer.cmdarg.pop
1759
+ }
1760
+
1761
+ case_body: kWHEN args then compstmt cases
1762
+ {
1763
+ result = [ @builder.when(val[0], val[1], val[2], val[3]),
1764
+ *val[4] ]
1765
+ }
1766
+
1767
+ cases: opt_else
1768
+ {
1769
+ result = [ val[0] ]
1770
+ }
1771
+ | case_body
1772
+
1773
+ p_case_body: kIN
1774
+ {
1775
+ @lexer.state = :expr_beg
1776
+ @lexer.command_start = false
1777
+ @pattern_variables.push
1778
+ @pattern_hash_keys.push
1779
+
1780
+ result = @lexer.in_kwarg
1781
+ @lexer.in_kwarg = true
1782
+ }
1783
+ p_top_expr then
1784
+ {
1785
+ @lexer.in_kwarg = val[1]
1786
+ }
1787
+ compstmt p_cases
1788
+ {
1789
+ result = [ @builder.in_pattern(val[0], *val[2], val[3], val[5]),
1790
+ *val[6] ]
1791
+ }
1792
+
1793
+ p_cases: opt_else
1794
+ {
1795
+ result = [ val[0] ]
1796
+ }
1797
+ | p_case_body
1798
+
1799
+ p_top_expr: p_top_expr_body
1800
+ {
1801
+ result = [ val[0], nil ]
1802
+ }
1803
+ | p_top_expr_body kIF_MOD expr_value
1804
+ {
1805
+ result = [ val[0], @builder.if_guard(val[1], val[2]) ]
1806
+ }
1807
+ | p_top_expr_body kUNLESS_MOD expr_value
1808
+ {
1809
+ result = [ val[0], @builder.unless_guard(val[1], val[2]) ]
1810
+ }
1811
+
1812
+ p_top_expr_body: p_expr
1813
+ | p_expr tCOMMA
1814
+ {
1815
+ # array patterns that end with comma
1816
+ # like 1, 2,
1817
+ # must be emitted as `array_pattern_with_tail`
1818
+ item = @builder.match_with_trailing_comma(val[0], val[1])
1819
+ result = @builder.array_pattern(nil, [ item ], nil)
1820
+ }
1821
+ | p_expr tCOMMA p_args
1822
+ {
1823
+ result = @builder.array_pattern(nil, [val[0]].concat(val[2]), nil)
1824
+ }
1825
+ | p_args_tail
1826
+ {
1827
+ result = @builder.array_pattern(nil, val[0], nil)
1828
+ }
1829
+ | p_kwargs
1830
+ {
1831
+ result = @builder.hash_pattern(nil, val[0], nil)
1832
+ }
1833
+
1834
+ p_expr: p_as
1835
+
1836
+ p_as: p_expr tASSOC p_variable
1837
+ {
1838
+ result = @builder.match_as(val[0], val[1], val[2])
1839
+ }
1840
+ | p_alt
1841
+
1842
+ p_alt: p_alt tPIPE p_expr_basic
1843
+ {
1844
+ result = @builder.match_alt(val[0], val[1], val[2])
1845
+ }
1846
+ | p_expr_basic
1847
+
1848
+ p_lparen: tLPAREN2
1849
+ {
1850
+ result = val[0]
1851
+ @pattern_hash_keys.push
1852
+ }
1853
+
1854
+ p_lbracket: tLBRACK2
1855
+ {
1856
+ result = val[0]
1857
+ @pattern_hash_keys.push
1858
+ }
1859
+
1860
+ p_expr_basic: p_value
1861
+ | p_const p_lparen p_args rparen
1862
+ {
1863
+ @pattern_hash_keys.pop
1864
+ pattern = @builder.array_pattern(nil, val[2], nil)
1865
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1866
+ }
1867
+ | p_const p_lparen p_kwargs rparen
1868
+ {
1869
+ @pattern_hash_keys.pop
1870
+ pattern = @builder.hash_pattern(nil, val[2], nil)
1871
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1872
+ }
1873
+ | p_const tLPAREN2 rparen
1874
+ {
1875
+ pattern = @builder.array_pattern(val[1], nil, val[2])
1876
+ result = @builder.const_pattern(val[0], val[1], pattern, val[2])
1877
+ }
1878
+ | p_const p_lbracket p_args rbracket
1879
+ {
1880
+ @pattern_hash_keys.pop
1881
+ pattern = @builder.array_pattern(nil, val[2], nil)
1882
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1883
+ }
1884
+ | p_const p_lbracket p_kwargs rbracket
1885
+ {
1886
+ @pattern_hash_keys.pop
1887
+ pattern = @builder.hash_pattern(nil, val[2], nil)
1888
+ result = @builder.const_pattern(val[0], val[1], pattern, val[3])
1889
+ }
1890
+ | p_const tLBRACK2 rbracket
1891
+ {
1892
+ pattern = @builder.array_pattern(val[1], nil, val[2])
1893
+ result = @builder.const_pattern(val[0], val[1], pattern, val[2])
1894
+ }
1895
+ | tLBRACK
1896
+ {
1897
+ @pattern_hash_keys.push
1898
+ }
1899
+ p_args rbracket
1900
+ {
1901
+ @pattern_hash_keys.pop
1902
+ result = @builder.array_pattern(val[0], val[2], val[3])
1903
+ }
1904
+ | tLBRACK rbracket
1905
+ {
1906
+ result = @builder.array_pattern(val[0], [], val[1])
1907
+ }
1908
+ | tLBRACE
1909
+ {
1910
+ @pattern_hash_keys.push
1911
+ result = @lexer.in_kwarg
1912
+ @lexer.in_kwarg = false
1913
+ }
1914
+ p_kwargs rbrace
1915
+ {
1916
+ @pattern_hash_keys.pop
1917
+ @lexer.in_kwarg = val[1]
1918
+ result = @builder.hash_pattern(val[0], val[2], val[3])
1919
+ }
1920
+ | tLBRACE rbrace
1921
+ {
1922
+ result = @builder.hash_pattern(val[0], [], val[1])
1923
+ }
1924
+ | tLPAREN
1925
+ {
1926
+ @pattern_hash_keys.push
1927
+ }
1928
+ p_expr rparen
1929
+ {
1930
+ @pattern_hash_keys.pop
1931
+ result = @builder.begin(val[0], val[2], val[3])
1932
+ }
1933
+
1934
+ p_args: p_expr
1935
+ {
1936
+ result = [ val[0] ]
1937
+ }
1938
+ | p_args_head
1939
+ {
1940
+ result = val[0]
1941
+ }
1942
+ | p_args_head p_arg
1943
+ {
1944
+ result = [ *val[0], val[1] ]
1945
+ }
1946
+ | p_args_head tSTAR tIDENTIFIER
1947
+ {
1948
+ match_rest = @builder.match_rest(val[1], val[2])
1949
+ result = [ *val[0], match_rest ]
1950
+ }
1951
+ | p_args_head tSTAR tIDENTIFIER tCOMMA p_args_post
1952
+ {
1953
+ match_rest = @builder.match_rest(val[1], val[2])
1954
+ result = [ *val[0], match_rest, *val[4] ]
1955
+ }
1956
+ | p_args_head tSTAR
1957
+ {
1958
+ result = [ *val[0], @builder.match_rest(val[1]) ]
1959
+ }
1960
+ | p_args_head tSTAR tCOMMA p_args_post
1961
+ {
1962
+ result = [ *val[0], @builder.match_rest(val[1]), *val[3] ]
1963
+ }
1964
+ | p_args_tail
1965
+
1966
+ p_args_head: p_arg tCOMMA
1967
+ {
1968
+ # array patterns that end with comma
1969
+ # like [1, 2,]
1970
+ # must be emitted as `array_pattern_with_tail`
1971
+ item = @builder.match_with_trailing_comma(val[0], val[1])
1972
+ result = [ item ]
1973
+ }
1974
+ | p_args_head p_arg tCOMMA
1975
+ {
1976
+ # array patterns that end with comma
1977
+ # like [1, 2,]
1978
+ # must be emitted as `array_pattern_with_tail`
1979
+ last_item = @builder.match_with_trailing_comma(val[1], val[2])
1980
+ result = [ *val[0], last_item ]
1981
+ }
1982
+
1983
+ p_args_tail: tSTAR tIDENTIFIER
1984
+ {
1985
+ match_rest = @builder.match_rest(val[0], val[1])
1986
+ result = [ match_rest ]
1987
+ }
1988
+ | tSTAR tIDENTIFIER tCOMMA p_args_post
1989
+ {
1990
+ match_rest = @builder.match_rest(val[0], val[1])
1991
+ result = [ match_rest, *val[3] ]
1992
+ }
1993
+ | tSTAR
1994
+ {
1995
+ match_rest = @builder.match_rest(val[0])
1996
+ result = [ match_rest ]
1997
+ }
1998
+ | tSTAR tCOMMA p_args_post
1999
+ {
2000
+ match_rest = @builder.match_rest(val[0])
2001
+ result = [ match_rest, *val[2] ]
2002
+ }
2003
+
2004
+ p_args_post: p_arg
2005
+ {
2006
+ result = [ val[0] ]
2007
+ }
2008
+ | p_args_post tCOMMA p_arg
2009
+ {
2010
+ result = [ *val[0], val[2] ]
2011
+ }
2012
+
2013
+ p_arg: p_expr
2014
+
2015
+ p_kwargs: p_kwarg tCOMMA p_kwrest
2016
+ {
2017
+ result = [ *val[0], *val[2] ]
2018
+ }
2019
+ | p_kwarg
2020
+ {
2021
+ result = val[0]
2022
+ }
2023
+ | p_kwarg tCOMMA
2024
+ {
2025
+ result = val[0]
2026
+ }
2027
+ | p_kwrest
2028
+ {
2029
+ result = val[0]
2030
+ }
2031
+ | p_kwarg tCOMMA p_kwnorest
2032
+ {
2033
+ result = [ *val[0], *val[2] ]
2034
+ }
2035
+ | p_kwnorest
2036
+ {
2037
+ result = [ *val[0], *val[2] ]
2038
+ }
2039
+
2040
+ p_kwarg: p_kw
2041
+ {
2042
+ result = [ val[0] ]
2043
+ }
2044
+ | p_kwarg tCOMMA p_kw
2045
+ {
2046
+ result = [ *val[0], val[2] ]
2047
+ }
2048
+
2049
+ p_kw: p_kw_label p_expr
2050
+ {
2051
+ result = @builder.match_pair(*val[0], val[1])
2052
+ }
2053
+ | p_kw_label
2054
+ {
2055
+ result = @builder.match_label(*val[0])
2056
+ }
2057
+
2058
+ p_kw_label: tLABEL
2059
+ {
2060
+ check_kwarg_name(val[0])
2061
+ result = [:label, val[0]]
2062
+ }
2063
+ | tSTRING_BEG string_contents tLABEL_END
2064
+ {
2065
+ result = [:quoted, [val[0], val[1], val[2]]]
2066
+ }
2067
+
2068
+ p_kwrest: kwrest_mark tIDENTIFIER
2069
+ {
2070
+ result = [ @builder.match_rest(val[0], val[1]) ]
2071
+ }
2072
+ | kwrest_mark
2073
+ {
2074
+ result = [ @builder.match_rest(val[0], nil) ]
2075
+ }
2076
+
2077
+ p_kwnorest: kwrest_mark kNIL
2078
+ {
2079
+ result = [ @builder.match_nil_pattern(val[0], val[1]) ]
2080
+ }
2081
+
2082
+ p_value: p_primitive
2083
+ | p_primitive tDOT2 p_primitive
2084
+ {
2085
+ result = @builder.range_inclusive(val[0], val[1], val[2])
2086
+ }
2087
+ | p_primitive tDOT3 p_primitive
2088
+ {
2089
+ result = @builder.range_exclusive(val[0], val[1], val[2])
2090
+ }
2091
+ | p_primitive tDOT2
2092
+ {
2093
+ result = @builder.range_inclusive(val[0], val[1], nil)
2094
+ }
2095
+ | p_primitive tDOT3
2096
+ {
2097
+ result = @builder.range_exclusive(val[0], val[1], nil)
2098
+ }
2099
+ | p_variable
2100
+ | p_var_ref
2101
+ | p_const
2102
+ | tBDOT2 p_primitive
2103
+ {
2104
+ result = @builder.range_inclusive(nil, val[0], val[1])
2105
+ }
2106
+ | tBDOT3 p_primitive
2107
+ {
2108
+ result = @builder.range_exclusive(nil, val[0], val[1])
2109
+ }
2110
+
2111
+ p_primitive: literal
2112
+ | strings
2113
+ | xstring
2114
+ | regexp
2115
+ | words
2116
+ | qwords
2117
+ | symbols
2118
+ | qsymbols
2119
+ | keyword_variable
2120
+ {
2121
+ result = @builder.accessible(val[0])
2122
+ }
2123
+ | tLAMBDA lambda
2124
+ {
2125
+ lambda_call = @builder.call_lambda(val[0])
2126
+
2127
+ args, (begin_t, body, end_t) = val[1]
2128
+ result = @builder.block(lambda_call,
2129
+ begin_t, args, body, end_t)
2130
+ }
2131
+
2132
+ p_variable: tIDENTIFIER
2133
+ {
2134
+ result = @builder.match_var(val[0])
2135
+ }
2136
+
2137
+ p_var_ref: tCARET tIDENTIFIER
2138
+ {
2139
+ name = val[1][0]
2140
+ unless static_env.declared?(name)
2141
+ diagnostic :error, :undefined_lvar, { :name => name }, val[1]
2142
+ end
2143
+
2144
+ lvar = @builder.accessible(@builder.ident(val[1]))
2145
+ result = @builder.pin(val[0], lvar)
2146
+ }
2147
+
2148
+ p_const: tCOLON3 cname
2149
+ {
2150
+ result = @builder.const_global(val[0], val[1])
2151
+ }
2152
+ | p_const tCOLON2 cname
2153
+ {
2154
+ result = @builder.const_fetch(val[0], val[1], val[2])
2155
+ }
2156
+ | tCONSTANT
2157
+ {
2158
+ result = @builder.const(val[0])
2159
+ }
2160
+
2161
+ opt_rescue: kRESCUE exc_list exc_var then compstmt opt_rescue
2162
+ {
2163
+ assoc_t, exc_var = val[2]
2164
+
2165
+ if val[1]
2166
+ exc_list = @builder.array(nil, val[1], nil)
2167
+ end
2168
+
2169
+ result = [ @builder.rescue_body(val[0],
2170
+ exc_list, assoc_t, exc_var,
2171
+ val[3], val[4]),
2172
+ *val[5] ]
2173
+ }
2174
+ |
2175
+ {
2176
+ result = []
2177
+ }
2178
+
2179
+ exc_list: arg_value
2180
+ {
2181
+ result = [ val[0] ]
2182
+ }
2183
+ | mrhs
2184
+ | none
2185
+
2186
+ exc_var: tASSOC lhs
2187
+ {
2188
+ result = [ val[0], val[1] ]
2189
+ }
2190
+ | none
2191
+
2192
+ opt_ensure: kENSURE compstmt
2193
+ {
2194
+ result = [ val[0], val[1] ]
2195
+ }
2196
+ | none
2197
+
2198
+ literal: numeric
2199
+ | symbol
2200
+
2201
+ strings: string
2202
+ {
2203
+ result = @builder.string_compose(nil, val[0], nil)
2204
+ }
2205
+
2206
+ string: string1
2207
+ {
2208
+ result = [ val[0] ]
2209
+ }
2210
+ | string string1
2211
+ {
2212
+ result = val[0] << val[1]
2213
+ }
2214
+
2215
+ string1: tSTRING_BEG string_contents tSTRING_END
2216
+ {
2217
+ string = @builder.string_compose(val[0], val[1], val[2])
2218
+ result = @builder.dedent_string(string, @lexer.dedent_level)
2219
+ }
2220
+ | tSTRING
2221
+ {
2222
+ string = @builder.string(val[0])
2223
+ result = @builder.dedent_string(string, @lexer.dedent_level)
2224
+ }
2225
+ | tCHARACTER
2226
+ {
2227
+ result = @builder.character(val[0])
2228
+ }
2229
+
2230
+ xstring: tXSTRING_BEG xstring_contents tSTRING_END
2231
+ {
2232
+ string = @builder.xstring_compose(val[0], val[1], val[2])
2233
+ result = @builder.dedent_string(string, @lexer.dedent_level)
2234
+ }
2235
+
2236
+ regexp: tREGEXP_BEG regexp_contents tSTRING_END tREGEXP_OPT
2237
+ {
2238
+ opts = @builder.regexp_options(val[3])
2239
+ result = @builder.regexp_compose(val[0], val[1], val[2], opts)
2240
+ }
2241
+
2242
+ words: tWORDS_BEG word_list tSTRING_END
2243
+ {
2244
+ result = @builder.words_compose(val[0], val[1], val[2])
2245
+ }
2246
+
2247
+ word_list: # nothing
2248
+ {
2249
+ result = []
2250
+ }
2251
+ | word_list word tSPACE
2252
+ {
2253
+ result = val[0] << @builder.word(val[1])
2254
+ }
2255
+
2256
+ word: string_content
2257
+ {
2258
+ result = [ val[0] ]
2259
+ }
2260
+ | word string_content
2261
+ {
2262
+ result = val[0] << val[1]
2263
+ }
2264
+
2265
+ symbols: tSYMBOLS_BEG symbol_list tSTRING_END
2266
+ {
2267
+ result = @builder.symbols_compose(val[0], val[1], val[2])
2268
+ }
2269
+
2270
+ symbol_list: # nothing
2271
+ {
2272
+ result = []
2273
+ }
2274
+ | symbol_list word tSPACE
2275
+ {
2276
+ result = val[0] << @builder.word(val[1])
2277
+ }
2278
+
2279
+ qwords: tQWORDS_BEG qword_list tSTRING_END
2280
+ {
2281
+ result = @builder.words_compose(val[0], val[1], val[2])
2282
+ }
2283
+
2284
+ qsymbols: tQSYMBOLS_BEG qsym_list tSTRING_END
2285
+ {
2286
+ result = @builder.symbols_compose(val[0], val[1], val[2])
2287
+ }
2288
+
2289
+ qword_list: # nothing
2290
+ {
2291
+ result = []
2292
+ }
2293
+ | qword_list tSTRING_CONTENT tSPACE
2294
+ {
2295
+ result = val[0] << @builder.string_internal(val[1])
2296
+ }
2297
+
2298
+ qsym_list: # nothing
2299
+ {
2300
+ result = []
2301
+ }
2302
+ | qsym_list tSTRING_CONTENT tSPACE
2303
+ {
2304
+ result = val[0] << @builder.symbol_internal(val[1])
2305
+ }
2306
+
2307
+ string_contents: # nothing
2308
+ {
2309
+ result = []
2310
+ }
2311
+ | string_contents string_content
2312
+ {
2313
+ result = val[0] << val[1]
2314
+ }
2315
+
2316
+ xstring_contents: # nothing
2317
+ {
2318
+ result = []
2319
+ }
2320
+ | xstring_contents string_content
2321
+ {
2322
+ result = val[0] << val[1]
2323
+ }
2324
+
2325
+ regexp_contents: # nothing
2326
+ {
2327
+ result = []
2328
+ }
2329
+ | regexp_contents string_content
2330
+ {
2331
+ result = val[0] << val[1]
2332
+ }
2333
+
2334
+ string_content: tSTRING_CONTENT
2335
+ {
2336
+ result = @builder.string_internal(val[0])
2337
+ }
2338
+ | tSTRING_DVAR string_dvar
2339
+ {
2340
+ result = val[1]
2341
+ }
2342
+ | tSTRING_DBEG
2343
+ {
2344
+ @lexer.cmdarg.push(false)
2345
+ @lexer.cond.push(false)
2346
+ }
2347
+ compstmt tSTRING_DEND
2348
+ {
2349
+ @lexer.cmdarg.pop
2350
+ @lexer.cond.pop
2351
+
2352
+ result = @builder.begin(val[0], val[2], val[3])
2353
+ }
2354
+
2355
+ string_dvar: tGVAR
2356
+ {
2357
+ result = @builder.gvar(val[0])
2358
+ }
2359
+ | tIVAR
2360
+ {
2361
+ result = @builder.ivar(val[0])
2362
+ }
2363
+ | tCVAR
2364
+ {
2365
+ result = @builder.cvar(val[0])
2366
+ }
2367
+ | backref
2368
+
2369
+ symbol: ssym
2370
+ | dsym
2371
+
2372
+ ssym: tSYMBOL
2373
+ {
2374
+ @lexer.state = :expr_end
2375
+ result = @builder.symbol(val[0])
2376
+ }
2377
+
2378
+ dsym: tSYMBEG string_contents tSTRING_END
2379
+ {
2380
+ @lexer.state = :expr_end
2381
+ result = @builder.symbol_compose(val[0], val[1], val[2])
2382
+ }
2383
+
2384
+ numeric: simple_numeric
2385
+ {
2386
+ result = val[0]
2387
+ }
2388
+ | tUNARY_NUM simple_numeric =tLOWEST
2389
+ {
2390
+ if @builder.respond_to? :negate
2391
+ # AST builder interface compatibility
2392
+ result = @builder.negate(val[0], val[1])
2393
+ else
2394
+ result = @builder.unary_num(val[0], val[1])
2395
+ end
2396
+ }
2397
+
2398
+ simple_numeric: tINTEGER
2399
+ {
2400
+ @lexer.state = :expr_end
2401
+ result = @builder.integer(val[0])
2402
+ }
2403
+ | tFLOAT
2404
+ {
2405
+ @lexer.state = :expr_end
2406
+ result = @builder.float(val[0])
2407
+ }
2408
+ | tRATIONAL
2409
+ {
2410
+ @lexer.state = :expr_end
2411
+ result = @builder.rational(val[0])
2412
+ }
2413
+ | tIMAGINARY
2414
+ {
2415
+ @lexer.state = :expr_end
2416
+ result = @builder.complex(val[0])
2417
+ }
2418
+
2419
+ user_variable: tIDENTIFIER
2420
+ {
2421
+ result = @builder.ident(val[0])
2422
+ }
2423
+ | tIVAR
2424
+ {
2425
+ result = @builder.ivar(val[0])
2426
+ }
2427
+ | tGVAR
2428
+ {
2429
+ result = @builder.gvar(val[0])
2430
+ }
2431
+ | tCONSTANT
2432
+ {
2433
+ result = @builder.const(val[0])
2434
+ }
2435
+ | tCVAR
2436
+ {
2437
+ result = @builder.cvar(val[0])
2438
+ }
2439
+
2440
+ keyword_variable: kNIL
2441
+ {
2442
+ result = @builder.nil(val[0])
2443
+ }
2444
+ | kSELF
2445
+ {
2446
+ result = @builder.self(val[0])
2447
+ }
2448
+ | kTRUE
2449
+ {
2450
+ result = @builder.true(val[0])
2451
+ }
2452
+ | kFALSE
2453
+ {
2454
+ result = @builder.false(val[0])
2455
+ }
2456
+ | k__FILE__
2457
+ {
2458
+ result = @builder.__FILE__(val[0])
2459
+ }
2460
+ | k__LINE__
2461
+ {
2462
+ result = @builder.__LINE__(val[0])
2463
+ }
2464
+ | k__ENCODING__
2465
+ {
2466
+ result = @builder.__ENCODING__(val[0])
2467
+ }
2468
+
2469
+ var_ref: user_variable
2470
+ {
2471
+ if (node = val[0]) && node.type == :ident
2472
+ name = node.children[0]
2473
+
2474
+ if name =~ /\A_[1-9]\z/ && !static_env.declared?(name) && context.in_dynamic_block?
2475
+ # definitely an implicit param
2476
+ location = node.loc.expression
2477
+
2478
+ if max_numparam_stack.has_ordinary_params?
2479
+ diagnostic :error, :ordinary_param_defined, nil, [nil, location]
2480
+ end
2481
+
2482
+ raw_context = context.stack.dup
2483
+ raw_max_numparam_stack = max_numparam_stack.stack.dup
2484
+
2485
+ # ignore current block scope
2486
+ raw_context.pop
2487
+ raw_max_numparam_stack.pop
2488
+
2489
+ raw_context.reverse_each do |outer_scope|
2490
+ if outer_scope == :block || outer_scope == :lambda
2491
+ outer_scope_has_numparams = raw_max_numparam_stack.pop > 0
2492
+
2493
+ if outer_scope_has_numparams
2494
+ diagnostic :error, :numparam_used_in_outer_scope, nil, [nil, location]
2495
+ else
2496
+ # for now it's ok, but an outer scope can also be a block
2497
+ # with numparams, so we need to continue
2498
+ end
2499
+ else
2500
+ # found an outer scope that can't have numparams
2501
+ # like def/class/etc
2502
+ break
2503
+ end
2504
+ end
2505
+
2506
+ static_env.declare(name)
2507
+ max_numparam_stack.register(name[1].to_i)
2508
+ end
2509
+ end
2510
+
2511
+ result = @builder.accessible(val[0])
2512
+ }
2513
+ | keyword_variable
2514
+ {
2515
+ result = @builder.accessible(val[0])
2516
+ }
2517
+
2518
+ var_lhs: user_variable
2519
+ {
2520
+ result = @builder.assignable(val[0])
2521
+ }
2522
+ | keyword_variable
2523
+ {
2524
+ result = @builder.assignable(val[0])
2525
+ }
2526
+
2527
+ backref: tNTH_REF
2528
+ {
2529
+ result = @builder.nth_ref(val[0])
2530
+ }
2531
+ | tBACK_REF
2532
+ {
2533
+ result = @builder.back_ref(val[0])
2534
+ }
2535
+
2536
+ superclass: tLT
2537
+ {
2538
+ @lexer.state = :expr_value
2539
+ }
2540
+ expr_value term
2541
+ {
2542
+ result = [ val[0], val[2] ]
2543
+ }
2544
+ | # nothing
2545
+ {
2546
+ result = nil
2547
+ }
2548
+
2549
+ f_paren_args: tLPAREN2 f_args rparen
2550
+ {
2551
+ result = @builder.args(val[0], val[1], val[2])
2552
+
2553
+ @lexer.state = :expr_value
2554
+ }
2555
+ | tLPAREN2 args_forward rparen
2556
+ {
2557
+ result = @builder.forward_args(val[0], val[1], val[2])
2558
+ @static_env.declare_forward_args
2559
+
2560
+ @lexer.state = :expr_value
2561
+ }
2562
+
2563
+ f_arglist: f_paren_args
2564
+ | {
2565
+ result = @lexer.in_kwarg
2566
+ @lexer.in_kwarg = true
2567
+ }
2568
+ f_args term
2569
+ {
2570
+ @lexer.in_kwarg = val[0]
2571
+ result = @builder.args(nil, val[1], nil)
2572
+ }
2573
+
2574
+ args_tail: f_kwarg tCOMMA f_kwrest opt_f_block_arg
2575
+ {
2576
+ result = val[0].concat(val[2]).concat(val[3])
2577
+ }
2578
+ | f_kwarg opt_f_block_arg
2579
+ {
2580
+ result = val[0].concat(val[1])
2581
+ }
2582
+ | f_kwrest opt_f_block_arg
2583
+ {
2584
+ result = val[0].concat(val[1])
2585
+ }
2586
+ | f_no_kwarg opt_f_block_arg
2587
+ {
2588
+ result = val[0].concat(val[1])
2589
+ }
2590
+ | f_block_arg
2591
+ {
2592
+ result = [ val[0] ]
2593
+ }
2594
+
2595
+ opt_args_tail: tCOMMA args_tail
2596
+ {
2597
+ result = val[1]
2598
+ }
2599
+ | # nothing
2600
+ {
2601
+ result = []
2602
+ }
2603
+
2604
+ f_args: f_arg tCOMMA f_optarg tCOMMA f_rest_arg opt_args_tail
2605
+ {
2606
+ result = val[0].
2607
+ concat(val[2]).
2608
+ concat(val[4]).
2609
+ concat(val[5])
2610
+ }
2611
+ | f_arg tCOMMA f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2612
+ {
2613
+ result = val[0].
2614
+ concat(val[2]).
2615
+ concat(val[4]).
2616
+ concat(val[6]).
2617
+ concat(val[7])
2618
+ }
2619
+ | f_arg tCOMMA f_optarg opt_args_tail
2620
+ {
2621
+ result = val[0].
2622
+ concat(val[2]).
2623
+ concat(val[3])
2624
+ }
2625
+ | f_arg tCOMMA f_optarg tCOMMA f_arg opt_args_tail
2626
+ {
2627
+ result = val[0].
2628
+ concat(val[2]).
2629
+ concat(val[4]).
2630
+ concat(val[5])
2631
+ }
2632
+ | f_arg tCOMMA f_rest_arg opt_args_tail
2633
+ {
2634
+ result = val[0].
2635
+ concat(val[2]).
2636
+ concat(val[3])
2637
+ }
2638
+ | f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2639
+ {
2640
+ result = val[0].
2641
+ concat(val[2]).
2642
+ concat(val[4]).
2643
+ concat(val[5])
2644
+ }
2645
+ | f_arg opt_args_tail
2646
+ {
2647
+ result = val[0].
2648
+ concat(val[1])
2649
+ }
2650
+ | f_optarg tCOMMA f_rest_arg opt_args_tail
2651
+ {
2652
+ result = val[0].
2653
+ concat(val[2]).
2654
+ concat(val[3])
2655
+ }
2656
+ | f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_args_tail
2657
+ {
2658
+ result = val[0].
2659
+ concat(val[2]).
2660
+ concat(val[4]).
2661
+ concat(val[5])
2662
+ }
2663
+ | f_optarg opt_args_tail
2664
+ {
2665
+ result = val[0].
2666
+ concat(val[1])
2667
+ }
2668
+ | f_optarg tCOMMA f_arg opt_args_tail
2669
+ {
2670
+ result = val[0].
2671
+ concat(val[2]).
2672
+ concat(val[3])
2673
+ }
2674
+ | f_rest_arg opt_args_tail
2675
+ {
2676
+ result = val[0].
2677
+ concat(val[1])
2678
+ }
2679
+ | f_rest_arg tCOMMA f_arg opt_args_tail
2680
+ {
2681
+ result = val[0].
2682
+ concat(val[2]).
2683
+ concat(val[3])
2684
+ }
2685
+ | args_tail
2686
+ {
2687
+ result = val[0]
2688
+ }
2689
+ | # nothing
2690
+ {
2691
+ result = []
2692
+ }
2693
+
2694
+ args_forward: tBDOT3
2695
+ {
2696
+ result = val[0]
2697
+ }
2698
+
2699
+ f_bad_arg: tCONSTANT
2700
+ {
2701
+ diagnostic :error, :argument_const, nil, val[0]
2702
+ }
2703
+ | tIVAR
2704
+ {
2705
+ diagnostic :error, :argument_ivar, nil, val[0]
2706
+ }
2707
+ | tGVAR
2708
+ {
2709
+ diagnostic :error, :argument_gvar, nil, val[0]
2710
+ }
2711
+ | tCVAR
2712
+ {
2713
+ diagnostic :error, :argument_cvar, nil, val[0]
2714
+ }
2715
+
2716
+ f_norm_arg: f_bad_arg
2717
+ | tIDENTIFIER
2718
+ {
2719
+ @static_env.declare val[0][0]
2720
+
2721
+ @max_numparam_stack.has_ordinary_params!
2722
+
2723
+ result = val[0]
2724
+ }
2725
+
2726
+ f_arg_asgn: f_norm_arg
2727
+ {
2728
+ @current_arg_stack.set(val[0][0])
2729
+ result = val[0]
2730
+ }
2731
+
2732
+ f_arg_item: f_arg_asgn
2733
+ {
2734
+ @current_arg_stack.set(0)
2735
+ result = @builder.arg(val[0])
2736
+ }
2737
+ | tLPAREN f_margs rparen
2738
+ {
2739
+ result = @builder.multi_lhs(val[0], val[1], val[2])
2740
+ }
2741
+
2742
+ f_arg: f_arg_item
2743
+ {
2744
+ result = [ val[0] ]
2745
+ }
2746
+ | f_arg tCOMMA f_arg_item
2747
+ {
2748
+ result = val[0] << val[2]
2749
+ }
2750
+
2751
+ f_label: tLABEL
2752
+ {
2753
+ check_kwarg_name(val[0])
2754
+
2755
+ @static_env.declare val[0][0]
2756
+
2757
+ @max_numparam_stack.has_ordinary_params!
2758
+
2759
+ @current_arg_stack.set(val[0][0])
2760
+
2761
+ result = val[0]
2762
+ }
2763
+
2764
+ f_kw: f_label arg_value
2765
+ {
2766
+ @current_arg_stack.set(nil)
2767
+ result = @builder.kwoptarg(val[0], val[1])
2768
+ }
2769
+ | f_label
2770
+ {
2771
+ @current_arg_stack.set(nil)
2772
+ result = @builder.kwarg(val[0])
2773
+ }
2774
+
2775
+ f_block_kw: f_label primary_value
2776
+ {
2777
+ result = @builder.kwoptarg(val[0], val[1])
2778
+ }
2779
+ | f_label
2780
+ {
2781
+ result = @builder.kwarg(val[0])
2782
+ }
2783
+
2784
+ f_block_kwarg: f_block_kw
2785
+ {
2786
+ result = [ val[0] ]
2787
+ }
2788
+ | f_block_kwarg tCOMMA f_block_kw
2789
+ {
2790
+ result = val[0] << val[2]
2791
+ }
2792
+
2793
+ f_kwarg: f_kw
2794
+ {
2795
+ result = [ val[0] ]
2796
+ }
2797
+ | f_kwarg tCOMMA f_kw
2798
+ {
2799
+ result = val[0] << val[2]
2800
+ }
2801
+
2802
+ kwrest_mark: tPOW | tDSTAR
2803
+
2804
+ f_no_kwarg: kwrest_mark kNIL
2805
+ {
2806
+ result = [ @builder.kwnilarg(val[0], val[1]) ]
2807
+ }
2808
+
2809
+ f_kwrest: kwrest_mark tIDENTIFIER
2810
+ {
2811
+ @static_env.declare val[1][0]
2812
+
2813
+ result = [ @builder.kwrestarg(val[0], val[1]) ]
2814
+ }
2815
+ | kwrest_mark
2816
+ {
2817
+ result = [ @builder.kwrestarg(val[0]) ]
2818
+ }
2819
+
2820
+ f_opt: f_arg_asgn tEQL arg_value
2821
+ {
2822
+ @current_arg_stack.set(0)
2823
+ result = @builder.optarg(val[0], val[1], val[2])
2824
+ }
2825
+
2826
+ f_block_opt: f_arg_asgn tEQL primary_value
2827
+ {
2828
+ @current_arg_stack.set(0)
2829
+ result = @builder.optarg(val[0], val[1], val[2])
2830
+ }
2831
+
2832
+ f_block_optarg: f_block_opt
2833
+ {
2834
+ result = [ val[0] ]
2835
+ }
2836
+ | f_block_optarg tCOMMA f_block_opt
2837
+ {
2838
+ result = val[0] << val[2]
2839
+ }
2840
+
2841
+ f_optarg: f_opt
2842
+ {
2843
+ result = [ val[0] ]
2844
+ }
2845
+ | f_optarg tCOMMA f_opt
2846
+ {
2847
+ result = val[0] << val[2]
2848
+ }
2849
+
2850
+ restarg_mark: tSTAR2 | tSTAR
2851
+
2852
+ f_rest_arg: restarg_mark tIDENTIFIER
2853
+ {
2854
+ @static_env.declare val[1][0]
2855
+
2856
+ result = [ @builder.restarg(val[0], val[1]) ]
2857
+ }
2858
+ | restarg_mark
2859
+ {
2860
+ result = [ @builder.restarg(val[0]) ]
2861
+ }
2862
+
2863
+ blkarg_mark: tAMPER2 | tAMPER
2864
+
2865
+ f_block_arg: blkarg_mark tIDENTIFIER
2866
+ {
2867
+ @static_env.declare val[1][0]
2868
+
2869
+ result = @builder.blockarg(val[0], val[1])
2870
+ }
2871
+
2872
+ opt_f_block_arg: tCOMMA f_block_arg
2873
+ {
2874
+ result = [ val[1] ]
2875
+ }
2876
+ |
2877
+ {
2878
+ result = []
2879
+ }
2880
+
2881
+ singleton: var_ref
2882
+ | tLPAREN2 expr rparen
2883
+ {
2884
+ result = val[1]
2885
+ }
2886
+
2887
+ assoc_list: # nothing
2888
+ {
2889
+ result = []
2890
+ }
2891
+ | assocs trailer
2892
+
2893
+ assocs: assoc
2894
+ {
2895
+ result = [ val[0] ]
2896
+ }
2897
+ | assocs tCOMMA assoc
2898
+ {
2899
+ result = val[0] << val[2]
2900
+ }
2901
+
2902
+ assoc: arg_value tASSOC arg_value
2903
+ {
2904
+ result = @builder.pair(val[0], val[1], val[2])
2905
+ }
2906
+ | tLABEL arg_value
2907
+ {
2908
+ result = @builder.pair_keyword(val[0], val[1])
2909
+ }
2910
+ | tSTRING_BEG string_contents tLABEL_END arg_value
2911
+ {
2912
+ result = @builder.pair_quoted(val[0], val[1], val[2], val[3])
2913
+ }
2914
+ | tDSTAR arg_value
2915
+ {
2916
+ result = @builder.kwsplat(val[0], val[1])
2917
+ }
2918
+
2919
+ operation: tIDENTIFIER | tCONSTANT | tFID
2920
+ operation2: tIDENTIFIER | tCONSTANT | tFID | op
2921
+ operation3: tIDENTIFIER | tFID | op
2922
+ dot_or_colon: call_op | tCOLON2
2923
+ call_op: tDOT
2924
+ {
2925
+ result = [:dot, val[0][1]]
2926
+ }
2927
+ | tANDDOT
2928
+ {
2929
+ result = [:anddot, val[0][1]]
2930
+ }
2931
+ opt_terms: | terms
2932
+ opt_nl: | tNL
2933
+ rparen: opt_nl tRPAREN
2934
+ {
2935
+ result = val[1]
2936
+ }
2937
+ rbracket: opt_nl tRBRACK
2938
+ {
2939
+ result = val[1]
2940
+ }
2941
+ rbrace: opt_nl tRCURLY
2942
+ {
2943
+ result = val[1]
2944
+ }
2945
+ trailer: | tNL | tCOMMA
2946
+
2947
+ term: tSEMI
2948
+ {
2949
+ yyerrok
2950
+ }
2951
+ | tNL
2952
+
2953
+ terms: term
2954
+ | terms tSEMI
2955
+
2956
+ none: # nothing
2957
+ {
2958
+ result = nil
2959
+ }
2960
+ end
2961
+
2962
+ ---- header
2963
+
2964
+ require 'parser'
2965
+
2966
+ ---- inner
2967
+
2968
+ def version
2969
+ 28
2970
+ end
2971
+
2972
+ def default_encoding
2973
+ Encoding::UTF_8
2974
+ end