jruby-prism-parser 0.24.0-java → 1.4.0-java
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/BSDmakefile +58 -0
- data/CHANGELOG.md +269 -1
- data/CONTRIBUTING.md +0 -4
- data/Makefile +25 -18
- data/README.md +57 -6
- data/config.yml +1724 -140
- data/docs/build_system.md +39 -11
- data/docs/configuration.md +4 -0
- data/docs/cruby_compilation.md +1 -1
- data/docs/fuzzing.md +1 -1
- data/docs/parser_translation.md +14 -9
- data/docs/parsing_rules.md +4 -1
- data/docs/releasing.md +8 -10
- data/docs/relocation.md +34 -0
- data/docs/ripper_translation.md +72 -0
- data/docs/ruby_api.md +2 -1
- data/docs/serialization.md +29 -5
- data/ext/prism/api_node.c +3395 -1999
- data/ext/prism/api_pack.c +9 -0
- data/ext/prism/extconf.rb +55 -34
- data/ext/prism/extension.c +597 -346
- data/ext/prism/extension.h +6 -5
- data/include/prism/ast.h +2612 -455
- data/include/prism/defines.h +160 -2
- data/include/prism/diagnostic.h +188 -76
- data/include/prism/encoding.h +22 -4
- data/include/prism/node.h +89 -17
- data/include/prism/options.h +224 -12
- data/include/prism/pack.h +11 -0
- data/include/prism/parser.h +267 -66
- data/include/prism/prettyprint.h +8 -0
- data/include/prism/regexp.h +18 -8
- data/include/prism/static_literals.h +121 -0
- data/include/prism/util/pm_buffer.h +75 -2
- data/include/prism/util/pm_char.h +1 -2
- data/include/prism/util/pm_constant_pool.h +18 -9
- data/include/prism/util/pm_integer.h +126 -0
- data/include/prism/util/pm_list.h +1 -1
- data/include/prism/util/pm_newline_list.h +19 -0
- data/include/prism/util/pm_string.h +48 -8
- data/include/prism/version.h +3 -3
- data/include/prism.h +99 -5
- data/jruby-prism.jar +0 -0
- data/lib/prism/compiler.rb +11 -1
- data/lib/prism/desugar_compiler.rb +113 -74
- data/lib/prism/dispatcher.rb +45 -1
- data/lib/prism/dot_visitor.rb +201 -77
- data/lib/prism/dsl.rb +673 -461
- data/lib/prism/ffi.rb +233 -45
- data/lib/prism/inspect_visitor.rb +2389 -0
- data/lib/prism/lex_compat.rb +35 -16
- data/lib/prism/mutation_compiler.rb +24 -8
- data/lib/prism/node.rb +7731 -8460
- data/lib/prism/node_ext.rb +328 -32
- data/lib/prism/pack.rb +4 -0
- data/lib/prism/parse_result/comments.rb +34 -24
- data/lib/prism/parse_result/errors.rb +65 -0
- data/lib/prism/parse_result/newlines.rb +102 -12
- data/lib/prism/parse_result.rb +448 -44
- data/lib/prism/pattern.rb +28 -10
- data/lib/prism/polyfill/append_as_bytes.rb +15 -0
- data/lib/prism/polyfill/byteindex.rb +13 -0
- data/lib/prism/polyfill/unpack1.rb +14 -0
- data/lib/prism/reflection.rb +413 -0
- data/lib/prism/relocation.rb +504 -0
- data/lib/prism/serialize.rb +1940 -1198
- data/lib/prism/string_query.rb +30 -0
- data/lib/prism/translation/parser/builder.rb +61 -0
- data/lib/prism/translation/parser/compiler.rb +569 -195
- data/lib/prism/translation/parser/lexer.rb +516 -39
- data/lib/prism/translation/parser.rb +177 -12
- data/lib/prism/translation/parser33.rb +1 -1
- data/lib/prism/translation/parser34.rb +1 -1
- data/lib/prism/translation/parser35.rb +12 -0
- data/lib/prism/translation/ripper/sexp.rb +125 -0
- data/lib/prism/translation/ripper/shim.rb +5 -0
- data/lib/prism/translation/ripper.rb +3224 -462
- data/lib/prism/translation/ruby_parser.rb +194 -69
- data/lib/prism/translation.rb +4 -1
- data/lib/prism/version.rb +1 -1
- data/lib/prism/visitor.rb +13 -0
- data/lib/prism.rb +17 -27
- data/prism.gemspec +57 -17
- data/rbi/prism/compiler.rbi +12 -0
- data/rbi/prism/dsl.rbi +524 -0
- data/rbi/prism/inspect_visitor.rbi +12 -0
- data/rbi/prism/node.rbi +8722 -0
- data/rbi/prism/node_ext.rbi +107 -0
- data/rbi/prism/parse_result.rbi +404 -0
- data/rbi/prism/reflection.rbi +58 -0
- data/rbi/prism/string_query.rbi +12 -0
- data/rbi/prism/translation/parser.rbi +11 -0
- data/rbi/prism/translation/parser33.rbi +6 -0
- data/rbi/prism/translation/parser34.rbi +6 -0
- data/rbi/prism/translation/parser35.rbi +6 -0
- data/rbi/prism/translation/ripper.rbi +15 -0
- data/rbi/prism/visitor.rbi +473 -0
- data/rbi/prism.rbi +44 -7745
- data/sig/prism/compiler.rbs +9 -0
- data/sig/prism/dispatcher.rbs +16 -0
- data/sig/prism/dot_visitor.rbs +6 -0
- data/sig/prism/dsl.rbs +351 -0
- data/sig/prism/inspect_visitor.rbs +22 -0
- data/sig/prism/lex_compat.rbs +10 -0
- data/sig/prism/mutation_compiler.rbs +159 -0
- data/sig/prism/node.rbs +3614 -0
- data/sig/prism/node_ext.rbs +82 -0
- data/sig/prism/pack.rbs +43 -0
- data/sig/prism/parse_result.rbs +192 -0
- data/sig/prism/pattern.rbs +13 -0
- data/sig/prism/reflection.rbs +50 -0
- data/sig/prism/relocation.rbs +185 -0
- data/sig/prism/serialize.rbs +8 -0
- data/sig/prism/string_query.rbs +11 -0
- data/sig/prism/visitor.rbs +169 -0
- data/sig/prism.rbs +248 -4767
- data/src/diagnostic.c +672 -230
- data/src/encoding.c +211 -108
- data/src/node.c +7541 -1653
- data/src/options.c +135 -20
- data/src/pack.c +33 -17
- data/src/prettyprint.c +1543 -1485
- data/src/prism.c +7813 -3050
- data/src/regexp.c +225 -73
- data/src/serialize.c +101 -77
- data/src/static_literals.c +617 -0
- data/src/token_type.c +14 -13
- data/src/util/pm_buffer.c +187 -20
- data/src/util/pm_char.c +5 -5
- data/src/util/pm_constant_pool.c +39 -19
- data/src/util/pm_integer.c +670 -0
- data/src/util/pm_list.c +1 -1
- data/src/util/pm_newline_list.c +43 -5
- data/src/util/pm_string.c +213 -33
- data/src/util/pm_strncasecmp.c +13 -1
- data/src/util/pm_strpbrk.c +32 -6
- metadata +55 -19
- data/docs/ripper.md +0 -36
- data/include/prism/util/pm_state_stack.h +0 -42
- data/include/prism/util/pm_string_list.h +0 -44
- data/lib/prism/debug.rb +0 -206
- data/lib/prism/node_inspector.rb +0 -68
- data/lib/prism/translation/parser/rubocop.rb +0 -45
- data/rbi/prism_static.rbi +0 -207
- data/sig/prism_static.rbs +0 -201
- data/src/util/pm_state_stack.c +0 -25
- data/src/util/pm_string_list.c +0 -28
@@ -1,12 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
begin
|
4
|
+
require "ruby_parser"
|
5
|
+
rescue LoadError
|
6
|
+
warn(%q{Error: Unable to load ruby_parser. Add `gem "ruby_parser"` to your Gemfile.})
|
7
|
+
exit(1)
|
8
|
+
end
|
4
9
|
|
5
10
|
module Prism
|
6
11
|
module Translation
|
7
12
|
# This module is the entry-point for converting a prism syntax tree into the
|
8
13
|
# seattlerb/ruby_parser gem's syntax tree.
|
9
|
-
|
14
|
+
class RubyParser
|
10
15
|
# A prism visitor that builds Sexp objects.
|
11
16
|
class Compiler < ::Prism::Compiler
|
12
17
|
# This is the name of the file that we are compiling. We set it on every
|
@@ -50,7 +55,19 @@ module Prism
|
|
50
55
|
# a and b
|
51
56
|
# ^^^^^^^
|
52
57
|
def visit_and_node(node)
|
53
|
-
|
58
|
+
left = visit(node.left)
|
59
|
+
|
60
|
+
if left[0] == :and
|
61
|
+
# ruby_parser has the and keyword as right-associative as opposed to
|
62
|
+
# prism which has it as left-associative. We reverse that
|
63
|
+
# associativity here.
|
64
|
+
nest = left
|
65
|
+
nest = nest[2] while nest[2][0] == :and
|
66
|
+
nest[2] = s(node, :and, nest[2], visit(node.right))
|
67
|
+
left
|
68
|
+
else
|
69
|
+
s(node, :and, left, visit(node.right))
|
70
|
+
end
|
54
71
|
end
|
55
72
|
|
56
73
|
# []
|
@@ -130,7 +147,7 @@ module Prism
|
|
130
147
|
end
|
131
148
|
|
132
149
|
current = node.rescue_clause
|
133
|
-
until (current = current.
|
150
|
+
until (current = current.subsequent).nil?
|
134
151
|
result << visit(current)
|
135
152
|
end
|
136
153
|
end
|
@@ -192,19 +209,19 @@ module Prism
|
|
192
209
|
|
193
210
|
if node.opening == "("
|
194
211
|
result.line = node.opening_loc.start_line
|
195
|
-
result.
|
212
|
+
result.line_max = node.closing_loc.end_line
|
196
213
|
shadow_loc = false
|
197
214
|
end
|
198
215
|
|
199
216
|
if node.locals.any?
|
200
217
|
shadow = s(node, :shadow).concat(visit_all(node.locals))
|
201
218
|
shadow.line = node.locals.first.location.start_line
|
202
|
-
shadow.
|
219
|
+
shadow.line_max = node.locals.last.location.end_line
|
203
220
|
result << shadow
|
204
221
|
|
205
222
|
if shadow_loc
|
206
223
|
result.line = shadow.line
|
207
|
-
result.
|
224
|
+
result.line_max = shadow.line_max
|
208
225
|
end
|
209
226
|
end
|
210
227
|
|
@@ -246,6 +263,11 @@ module Prism
|
|
246
263
|
when RegularExpressionNode, InterpolatedRegularExpressionNode
|
247
264
|
return s(node, :match2, visit(node.receiver), visit(node.arguments.arguments.first))
|
248
265
|
end
|
266
|
+
|
267
|
+
case node.arguments.arguments.first
|
268
|
+
when RegularExpressionNode, InterpolatedRegularExpressionNode
|
269
|
+
return s(node, :match3, visit(node.arguments.arguments.first), visit(node.receiver))
|
270
|
+
end
|
249
271
|
end
|
250
272
|
end
|
251
273
|
|
@@ -271,9 +293,9 @@ module Prism
|
|
271
293
|
# ^^^^^^^^^^^^^^^
|
272
294
|
def visit_call_operator_write_node(node)
|
273
295
|
if op_asgn?(node)
|
274
|
-
s(node, op_asgn_type(node, :op_asgn), visit(node.receiver), visit_write_value(node.value), node.read_name, node.
|
296
|
+
s(node, op_asgn_type(node, :op_asgn), visit(node.receiver), visit_write_value(node.value), node.read_name, node.binary_operator)
|
275
297
|
else
|
276
|
-
s(node, op_asgn_type(node, :op_asgn2), visit(node.receiver), node.write_name, node.
|
298
|
+
s(node, op_asgn_type(node, :op_asgn2), visit(node.receiver), node.write_name, node.binary_operator, visit_write_value(node.value))
|
277
299
|
end
|
278
300
|
end
|
279
301
|
|
@@ -325,13 +347,13 @@ module Prism
|
|
325
347
|
# case foo; when bar; end
|
326
348
|
# ^^^^^^^^^^^^^^^^^^^^^^^
|
327
349
|
def visit_case_node(node)
|
328
|
-
s(node, :case, visit(node.predicate)).concat(visit_all(node.conditions)) << visit(node.
|
350
|
+
s(node, :case, visit(node.predicate)).concat(visit_all(node.conditions)) << visit(node.else_clause)
|
329
351
|
end
|
330
352
|
|
331
353
|
# case foo; in bar; end
|
332
354
|
# ^^^^^^^^^^^^^^^^^^^^^
|
333
355
|
def visit_case_match_node(node)
|
334
|
-
s(node, :case, visit(node.predicate)).concat(visit_all(node.conditions)) << visit(node.
|
356
|
+
s(node, :case, visit(node.predicate)).concat(visit_all(node.conditions)) << visit(node.else_clause)
|
335
357
|
end
|
336
358
|
|
337
359
|
# class Foo; end
|
@@ -372,7 +394,7 @@ module Prism
|
|
372
394
|
# @@foo += bar
|
373
395
|
# ^^^^^^^^^^^^
|
374
396
|
def visit_class_variable_operator_write_node(node)
|
375
|
-
s(node, class_variable_write_type, node.name, s(node, :call, s(node, :cvar, node.name), node.
|
397
|
+
s(node, class_variable_write_type, node.name, s(node, :call, s(node, :cvar, node.name), node.binary_operator, visit_write_value(node.value)))
|
376
398
|
end
|
377
399
|
|
378
400
|
# @@foo &&= bar
|
@@ -417,7 +439,7 @@ module Prism
|
|
417
439
|
# Foo += bar
|
418
440
|
# ^^^^^^^^^^^
|
419
441
|
def visit_constant_operator_write_node(node)
|
420
|
-
s(node, :cdecl, node.name, s(node, :call, s(node, :const, node.name), node.
|
442
|
+
s(node, :cdecl, node.name, s(node, :call, s(node, :const, node.name), node.binary_operator, visit_write_value(node.value)))
|
421
443
|
end
|
422
444
|
|
423
445
|
# Foo &&= bar
|
@@ -442,9 +464,9 @@ module Prism
|
|
442
464
|
# ^^^^^^^^
|
443
465
|
def visit_constant_path_node(node)
|
444
466
|
if node.parent.nil?
|
445
|
-
s(node, :colon3, node.
|
467
|
+
s(node, :colon3, node.name)
|
446
468
|
else
|
447
|
-
s(node, :colon2, visit(node.parent), node.
|
469
|
+
s(node, :colon2, visit(node.parent), node.name)
|
448
470
|
end
|
449
471
|
end
|
450
472
|
|
@@ -460,7 +482,7 @@ module Prism
|
|
460
482
|
# Foo::Bar += baz
|
461
483
|
# ^^^^^^^^^^^^^^^
|
462
484
|
def visit_constant_path_operator_write_node(node)
|
463
|
-
s(node, :op_asgn, visit(node.target), node.
|
485
|
+
s(node, :op_asgn, visit(node.target), node.binary_operator, visit_write_value(node.value))
|
464
486
|
end
|
465
487
|
|
466
488
|
# Foo::Bar &&= baz
|
@@ -480,9 +502,9 @@ module Prism
|
|
480
502
|
def visit_constant_path_target_node(node)
|
481
503
|
inner =
|
482
504
|
if node.parent.nil?
|
483
|
-
s(node, :colon3, node.
|
505
|
+
s(node, :colon3, node.name)
|
484
506
|
else
|
485
|
-
s(node, :colon2, visit(node.parent), node.
|
507
|
+
s(node, :colon2, visit(node.parent), node.name)
|
486
508
|
end
|
487
509
|
|
488
510
|
s(node, :const, inner)
|
@@ -627,7 +649,7 @@ module Prism
|
|
627
649
|
# $foo += bar
|
628
650
|
# ^^^^^^^^^^^
|
629
651
|
def visit_global_variable_operator_write_node(node)
|
630
|
-
s(node, :gasgn, node.name, s(node, :call, s(node, :gvar, node.name), node.
|
652
|
+
s(node, :gasgn, node.name, s(node, :call, s(node, :gvar, node.name), node.binary_operator, visit(node.value)))
|
631
653
|
end
|
632
654
|
|
633
655
|
# $foo &&= bar
|
@@ -678,7 +700,7 @@ module Prism
|
|
678
700
|
# foo ? bar : baz
|
679
701
|
# ^^^^^^^^^^^^^^^
|
680
702
|
def visit_if_node(node)
|
681
|
-
s(node, :if, visit(node.predicate), visit(node.statements), visit(node.
|
703
|
+
s(node, :if, visit(node.predicate), visit(node.statements), visit(node.subsequent))
|
682
704
|
end
|
683
705
|
|
684
706
|
# 1i
|
@@ -719,7 +741,7 @@ module Prism
|
|
719
741
|
arglist << visit(node.block) if !node.block.nil?
|
720
742
|
end
|
721
743
|
|
722
|
-
s(node, :op_asgn1, visit(node.receiver), arglist, node.
|
744
|
+
s(node, :op_asgn1, visit(node.receiver), arglist, node.binary_operator, visit_write_value(node.value))
|
723
745
|
end
|
724
746
|
|
725
747
|
# foo[bar] &&= baz
|
@@ -775,7 +797,7 @@ module Prism
|
|
775
797
|
# @foo += bar
|
776
798
|
# ^^^^^^^^^^^
|
777
799
|
def visit_instance_variable_operator_write_node(node)
|
778
|
-
s(node, :iasgn, node.name, s(node, :call, s(node, :ivar, node.name), node.
|
800
|
+
s(node, :iasgn, node.name, s(node, :call, s(node, :ivar, node.name), node.binary_operator, visit_write_value(node.value)))
|
779
801
|
end
|
780
802
|
|
781
803
|
# @foo &&= bar
|
@@ -805,17 +827,29 @@ module Prism
|
|
805
827
|
# if /foo #{bar}/ then end
|
806
828
|
# ^^^^^^^^^^^^
|
807
829
|
def visit_interpolated_match_last_line_node(node)
|
808
|
-
|
830
|
+
parts = visit_interpolated_parts(node.parts)
|
831
|
+
regexp =
|
832
|
+
if parts.length == 1
|
833
|
+
s(node, :lit, Regexp.new(parts.first, node.options))
|
834
|
+
else
|
835
|
+
s(node, :dregx).concat(parts).tap do |result|
|
836
|
+
options = node.options
|
837
|
+
result << options if options != 0
|
838
|
+
end
|
839
|
+
end
|
840
|
+
|
841
|
+
s(node, :match, regexp)
|
809
842
|
end
|
810
843
|
|
811
844
|
# /foo #{bar}/
|
812
845
|
# ^^^^^^^^^^^^
|
813
846
|
def visit_interpolated_regular_expression_node(node)
|
814
|
-
|
815
|
-
|
816
|
-
|
847
|
+
parts = visit_interpolated_parts(node.parts)
|
848
|
+
|
849
|
+
if parts.length == 1
|
850
|
+
s(node, :lit, Regexp.new(parts.first, node.options))
|
817
851
|
else
|
818
|
-
s(node, :dregx).concat(
|
852
|
+
s(node, :dregx).concat(parts).tap do |result|
|
819
853
|
options = node.options
|
820
854
|
result << options if options != 0
|
821
855
|
end
|
@@ -825,47 +859,102 @@ module Prism
|
|
825
859
|
# "foo #{bar}"
|
826
860
|
# ^^^^^^^^^^^^
|
827
861
|
def visit_interpolated_string_node(node)
|
828
|
-
|
829
|
-
|
830
|
-
unescaped = node.parts.map { |part| part.is_a?(StringNode) ? part.unescaped : part.statements.body.first.unescaped }.join
|
831
|
-
s(node, :str, unescaped)
|
832
|
-
else
|
833
|
-
s(node, :dstr).concat(visit_interpolated_parts(node.parts))
|
834
|
-
end
|
862
|
+
parts = visit_interpolated_parts(node.parts)
|
863
|
+
parts.length == 1 ? s(node, :str, parts.first) : s(node, :dstr).concat(parts)
|
835
864
|
end
|
836
865
|
|
837
866
|
# :"foo #{bar}"
|
838
867
|
# ^^^^^^^^^^^^^
|
839
868
|
def visit_interpolated_symbol_node(node)
|
840
|
-
|
841
|
-
|
842
|
-
s(node, :lit, unescaped.to_sym)
|
843
|
-
else
|
844
|
-
s(node, :dsym).concat(visit_interpolated_parts(node.parts))
|
845
|
-
end
|
869
|
+
parts = visit_interpolated_parts(node.parts)
|
870
|
+
parts.length == 1 ? s(node, :lit, parts.first.to_sym) : s(node, :dsym).concat(parts)
|
846
871
|
end
|
847
872
|
|
848
873
|
# `foo #{bar}`
|
849
874
|
# ^^^^^^^^^^^^
|
850
875
|
def visit_interpolated_x_string_node(node)
|
851
|
-
|
852
|
-
|
876
|
+
source = node.heredoc? ? node.parts.first : node
|
877
|
+
parts = visit_interpolated_parts(node.parts)
|
878
|
+
parts.length == 1 ? s(source, :xstr, parts.first) : s(source, :dxstr).concat(parts)
|
853
879
|
end
|
854
880
|
|
855
881
|
# Visit the interpolated content of the string-like node.
|
856
882
|
private def visit_interpolated_parts(parts)
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
883
|
+
visited = []
|
884
|
+
|
885
|
+
parts.each do |part|
|
886
|
+
result = visit(part)
|
887
|
+
|
888
|
+
if result[0] == :evstr && result[1]
|
889
|
+
if result[1][0] == :str
|
890
|
+
visited << result[1]
|
891
|
+
elsif result[1][0] == :dstr
|
892
|
+
visited.concat(result[1][1..-1])
|
861
893
|
else
|
862
|
-
|
863
|
-
results << visit(part)
|
894
|
+
visited << result
|
864
895
|
end
|
896
|
+
visited << :space
|
897
|
+
elsif result[0] == :dstr
|
898
|
+
if !visited.empty? && part.parts[0].is_a?(StringNode)
|
899
|
+
# If we are in the middle of an implicitly concatenated string,
|
900
|
+
# we should not have a bare string as the first part. In this
|
901
|
+
# case we need to visit just that first part and then we can
|
902
|
+
# push the rest of the parts onto the visited array.
|
903
|
+
result[1] = visit(part.parts[0])
|
904
|
+
end
|
905
|
+
visited.concat(result[1..-1])
|
865
906
|
else
|
866
|
-
|
907
|
+
visited << result
|
908
|
+
end
|
909
|
+
end
|
910
|
+
|
911
|
+
state = :beginning #: :beginning | :string_content | :interpolated_content
|
912
|
+
results = []
|
913
|
+
|
914
|
+
visited.each_with_index do |result, index|
|
915
|
+
case state
|
916
|
+
when :beginning
|
917
|
+
if result.is_a?(String)
|
918
|
+
results << result
|
919
|
+
state = :string_content
|
920
|
+
elsif result.is_a?(Array) && result[0] == :str
|
921
|
+
results << result[1]
|
922
|
+
state = :string_content
|
923
|
+
else
|
924
|
+
results << ""
|
925
|
+
results << result
|
926
|
+
state = :interpolated_content
|
927
|
+
end
|
928
|
+
when :string_content
|
929
|
+
if result == :space
|
930
|
+
# continue
|
931
|
+
elsif result.is_a?(String)
|
932
|
+
results[0] = "#{results[0]}#{result}"
|
933
|
+
elsif result.is_a?(Array) && result[0] == :str
|
934
|
+
results[0] = "#{results[0]}#{result[1]}"
|
935
|
+
else
|
936
|
+
results << result
|
937
|
+
state = :interpolated_content
|
938
|
+
end
|
939
|
+
when :interpolated_content
|
940
|
+
if result == :space
|
941
|
+
# continue
|
942
|
+
elsif visited[index - 1] != :space && result.is_a?(Array) && result[0] == :str && results[-1][0] == :str && (results[-1].line_max == result.line)
|
943
|
+
results[-1][1] = "#{results[-1][1]}#{result[1]}"
|
944
|
+
results[-1].line_max = result.line_max
|
945
|
+
else
|
946
|
+
results << result
|
947
|
+
end
|
867
948
|
end
|
868
949
|
end
|
950
|
+
|
951
|
+
results
|
952
|
+
end
|
953
|
+
|
954
|
+
# -> { it }
|
955
|
+
# ^^
|
956
|
+
def visit_it_local_variable_read_node(node)
|
957
|
+
s(node, :call, nil, :it)
|
869
958
|
end
|
870
959
|
|
871
960
|
# foo(bar: baz)
|
@@ -922,7 +1011,7 @@ module Prism
|
|
922
1011
|
# foo += bar
|
923
1012
|
# ^^^^^^^^^^
|
924
1013
|
def visit_local_variable_operator_write_node(node)
|
925
|
-
s(node, :lasgn, node.name, s(node, :call, s(node, :lvar, node.name), node.
|
1014
|
+
s(node, :lasgn, node.name, s(node, :call, s(node, :lvar, node.name), node.binary_operator, visit_write_value(node.value)))
|
926
1015
|
end
|
927
1016
|
|
928
1017
|
# foo &&= bar
|
@@ -1080,7 +1169,19 @@ module Prism
|
|
1080
1169
|
# a or b
|
1081
1170
|
# ^^^^^^
|
1082
1171
|
def visit_or_node(node)
|
1083
|
-
|
1172
|
+
left = visit(node.left)
|
1173
|
+
|
1174
|
+
if left[0] == :or
|
1175
|
+
# ruby_parser has the or keyword as right-associative as opposed to
|
1176
|
+
# prism which has it as left-associative. We reverse that
|
1177
|
+
# associativity here.
|
1178
|
+
nest = left
|
1179
|
+
nest = nest[2] while nest[2][0] == :or
|
1180
|
+
nest[2] = s(node, :or, nest[2], visit(node.right))
|
1181
|
+
left
|
1182
|
+
else
|
1183
|
+
s(node, :or, left, visit(node.right))
|
1184
|
+
end
|
1084
1185
|
end
|
1085
1186
|
|
1086
1187
|
# def foo(bar, *baz); end
|
@@ -1274,6 +1375,11 @@ module Prism
|
|
1274
1375
|
s(node, :self)
|
1275
1376
|
end
|
1276
1377
|
|
1378
|
+
# A shareable constant.
|
1379
|
+
def visit_shareable_constant_node(node)
|
1380
|
+
visit(node.write)
|
1381
|
+
end
|
1382
|
+
|
1277
1383
|
# class << self; end
|
1278
1384
|
# ^^^^^^^^^^^^^^^^^^
|
1279
1385
|
def visit_singleton_class_node(node)
|
@@ -1292,7 +1398,7 @@ module Prism
|
|
1292
1398
|
# __FILE__
|
1293
1399
|
# ^^^^^^^^
|
1294
1400
|
def visit_source_file_node(node)
|
1295
|
-
s(node, :str,
|
1401
|
+
s(node, :str, node.filepath)
|
1296
1402
|
end
|
1297
1403
|
|
1298
1404
|
# __LINE__
|
@@ -1331,7 +1437,14 @@ module Prism
|
|
1331
1437
|
# "foo"
|
1332
1438
|
# ^^^^^
|
1333
1439
|
def visit_string_node(node)
|
1334
|
-
|
1440
|
+
unescaped = node.unescaped
|
1441
|
+
|
1442
|
+
if node.forced_binary_encoding?
|
1443
|
+
unescaped = unescaped.dup
|
1444
|
+
unescaped.force_encoding(Encoding::BINARY)
|
1445
|
+
end
|
1446
|
+
|
1447
|
+
s(node, :str, unescaped)
|
1335
1448
|
end
|
1336
1449
|
|
1337
1450
|
# super(foo)
|
@@ -1373,7 +1486,7 @@ module Prism
|
|
1373
1486
|
# bar unless foo
|
1374
1487
|
# ^^^^^^^^^^^^^^
|
1375
1488
|
def visit_unless_node(node)
|
1376
|
-
s(node, :if, visit(node.predicate), visit(node.
|
1489
|
+
s(node, :if, visit(node.predicate), visit(node.else_clause), visit(node.statements))
|
1377
1490
|
end
|
1378
1491
|
|
1379
1492
|
# until foo; bar end
|
@@ -1407,7 +1520,7 @@ module Prism
|
|
1407
1520
|
|
1408
1521
|
if node.heredoc?
|
1409
1522
|
result.line = node.content_loc.start_line
|
1410
|
-
result.
|
1523
|
+
result.line_max = node.content_loc.end_line
|
1411
1524
|
end
|
1412
1525
|
|
1413
1526
|
result
|
@@ -1434,7 +1547,7 @@ module Prism
|
|
1434
1547
|
result = Sexp.new(*arguments)
|
1435
1548
|
result.file = file
|
1436
1549
|
result.line = node.location.start_line
|
1437
|
-
result.
|
1550
|
+
result.line_max = node.location.end_line
|
1438
1551
|
result
|
1439
1552
|
end
|
1440
1553
|
|
@@ -1446,7 +1559,7 @@ module Prism
|
|
1446
1559
|
else
|
1447
1560
|
parameters =
|
1448
1561
|
case block.parameters
|
1449
|
-
when nil, NumberedParametersNode
|
1562
|
+
when nil, ItParametersNode, NumberedParametersNode
|
1450
1563
|
0
|
1451
1564
|
else
|
1452
1565
|
visit(block.parameters)
|
@@ -1490,31 +1603,43 @@ module Prism
|
|
1490
1603
|
|
1491
1604
|
private_constant :Compiler
|
1492
1605
|
|
1606
|
+
# Parse the given source and translate it into the seattlerb/ruby_parser
|
1607
|
+
# gem's Sexp format.
|
1608
|
+
def parse(source, filepath = "(string)")
|
1609
|
+
translate(Prism.parse(source, filepath: filepath, partial_script: true), filepath)
|
1610
|
+
end
|
1611
|
+
|
1612
|
+
# Parse the given file and translate it into the seattlerb/ruby_parser
|
1613
|
+
# gem's Sexp format.
|
1614
|
+
def parse_file(filepath)
|
1615
|
+
translate(Prism.parse_file(filepath, partial_script: true), filepath)
|
1616
|
+
end
|
1617
|
+
|
1493
1618
|
class << self
|
1494
1619
|
# Parse the given source and translate it into the seattlerb/ruby_parser
|
1495
1620
|
# gem's Sexp format.
|
1496
1621
|
def parse(source, filepath = "(string)")
|
1497
|
-
|
1622
|
+
new.parse(source, filepath)
|
1498
1623
|
end
|
1499
1624
|
|
1500
1625
|
# Parse the given file and translate it into the seattlerb/ruby_parser
|
1501
1626
|
# gem's Sexp format.
|
1502
1627
|
def parse_file(filepath)
|
1503
|
-
|
1628
|
+
new.parse_file(filepath)
|
1504
1629
|
end
|
1630
|
+
end
|
1505
1631
|
|
1506
|
-
|
1507
|
-
|
1508
|
-
# Translate the given parse result and filepath into the
|
1509
|
-
# seattlerb/ruby_parser gem's Sexp format.
|
1510
|
-
def translate(result, filepath)
|
1511
|
-
if result.failure?
|
1512
|
-
error = result.errors.first
|
1513
|
-
raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
|
1514
|
-
end
|
1632
|
+
private
|
1515
1633
|
|
1516
|
-
|
1634
|
+
# Translate the given parse result and filepath into the
|
1635
|
+
# seattlerb/ruby_parser gem's Sexp format.
|
1636
|
+
def translate(result, filepath)
|
1637
|
+
if result.failure?
|
1638
|
+
error = result.errors.first
|
1639
|
+
raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}"
|
1517
1640
|
end
|
1641
|
+
|
1642
|
+
result.value.accept(Compiler.new(filepath))
|
1518
1643
|
end
|
1519
1644
|
end
|
1520
1645
|
end
|
data/lib/prism/translation.rb
CHANGED
@@ -3,8 +3,11 @@
|
|
3
3
|
module Prism
|
4
4
|
# This module is responsible for converting the prism syntax tree into other
|
5
5
|
# syntax trees.
|
6
|
-
module Translation
|
6
|
+
module Translation # steep:ignore
|
7
7
|
autoload :Parser, "prism/translation/parser"
|
8
|
+
autoload :Parser33, "prism/translation/parser33"
|
9
|
+
autoload :Parser34, "prism/translation/parser34"
|
10
|
+
autoload :Parser35, "prism/translation/parser35"
|
8
11
|
autoload :Ripper, "prism/translation/ripper"
|
9
12
|
autoload :RubyParser, "prism/translation/ruby_parser"
|
10
13
|
end
|
data/lib/prism/version.rb
CHANGED
data/lib/prism/visitor.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
=begin
|
3
4
|
This file is generated by the templates/template.rb script and should not be
|
4
5
|
modified manually. See templates/lib/prism/visitor.rb.erb
|
@@ -14,16 +15,19 @@ module Prism
|
|
14
15
|
# Calls `accept` on the given node if it is not `nil`, which in turn should
|
15
16
|
# call back into this visitor by calling the appropriate `visit_*` method.
|
16
17
|
def visit(node)
|
18
|
+
# @type self: _Visitor
|
17
19
|
node&.accept(self)
|
18
20
|
end
|
19
21
|
|
20
22
|
# Visits each node in `nodes` by calling `accept` on each one.
|
21
23
|
def visit_all(nodes)
|
24
|
+
# @type self: _Visitor
|
22
25
|
nodes.each { |node| node&.accept(self) }
|
23
26
|
end
|
24
27
|
|
25
28
|
# Visits the child nodes of `node` by calling `accept` on each one.
|
26
29
|
def visit_child_nodes(node)
|
30
|
+
# @type self: _Visitor
|
27
31
|
node.compact_child_nodes.each { |node| node.accept(self) }
|
28
32
|
end
|
29
33
|
end
|
@@ -309,6 +313,12 @@ module Prism
|
|
309
313
|
# Visit a InterpolatedXStringNode node
|
310
314
|
alias visit_interpolated_x_string_node visit_child_nodes
|
311
315
|
|
316
|
+
# Visit a ItLocalVariableReadNode node
|
317
|
+
alias visit_it_local_variable_read_node visit_child_nodes
|
318
|
+
|
319
|
+
# Visit a ItParametersNode node
|
320
|
+
alias visit_it_parameters_node visit_child_nodes
|
321
|
+
|
312
322
|
# Visit a KeywordHashNode node
|
313
323
|
alias visit_keyword_hash_node visit_child_nodes
|
314
324
|
|
@@ -441,6 +451,9 @@ module Prism
|
|
441
451
|
# Visit a SelfNode node
|
442
452
|
alias visit_self_node visit_child_nodes
|
443
453
|
|
454
|
+
# Visit a ShareableConstantNode node
|
455
|
+
alias visit_shareable_constant_node visit_child_nodes
|
456
|
+
|
444
457
|
# Visit a SingletonClassNode node
|
445
458
|
alias visit_singleton_class_node visit_child_nodes
|
446
459
|
|
data/lib/prism.rb
CHANGED
@@ -13,30 +13,31 @@ module Prism
|
|
13
13
|
|
14
14
|
autoload :BasicVisitor, "prism/visitor"
|
15
15
|
autoload :Compiler, "prism/compiler"
|
16
|
-
autoload :Debug, "prism/debug"
|
17
16
|
autoload :DesugarCompiler, "prism/desugar_compiler"
|
18
17
|
autoload :Dispatcher, "prism/dispatcher"
|
19
18
|
autoload :DotVisitor, "prism/dot_visitor"
|
20
19
|
autoload :DSL, "prism/dsl"
|
20
|
+
autoload :InspectVisitor, "prism/inspect_visitor"
|
21
21
|
autoload :LexCompat, "prism/lex_compat"
|
22
22
|
autoload :LexRipper, "prism/lex_compat"
|
23
23
|
autoload :MutationCompiler, "prism/mutation_compiler"
|
24
|
-
autoload :NodeInspector, "prism/node_inspector"
|
25
24
|
autoload :Pack, "prism/pack"
|
26
25
|
autoload :Pattern, "prism/pattern"
|
26
|
+
autoload :Reflection, "prism/reflection"
|
27
|
+
autoload :Relocation, "prism/relocation"
|
27
28
|
autoload :Serialize, "prism/serialize"
|
29
|
+
autoload :StringQuery, "prism/string_query"
|
28
30
|
autoload :Translation, "prism/translation"
|
29
31
|
autoload :Visitor, "prism/visitor"
|
30
32
|
|
31
33
|
# Some of these constants are not meant to be exposed, so marking them as
|
32
34
|
# private here.
|
33
35
|
|
34
|
-
private_constant :Debug
|
35
36
|
private_constant :LexCompat
|
36
37
|
private_constant :LexRipper
|
37
38
|
|
38
39
|
# :call-seq:
|
39
|
-
# Prism::lex_compat(source, **options) ->
|
40
|
+
# Prism::lex_compat(source, **options) -> LexCompat::Result
|
40
41
|
#
|
41
42
|
# Returns a parse result whose value is an array of tokens that closely
|
42
43
|
# resembles the return value of Ripper::lex. The main difference is that the
|
@@ -44,7 +45,7 @@ module Prism
|
|
44
45
|
#
|
45
46
|
# For supported options, see Prism::parse.
|
46
47
|
def self.lex_compat(source, **options)
|
47
|
-
LexCompat.new(source, **options).result
|
48
|
+
LexCompat.new(source, **options).result # steep:ignore
|
48
49
|
end
|
49
50
|
|
50
51
|
# :call-seq:
|
@@ -54,46 +55,35 @@ module Prism
|
|
54
55
|
# returns the same tokens. Raises SyntaxError if the syntax in source is
|
55
56
|
# invalid.
|
56
57
|
def self.lex_ripper(source)
|
57
|
-
LexRipper.new(source).result
|
58
|
+
LexRipper.new(source).result # steep:ignore
|
58
59
|
end
|
59
60
|
|
60
61
|
# :call-seq:
|
61
|
-
# Prism::load(source, serialized) -> ParseResult
|
62
|
+
# Prism::load(source, serialized, freeze) -> ParseResult
|
62
63
|
#
|
63
64
|
# Load the serialized AST using the source as a reference into a tree.
|
64
|
-
def self.load(source, serialized)
|
65
|
-
Serialize.
|
66
|
-
end
|
67
|
-
|
68
|
-
# :call-seq:
|
69
|
-
# Prism::parse_failure?(source, **options) -> bool
|
70
|
-
#
|
71
|
-
# Returns true if the source parses with errors.
|
72
|
-
def self.parse_failure?(source, **options)
|
73
|
-
!parse_success?(source, **options)
|
74
|
-
end
|
75
|
-
|
76
|
-
# :call-seq:
|
77
|
-
# Prism::parse_file_failure?(filepath, **options) -> bool
|
78
|
-
#
|
79
|
-
# Returns true if the file at filepath parses with errors.
|
80
|
-
def self.parse_file_failure?(filepath, **options)
|
81
|
-
!parse_file_success?(filepath, **options)
|
65
|
+
def self.load(source, serialized, freeze = false)
|
66
|
+
Serialize.load_parse(source, serialized, freeze)
|
82
67
|
end
|
83
68
|
end
|
84
69
|
|
70
|
+
require_relative "prism/polyfill/byteindex"
|
85
71
|
require_relative "prism/node"
|
86
72
|
require_relative "prism/node_ext"
|
87
73
|
require_relative "prism/parse_result"
|
88
|
-
require_relative "prism/parse_result/comments"
|
89
|
-
require_relative "prism/parse_result/newlines"
|
90
74
|
|
91
75
|
# This is a Ruby implementation of the prism parser. If we're running on CRuby
|
92
76
|
# and we haven't explicitly set the PRISM_FFI_BACKEND environment variable, then
|
93
77
|
# it's going to require the built library. Otherwise, it's going to require a
|
94
78
|
# module that uses FFI to call into the library.
|
95
79
|
if RUBY_ENGINE == "ruby" and !ENV["PRISM_FFI_BACKEND"]
|
80
|
+
# The C extension is the default backend on CRuby.
|
81
|
+
Prism::BACKEND = :CEXT
|
82
|
+
|
96
83
|
require "prism/prism"
|
97
84
|
else
|
85
|
+
# The FFI backend is used on other Ruby implementations.
|
86
|
+
Prism::BACKEND = :FFI
|
87
|
+
|
98
88
|
require_relative "prism/ffi"
|
99
89
|
end
|