whitequark / parser

A Ruby parser.
Other
1.58k stars 198 forks source link

+ ruby31.y: parse forward argument without parentheses #832

Closed iliabylich closed 2 years ago

iliabylich commented 2 years ago

This commit tracks upstream commit ruby/ruby@13a9597.

Closes https://github.com/whitequark/parser/issues/823.

Also I think I've found a bug in MRI implementation. This code is valid on 3.0:

def foo a = 1...
2
  p a
end

foo

It's a method with 1 argument that has a default value 1...2. Current MRI/master gives an error:

../parser/test.rb:1: syntax error, unexpected (..., expecting ';' or '\n'
def foo a = 1...
../parser/test.rb:4: syntax error, unexpected `end', expecting end-of-input

... because it treats all triple-dot tokens followed by newline between method-name and method-body as "forward argument".

I think I know how to fix it, there's a single flag for "being in a method arguments definition section", right now it's set once at the beginning of arglist (with a few exceptions that temporary unset-on-enter and restore-on-exit), and for example optional arguments do not affect it, but they should in a very similar way, push on reading default value, pop on leave.

Let's pretend it works as intended 👀