erikrose / parsimonious

The fastest pure-Python PEG parser I can muster
MIT License
1.79k stars 126 forks source link

Bug: Unexpected ParserError #233

Open spacemanspiff2007 opened 1 year ago

spacemanspiff2007 commented 1 year ago

I hope you don't mind that I ask another about grammar but I'm stuck and the error makes no sense to me.

Example to reproduce

from parsimonious.grammar import Grammar

grammar = Grammar(r""" 
    properties_def   = "[" ws? properties_comma? "]" ws?
    properties_comma = property_def ("," ws? property_def)*
    property_def     = id ws? "=" ws? property_value ws?
    property_value   = bool / number / string

    # Primitives
    bool   = "true" / "false"
    number = ~r'-?\d*(\.\d+)?'
    string = ~r'"[^"]*"' / ~r"'[^']*'" 
    id     = ~r'\w[\w-]*'
    ws     = ~r"\s+"
   """)

properties_str = 'profile="system:hysteresis",lower="29 °C",upper="30 °C"'

# this works
grammar['properties_comma'].match(properties_str)

# parsimonious.exceptions.ParseError: Rule 'ws' didn't match at '"system:hysteresis",' (line 1, column 10).
grammar['properties_def'].match('[' + properties_str + ']')

This works:

grammar['properties_comma'].match('profile="system:hysteresis",lower="29 °C",upper="30 °C"')

This throws an error:

grammar['properties_def'].match('[profile="system:hysteresis",lower="29 °C",upper="30 °C"]')
parsimonious.exceptions.ParseError: Rule 'ws' didn't match at '"system:hysteresis",' (line 1, column 10).

This seems to be the faulty rule:

"[" ws? properties_comma? "]" ws?

which does not match

'[' + properties_str + ']'

However I marked both ws rules as optional so the ParseError makes no sense. Do you have any idea why this ws error is raised?

Thank you for your help!

spacemanspiff2007 commented 1 year ago

Another PEG parser does not throw the error with the same rule so this is most likely a bug