Repository navigation
fix(parser): allow a ternary split across lines (newline before :) - #16
Merged
Merged
Conversation
MRI (ruby 4.0.x) accepts a ternary `cond ? then : else` whose arms and separators span physical lines: newlines after `?`, on either side of `:`, and before the else arm. The lexer already joins a line ending in `?` or `:` (isContinuationOp), but the newline it cannot swallow is the one BEFORE the `:` (the preceding token is a value, not a continuation operator), so `flag ?`<nl>`"yes"`<nl>`: "no"` failed with `expected :, got NEWLINE`. parseTernary now skips newlines at each ternary seam once a `?` has been consumed, so blank lines anywhere inside the ternary are tolerated too. A newline BEFORE the `?` still terminates the condition first and is not a ternary, matching MRI. Single-line ternary, `?c` char literals, and `foo?`-suffixed method names are unaffected. Cross-checked against MRI 4.0.5. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MRI (
ruby4.0.x) accepts a ternarycond ? then : elsewhose arms and separators span physical lines — newlines after?, on either side of:, and before the else arm. The parser rejected the shape found while authoring the Ruby wasmdesk client:with
parse error: expected :, got NEWLINE. The lexer's line-continuation joins a line ending in?or:(isContinuationOp), but the newline it cannot know to swallow is the one before the:(the preceding token is a value, not a continuation operator).Fix
parseTernarynowskipNewlines()at each ternary seam once a?has been consumed — after?, before:, and after:— so any of MRI's line breaks (and blank lines) inside the ternary parse. A newline before the?still terminates the condition first and is never a ternary, matching MRI.Not broken
Single-line ternary,
a ? b : cprecedence,?ccharacter literals, andfoo?/x.empty?predicate methods — all covered byTestMultilineTernaryNegatives.Every case cross-checked against MRI 4.0.5.
Gate
go vet, gofmt clean-race+ 100.0% coverage (CI coverpkg gate)🤖 Generated with Claude Code