I am using plyplus to design a simple grammar and I have been struggling with some weird error for a while. Please bear in mind I am a newbie. Here is a piece of code that reproduces the issue:
from plyplus import Grammar
list_parser = Grammar("""
start: context* ;
context : WORD '{' (rule)* '}' ;
rule: 'require' space_marker ;
space_marker: 'newline'
| 'tab'
| 'space'
;
WORD: '\w+' ;
SPACES: '[ \t\n]+' (%ignore) ;
""", auto_filter_tokens=False)
res = list_parser.parse("test { require tab }")
If my input string contains require space
or require newline
, it works perfectly fine. However, as soon as I provide require tab
, an exception is thrown:
Traceback (most recent call last):
File "/Users/bore/Projects/ThesisCode/CssCoco/coco/plytest.py", line 18, in <module>
res = list_parser.parse("test { require tab }")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plyplus/plyplus.py", line 584, in parse
return self._grammar.parse(text)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plyplus/plyplus.py", line 668, in parse
raise ParseError('\n'.join(self.errors))
plyplus.plyplus.ParseError: Syntax error in input at 'tab' (type WORD) line 1 col 16
Ironically, I do not get this exception every time I run the code, but exactly once in three times. I noticed that if I change the grammar and the input from tab
to ta
, I get the same exception every time I run the code. Also, if I change it to tabb
, the error is gone.
The error suggests that tab
is parsed as a WORD instead of a space_marker. However, tabb
is also a WORD. From my trial and error it seems that plyplus is sensitive to the specific string I provide as a keyword. Am I missing something? Any help/hints/comments will be highly appreciated!