whitequark / whitequark/parser
Offsets with \r\n in source
Nobody has claimed this yet.
- Dominant language
- Yacc
- Stars
- 1.6k
- Forks
- 205
- PR merge metrics
- No merged PRs in 30d
Description
When you parse source that contains \r\n in the source, they are automatically converted into \n, as per https://github.com/whitequark/parser/blob/8bd5ec3416d58c28ab230b6e34a3bba1b58a8f06/lib/parser/source/buffer.rb#L190. 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the CRLF normalization in lib/parser/source/buffer.rb and the shared-buffer usage in test/prism/ruby/parser_test.rb, using Parser::CurrentRuby.parse as the entry point. Determine the agreed behavior for preserving original source or handling offsets, then add regression coverage showing that CRLF input remains usable by both parsers without losing the original source.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100