I've been trying to work out the basic skeleton for a language I've been designing, and I'm attempting to use Parsimonious to do the parsing for me. As of right now, I've, declared the following grammar:
grammar = Grammar(
"""
program = expr*
expr = _ "{" lvalue (rvalue / expr)* "}" _
lvalue = _ ~"[a-z0-9\\-]+" _
rvalue = _ ~".+" _
_ = ~"[\\n\\s]*"
"""
)
When I try to output the resulting AST of a simple input string like "{ do-something some-argument }"
:
print(grammar.parse("{ do-something some-argument }"))
Parsimonious decides to flat-out reject it, and then gives me this somewhat cryptic error:
Traceback (most recent call last): File "tests.py", line 13, in <module> print(grammar.parse("{ do-something some-argument }")) File "/usr/local/lib/python2.7/dist-packages/parsimonious/grammar.py", line 112, in parse return self.default_rule.parse(text, pos=pos) File "/usr/local/lib/python2.7/dist-packages/parsimonious/expressions.py", line 109, in parse raise IncompleteParseError(text, node.end, self) parsimonious.exceptions.IncompleteParseError: Rule 'program' matched in its entirety, but it didn't consume all the text. The non-matching portion of the text begins with '{ do-something some-' (line 1, column 1).
At first I thought this might be an issue related to my whitespace rule, _
, but after a few failed attempts at removing the whitespace rule in certain places, I was still coming up with the same error.
I've tried searching online, but all I've found that seems to be remotely related, is this question, which didn't help me in any way.
Am I doing something wrong with my grammar? Am I not parsing the input in the correct way? If anyone has a possible solution to this, it'd be greatly appreciated.
rvalue = _ ~"[a-z\\-]+" _
works, with nothing else changed. I do not know Parsimonious at all well, but I'm reasonably familiar with regexes and used to be with yacc; my hypothesis is that.+
is greedily matching the rest of the input string, leaving nothing to match the rest of the production. – Sclerite.+
to something else helped. If you want to post an answer about it, feel free to :-). – Marrakech