I'm trying to parse a little language with apostrophes in it using pyparsing, it was all going well until suddenly I started getting mysterious errors that I can't debug.
I've cut down my parser to the minimal thing that causes the error:
As an example of what I'm trying to do, consider a language with ones, and nested lists of ones.
e.g. 1(11)1(1((11)1))
which can be parsed so:
from pyparsing import *
sound=Regex(r"1")
beat=sound ^ nestedExpr(content=sound)
tune=OneOrMore(beat)
print(tune.parseString("1"))
print(tune.parseString("11"))
print(tune.parseString("(1)"))
print(tune.parseString("(1(1))"))
But if I try to add the apostrophe, so the fundamental unit is '1
instead:
e.g. '1('1'1)'1('1(('1'1)'1))
sound=Regex(r"'1")
beat=sound ^ nestedExpr(content=sound)
tune=OneOrMore(beat)
#These all work as expected
print(tune.parseString("'1"))
print(tune.parseString("'1'1"))
print(tune.parseString("('1)"))
#but
print(tune.parseString("('1('1))"))
causes an exception
pyparsing.ParseException: Expected {Re:("'1") ^ nested () expression}, found '1' (at char 5), (line:1, col:6)
Can anyone tell me how to make the second example work like the first one, so that any string accepted by the first one will be accepted by the first one after replacing every 1
with '1
?
nestedExpr
has a default of ignoreExpr=quotedString
. This attempts to match the leading '
as a quote. Disable it by setting ignoreExpr=None
:
>>> sound=Regex(r"'1")
>>> beat=sound ^ nestedExpr(content=sound, ignoreExpr=None)
>>> tune=OneOrMore(beat)
>>> print(tune.parseString("('1('1))"))
[["'1", ["'1"]]]