diff options
author | aycabta <[email protected]> | 2020-06-07 23:29:01 +0900 |
---|---|---|
committer | aycabta <[email protected]> | 2020-07-22 02:31:46 +0900 |
commit | c72a2fad9717a6090aa792c31eb0043886d0fb39 (patch) | |
tree | a4de42bfbdb0792d131f502a28292a57c17a1fdc /lib/irb/ruby-lex.rb | |
parent | 1dfd24a7fc4e2877d49d2a3326925080e2972cf1 (diff) |
[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 <[email protected]>
https://github.com/ruby/irb/commit/e593cc65b7
Diffstat (limited to 'lib/irb/ruby-lex.rb')
-rw-r--r-- | lib/irb/ruby-lex.rb | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 02bc548809..c7a47fd99d 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -31,14 +31,13 @@ class RubyLex 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 @@ -90,8 +89,8 @@ class RubyLex 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 @@ -226,8 +225,8 @@ class RubyLex 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 |