whitequark / whitequark/parser

Newlines before comments

Open
#1,025 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Yacc
Stars
1.6k
Forks
205
PR merge metrics
No merged PRs in 30d

Description

In the following code:

class C
  # foo
end

I would expect the newline token after the C constant to be emitted before the comment token, but it is the opposite:

irb(main):005> Parser::CurrentRuby.new.tokenize(Parser::Source::Buffer.new("", source: "class C\n# foo\nend\n"))
=> 
[s(:class,
  s(:const, nil, :C), nil, nil),
 [#<Parser::Source::Comment :2:1 "# foo">],
 [[:kCLASS, ["class", #<Parser::Source::Range  0...5>]],
  [:tCONSTANT, ["C", #<Parser::Source::Range  6...7>]],
  [:tCOMMENT, ["# foo", #<Parser::Source::Range  8...13>]],
  [:tNL, [nil, #<Parser::Source::Range  7...8>]],
  [:kEND, ["end", #<Parser::Source::Range  14...17>]],
  [:tNL, [nil, #<Parser::Source::Range  17...18>]]]]

I can work around this, but I just want to clarify what the rule is here?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Parser::CurrentRuby.tokenize example using the shown class and comment source, and inspect how the comment and newline tokens are emitted. Compare the observed order with the parser's intended rule for comments and newlines; the issue is done when that rule is clarified and the expected behavior is established.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.