Skip to content

fix(parser): allow a ternary split across lines (newline before :) - #16

Merged
tannevaled merged 1 commit into
mainfrom
fix/multiline-ternary
Aug 3, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/multiline-ternary

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Problem

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 parser rejected the shape found while authoring the Ruby wasmdesk client:

y = flag ?
  "yes"
: "no"

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

parseTernary now skipNewlines() 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 : c precedence, ?c character literals, and foo? / x.empty? predicate methods — all covered by TestMultilineTernaryNegatives.

Every case cross-checked against MRI 4.0.5.

Gate

  • go vet, gofmt clean
  • -race + 100.0% coverage (CI coverpkg gate)
  • Builds on all six 64-bit arches (amd64/arm64/riscv64/loong64/ppc64le/s390x) + js/wasm + wasip1/wasm

🤖 Generated with Claude Code

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>
@tannevaled
tannevaled merged commit 9f5f8f3 into main Aug 3, 2026
11 checks passed
@tannevaled
tannevaled deleted the fix/multiline-ternary branch August 3, 2026 10:58
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.

1 participant