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

Offsets with \r\n in source #1020

Open
kddnewton opened this issue Jun 7, 2024 · 0 comments · May be fixed by #1022
Open

Offsets with \r\n in source #1020

kddnewton opened this issue Jun 7, 2024 · 0 comments · May be fixed by #1022

Comments

@kddnewton
Copy link

When you parse source that contains \r\n in the source, they are automatically converted into \n, as per

@source = input.gsub("\r\n".freeze, "\n".freeze).freeze
. The issue is that this can really throw off source locations. For example:

Parser::CurrentRuby.parse("1\r\n2\r\n3").children[2].loc
# => #<Parser::Source::Map::Operator:0x000000010b47c3d0 @expression=#<Parser::Source::Range (string) 4...5>, @node=s(:int, 3), @operator=nil>

This is saying the source range is 4...5, which is one of the \r characters.

For prism's purposes it's okay if the locations are different, I'll just make it so that it doesn't compare locations for files that contain \r\n. My issue is that I use the source buffer to parse with both parsers (https://github.com/ruby/prism/blob/90d570aa50bfff43c66e5f6c600370a61c091329/test/prism/ruby/parser_test.rb#L188-L208) but the source has already been modified internally in the buffer with no way to retrieve the original.

For a solution, I'm wondering if either:

(a) The \r\n gsub can be removed (and therefore have the parser instead of the buffer replace \r\n when necessary)
(b) The Buffer class could support an auto_clrf parameter (or something) that would disable that behavior

@iliabylich iliabylich linked a pull request Jun 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant