From f5ffaa2ab8e67668604c2542cead207a5d3110cf Mon Sep 17 00:00:00 2001 From: Paul McGuire Date: Sun, 13 Oct 2019 13:30:17 -0500 Subject: Backport changes from 3.0.0 to fix PrecededBy bug (Issue #127) --- pyparsing.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'pyparsing.py') diff --git a/pyparsing.py b/pyparsing.py index ffbe78b..17e0bdf 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -4570,6 +4570,7 @@ class PrecededBy(ParseElementEnhance): self.retreat = retreat self.errmsg = "not preceded by " + str(expr) self.skipWhitespace = False + self.parseAction.append(lambda s, l, t: t.__delitem__(slice(None, None))) def parseImpl(self, instring, loc=0, doActions=True): if self.exact: @@ -4580,19 +4581,18 @@ class PrecededBy(ParseElementEnhance): else: # retreat specified a maximum lookbehind window, iterate test_expr = self.expr + StringEnd() - instring_slice = instring[:loc] + instring_slice = instring[max(0, loc - self.retreat):loc] last_expr = ParseException(instring, loc, self.errmsg) - for offset in range(1, min(loc, self.retreat + 1)): + for offset in range(1, min(loc, self.retreat + 1)+1): try: - _, ret = test_expr._parse(instring_slice, loc - offset) + # print('trying', offset, instring_slice, repr(instring_slice[loc - offset:])) + _, ret = test_expr._parse(instring_slice, len(instring_slice) - offset) except ParseBaseException as pbe: last_expr = pbe else: break else: raise last_expr - # return empty list of tokens, but preserve any defined results names - del ret[:] return loc, ret -- cgit v1.2.1