Skip to content

Commit 89a4643

Browse files
authored
Merge pull request #3871 from Earlopain/lex-ripper-plain
Remove `Prism.lex_ripper`
2 parents c774ec2 + 31be379 commit 89a4643

11 files changed

Lines changed: 29 additions & 92 deletions

File tree

Steepfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ target :lib do
1010
# TODO: Type-checking these files is still WIP
1111
ignore "lib/prism/desugar_compiler.rb"
1212
ignore "lib/prism/lex_compat.rb"
13-
ignore "lib/prism/lex_ripper.rb"
1413
ignore "lib/prism/serialize.rb"
1514
ignore "lib/prism/ffi.rb"
1615
ignore "lib/prism/translation"

bin/prism

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,10 @@ module Prism
210210

211211
# bin/prism lex_compat [source]
212212
def lex_compat(argv)
213+
require "ripper"
213214
source, filepath = read_source(argv)
214215

215-
ripper_value =
216-
begin
217-
Prism.lex_ripper(source)
218-
rescue ArgumentError, SyntaxError
219-
# If Ripper raises a syntax error, we want to continue as if it didn't
220-
# return any tokens at all. prism won't raise a syntax error, so it's
221-
# nicer to still be able to see the tokens that prism generated.
222-
[]
223-
end
224-
216+
ripper_value = Ripper.lex(source)
225217
prism_compat = Prism.lex_compat(source, filepath: filepath)
226218
prism = Prism.lex(source, filepath: filepath)
227219

lib/prism.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Prism
2020
autoload :DSL, "prism/dsl"
2121
autoload :InspectVisitor, "prism/inspect_visitor"
2222
autoload :LexCompat, "prism/lex_compat"
23-
autoload :LexRipper, "prism/lex_ripper"
2423
autoload :MutationCompiler, "prism/mutation_compiler"
2524
autoload :Pack, "prism/pack"
2625
autoload :Pattern, "prism/pattern"
@@ -35,7 +34,6 @@ module Prism
3534
# private here.
3635

3736
private_constant :LexCompat
38-
private_constant :LexRipper
3937

4038
# Raised when requested to parse as the currently running Ruby version but Prism has no support for it.
4139
class CurrentVersionError < ArgumentError
@@ -68,15 +66,6 @@ def self.lex_compat(source, **options)
6866
LexCompat.new(source, **options).result # steep:ignore
6967
end
7068

71-
# :call-seq:
72-
# Prism::lex_ripper(source) -> Array
73-
#
74-
# This wraps the result of Ripper.lex. It produces almost exactly the
75-
# same tokens. Raises SyntaxError if the syntax in source is invalid.
76-
def self.lex_ripper(source)
77-
LexRipper.new(source).result # steep:ignore
78-
end
79-
8069
# :call-seq:
8170
# Prism::load(source, serialized, freeze) -> ParseResult
8271
#

lib/prism/lex_ripper.rb

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

prism.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ 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",
8180
"lib/prism/mutation_compiler.rb",
8281
"lib/prism/node_ext.rb",
8382
"lib/prism/node.rb",

rakelib/lex.rake

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# frozen_string_literal: true
22
# typed: ignore
33

4+
require "ripper"
5+
46
module Prism
5-
# This class is responsible for lexing files with both lex_compat and
6-
# lex_ripper and ensuring they match up. It keeps track of the files which
7+
# This class is responsible for lexing files with both prism and
8+
# ripper and ensuring they match up. It keeps track of the files which
79
# failed to match up, and the files which passed.
810
class LexTask
911
attr_reader :failing_files, :passing_file_count
@@ -28,7 +30,7 @@ module Prism
2830
end
2931

3032
result = Prism.lex_compat(source)
31-
if result.errors.empty? && Prism.lex_ripper(source) == result.value
33+
if result.errors.empty? && Ripper.lex(source) == result.value
3234
@passing_file_count += 1
3335
true
3436
else
@@ -98,6 +100,10 @@ TARGETS = {
98100

99101
# Requires an implicit -x, which ripper does not respect
100102
"tool/merger.rb",
103+
104+
# Contains `"x#$%"` which looks like bare interpolation but $% is not a valid global.
105+
# This confuses ripper, it emits two string tokens. https://bugs.ruby-lang.org/issues/21849
106+
"ext/psych/lib/psych/scalar_scanner.rb",
101107
]
102108
},
103109
discourse: {
@@ -265,6 +271,16 @@ TOP_100_GEMS_INVALID_SYNTAX_PREFIXES = %w[
265271
top-100-gems/devise-4.9.2/lib/generators/templates/controllers/
266272
top-100-gems/fastlane-2.212.1/fastlane/lib/assets/custom_action_template.rb
267273
]
274+
TOP_100_GEMS_LEX_RIPPER_BUG = [
275+
# Contains code like `"x#$%"` which looks like bare interpolation but $% is not a valid global.
276+
# This confuses ripper, it emits two string tokens. https://bugs.ruby-lang.org/issues/21849
277+
"faker-3.1.1/lib/faker/default/internet.rb",
278+
"ruby_parser-3.20.0/test/test_ruby_parser.rb",
279+
"rouge-4.1.0/lib/rouge/lexers/cisco_ios.rb",
280+
"rouge-4.1.0/lib/rouge/lexers/ghc_cmm.rb",
281+
"rouge-4.1.0/lib/rouge/lexers/nasm.rb",
282+
"rouge-4.1.0/lib/rouge/lexers/velocity.rb",
283+
]
268284

269285
namespace :download do
270286
directory TOP_100_GEMS_DIR
@@ -346,7 +362,10 @@ task "lex:topgems": ["download:topgems", :compile] do
346362
lex_task.compare(filepath)
347363
end
348364

349-
gem_failing_files = lex_task.failing_files.map { |todo| todo.delete_prefix("#{directory}/") }
365+
gem_failing_files = lex_task.failing_files.filter_map do |todo|
366+
next if TOP_100_GEMS_LEX_RIPPER_BUG.any? { |path| todo.end_with?(path) }
367+
todo.delete_prefix("#{directory}/")
368+
end
350369
failing_files[gem_name] = gem_failing_files if gem_failing_files.any?
351370
end
352371

rakelib/typecheck.rake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ namespace :typecheck do
2020
File.write("sorbet/typed_overrides.yml", ERB.new(<<~YAML, trim_mode: "-").result_with_hash(locals))
2121
false:
2222
- ./lib/prism/lex_compat.rb
23-
- ./lib/prism/lex_ripper.rb
2423
- ./lib/prism/node_ext.rb
2524
- ./lib/prism/parse_result.rb
2625
- ./lib/prism/visitor.rb

rbi/prism.rbi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ module Prism
1616
sig { params(source: String, options: T::Hash[Symbol, T.untyped]).returns(Prism::LexCompat::Result) }
1717
def self.lex_compat(source, **options); end
1818

19-
sig { params(source: String).returns(T::Array[T.untyped]) }
20-
def self.lex_ripper(source); end
21-
2219
sig { params(source: String, serialized: String, freeze: T.nilable(T::Boolean)).returns(Prism::ParseResult) }
2320
def self.load(source, serialized, freeze = false); end
2421

templates/sig/prism.rbs.erb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ module Prism
4242
?bool freeze
4343
) -> ParseResult
4444

45-
def self.lex_ripper: (
46-
String source
47-
) -> Array[[[Integer, Integer], Symbol, String, untyped]]
48-
4945
# Methods taking a path to a Ruby file:
5046
<%-
5147
{

test/prism/bom_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
return if RUBY_ENGINE != "ruby"
66

77
require_relative "test_helper"
8+
require "ripper"
89

910
module Prism
1011
class BOMTest < TestCase
@@ -53,7 +54,7 @@ def test_string
5354

5455
def assert_bom(source)
5556
bommed = "\xEF\xBB\xBF#{source}"
56-
assert_equal Prism.lex_ripper(bommed), Prism.lex_compat(bommed).value
57+
assert_equal Ripper.lex(bommed), Prism.lex_compat(bommed).value
5758
end
5859
end
5960
end

0 commit comments

Comments
 (0)