Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,10 @@ func (l *Lexer) lexSymbol(spaceBefore bool, line, col int) token.Token {
return token.Token{Type: token.SYMBOL, Lit: op, Line: line, Col: col, SpaceBefore: spaceBefore}
}
start := l.pos
// Variable-name symbols: :@ivar, :@@cvar, :$global.
// Variable-name symbols: :@ivar, :@@cvar, :$global — including the special
// globals (:$~ :$! :$& :$` …), the option globals (:$-w), and match-group
// references (:$1), which are complete as soon as their one special byte /
// digit run is consumed and take no ?/!/= suffix.
switch l.peek() {
case '@':
l.advance()
Expand All @@ -863,6 +866,25 @@ func (l *Lexer) lexSymbol(spaceBefore bool, line, col int) token.Token {
}
case '$':
l.advance()
switch c := l.peek(); {
case c == '-':
l.advance()
if isIdentPart(l.peek()) {
l.advance()
}
l.state = exprEnd
return token.Token{Type: token.SYMBOL, Lit: string(l.src[start:l.pos]), Line: line, Col: col, SpaceBefore: spaceBefore}
case isSpecialGvar(c):
l.advance()
l.state = exprEnd
return token.Token{Type: token.SYMBOL, Lit: string(l.src[start:l.pos]), Line: line, Col: col, SpaceBefore: spaceBefore}
case c >= '1' && c <= '9':
for l.peek() >= '0' && l.peek() <= '9' {
l.advance()
}
l.state = exprEnd
return token.Token{Type: token.SYMBOL, Lit: string(l.src[start:l.pos]), Line: line, Col: col, SpaceBefore: spaceBefore}
}
}
for isIdentPart(l.peek()) {
l.advance()
Expand Down
49 changes: 39 additions & 10 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,14 +952,19 @@ func (p *Parser) parseReturn() ast.Node {
if p.atStatementEnd() {
return &ast.Return{}
}
first := p.parseExprOrAssign()
first := p.parseRhsElem()
if !p.is(token.COMMA) {
// A bare `return *ary` splats into a one-element array (`return [*ary]`),
// matching `x = *ary`; a plain value is returned as-is.
if sp, ok := first.(*ast.SplatArg); ok {
return &ast.Return{Value: &ast.ArrayLit{Elems: []ast.Node{sp}}}
}
return &ast.Return{Value: first}
}
// `return a, b, …` returns an array of the values.
// `return a, b, …` returns an array of the values (a `*splat` element allowed).
elems := []ast.Node{first}
for p.accept(token.COMMA) {
elems = append(elems, p.parseExprOrAssign())
elems = append(elems, p.parseRhsElem())
}
return &ast.Return{Value: &ast.ArrayLit{Elems: elems}}
}
Expand Down Expand Up @@ -1017,13 +1022,17 @@ func (p *Parser) parseNext() ast.Node {
// or a comma-separated list gathered into an array (`next table, true`,
// `break a, b`), matching `return a, b`.
func (p *Parser) parseJumpValue() ast.Node {
first := p.parseExprOrAssign()
first := p.parseRhsElem()
if !p.is(token.COMMA) {
// `break *ary` / `next *ary` splat into a one-element array, as `return *ary`.
if sp, ok := first.(*ast.SplatArg); ok {
return &ast.ArrayLit{Elems: []ast.Node{sp}}
}
return first
}
elems := []ast.Node{first}
for p.accept(token.COMMA) {
elems = append(elems, p.parseExprOrAssign())
elems = append(elems, p.parseRhsElem())
}
return &ast.ArrayLit{Elems: elems}
}
Expand Down Expand Up @@ -2012,17 +2021,26 @@ func (p *Parser) parseTernary() ast.Node {
// blank lines anywhere inside the ternary are tolerated too, matching MRI. A
// newline *before* the `?` never gets here: it terminates the condition first.
p.skipNewlines()
// Each branch may itself be an assignment (`c ? a = b : d`,
// `x ? ENV["k"] = v : super`), which MRI permits in this position even though
// `=` otherwise binds looser than `?:`.
then := p.maybeInlineAssign(p.parseTernary())
then := p.ternaryArm()
p.skipNewlines()
p.expect(token.COLON)
p.skipNewlines()
els := p.maybeInlineAssign(p.parseTernary())
els := p.ternaryArm()
return &ast.If{Cond: cond, Then: []ast.Node{then}, Else: []ast.Node{els}}
}

// ternaryArm parses one arm of a ternary. A bare value-less control-flow jump is
// allowed here (`cond ? next : x`, `flag ? break : y`), matching MRI. Otherwise
// the arm may itself be an assignment (`c ? a = b : d`, `x ? ENV["k"] = v :
// super`), which MRI permits in this position even though `=` binds looser than
// `?:`.
func (p *Parser) ternaryArm() ast.Node {
if j, ok := p.valuelessJumpOperand(); ok {
return j
}
return p.maybeInlineAssign(p.parseTernary())
}

// parseRange handles `lo..hi` / `lo...hi`, binding looser than the binary
// operators but tighter than assignment.
func (p *Parser) parseRange() ast.Node {
Expand Down Expand Up @@ -2643,6 +2661,17 @@ func (p *Parser) parseBlockParams(until token.Type) (names []string, defaults, p
}
group := 0
for {
if p.is(until) || p.is(token.NEWLINE) {
// A trailing comma (`|a,|`, `{ |k,| }`) forces array destructuring of a
// single block argument — equivalent to an anonymous rest `|a, *|`, so
// `[1, 2]` yields a == 1 rather than a == [1, 2].
if splat < 0 {
splat = len(names)
names = append(names, "*")
defaults = append(defaults, nil)
}
break
}
if p.accept(token.AMPER) { // &block param (always last), mirroring def params
// `->(&) {}` — anonymous block param, sentinel name "&".
if p.is(token.IDENT) {
Expand Down
120 changes: 120 additions & 0 deletions robustness_batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package parser

import (
"testing"

"github.com/go-ruby-parser/parser/ast"
)

// TestJumpValueSplat covers a `*splat` as the value of return/break/next, which
// MRI gathers into an implicit array (`return *ary` ≡ `return [*ary]`).
func TestJumpValueSplat(t *testing.T) {
for _, src := range []string{
`def r; return *ary; end`,
`def r; return 1, *ary; end`,
`while true; break *[1, 2]; end`,
`x.each { next *nil }`,
`loop { break *a, *b }`,
} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) error: %v", src, err)
}
}
// `return *ary` wraps the splat in a one-element array.
prog, err := Parse(`def r; return *ary; end`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
def := prog.Body[0].(*ast.MethodDef)
ret := def.Body[len(def.Body)-1].(*ast.Return)
arr, ok := ret.Value.(*ast.ArrayLit)
if !ok || len(arr.Elems) != 1 {
t.Fatalf("return *ary: expected one-element ArrayLit, got %#v", ret.Value)
}
if _, ok := arr.Elems[0].(*ast.SplatArg); !ok {
t.Fatalf("return *ary: expected SplatArg element, got %T", arr.Elems[0])
}
}

// TestBlockParamTrailingComma covers a trailing comma in a block parameter list
// (`{ |a,| }`), which MRI treats as forcing array destructuring of a single
// argument — equivalent to an anonymous rest `|a, *|`.
func TestBlockParamTrailingComma(t *testing.T) {
for _, src := range []string{
`m { |a,| a }`,
`h.each_pair { |k,| k }`,
`lambda { |a, | a }`,
`define_method(:m) { |a,| a }`,
`m { |a, b,| a }`,
} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) error: %v", src, err)
}
}
// `{ |a,| }` records an anonymous rest so a single array argument destructures.
prog, err := Parse(`m { |a,| a }`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
call := prog.Body[0].(*ast.Call)
if call.Block == nil {
t.Fatal("expected a block")
}
if call.Block.SplatIndex != 1 {
t.Fatalf("expected an anonymous rest at index 1 (|a, *|), got SplatIndex=%d params=%v",
call.Block.SplatIndex, call.Block.Params)
}
}

// TestTernaryJumpArm covers a bare value-less control-flow jump as a ternary arm
// (`cond ? next : x`, `flag ? break : y`), which MRI permits.
func TestTernaryJumpArm(t *testing.T) {
for _, src := range []string{
`(x == 3 ? next : y) while i < 10`,
`(x == 3 ? next : y) until i > 10`,
`z = cond ? break : 5`,
`a ? next : b`,
`a ? b : redo`,
`a ? return : c`,
} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) error: %v", src, err)
}
}
// The ordinary (non-jump) ternary is unaffected.
for _, src := range []string{`cond ? 1 : 2`, `x ? a = 1 : b = 2`} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) regressed: %v", src, err)
}
}
}

// TestSpecialGvarSymbol covers symbols naming the special global variables
// (`:$~`, `:$!`, `:$&`), the option globals (`:$-w`) and match-group references
// (`:$1`), alongside the ordinary `:$name` / `:@ivar` symbol forms.
func TestSpecialGvarSymbol(t *testing.T) {
for _, src := range []string{
`bind.local_variable_get(:$~)`,
`p [:$!, :$&, :$', :$/, :$;, :$,, :$., :$<, :$>, :$0, :$*, :$$, :$?, :$:, :$"]`,
"p :$`",
`p :$1`,
`p :$12`,
`p :$-w`,
`p :$stdin`,
`p :$LOAD_PATH`,
} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) error: %v", src, err)
}
}
// The symbol names the special global including its `$`.
prog, err := Parse(`p :$~`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
call := prog.Body[0].(*ast.Call)
sym, ok := call.Args[0].(*ast.SymbolLit)
if !ok || sym.Name != "$~" {
t.Fatalf("expected SymbolLit :$~, got %#v", call.Args[0])
}
}