Skip to content

Commit 3bfc865

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Move LexRipper into its own file
It has a hard dependency on ripper that can't be removed. This makes it so that ripper can be loaded only when the class is actually used. ruby/prism@3b5b4a8a6d
1 parent dcfbbdc commit 3bfc865

File tree

4 files changed

+66
-59
lines changed

4 files changed

+66
-59
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: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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",

0 commit comments

Comments
 (0)