mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/irb] Simplify RubyLex.compile_with_errors_suppressed
nobu-san reviewed,
https://github.com/ruby/irb/pull/106#pullrequestreview-423400033
> How about lexer = Ripper::Lexer.new(";\n#{code}", nil, 0)?
> Encoding pragma is effective only at the beginning.
> And the semicolon and newline will be skipped because the position is before
> the initial pos.
I employ the way.
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
e593cc65b7
This commit is contained in:
parent
1dfd24a7fc
commit
c72a2fad97
2 changed files with 11 additions and 12 deletions
|
@ -155,8 +155,8 @@ module IRB # :nodoc:
|
||||||
pos = [1, 0]
|
pos = [1, 0]
|
||||||
|
|
||||||
verbose, $VERBOSE = $VERBOSE, nil
|
verbose, $VERBOSE = $VERBOSE, nil
|
||||||
RubyLex.compile_with_errors_suppressed(code) do |inner_code|
|
RubyLex.compile_with_errors_suppressed(code) do |inner_code, line_no|
|
||||||
lexer = Ripper::Lexer.new(inner_code)
|
lexer = Ripper::Lexer.new(inner_code, '(ripper)', line_no)
|
||||||
if lexer.respond_to?(:scan) # Ruby 2.7+
|
if lexer.respond_to?(:scan) # Ruby 2.7+
|
||||||
lexer.scan.each do |elem|
|
lexer.scan.each do |elem|
|
||||||
str = elem.tok
|
str = elem.tok
|
||||||
|
|
|
@ -31,14 +31,13 @@ class RubyLex
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.compile_with_errors_suppressed(code)
|
def self.compile_with_errors_suppressed(code)
|
||||||
|
line_no = 1
|
||||||
begin
|
begin
|
||||||
result = yield code
|
result = yield code, line_no
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
magic_comment_regexp = /\A(?<shebang>#.*\n)?#\s*(?:encoding|coding)\s*:.*(?<nl>\n)?/
|
code = ";\n#{code}"
|
||||||
if e.message.match?(/unknown encoding name/) && code.match?(magic_comment_regexp)
|
line_no = 0
|
||||||
code = code.gsub(magic_comment_regexp, "\\k<shebang>#\\k<nl>")
|
result = yield code, line_no
|
||||||
retry
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
@ -90,8 +89,8 @@ class RubyLex
|
||||||
def ripper_lex_without_warning(code)
|
def ripper_lex_without_warning(code)
|
||||||
verbose, $VERBOSE = $VERBOSE, nil
|
verbose, $VERBOSE = $VERBOSE, nil
|
||||||
tokens = nil
|
tokens = nil
|
||||||
self.class.compile_with_errors_suppressed(code) do |inner_code|
|
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no|
|
||||||
tokens = Ripper.lex(inner_code)
|
tokens = Ripper.lex(inner_code, '-', line_no)
|
||||||
end
|
end
|
||||||
$VERBOSE = verbose
|
$VERBOSE = verbose
|
||||||
tokens
|
tokens
|
||||||
|
@ -226,8 +225,8 @@ class RubyLex
|
||||||
when 'jruby'
|
when 'jruby'
|
||||||
JRuby.compile_ir(code)
|
JRuby.compile_ir(code)
|
||||||
else
|
else
|
||||||
self.class.compile_with_errors_suppressed(code) do |inner_code|
|
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no|
|
||||||
RubyVM::InstructionSequence.compile(inner_code)
|
RubyVM::InstructionSequence.compile(inner_code, nil, nil, line_no)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue EncodingError
|
rescue EncodingError
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue