Skip to content

parser: add Builders::Default#value, string_value and loc - #1104

Open
apiology wants to merge 3 commits into
ruby:mainfrom
apiology:parser-builder-value
Open

apiology wants to merge 3 commits into
ruby:mainfrom
apiology:parser-builder-value

Conversation

@apiology

Copy link
Copy Markdown
Contributor

Claude: This PR was written by Claude Code on behalf of @apiology.

Problem:

A Parser::Builders::Default subclass gets untyped back from value, and cannot call string_value or loc:

class FlawedBuilder < ::Parser::Builders::Default
  # @param token [Array(String, ::Parser::Source::Range)]
  # FlawedBuilder#string_value return type could not be inferred
  def string_value token
    value(token)
  end
end

class TestBuilder < Parser::Builders::Default
  def token_loc(range)
    # test.rb:89:4: [error] Type `::TestBuilder` does not have method `loc`
    loc(["foo", range])&.source
  end
end

The parser RBS has no Builders::Default signatures, so every builder subclass loses the token's type.

Solution:

Add private value: [T] ([T, Source::Range] token) -> T, string_value: ([String, Source::Range] token) -> String and loc: ([top, Source::Range]? token) -> Source::Range?, matching value, string_value and loc in parser v3.3.12.0.

apiology and others added 3 commits October 16, 2026 14:41
Builders::Default#value(token) returns token[0], the value half of a
[value, range] token pair. Subclasses of the default builder call it,
so type it generically over the value.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GgkyXKEZWpEFXo6ceA5MSw
string_value(token) returns the String half of a token pair. loc(token)
returns the Range half, or nil when the token or its value is nil.
Both are private helpers that builder subclasses call.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GgkyXKEZWpEFXo6ceA5MSw
The lexer builds every token as [value, range(s, e)], so a token's
second element is always a Parser::Source::Range. Replace the untyped
slots in value, string_value and loc, and pass a real range in the
tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GgkyXKEZWpEFXo6ceA5MSw
@apiology
apiology marked this pull request as ready for review October 16, 2026 22:12
@github-actions

Copy link
Copy Markdown

@apiology Thanks for your contribution!

Please follow the instructions below for each change.
See also: https://github.com/ruby/gem_rbs_collection/blob/main/docs/CONTRIBUTING.md

Available commands

You can use the following commands by commenting on this PR.

  • /merge: Merge this PR if CI passes

parser

You changed RBS files for an existing gem.
You need to get approval from the reviewers of this gem.

@pocke, @kozy4324, please review this pull request.
If this change is acceptable, please make a review comment including APPROVE from here.
Screen Shot 2024-03-19 at 14 13 36

After that, the PR author or the reviewers can merge this PR.
Just comment /merge to merge this PR.

@kozy4324 kozy4324 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVE

@github-actions

Copy link
Copy Markdown

Thanks for your review, @kozy4324!

@apiology, @kozy4324 This PR is ready to be merged.
Just comment /merge to merge this PR.

@apiology

Copy link
Copy Markdown
Contributor Author

/merge

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 this pull request may close these issues.

2 participants