fix: skip leading whitespace before parse-action loc on Or and MatchFirst

Parse actions attached directly to an Or ('^') or MatchFirst ('|')
expression received the raw, un-whitespace-skipped loc instead of the
whitespace-skipped location of the matched tokens. And ('+') already
handled this correctly by setting callPreparse = True in its __init__;
Or and MatchFirst never did the equivalent correction, so they always
fell through to the callPreparse = False default set by the shared
ParseExpression base class.

Set callPreparse on Or and MatchFirst using the same "do all
alternatives pre-skip whitespace" condition Or.parseImpl already uses
internally, so tokens_start is corrected only when it is safe to do so
without changing which alternative matches (e.g. alternatives such as
White() or AtStringStart() that intentionally disable callPreparse
still prevent the pre-skip, exactly as before).

Fixes #557
This commit is contained in:
dualfroz committed 2026-09-06 01:12:11 +02:00
1 parent 1228fc2879
commit 0d6f24d7cc
2 files changed
+30

No files matched your search

+23
View File
@@ -8910,6 +8910,29 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
"failed testing parse actions being run inside a NotAny",
)
def testParseActionLocOnOrAndMatchFirst(self):
# a parse action on Or ('^') or MatchFirst ('|') should receive the
# whitespace-skipped loc, just as And ('+') does
src = " abc"
expected_loc = src.index("abc")
word = pp.Word(pp.alphas)
for label, expr in (
("Or", word ^ pp.Literal("xyz")),
("MatchFirst", word | pp.Literal("xyz")),
):
with self.subTest(label=label):
seen_locs = []
expr = expr.copy().set_parse_action(
lambda s, loc, toks: seen_locs.append(loc)
)
expr.parse_string(src)
self.assertEqual(
[expected_loc],
seen_locs,
f"{label} parse action received wrong loc (whitespace not skipped)",
)
def testParseResultsNameBelowUngroupedName(self):
rule_num = pp.Regex("[0-9]+")("LIT_NUM*")
list_num = pp.Group(