indentedBlock does not work with enablePackrat
Brought to you by:
ptmcg
Packrat parsing seems to interfere with the indentedBlock parser. Some
legitimate input is no longer recognized when packrat parsing is
switched on.
A script to demonstrate the problem is here:
http://pastebin.com/f5b006f4d
When line 7 is uncommented an exception is raised while parsing the
3rd example 'program'.
Maybe I'm using indentedBlock somehow wrongly?
A patch is attached to the bug report, that solves this bug for me. It adds a data attribute "self.noPackrat" to "ParserElement". It switches off packrat parsing for this specific parser when it is set to True. "ParserElement._parseCache" is modified to respect the new attribute.
File Added: diff_indented_block_fix.txt
The patch which solves the bug for me.
File Added: example_indented_block.py
The test script. To have complete bug description here.
Very likely an incompatibility between packratting and indentedBlock. Thanks for the patch, I'll give it a look over.
-- Paul
To explain the patch a bit more:
Packrat parsing assumes, that a parser always creates the same result for a given input. But this assumption is wrong for the "PEER" parser in "indentedBlock". It changes its mind after backtracking at the end of a block.
1. - At the end of an indented block, when the indent decreases, "PEER" creates a parse error. This is the reason for the block to end. "UNDENT" then removes one entry from the parse-stack.
2. - The parser for the enclosing block, with the lower indent, then asks "PEER" to parse the same input again. "PEER" is now supposed to succeed because the parse-stack has changed. This however can't work with packrat parsing because "PEER"'s parse result on this input has been stored in the cache. Instead of asking "PEER" for a second time to parse the input, the parse error will be retrieved and raised again.
Therefore I have added some infrastructure to exempt individual parsers from caching their results. Maybe these changes are also useful for other parsers that don't work with packrat parsing. (Maybe you know of other parsers that are incompatible with packrat parsing.)
-- Eike
Sourceforge.. Nifty :)