Commit 34f0ba2b authored by Igor Dejanovic's avatar Igor Dejanovic

Fixing bug in determining position where NonTerminal was recognized.

parent 58e82c62
...@@ -152,19 +152,26 @@ class ParsingExpression(object): ...@@ -152,19 +152,26 @@ class ParsingExpression(object):
if parser.debug: if parser.debug:
print "Parsing %s" % self.name print "Parsing %s" % self.name
parser._skip_ws() parser._skip_ws()
# Set the begining position in input stream of
# this parsing expression
self.c_pos = parser.position self.c_pos = parser.position
def parse(self, parser): def parse(self, parser):
self._parse_intro(parser) self._parse_intro(parser)
#Memoization. # Current position could change in recursive calls
#If this position is already parsed by this parser expression than use # so save it.
#the result c_pos = self.c_pos
if self.c_pos in self.result_cache:
# Memoization.
# If this position is already parsed by this parser expression than use
# the result
if c_pos in self.result_cache:
if parser.debug: if parser.debug:
print "Result for [%s, %s] founded in result_cache." % \ print "Result for [%s, %s] founded in result_cache." % \
(self, self.c_pos) (self, self.c_pos)
result, new_pos = self.result_cache[self.c_pos] result, new_pos = self.result_cache[c_pos]
parser.position = new_pos parser.position = new_pos
return result return result
...@@ -180,15 +187,15 @@ class ParsingExpression(object): ...@@ -180,15 +187,15 @@ class ParsingExpression(object):
if self.root: if self.root:
result = flatten(result) result = flatten(result)
if len(result) > 1: if len(result) > 1:
result = NonTerminal(self.rule, self.c_pos, result) result = NonTerminal(self.rule, c_pos, result)
else: else:
result = result[0] result = result[0]
else: else:
if self.root: if self.root:
result = NonTerminal(self.rule, self.c_pos, result) result = NonTerminal(self.rule, c_pos, result)
# Result caching for use by memoization. # Result caching for use by memoization.
self.result_cache[self.c_pos] = (result, parser.position) self.result_cache[c_pos] = (result, parser.position)
return result return result
......
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