[ruby/prism] Fix token incompatibility for Prism::Translation::Parser::Lexer
Prism::Translation::Parser::Lexer
This PR fixes token incompatibility for Prism::Translation::Parser::Lexer when using backquoted heredoc indetiner:
<<-` FOO` a b FOO
Returns tXSTRING_BEG as the first token:
tXSTRING_BEG
$ bundle exec ruby -Ilib -rparser/ruby33 -ve \ 'buf = Parser::Source::Buffer.new("example.rb"); buf.source = File.read("example.rb"); p Parser::Ruby33.new.tokenize(buf)' ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] [s(:xstr, s(:str, "a\n"), s(:str, "b\n")), [], [[:tXSTRING_BEG, ["<<`", #<Parser::Source::Range example.rb 0...10>]], [:tSTRING_CONTENT, ["a\n", #<Parser::Source::Range example.rb 11...13>]], [:tSTRING_CONTENT, ["b\n", #<Parser::Source::Range example.rb 13...15>]], [:tSTRING_END, [" FOO", #<Parser::Source::Range example.rb 15...23>]], [:tNL, [nil, #<Parser::Source::Range example.rb 10...11>]]]]
Prism::Translation::Parser
Previously, the tokens returned by the Parser gem were different. The escaped backslash does not match in the tSTRING_BEG token and value of tSTRING_END token.
tSTRING_BEG
tSTRING_END
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'buf = Parser::Source::Buffer.new("example.rb"); buf.source = File.read("example.rb"); p Prism::Translation::Parser33.new.tokenize(buf)' ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] [s(:xstr, s(:str, "a\n"), s(:str, "b\n")), [], [[:tSTRING_BEG, ["<<\"", #<Parser::Source::Range example.rb 0...10>]], [:tSTRING_CONTENT, ["a\n", #<Parser::Source::Range example.rb 11...13>]], [:tSTRING_CONTENT, ["b\n", #<Parser::Source::Range example.rb 13...15>]], [:tSTRING_END, ["` FOO`", #<Parser::Source::Range example.rb 15...23>]], [:tNL, [nil, #<Parser::Source::Range example.rb 10...11>]]]]
After this correction, the AST and tokens returned by the Parser gem are the same:
$ bunlde exec ruby -Ilib -rprism -rprism/translation/parser33 -ve \ 'buf = Parser::Source::Buffer.new("example.rb"); buf.source = File.read("example.rb"); p Prism::Translation::Parser33.new.tokenize(buf)' ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22] [s(:xstr, s(:str, "a\n"), s(:str, "b\n")), [], [[:tXSTRING_BEG, ["<<`", #<Parser::Source::Range example.rb 0...10>]], [:tSTRING_CONTENT, ["a\n", #<Parser::Source::Range example.rb 11...13>]], [:tSTRING_CONTENT, ["b\n", #<Parser::Source::Range example.rb 13...15>]], [:tSTRING_END, [" FOO", #<Parser::Source::Range example.rb 15...23>]], [:tNL, [nil, #<Parser::Source::Range example.rb 10...11>]]]]
https://github.com/ruby/prism/commit/308f8d85a1
[ruby/prism] Fix token incompatibility for
Prism::Translation::Parser::LexerThis PR fixes token incompatibility for
Prism::Translation::Parser::Lexerwhen using backquoted heredoc indetiner:Parser gem (Expected)¶
Returns
tXSTRING_BEGas the first token:Prism::Translation::Parser(Actual)¶Previously, the tokens returned by the Parser gem were different. The escaped backslash does not match in the
tSTRING_BEGtoken andvalue of
tSTRING_ENDtoken.After this correction, the AST and tokens returned by the Parser gem are the same:
https://github.com/ruby/prism/commit/308f8d85a1