Repository navigation
Close real-world front-end gaps: ::-paths, and/or/not, masgn consts, ..., %r/%x/%s, keyword method names - #1
Merged
Conversation
…..., %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>
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.
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 fewruby -e/ruby -ccross-checks.What landed
P1 —
::scope resolution in constant-path positions (the big lever)::-paths and a leading::now parse everywhere MRI allows:class Foo::Bar,module A::B,class ::Topclass E < Foo::Bar,class G < ::StandardError::TopLevel,defined?(::Foo),puts ::Foo(=puts(::Foo))ClassDef/ModuleDefgainNamePath,ClassDefgainsSuperExpr, andScopedConstgainsGlobalfor the leading-::form. The all-bare-constant case keeps the existing string fields (NamePath/SuperExprstay nil), so existing consumers are unaffected.P2 —
and/or/notlow-precedence keyword operatorsLower precedence than
=, above the trailing if/unless/while/until modifiers. Desugar to the existing&&/||/!nodes.a or return,x = 1 and breakadmit 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.MultiAssigngains a parallelTargetsslice, left nil for the all-locals fast path so existing consumers keep working.P4 — argument forwarding
...def f(...)/def f(a, ...)setMethodDef.Forward;g(...)/g(1, ...)produce the newForwardArgsnode. Ranges (1...5) still parse correctly.P5 —
%r/%x/%spercent literals%r{…}flags→RegexpLit,%x{…}→ newXStrnode,%s{…}→SymbolLit. Nesting and escape semantics match MRI (%rkeeps the backslash in source for an escaped delimiter;%x/%sare 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
github.com/go-ruby-parser/parserand confirmed all repros parse through the exact path rbgo imports (parser.Parse). The fullrbgobinary could not be built locally due to an unrelated, pre-existing brokencloud-boot/godoomreplace in the rbgo repo (no rbgo changes were made).gofmt+go vetclean, 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.ForwardArgsandMethodDef.Forward— capture and splice forwarded args.ast.XStr— execute the backtick command.🤖 Generated with Claude Code