parsimonious icon indicating copy to clipboard operation
parsimonious copied to clipboard

Bug: Unexpected ParserError

Open spacemanspiff2007 opened this issue 2 years ago • 1 comments

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 avatar Jun 30 '23 04:06 spacemanspiff2007

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

spacemanspiff2007 avatar Jul 13 '23 10:07 spacemanspiff2007