Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation::Parser: small lexer incompatibilities with heredocs #2482

Open
noahgibbs opened this issue Feb 22, 2024 · 0 comments
Open

Translation::Parser: small lexer incompatibilities with heredocs #2482

noahgibbs opened this issue Feb 22, 2024 · 0 comments
Labels
bug Something isn't working rubocop Errors that affect Rubocop use of Prism

Comments

@noahgibbs
Copy link
Contributor

Here's the sample code I'm starting from:

# parser_gem_lexer_test.rb
require "parser/current"
require "prism"
require "prism/translation/parser"

h = <<'HEREDOC'
  <<~RUBY
    1
  RUBY
HEREDOC

b = Parser::Source::Buffer.new("inline ruby", 1)
b.source = h

_, _, tokens = Parser::CurrentRuby.default_parser.tokenize(b)
puts "Parser:"
pp tokens

_, _, tokens = Prism::Translation::Parser.new.tokenize(b)
puts "Prism:"
pp tokens

If I run this with "ruby -Ilib ./parser_gem_lexer_test.rb" I get similar output, but not quite identical:

Parser:
[[:tSTRING_BEG, ["<<\"", #<Parser::Source::Range inline ruby 2...9>]],
 [:tSTRING_CONTENT, ["1\n", #<Parser::Source::Range inline ruby 10...16>]],
 [:tSTRING_END, ["RUBY", #<Parser::Source::Range inline ruby 16...22>]],
 [:tNL, [nil, #<Parser::Source::Range inline ruby 9...10>]]]
Prism:
[[:tSTRING_BEG, ["<<\"", #<Parser::Source::Range inline ruby 2...9>]],
 [:tSTRING_CONTENT, ["    1\n", #<Parser::Source::Range inline ruby 10...16>]],
 [:tSTRING_END, ["  RUBY\n", #<Parser::Source::Range inline ruby 16...23>]],
 [:tNL, [nil, #<Parser::Source::Range inline ruby 9...10>]]]

Notice the difference between the source locations (16...22 vs 16...23) and string values ("RUBY" vs " RUBY\n").

I figured out a partial fix for the tilde-heredocs here: https://github.com/noahgibbs/prism/tree/parser_heredocs

However, it's only a partial fix -- there's a lot to heredocs, and a number of related lexer incompatibilities. And my solution was a lot of code. Rather than submitting a PR now I'll make a note of the problem here and come back to it later. If we revisit heredocs in the Translation::Parser lexer we'll probably want to do more than just this.

@noahgibbs noahgibbs added the bug Something isn't working label Feb 22, 2024
@kddnewton kddnewton added the rubocop Errors that affect Rubocop use of Prism label Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rubocop Errors that affect Rubocop use of Prism
Projects
None yet
Development

No branches or pull requests

2 participants