Commit 80101efb authored by Igor Dejanovic's avatar Igor Dejanovic

NoMatch memoization

parent 5374d79c
...@@ -178,6 +178,12 @@ class ParsingExpression(object): ...@@ -178,6 +178,12 @@ class ParsingExpression(object):
print("** Cache hit for [{}, {}] = '{}'" print("** Cache hit for [{}, {}] = '{}'"
.format(self.name, c_pos, str(result))) .format(self.name, c_pos, str(result)))
print("<< Leaving rule {}".format(self.name)) print("<< Leaving rule {}".format(self.name))
# If NoMatch is recorded at this position raise.
if isinstance(result, NoMatch):
parser._nm_raise(result)
# else return cached result
return result return result
# We are descending down # We are descending down
...@@ -192,8 +198,10 @@ class ParsingExpression(object): ...@@ -192,8 +198,10 @@ class ParsingExpression(object):
try: try:
result = self._parse(parser) result = self._parse(parser)
except NoMatch: except NoMatch as e:
parser.position = c_pos # Backtracking parser.position = c_pos # Backtracking
# Memoize NoMatch at this position for this rule
self.result_cache[c_pos] = (e, c_pos)
raise raise
finally: finally:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment