natalie-lang / natalie_parser

NatalieParser is a zero-dependency, from-scratch, hand-written recursive descent parser for the Ruby Programming Language.
MIT License
63 stars 8 forks source link

Parse issue in while with begin/end conditional #52

Closed nanobowers closed 1 year ago

nanobowers commented 1 year ago

Not sure i've written code like this - I found it out in the wild when testing the parser.

l = 0
while begin l+=1; l<5 end
  puts l
end
08:26 $ ruby -I lib:ext -r natalie_parser -e 'p NatalieParser.parse(File.read("../temp_natalie_parser_tests/whilecond.rb"))'
-e:1:in `parse': (string)#2: syntax error, unexpected name 'l' (expected: 'end-of-line') (SyntaxError)
while begin l+=1; l<5 end
            ^ here, expected 'end-of-line'
        from -e:1:in `<main>'
nanobowers commented 1 year ago

I found another case that is similar but different, it tries to parse to the EOF (or end of text) and seems to be expecting a missing end statement:

def abc
  a,b = true,false
  while a && b do
    puts :hi
  end
end

In case this helps, workarounds that make the problem go away are changing the while line to one of these two:

while a && b         # without the "do"
while (a && b) do    # add parentheses
seven1m commented 1 year ago

Archiving this repo in favor of YARP...