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 error on modulo with cuddling parentheses #59

Closed herwinw closed 1 year ago

herwinw commented 1 year ago

Source: https://github.com/ruby/prime/blob/586cbdd2af7a5905121c2c93555cdc0bc3c07b50/lib/prime.rb#L46

MRI parses this code differently from Natalie. Compare these two results:

$ ruby -e 'x=13; y=5; p x%(y)'
3
$ bin/natalie -e 'x=13; y=5; p x%(y)'
13
"y"

MRI interprets %( as a String literal and somehow creates an array of these values and passes that to p.

If we remove the print, or replace it with something like an assignment, Natalie raises a parse error

$ bin/natalie -e 'x=13; y=5; x%(y)'
lib/natalie/parser.rb:32:in `parse': -e#1: syntax error, unexpected dstr '(' (expected: 'end-of-line') (SyntaxError)
x=13; y=5; x%(y)
            ^ here, expected 'end-of-line'

Adding a space between x and % does not change the behaviour (but a space between % and ( does).

seven1m commented 1 year ago

Archiving this repo in favor of YARP...