Skip to content

Commit

Permalink
Fix whitespace skipping bug introduced while reverting LineStart() ch…
Browse files Browse the repository at this point in the history
…anges - Issue #319
  • Loading branch information
ptmcg committed Oct 27, 2021
1 parent 46f4af6 commit 1bbc832
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -6,6 +6,10 @@ Version 3.0.3 -
---------------
- Fixed regex typo in `one_of` fix for `as_keyword=True`.

- Fixed a whitespace-skipping bug, Issue #319, introduced as part of the revert
of the `LineStart` changes. Reported by Marc-Alexandre Côté,
thanks!


Version 3.0.2 -
---------------
Expand Down
9 changes: 5 additions & 4 deletions pyparsing/core.py
Expand Up @@ -3853,7 +3853,9 @@ def __init__(self, exprs: IterableType[ParserElement], savelist: bool = False):
def streamline(self):
super().streamline()
if self.exprs:
self.mayReturnEmpty = any(e.mayReturnEmpty for e in self.exprs)
self.saveAsList = any(e.saveAsList for e in self.exprs)
self.skipWhitespace = all(e.skipWhitespace for e in self.exprs)
else:
self.saveAsList = False
return self
Expand Down Expand Up @@ -3967,7 +3969,7 @@ def _setResultsName(self, name, listAllMatches=False):

class MatchFirst(ParseExpression):
"""Requires that at least one :class:`ParseExpression` is found. If
two expressions match, the first one listed is the one that will
more than one expression matches, the first one listed is the one that will
match. May be constructed using the ``'|'`` operator.
Example::
Expand All @@ -3987,7 +3989,6 @@ def __init__(self, exprs: IterableType[ParserElement], savelist: bool = False):
super().__init__(exprs, savelist)
if self.exprs:
self.mayReturnEmpty = any(e.mayReturnEmpty for e in self.exprs)
self.callPreparse = all(e.callPreparse for e in self.exprs)
self.skipWhitespace = all(e.skipWhitespace for e in self.exprs)
else:
self.mayReturnEmpty = True
Expand All @@ -4000,7 +4001,7 @@ def streamline(self):
if self.exprs:
self.saveAsList = any(e.saveAsList for e in self.exprs)
self.mayReturnEmpty = any(e.mayReturnEmpty for e in self.exprs)
self.callPreparse = all(e.callPreparse for e in self.exprs)
self.skipWhitespace = all(e.skipWhitespace for e in self.exprs)
else:
self.saveAsList = False
self.mayReturnEmpty = True
Expand All @@ -4013,7 +4014,7 @@ def parseImpl(self, instring, loc, doActions=True):
for e in self.exprs:
try:
return e._parse(
instring, loc, doActions, callPreParse=not self.callPreparse
instring, loc, doActions,
)
except ParseFatalException as pfe:
pfe.__traceback__ = None
Expand Down
23 changes: 13 additions & 10 deletions tests/test_unit.py
Expand Up @@ -3617,16 +3617,19 @@ def testLineStart2(self):

def testLineStartWithLeadingSpaces(self):
# testing issue #272
# reverted in 3.0.2 - LineStart() + expr will match expr even if there
# are leading spaces. To force "only at column 1" matching, use
# AtLineStart(expr).
instring = dedent(
"""
a
b
c
d
e
f
g
"""
a
b
c
d
e
f
g
"""
)
print(pp.testing.with_line_numbers(instring))

Expand Down Expand Up @@ -7711,10 +7714,10 @@ def testEnableDebugWithCachedExpressionsMarkedWithAsterisk(self):
Match leading_a at loc 1(1,2)
aba
^
*Match A at loc 1(1,2)
Match A at loc 1(1,2)
aba
^
*Match A failed, ParseException raised: Expected A, found 'ba' (at char 1), (line:1, col:2)
Match A failed, ParseException raised: Expected A, found 'ba' (at char 1), (line:1, col:2)
Match leading_a failed, ParseException raised: Expected A, found 'ba' (at char 1), (line:1, col:2)
*Match B at loc 1(1,2)
aba
Expand Down

0 comments on commit 1bbc832

Please sign in to comment.