parser 2.6.0.0 → 2.7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +11 -11
- data/CHANGELOG.md +88 -0
- data/README.md +7 -0
- data/Rakefile +1 -0
- data/doc/AST_FORMAT.md +410 -3
- data/lib/parser/all.rb +1 -0
- data/lib/parser/ast/processor.rb +39 -1
- data/lib/parser/base.rb +23 -1
- data/lib/parser/builders/default.rb +312 -15
- data/lib/parser/context.rb +12 -0
- data/lib/parser/current.rb +15 -6
- data/lib/parser/current_arg_stack.rb +43 -0
- data/lib/parser/lexer/dedenter.rb +48 -49
- data/lib/parser/lexer.rb +11934 -11278
- data/lib/parser/lexer.rl +207 -58
- data/lib/parser/macruby.rb +1509 -1507
- data/lib/parser/macruby.y +1 -1
- data/lib/parser/max_numparam_stack.rb +48 -0
- data/lib/parser/messages.rb +55 -44
- data/lib/parser/meta.rb +9 -1
- data/lib/parser/ruby18.rb +1255 -1248
- data/lib/parser/ruby18.y +1 -1
- data/lib/parser/ruby19.rb +1645 -1638
- data/lib/parser/ruby19.y +1 -1
- data/lib/parser/ruby20.rb +1766 -1767
- data/lib/parser/ruby20.y +1 -1
- data/lib/parser/ruby21.rb +1654 -1656
- data/lib/parser/ruby21.y +1 -1
- data/lib/parser/ruby22.rb +1772 -1767
- data/lib/parser/ruby22.y +1 -1
- data/lib/parser/ruby23.rb +1088 -1092
- data/lib/parser/ruby23.y +1 -1
- data/lib/parser/ruby24.rb +1837 -1843
- data/lib/parser/ruby24.y +1 -1
- data/lib/parser/ruby25.rb +1750 -1755
- data/lib/parser/ruby25.y +2 -2
- data/lib/parser/ruby26.rb +1748 -1752
- data/lib/parser/ruby26.y +2 -2
- data/lib/parser/ruby27.rb +7749 -0
- data/lib/parser/ruby27.y +2923 -0
- data/lib/parser/rubymotion.rb +1492 -1497
- data/lib/parser/rubymotion.y +1 -1
- data/lib/parser/runner/ruby_parse.rb +2 -2
- data/lib/parser/runner.rb +5 -0
- data/lib/parser/source/tree_rewriter.rb +2 -2
- data/lib/parser/static_environment.rb +10 -0
- data/lib/parser/tree_rewriter.rb +2 -1
- data/lib/parser/variables_stack.rb +32 -0
- data/lib/parser/version.rb +1 -1
- data/lib/parser.rb +3 -0
- data/parser.gemspec +3 -2
- data/test/helper.rb +8 -0
- data/test/parse_helper.rb +2 -1
- data/test/test_current.rb +2 -0
- data/test/test_lexer.rb +72 -7
- data/test/test_parser.rb +2218 -8
- data/test/test_runner_parse.rb +35 -0
- metadata +20 -8
data/lib/parser/base.rb
CHANGED
|
@@ -114,6 +114,10 @@ module Parser
|
|
|
114
114
|
attr_reader :static_env
|
|
115
115
|
attr_reader :source_buffer
|
|
116
116
|
attr_reader :context
|
|
117
|
+
attr_reader :max_numparam_stack
|
|
118
|
+
attr_reader :current_arg_stack
|
|
119
|
+
attr_reader :pattern_variables
|
|
120
|
+
attr_reader :pattern_hash_keys
|
|
117
121
|
|
|
118
122
|
##
|
|
119
123
|
# @param [Parser::Builders::Default] builder The AST builder to use.
|
|
@@ -123,13 +127,28 @@ module Parser
|
|
|
123
127
|
|
|
124
128
|
@static_env = StaticEnvironment.new
|
|
125
129
|
|
|
130
|
+
# Stack that holds current parsing context
|
|
131
|
+
@context = Context.new
|
|
132
|
+
|
|
133
|
+
# Maximum numbered parameters stack
|
|
134
|
+
@max_numparam_stack = MaxNumparamStack.new
|
|
135
|
+
|
|
136
|
+
# Current argument names stack
|
|
137
|
+
@current_arg_stack = CurrentArgStack.new
|
|
138
|
+
|
|
139
|
+
# Stack of set of variables used in the current pattern
|
|
140
|
+
@pattern_variables = VariablesStack.new
|
|
141
|
+
|
|
142
|
+
# Stack of set of keys used in the current hash in pattern matchinig
|
|
143
|
+
@pattern_hash_keys = VariablesStack.new
|
|
144
|
+
|
|
126
145
|
@lexer = Lexer.new(version)
|
|
127
146
|
@lexer.diagnostics = @diagnostics
|
|
128
147
|
@lexer.static_env = @static_env
|
|
148
|
+
@lexer.context = @context
|
|
129
149
|
|
|
130
150
|
@builder = builder
|
|
131
151
|
@builder.parser = self
|
|
132
|
-
@context = Context.new
|
|
133
152
|
|
|
134
153
|
# Last emitted token
|
|
135
154
|
@last_token = nil
|
|
@@ -150,6 +169,9 @@ module Parser
|
|
|
150
169
|
@lexer.reset
|
|
151
170
|
@static_env.reset
|
|
152
171
|
@context.reset
|
|
172
|
+
@current_arg_stack.reset
|
|
173
|
+
@pattern_variables.reset
|
|
174
|
+
@pattern_hash_keys.reset
|
|
153
175
|
|
|
154
176
|
self
|
|
155
177
|
end
|
|
@@ -80,6 +80,21 @@ module Parser
|
|
|
80
80
|
attr_accessor :emit_index
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
+
class << self
|
|
84
|
+
##
|
|
85
|
+
# AST compatibility attribute; causes a single non-mlhs
|
|
86
|
+
# block argument to be wrapped in s(:procarg0).
|
|
87
|
+
#
|
|
88
|
+
# If set to false (the default), block arguments `|a|` are emitted as
|
|
89
|
+
# `s(:args, s(:procarg0, :a))`
|
|
90
|
+
#
|
|
91
|
+
# If set to true, block arguments `|a|` are emitted as
|
|
92
|
+
# `s(:args, s(:procarg0, s(:arg, :a))`
|
|
93
|
+
#
|
|
94
|
+
# @return [Boolean]
|
|
95
|
+
attr_accessor :emit_arg_inside_procarg0
|
|
96
|
+
end
|
|
97
|
+
|
|
83
98
|
@emit_index = false
|
|
84
99
|
|
|
85
100
|
class << self
|
|
@@ -90,6 +105,7 @@ module Parser
|
|
|
90
105
|
@emit_procarg0 = true
|
|
91
106
|
@emit_encoding = true
|
|
92
107
|
@emit_index = true
|
|
108
|
+
@emit_arg_inside_procarg0 = true
|
|
93
109
|
end
|
|
94
110
|
end
|
|
95
111
|
|
|
@@ -477,6 +493,11 @@ module Parser
|
|
|
477
493
|
name, = *node
|
|
478
494
|
|
|
479
495
|
if @parser.static_env.declared?(name)
|
|
496
|
+
if name.to_s == parser.current_arg_stack.top
|
|
497
|
+
diagnostic :error, :circular_argument_reference,
|
|
498
|
+
{ :var_name => name.to_s }, node.loc.expression
|
|
499
|
+
end
|
|
500
|
+
|
|
480
501
|
node.updated(:lvar)
|
|
481
502
|
else
|
|
482
503
|
name, = *node
|
|
@@ -535,6 +556,9 @@ module Parser
|
|
|
535
556
|
|
|
536
557
|
when :ident
|
|
537
558
|
name, = *node
|
|
559
|
+
|
|
560
|
+
check_assignment_to_numparam(node)
|
|
561
|
+
|
|
538
562
|
@parser.static_env.declare(name)
|
|
539
563
|
|
|
540
564
|
node.updated(:lvasgn)
|
|
@@ -663,6 +687,14 @@ module Parser
|
|
|
663
687
|
collection_map(begin_t, args, end_t))
|
|
664
688
|
end
|
|
665
689
|
|
|
690
|
+
def numargs(max_numparam)
|
|
691
|
+
n(:numargs, [ max_numparam ], nil)
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
def forward_args(begin_t, dots_t, end_t)
|
|
695
|
+
n(:forward_args, [], collection_map(begin_t, token_map(dots_t), end_t))
|
|
696
|
+
end
|
|
697
|
+
|
|
666
698
|
def arg(name_t)
|
|
667
699
|
n(:arg, [ value(name_t).to_sym ],
|
|
668
700
|
variable_map(name_t))
|
|
@@ -705,6 +737,11 @@ module Parser
|
|
|
705
737
|
end
|
|
706
738
|
end
|
|
707
739
|
|
|
740
|
+
def kwnilarg(dstar_t, nil_t)
|
|
741
|
+
n0(:kwnilarg,
|
|
742
|
+
arg_prefix_map(dstar_t, nil_t))
|
|
743
|
+
end
|
|
744
|
+
|
|
708
745
|
def shadowarg(name_t)
|
|
709
746
|
n(:shadowarg, [ value(name_t).to_sym ],
|
|
710
747
|
variable_map(name_t))
|
|
@@ -717,7 +754,12 @@ module Parser
|
|
|
717
754
|
|
|
718
755
|
def procarg0(arg)
|
|
719
756
|
if self.class.emit_procarg0
|
|
720
|
-
arg.
|
|
757
|
+
if arg.type == :arg && self.class.emit_arg_inside_procarg0
|
|
758
|
+
n(:procarg0, [ arg ],
|
|
759
|
+
Source::Map::Collection.new(nil, nil, arg.location.expression))
|
|
760
|
+
else
|
|
761
|
+
arg.updated(:procarg0)
|
|
762
|
+
end
|
|
721
763
|
else
|
|
722
764
|
arg
|
|
723
765
|
end
|
|
@@ -802,6 +844,10 @@ module Parser
|
|
|
802
844
|
end
|
|
803
845
|
end
|
|
804
846
|
|
|
847
|
+
def forwarded_args(dots_t)
|
|
848
|
+
n(:forwarded_args, [], token_map(dots_t))
|
|
849
|
+
end
|
|
850
|
+
|
|
805
851
|
def call_method(receiver, dot_t, selector_t,
|
|
806
852
|
lparen_t=nil, args=[], rparen_t=nil)
|
|
807
853
|
type = call_type_for_dot(dot_t)
|
|
@@ -831,19 +877,26 @@ module Parser
|
|
|
831
877
|
end
|
|
832
878
|
|
|
833
879
|
last_arg = call_args.last
|
|
834
|
-
if last_arg && last_arg.type == :block_pass
|
|
880
|
+
if last_arg && (last_arg.type == :block_pass || last_arg.type == :forwarded_args)
|
|
835
881
|
diagnostic :error, :block_and_blockarg, nil, last_arg.loc.expression, [loc(begin_t)]
|
|
836
882
|
end
|
|
837
883
|
|
|
884
|
+
if args.type == :numargs
|
|
885
|
+
block_type = :numblock
|
|
886
|
+
args = args.children[0]
|
|
887
|
+
else
|
|
888
|
+
block_type = :block
|
|
889
|
+
end
|
|
890
|
+
|
|
838
891
|
if [:send, :csend, :index, :super, :zsuper, :lambda].include?(method_call.type)
|
|
839
|
-
n(
|
|
892
|
+
n(block_type, [ method_call, args, body ],
|
|
840
893
|
block_map(method_call.loc.expression, begin_t, end_t))
|
|
841
894
|
else
|
|
842
895
|
# Code like "return foo 1 do end" is reduced in a weird sequence.
|
|
843
896
|
# Here, method_call is actually (return).
|
|
844
897
|
actual_send, = *method_call
|
|
845
898
|
block =
|
|
846
|
-
n(
|
|
899
|
+
n(block_type, [ actual_send, args, body ],
|
|
847
900
|
block_map(actual_send.loc.expression, begin_t, end_t))
|
|
848
901
|
|
|
849
902
|
n(method_call.type, [ block ],
|
|
@@ -1155,6 +1208,188 @@ module Parser
|
|
|
1155
1208
|
end
|
|
1156
1209
|
end
|
|
1157
1210
|
|
|
1211
|
+
#
|
|
1212
|
+
# PATTERN MATCHING
|
|
1213
|
+
#
|
|
1214
|
+
|
|
1215
|
+
def case_match(case_t, expr, in_bodies, else_t, else_body, end_t)
|
|
1216
|
+
n(:case_match, [ expr, *(in_bodies << else_body)],
|
|
1217
|
+
condition_map(case_t, expr, nil, nil, else_t, else_body, end_t))
|
|
1218
|
+
end
|
|
1219
|
+
|
|
1220
|
+
def in_match(lhs, in_t, rhs)
|
|
1221
|
+
n(:in_match, [lhs, rhs],
|
|
1222
|
+
binary_op_map(lhs, in_t, rhs))
|
|
1223
|
+
end
|
|
1224
|
+
|
|
1225
|
+
def in_pattern(in_t, pattern, guard, then_t, body)
|
|
1226
|
+
children = [pattern, guard, body]
|
|
1227
|
+
n(:in_pattern, children,
|
|
1228
|
+
keyword_map(in_t, then_t, children.compact, nil))
|
|
1229
|
+
end
|
|
1230
|
+
|
|
1231
|
+
def if_guard(if_t, if_body)
|
|
1232
|
+
n(:if_guard, [if_body], guard_map(if_t, if_body))
|
|
1233
|
+
end
|
|
1234
|
+
|
|
1235
|
+
def unless_guard(unless_t, unless_body)
|
|
1236
|
+
n(:unless_guard, [unless_body], guard_map(unless_t, unless_body))
|
|
1237
|
+
end
|
|
1238
|
+
|
|
1239
|
+
def match_var(name_t)
|
|
1240
|
+
name = value(name_t).to_sym
|
|
1241
|
+
|
|
1242
|
+
check_duplicate_pattern_variable(name, loc(name_t))
|
|
1243
|
+
@parser.static_env.declare(name)
|
|
1244
|
+
|
|
1245
|
+
n(:match_var, [ name ],
|
|
1246
|
+
variable_map(name_t))
|
|
1247
|
+
end
|
|
1248
|
+
|
|
1249
|
+
def match_hash_var(name_t)
|
|
1250
|
+
name = value(name_t).to_sym
|
|
1251
|
+
|
|
1252
|
+
expr_l = loc(name_t)
|
|
1253
|
+
name_l = expr_l.adjust(end_pos: -1)
|
|
1254
|
+
|
|
1255
|
+
check_duplicate_pattern_variable(name, name_l)
|
|
1256
|
+
@parser.static_env.declare(name)
|
|
1257
|
+
|
|
1258
|
+
n(:match_var, [ name ],
|
|
1259
|
+
Source::Map::Variable.new(name_l, expr_l))
|
|
1260
|
+
end
|
|
1261
|
+
|
|
1262
|
+
def match_hash_var_from_str(begin_t, strings, end_t)
|
|
1263
|
+
if strings.length > 1
|
|
1264
|
+
diagnostic :error, :pm_interp_in_var_name, nil, loc(begin_t).join(loc(end_t))
|
|
1265
|
+
end
|
|
1266
|
+
|
|
1267
|
+
string = strings[0]
|
|
1268
|
+
|
|
1269
|
+
case string.type
|
|
1270
|
+
when :str
|
|
1271
|
+
# MRI supports plain strings in hash pattern matching
|
|
1272
|
+
name, = *string
|
|
1273
|
+
name_l = string.loc.expression
|
|
1274
|
+
|
|
1275
|
+
check_lvar_name(name, name_l)
|
|
1276
|
+
check_duplicate_pattern_variable(name, name_l)
|
|
1277
|
+
|
|
1278
|
+
@parser.static_env.declare(name)
|
|
1279
|
+
|
|
1280
|
+
if (begin_l = string.loc.begin)
|
|
1281
|
+
# exclude beginning of the string from the location of the variable
|
|
1282
|
+
name_l = name_l.adjust(begin_pos: begin_l.length)
|
|
1283
|
+
end
|
|
1284
|
+
|
|
1285
|
+
if (end_l = string.loc.end)
|
|
1286
|
+
# exclude end of the string from the location of the variable
|
|
1287
|
+
name_l = name_l.adjust(end_pos: -end_l.length)
|
|
1288
|
+
end
|
|
1289
|
+
|
|
1290
|
+
expr_l = loc(begin_t).join(string.loc.expression).join(loc(end_t))
|
|
1291
|
+
n(:match_var, [ name.to_sym ],
|
|
1292
|
+
Source::Map::Variable.new(name_l, expr_l))
|
|
1293
|
+
when :begin
|
|
1294
|
+
match_hash_var_from_str(begin_t, string.children, end_t)
|
|
1295
|
+
end
|
|
1296
|
+
end
|
|
1297
|
+
|
|
1298
|
+
def match_rest(star_t, name_t = nil)
|
|
1299
|
+
if name_t.nil?
|
|
1300
|
+
n0(:match_rest,
|
|
1301
|
+
unary_op_map(star_t))
|
|
1302
|
+
else
|
|
1303
|
+
name = match_var(name_t)
|
|
1304
|
+
n(:match_rest, [ name ],
|
|
1305
|
+
unary_op_map(star_t, name))
|
|
1306
|
+
end
|
|
1307
|
+
end
|
|
1308
|
+
|
|
1309
|
+
def hash_pattern(lbrace_t, kwargs, rbrace_t)
|
|
1310
|
+
args = check_duplicate_args(kwargs)
|
|
1311
|
+
n(:hash_pattern, args,
|
|
1312
|
+
collection_map(lbrace_t, args, rbrace_t))
|
|
1313
|
+
end
|
|
1314
|
+
|
|
1315
|
+
def array_pattern(lbrack_t, elements, rbrack_t)
|
|
1316
|
+
trailing_comma = false
|
|
1317
|
+
|
|
1318
|
+
elements = elements.map do |element|
|
|
1319
|
+
if element.type == :match_with_trailing_comma
|
|
1320
|
+
trailing_comma = true
|
|
1321
|
+
element.children.first
|
|
1322
|
+
else
|
|
1323
|
+
trailing_comma = false
|
|
1324
|
+
element
|
|
1325
|
+
end
|
|
1326
|
+
end
|
|
1327
|
+
|
|
1328
|
+
node_type = trailing_comma ? :array_pattern_with_tail : :array_pattern
|
|
1329
|
+
n(node_type, elements,
|
|
1330
|
+
collection_map(lbrack_t, elements, rbrack_t))
|
|
1331
|
+
end
|
|
1332
|
+
|
|
1333
|
+
def match_with_trailing_comma(match)
|
|
1334
|
+
n(:match_with_trailing_comma, [ match ], nil)
|
|
1335
|
+
end
|
|
1336
|
+
|
|
1337
|
+
def const_pattern(const, ldelim_t, pattern, rdelim_t)
|
|
1338
|
+
n(:const_pattern, [const, pattern],
|
|
1339
|
+
collection_map(ldelim_t, [pattern], rdelim_t))
|
|
1340
|
+
end
|
|
1341
|
+
|
|
1342
|
+
def pin(pin_t, var)
|
|
1343
|
+
n(:pin, [ var ],
|
|
1344
|
+
send_unary_op_map(pin_t, var))
|
|
1345
|
+
end
|
|
1346
|
+
|
|
1347
|
+
def match_alt(left, pipe_t, right)
|
|
1348
|
+
source_map = binary_op_map(left, pipe_t, right)
|
|
1349
|
+
|
|
1350
|
+
n(:match_alt, [ left, right ],
|
|
1351
|
+
source_map)
|
|
1352
|
+
end
|
|
1353
|
+
|
|
1354
|
+
def match_as(value, assoc_t, as)
|
|
1355
|
+
source_map = binary_op_map(value, assoc_t, as)
|
|
1356
|
+
|
|
1357
|
+
n(:match_as, [ value, as ],
|
|
1358
|
+
source_map)
|
|
1359
|
+
end
|
|
1360
|
+
|
|
1361
|
+
def match_nil_pattern(dstar_t, nil_t)
|
|
1362
|
+
n0(:match_nil_pattern,
|
|
1363
|
+
arg_prefix_map(dstar_t, nil_t))
|
|
1364
|
+
end
|
|
1365
|
+
|
|
1366
|
+
def match_pair(label_type, label, value)
|
|
1367
|
+
if label_type == :label
|
|
1368
|
+
check_duplicate_pattern_key(label[0], label[1])
|
|
1369
|
+
pair_keyword(label, value)
|
|
1370
|
+
else
|
|
1371
|
+
begin_t, parts, end_t = label
|
|
1372
|
+
|
|
1373
|
+
# quoted label like "label": value
|
|
1374
|
+
if (var_name = static_string(parts))
|
|
1375
|
+
loc = loc(begin_t).join(loc(end_t))
|
|
1376
|
+
check_duplicate_pattern_key(var_name, loc)
|
|
1377
|
+
end
|
|
1378
|
+
|
|
1379
|
+
pair_quoted(begin_t, parts, end_t, value)
|
|
1380
|
+
end
|
|
1381
|
+
end
|
|
1382
|
+
|
|
1383
|
+
def match_label(label_type, label)
|
|
1384
|
+
if label_type == :label
|
|
1385
|
+
match_hash_var(label)
|
|
1386
|
+
else
|
|
1387
|
+
# quoted label like "label": value
|
|
1388
|
+
begin_t, strings, end_t = label
|
|
1389
|
+
match_hash_var_from_str(begin_t, strings, end_t)
|
|
1390
|
+
end
|
|
1391
|
+
end
|
|
1392
|
+
|
|
1158
1393
|
private
|
|
1159
1394
|
|
|
1160
1395
|
#
|
|
@@ -1210,18 +1445,18 @@ module Parser
|
|
|
1210
1445
|
case this_arg.type
|
|
1211
1446
|
when :arg, :optarg, :restarg, :blockarg,
|
|
1212
1447
|
:kwarg, :kwoptarg, :kwrestarg,
|
|
1213
|
-
:shadowarg
|
|
1448
|
+
:shadowarg
|
|
1214
1449
|
|
|
1215
|
-
|
|
1450
|
+
check_duplicate_arg(this_arg, map)
|
|
1216
1451
|
|
|
1217
|
-
|
|
1218
|
-
that_name, = *that_arg
|
|
1452
|
+
when :procarg0
|
|
1219
1453
|
|
|
1220
|
-
if
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1454
|
+
if this_arg.children[0].is_a?(Symbol)
|
|
1455
|
+
# s(:procarg0, :a)
|
|
1456
|
+
check_duplicate_arg(this_arg, map)
|
|
1457
|
+
else
|
|
1458
|
+
# s(:procarg0, s(:arg, :a), ...)
|
|
1459
|
+
check_duplicate_args(this_arg.children, map)
|
|
1225
1460
|
end
|
|
1226
1461
|
|
|
1227
1462
|
when :mlhs
|
|
@@ -1230,6 +1465,33 @@ module Parser
|
|
|
1230
1465
|
end
|
|
1231
1466
|
end
|
|
1232
1467
|
|
|
1468
|
+
def check_duplicate_arg(this_arg, map={})
|
|
1469
|
+
this_name, = *this_arg
|
|
1470
|
+
|
|
1471
|
+
that_arg = map[this_name]
|
|
1472
|
+
that_name, = *that_arg
|
|
1473
|
+
|
|
1474
|
+
if that_arg.nil?
|
|
1475
|
+
map[this_name] = this_arg
|
|
1476
|
+
elsif arg_name_collides?(this_name, that_name)
|
|
1477
|
+
diagnostic :error, :duplicate_argument, nil,
|
|
1478
|
+
this_arg.loc.name, [ that_arg.loc.name ]
|
|
1479
|
+
end
|
|
1480
|
+
end
|
|
1481
|
+
|
|
1482
|
+
def check_assignment_to_numparam(node)
|
|
1483
|
+
name = node.children[0].to_s
|
|
1484
|
+
|
|
1485
|
+
assigning_to_numparam =
|
|
1486
|
+
@parser.context.in_dynamic_block? &&
|
|
1487
|
+
name =~ /\A_([1-9])\z/ &&
|
|
1488
|
+
@parser.max_numparam_stack.has_numparams?
|
|
1489
|
+
|
|
1490
|
+
if assigning_to_numparam
|
|
1491
|
+
diagnostic :error, :cant_assign_to_numparam, { :name => name }, node.loc.expression
|
|
1492
|
+
end
|
|
1493
|
+
end
|
|
1494
|
+
|
|
1233
1495
|
def arg_name_collides?(this_name, that_name)
|
|
1234
1496
|
case @parser.version
|
|
1235
1497
|
when 18
|
|
@@ -1245,6 +1507,32 @@ module Parser
|
|
|
1245
1507
|
end
|
|
1246
1508
|
end
|
|
1247
1509
|
|
|
1510
|
+
def check_lvar_name(name, loc)
|
|
1511
|
+
if name =~ /\A[[[:lower:]]|_][[[:alnum:]]_]*\z/
|
|
1512
|
+
# OK
|
|
1513
|
+
else
|
|
1514
|
+
diagnostic :error, :lvar_name, { name: name }, loc
|
|
1515
|
+
end
|
|
1516
|
+
end
|
|
1517
|
+
|
|
1518
|
+
def check_duplicate_pattern_variable(name, loc)
|
|
1519
|
+
return if name.to_s.start_with?('_')
|
|
1520
|
+
|
|
1521
|
+
if @parser.pattern_variables.declared?(name)
|
|
1522
|
+
diagnostic :error, :duplicate_variable_name, { name: name.to_s }, loc
|
|
1523
|
+
end
|
|
1524
|
+
|
|
1525
|
+
@parser.pattern_variables.declare(name)
|
|
1526
|
+
end
|
|
1527
|
+
|
|
1528
|
+
def check_duplicate_pattern_key(name, loc)
|
|
1529
|
+
if @parser.pattern_hash_keys.declared?(name)
|
|
1530
|
+
diagnostic :error, :duplicate_pattern_key, { name: name.to_s }, loc
|
|
1531
|
+
end
|
|
1532
|
+
|
|
1533
|
+
@parser.pattern_hash_keys.declare(name)
|
|
1534
|
+
end
|
|
1535
|
+
|
|
1248
1536
|
#
|
|
1249
1537
|
# SOURCE MAPS
|
|
1250
1538
|
#
|
|
@@ -1389,10 +1677,12 @@ module Parser
|
|
|
1389
1677
|
end
|
|
1390
1678
|
|
|
1391
1679
|
def range_map(start_e, op_t, end_e)
|
|
1392
|
-
if end_e
|
|
1680
|
+
if start_e && end_e
|
|
1393
1681
|
expr_l = join_exprs(start_e, end_e)
|
|
1394
|
-
|
|
1682
|
+
elsif start_e
|
|
1395
1683
|
expr_l = start_e.loc.expression.join(loc(op_t))
|
|
1684
|
+
elsif end_e
|
|
1685
|
+
expr_l = loc(op_t).join(end_e.loc.expression)
|
|
1396
1686
|
end
|
|
1397
1687
|
|
|
1398
1688
|
Source::Map::Operator.new(loc(op_t), expr_l)
|
|
@@ -1591,6 +1881,13 @@ module Parser
|
|
|
1591
1881
|
begin_l.join(end_l))
|
|
1592
1882
|
end
|
|
1593
1883
|
|
|
1884
|
+
def guard_map(keyword_t, guard_body_e)
|
|
1885
|
+
keyword_l = loc(keyword_t)
|
|
1886
|
+
guard_body_l = guard_body_e.loc.expression
|
|
1887
|
+
|
|
1888
|
+
Source::Map::Keyword.new(keyword_l, nil, nil, keyword_l.join(guard_body_l))
|
|
1889
|
+
end
|
|
1890
|
+
|
|
1594
1891
|
#
|
|
1595
1892
|
# HELPERS
|
|
1596
1893
|
#
|
data/lib/parser/context.rb
CHANGED
|
@@ -47,5 +47,17 @@ module Parser
|
|
|
47
47
|
end
|
|
48
48
|
alias module_definition_allowed? class_definition_allowed?
|
|
49
49
|
alias dynamic_const_definition_allowed? class_definition_allowed?
|
|
50
|
+
|
|
51
|
+
def in_block?
|
|
52
|
+
@stack.last == :block
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def in_lambda?
|
|
56
|
+
@stack.last == :lambda
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def in_dynamic_block?
|
|
60
|
+
in_block? || in_lambda?
|
|
61
|
+
end
|
|
50
62
|
end
|
|
51
63
|
end
|
data/lib/parser/current.rb
CHANGED
|
@@ -48,7 +48,7 @@ module Parser
|
|
|
48
48
|
CurrentRuby = Ruby23
|
|
49
49
|
|
|
50
50
|
when /^2\.4\./
|
|
51
|
-
current_version = '2.4.
|
|
51
|
+
current_version = '2.4.9'
|
|
52
52
|
if RUBY_VERSION != current_version
|
|
53
53
|
warn_syntax_deviation 'parser/ruby24', current_version
|
|
54
54
|
end
|
|
@@ -57,7 +57,7 @@ module Parser
|
|
|
57
57
|
CurrentRuby = Ruby24
|
|
58
58
|
|
|
59
59
|
when /^2\.5\./
|
|
60
|
-
current_version = '2.5.
|
|
60
|
+
current_version = '2.5.7'
|
|
61
61
|
if RUBY_VERSION != current_version
|
|
62
62
|
warn_syntax_deviation 'parser/ruby25', current_version
|
|
63
63
|
end
|
|
@@ -66,7 +66,7 @@ module Parser
|
|
|
66
66
|
CurrentRuby = Ruby25
|
|
67
67
|
|
|
68
68
|
when /^2\.6\./
|
|
69
|
-
current_version = '2.6.
|
|
69
|
+
current_version = '2.6.5'
|
|
70
70
|
if RUBY_VERSION != current_version
|
|
71
71
|
warn_syntax_deviation 'parser/ruby26', current_version
|
|
72
72
|
end
|
|
@@ -74,10 +74,19 @@ module Parser
|
|
|
74
74
|
require 'parser/ruby26'
|
|
75
75
|
CurrentRuby = Ruby26
|
|
76
76
|
|
|
77
|
+
when /^2\.7\./
|
|
78
|
+
current_version = '2.7.0'
|
|
79
|
+
if RUBY_VERSION != current_version
|
|
80
|
+
warn_syntax_deviation 'parser/ruby27', current_version
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
require 'parser/ruby27'
|
|
84
|
+
CurrentRuby = Ruby27
|
|
85
|
+
|
|
77
86
|
else # :nocov:
|
|
78
87
|
# Keep this in sync with released Ruby.
|
|
79
|
-
warn_syntax_deviation 'parser/
|
|
80
|
-
require 'parser/
|
|
81
|
-
CurrentRuby =
|
|
88
|
+
warn_syntax_deviation 'parser/ruby27', '2.7.x'
|
|
89
|
+
require 'parser/ruby27'
|
|
90
|
+
CurrentRuby = Ruby27
|
|
82
91
|
end
|
|
83
92
|
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Parser
|
|
4
|
+
# Stack that holds names of current arguments,
|
|
5
|
+
# i.e. while parsing
|
|
6
|
+
# def m1(a = (def m2(b = def m3(c = 1); end); end)); end
|
|
7
|
+
# ^
|
|
8
|
+
# stack is [:a, :b, :c]
|
|
9
|
+
#
|
|
10
|
+
# Emulates `p->cur_arg` in MRI's parse.y
|
|
11
|
+
#
|
|
12
|
+
# @api private
|
|
13
|
+
#
|
|
14
|
+
class CurrentArgStack
|
|
15
|
+
attr_reader :stack
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@stack = []
|
|
19
|
+
freeze
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def push(value)
|
|
23
|
+
@stack << value
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def set(value)
|
|
27
|
+
pop
|
|
28
|
+
push(value)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def pop
|
|
32
|
+
@stack.pop
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def reset
|
|
36
|
+
@stack.clear
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def top
|
|
40
|
+
@stack.last
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|