Skip to content

Close real-world front-end gaps: ::-paths, and/or/not, masgn consts, ..., %r/%x/%s, keyword method names - #1

Merged
tannevaled merged 1 commit into
mainfrom
frontend-realworld-gaps
Jun 26, 2026
Merged

tannevaled merged 1 commit into
mainfrom
frontend-realworld-gaps

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Confronting rbgo (go-embedded-ruby) with Rails, Puppet, and real gems showed that parser gaps are the dominant blocker to running real Ruby. This PR closes the highest-leverage ones. Every feature is verified for parity against MRI 4.0.5 (ruby -c), with parser-level table tests asserting the AST and a few ruby -e/ruby -c cross-checks.

What landed

P1 — :: scope resolution in constant-path positions (the big lever)

::-paths and a leading :: now parse everywhere MRI allows:

  • class/module name: class Foo::Bar, module A::B, class ::Top
  • superclass: class E < Foo::Bar, class G < ::StandardError
  • value: ::TopLevel, defined?(::Foo), puts ::Foo (= puts(::Foo))

ClassDef/ModuleDef gain NamePath, ClassDef gains SuperExpr, and ScopedConst gains Global for the leading-:: form. The all-bare-constant case keeps the existing string fields (NamePath/SuperExpr stay nil), so existing consumers are unaffected.

P2 — and / or / not low-precedence keyword operators

Lower precedence than =, above the trailing if/unless/while/until modifiers. Desugar to the existing &&/||/! nodes. a or return, x = 1 and break admit the jump keywords as operands. Wired into conditions, parenthesised groups, and modifiers.

P3 — multiple assignment to constant targets

A, B = 1, 2, A, b = 1, 2, a, *B = 1, 2, 3. MultiAssign gains a parallel Targets slice, left nil for the all-locals fast path so existing consumers keep working.

P4 — argument forwarding ...

def f(...) / def f(a, ...) set MethodDef.Forward; g(...) / g(1, ...) produce the new ForwardArgs node. Ranges (1...5) still parse correctly.

P5 — %r / %x / %s percent literals

%r{…}flags → RegexpLit, %x{…} → new XStr node, %s{…} → SymbolLit. Nesting and escape semantics match MRI (%r keeps the backslash in source for an escaped delimiter; %x/%s are string-like).

P6 — keyword-named methods + panic hardening

def do, def then, def in, def class, … now parse (any reserved word in def-name position). The token cursor now clamps at EOF, so malformed input (e.g. an unterminated string interpolation like "#{a) returns a clean parse error instead of indexing past the slice and panicking — a parser must never panic on bad input.

Verification

  • All 18 confirmed repros parse, cross-checked against MRI 4.0.5.
  • End-to-end: built a throwaway module importing this branch as github.com/go-ruby-parser/parser and confirmed all repros parse through the exact path rbgo imports (parser.Parse). The full rbgo binary could not be built locally due to an unrelated, pre-existing broken cloud-boot/godoom replace in the rbgo repo (no rbgo changes were made).
  • 100% coverage (CI gate), gofmt + go vet clean, CGO=0.

Follow-ups for rbgo (compiler side, not in this PR)

The new/extended AST fields are additive; rbgo's compiler will need cases to act on them:

  • ClassDef.NamePath / ClassDef.SuperExpr / ModuleDef.NamePath — define a class/module at a ::-path or with a ::-path superclass.
  • MultiAssign.Targets (non-nil) — store masgn results into constant targets.
  • ast.ForwardArgs and MethodDef.Forward — capture and splice forwarded args.
  • ast.XStr — execute the backtick command.

🤖 Generated with Claude Code

…..., %r/%x/%s, keyword method names

Confronting rbgo with Rails/Puppet/real gems showed the dominant blockers
to running real Ruby were parser gaps. This closes the highest-leverage
ones, all verified for parity against MRI 4.0.5 (`ruby -c`):

P1 — `::` scope resolution in constant-path positions. `::`-paths (and a
leading `::`) now parse as a class/module name (`class Foo::Bar`,
`module A::B`, `class ::Top`), a superclass (`class E < Foo::Bar`,
`class G < ::StandardError`), and a value (`::TopLevel`, `defined?(::Foo)`,
`puts ::Foo`). ClassDef/ModuleDef gain NamePath, ClassDef gains SuperExpr;
ScopedConst gains Global for the leading-`::` form. The all-bare-constant
case keeps the existing string fields (NamePath/SuperExpr stay nil) so
downstream consumers are unaffected.

P2 — `and`/`or`/`not` low-precedence keyword operators, below `=` and above
the trailing if/unless/while/until modifiers. They desugar to the existing
`&&`/`||`/`!` nodes; `a or return` / `x = 1 and break` admit the jump
keywords as operands. Wired into conditions, parens, and modifiers.

P3 — multiple assignment to constant targets (`A, B = 1, 2`, `A, *B = …`).
MultiAssign gains a parallel Targets slice, left nil for the all-locals
fast path so existing consumers keep working.

P4 — argument forwarding `...`: `def f(...)` / `def f(a, ...)` set
MethodDef.Forward; `g(...)` / `g(1, ...)` produce the new ForwardArgs node.

P5 — `%r{…}flags` (regexp), `%x{…}` (backtick command, new XStr node), and
`%s{…}` (symbol) percent literals, with nesting/escape semantics matching
MRI (`%r` keeps the backslash in source; `%x`/`%s` are string-like).

P6 — keyword-named methods in def position (`def do`, `def then`, `def in`,
…), and panic hardening: the token cursor now clamps at EOF, so malformed
input (e.g. an unterminated string interpolation) returns a clean parse
error instead of indexing past the slice and panicking.

100% coverage (CI gate), gofmt + go vet clean, CGO=0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 264238e into main Jun 26, 2026
9 checks passed
@tannevaled
tannevaled deleted the frontend-realworld-gaps branch June 26, 2026 05:25
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