Skip to content

Commit a10f7fa

Browse files
committed
Sync Prism to 1.8.0
1 parent ac59694 commit a10f7fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+383
-206
lines changed

lib/prism.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Prism
2020
autoload :DSL, "prism/dsl"
2121
autoload :InspectVisitor, "prism/inspect_visitor"
2222
autoload :LexCompat, "prism/lex_compat"
23-
autoload :LexRipper, "prism/lex_compat"
23+
autoload :LexRipper, "prism/lex_ripper"
2424
autoload :MutationCompiler, "prism/mutation_compiler"
2525
autoload :Pack, "prism/pack"
2626
autoload :Pattern, "prism/pattern"

lib/prism/lex_compat.rb

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# :markup: markdown
33

44
require "delegate"
5-
require "ripper"
65

76
module Prism
87
# This class is responsible for lexing the source using prism and then
@@ -249,8 +248,8 @@ def ==(other) # :nodoc:
249248
class IdentToken < Token
250249
def ==(other) # :nodoc:
251250
(self[0...-1] == other[0...-1]) && (
252-
(other[3] == Ripper::EXPR_LABEL | Ripper::EXPR_END) ||
253-
(other[3] & Ripper::EXPR_ARG_ANY != 0)
251+
(other[3] == Translation::Ripper::EXPR_LABEL | Translation::Ripper::EXPR_END) ||
252+
(other[3] & (Translation::Ripper::EXPR_ARG | Translation::Ripper::EXPR_CMDARG) != 0)
254253
)
255254
end
256255
end
@@ -261,8 +260,8 @@ class IgnoredNewlineToken < Token
261260
def ==(other) # :nodoc:
262261
return false unless self[0...-1] == other[0...-1]
263262

264-
if self[3] == Ripper::EXPR_ARG | Ripper::EXPR_LABELED
265-
other[3] & Ripper::EXPR_ARG | Ripper::EXPR_LABELED != 0
263+
if self[3] == Translation::Ripper::EXPR_ARG | Translation::Ripper::EXPR_LABELED
264+
other[3] & Translation::Ripper::EXPR_ARG | Translation::Ripper::EXPR_LABELED != 0
266265
else
267266
self[3] == other[3]
268267
end
@@ -280,8 +279,8 @@ def ==(other) # :nodoc:
280279
class ParamToken < Token
281280
def ==(other) # :nodoc:
282281
(self[0...-1] == other[0...-1]) && (
283-
(other[3] == Ripper::EXPR_END) ||
284-
(other[3] == Ripper::EXPR_END | Ripper::EXPR_LABEL)
282+
(other[3] == Translation::Ripper::EXPR_END) ||
283+
(other[3] == Translation::Ripper::EXPR_END | Translation::Ripper::EXPR_LABEL)
285284
)
286285
end
287286
end
@@ -615,6 +614,11 @@ def self.build(opening)
615614

616615
private_constant :Heredoc
617616

617+
# In previous versions of Ruby, Ripper wouldn't flush the bom before the
618+
# first token, so we had to have a hack in place to account for that.
619+
BOM_FLUSHED = RUBY_VERSION >= "3.3.0"
620+
private_constant :BOM_FLUSHED
621+
618622
attr_reader :source, :options
619623

620624
def initialize(source, **options)
@@ -630,13 +634,9 @@ def result
630634

631635
result = Prism.lex(source, **options)
632636
result_value = result.value
633-
previous_state = nil #: Ripper::Lexer::State?
637+
previous_state = nil #: State?
634638
last_heredoc_end = nil #: Integer?
635639

636-
# In previous versions of Ruby, Ripper wouldn't flush the bom before the
637-
# first token, so we had to have a hack in place to account for that. This
638-
# checks for that behavior.
639-
bom_flushed = Ripper.lex("\xEF\xBB\xBF# test")[0][0][1] == 0
640640
bom = source.byteslice(0..2) == "\xEF\xBB\xBF"
641641

642642
result_value.each_with_index do |(token, lex_state), index|
@@ -651,7 +651,7 @@ def result
651651
if bom && lineno == 1
652652
column -= 3
653653

654-
if index == 0 && column == 0 && !bom_flushed
654+
if index == 0 && column == 0 && !BOM_FLUSHED
655655
flushed =
656656
case token.type
657657
when :BACK_REFERENCE, :INSTANCE_VARIABLE, :CLASS_VARIABLE,
@@ -675,7 +675,7 @@ def result
675675

676676
event = RIPPER.fetch(token.type)
677677
value = token.value
678-
lex_state = Ripper::Lexer::State.new(lex_state)
678+
lex_state = Translation::Ripper::Lexer::State.new(lex_state)
679679

680680
token =
681681
case event
@@ -689,7 +689,7 @@ def result
689689
last_heredoc_end = token.location.end_offset
690690
IgnoreStateToken.new([[lineno, column], event, value, lex_state])
691691
when :on_ident
692-
if lex_state == Ripper::EXPR_END
692+
if lex_state == Translation::Ripper::EXPR_END
693693
# If we have an identifier that follows a method name like:
694694
#
695695
# def foo bar
@@ -699,7 +699,7 @@ def result
699699
# yet. We do this more accurately, so we need to allow comparing
700700
# against both END and END|LABEL.
701701
ParamToken.new([[lineno, column], event, value, lex_state])
702-
elsif lex_state == Ripper::EXPR_END | Ripper::EXPR_LABEL
702+
elsif lex_state == Translation::Ripper::EXPR_END | Translation::Ripper::EXPR_LABEL
703703
# In the event that we're comparing identifiers, we're going to
704704
# allow a little divergence. Ripper doesn't account for local
705705
# variables introduced through named captures in regexes, and we
@@ -739,7 +739,7 @@ def result
739739
counter += { on_embexpr_beg: -1, on_embexpr_end: 1 }[current_event] || 0
740740
end
741741

742-
Ripper::Lexer::State.new(result_value[current_index][1])
742+
Translation::Ripper::Lexer::State.new(result_value[current_index][1])
743743
else
744744
previous_state
745745
end
@@ -867,62 +867,4 @@ def result
867867
end
868868

869869
private_constant :LexCompat
870-
871-
# This is a class that wraps the Ripper lexer to produce almost exactly the
872-
# same tokens.
873-
class LexRipper # :nodoc:
874-
attr_reader :source
875-
876-
def initialize(source)
877-
@source = source
878-
end
879-
880-
def result
881-
previous = [] #: [[Integer, Integer], Symbol, String, untyped] | []
882-
results = [] #: Array[[[Integer, Integer], Symbol, String, untyped]]
883-
884-
lex(source).each do |token|
885-
case token[1]
886-
when :on_sp
887-
# skip
888-
when :on_tstring_content
889-
if previous[1] == :on_tstring_content && (token[2].start_with?("\#$") || token[2].start_with?("\#@"))
890-
previous[2] << token[2]
891-
else
892-
results << token
893-
previous = token
894-
end
895-
when :on_words_sep
896-
if previous[1] == :on_words_sep
897-
previous[2] << token[2]
898-
else
899-
results << token
900-
previous = token
901-
end
902-
else
903-
results << token
904-
previous = token
905-
end
906-
end
907-
908-
results
909-
end
910-
911-
private
912-
913-
if Ripper.method(:lex).parameters.assoc(:keyrest)
914-
def lex(source)
915-
Ripper.lex(source, raise_errors: true)
916-
end
917-
else
918-
def lex(source)
919-
ripper = Ripper::Lexer.new(source)
920-
ripper.lex.tap do |result|
921-
raise SyntaxError, ripper.errors.map(&:message).join(' ;') if ripper.errors.any?
922-
end
923-
end
924-
end
925-
end
926-
927-
private_constant :LexRipper
928870
end

lib/prism/lex_ripper.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
# :markup: markdown
3+
4+
require "ripper"
5+
6+
module Prism
7+
# This is a class that wraps the Ripper lexer to produce almost exactly the
8+
# same tokens.
9+
class LexRipper # :nodoc:
10+
attr_reader :source
11+
12+
def initialize(source)
13+
@source = source
14+
end
15+
16+
def result
17+
previous = [] #: [[Integer, Integer], Symbol, String, untyped] | []
18+
results = [] #: Array[[[Integer, Integer], Symbol, String, untyped]]
19+
20+
lex(source).each do |token|
21+
case token[1]
22+
when :on_sp
23+
# skip
24+
when :on_tstring_content
25+
if previous[1] == :on_tstring_content && (token[2].start_with?("\#$") || token[2].start_with?("\#@"))
26+
previous[2] << token[2]
27+
else
28+
results << token
29+
previous = token
30+
end
31+
when :on_words_sep
32+
if previous[1] == :on_words_sep
33+
previous[2] << token[2]
34+
else
35+
results << token
36+
previous = token
37+
end
38+
else
39+
results << token
40+
previous = token
41+
end
42+
end
43+
44+
results
45+
end
46+
47+
private
48+
49+
if Ripper.method(:lex).parameters.assoc(:keyrest)
50+
def lex(source)
51+
Ripper.lex(source, raise_errors: true)
52+
end
53+
else
54+
def lex(source)
55+
ripper = Ripper::Lexer.new(source)
56+
ripper.lex.tap do |result|
57+
raise SyntaxError, ripper.errors.map(&:message).join(' ;') if ripper.errors.any?
58+
end
59+
end
60+
end
61+
end
62+
63+
private_constant :LexRipper
64+
end

lib/prism/prism.gemspec

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Gem::Specification.new do |spec|
44
spec.name = "prism"
5-
spec.version = "1.7.0"
5+
spec.version = "1.8.0"
66
spec.authors = ["Shopify"]
77
spec.email = ["ruby@shopify.com"]
88

@@ -77,6 +77,7 @@ Gem::Specification.new do |spec|
7777
"lib/prism/ffi.rb",
7878
"lib/prism/inspect_visitor.rb",
7979
"lib/prism/lex_compat.rb",
80+
"lib/prism/lex_ripper.rb",
8081
"lib/prism/mutation_compiler.rb",
8182
"lib/prism/node_ext.rb",
8283
"lib/prism/node.rb",
@@ -98,15 +99,12 @@ Gem::Specification.new do |spec|
9899
"lib/prism/translation.rb",
99100
"lib/prism/translation/parser.rb",
100101
"lib/prism/translation/parser_current.rb",
101-
"lib/prism/translation/parser33.rb",
102-
"lib/prism/translation/parser34.rb",
103-
"lib/prism/translation/parser35.rb",
104-
"lib/prism/translation/parser40.rb",
105-
"lib/prism/translation/parser41.rb",
102+
"lib/prism/translation/parser_versions.rb",
106103
"lib/prism/translation/parser/builder.rb",
107104
"lib/prism/translation/parser/compiler.rb",
108105
"lib/prism/translation/parser/lexer.rb",
109106
"lib/prism/translation/ripper.rb",
107+
"lib/prism/translation/ripper/lexer.rb",
110108
"lib/prism/translation/ripper/sexp.rb",
111109
"lib/prism/translation/ripper/shim.rb",
112110
"lib/prism/translation/ruby_parser.rb",
@@ -122,11 +120,7 @@ Gem::Specification.new do |spec|
122120
"rbi/prism/reflection.rbi",
123121
"rbi/prism/string_query.rbi",
124122
"rbi/prism/translation/parser.rbi",
125-
"rbi/prism/translation/parser33.rbi",
126-
"rbi/prism/translation/parser34.rbi",
127-
"rbi/prism/translation/parser35.rbi",
128-
"rbi/prism/translation/parser40.rbi",
129-
"rbi/prism/translation/parser41.rbi",
123+
"rbi/prism/translation/parser_versions.rbi",
130124
"rbi/prism/translation/ripper.rbi",
131125
"rbi/prism/visitor.rbi",
132126
"sig/prism.rbs",

lib/prism/translation.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ module Prism
77
module Translation # steep:ignore
88
autoload :Parser, "prism/translation/parser"
99
autoload :ParserCurrent, "prism/translation/parser_current"
10-
autoload :Parser33, "prism/translation/parser33"
11-
autoload :Parser34, "prism/translation/parser34"
12-
autoload :Parser35, "prism/translation/parser35"
13-
autoload :Parser40, "prism/translation/parser40"
14-
autoload :Parser41, "prism/translation/parser41"
10+
autoload :Parser33, "prism/translation/parser_versions"
11+
autoload :Parser34, "prism/translation/parser_versions"
12+
autoload :Parser35, "prism/translation/parser_versions"
13+
autoload :Parser40, "prism/translation/parser_versions"
14+
autoload :Parser41, "prism/translation/parser_versions"
1515
autoload :Ripper, "prism/translation/ripper"
1616
autoload :RubyParser, "prism/translation/ruby_parser"
1717
end

lib/prism/translation/parser33.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/prism/translation/parser34.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/prism/translation/parser35.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/prism/translation/parser40.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/prism/translation/parser41.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)