Skip to content

Commit

Permalink
Simplify RubyLex.compile_with_errors_suppressed
Browse files Browse the repository at this point in the history
nobu-san reviewed,

#106 (review)
> 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>
  • Loading branch information
aycabta and nobu committed Jun 8, 2020
1 parent bd4ef82 commit e593cc6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/irb/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def scan(code, allow_last_error:)
pos = [1, 0]

verbose, $VERBOSE = $VERBOSE, nil
RubyLex.compile_with_errors_suppressed(code) do |inner_code|
lexer = Ripper::Lexer.new(inner_code)
RubyLex.compile_with_errors_suppressed(code) do |inner_code, line_no|
lexer = Ripper::Lexer.new(inner_code, '(ripper)', line_no)
if lexer.respond_to?(:scan) # Ruby 2.7+
lexer.scan.each do |elem|
str = elem.tok
Expand Down
19 changes: 9 additions & 10 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ def initialize
end

def self.compile_with_errors_suppressed(code)
line_no = 1
begin
result = yield code
result = yield code, line_no
rescue ArgumentError => e
magic_comment_regexp = /\A(?<shebang>#.*\n)?#\s*(?:encoding|coding)\s*:.*(?<nl>\n)?/
if e.message.match?(/unknown encoding name/) && code.match?(magic_comment_regexp)
code = code.gsub(magic_comment_regexp, "\\k<shebang>#\\k<nl>")
retry
end
code = ";\n#{code}"
line_no = 0
result = yield code, line_no
end
result
end
Expand Down Expand Up @@ -90,8 +89,8 @@ def set_prompt(p = nil, &block)
def ripper_lex_without_warning(code)
verbose, $VERBOSE = $VERBOSE, nil
tokens = nil
self.class.compile_with_errors_suppressed(code) do |inner_code|
tokens = Ripper.lex(inner_code)
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no|
tokens = Ripper.lex(inner_code, '-', line_no)
end
$VERBOSE = verbose
tokens
Expand Down Expand Up @@ -226,8 +225,8 @@ def check_code_block(code)
when 'jruby'
JRuby.compile_ir(code)
else
self.class.compile_with_errors_suppressed(code) do |inner_code|
RubyVM::InstructionSequence.compile(inner_code)
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no|
RubyVM::InstructionSequence.compile(inner_code, nil, nil, line_no)
end
end
rescue EncodingError
Expand Down

0 comments on commit e593cc6

Please sign in to comment.