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

+7
View File
@@ -4703,6 +4703,9 @@ class Or(ParseExpression):
if self.exprs:
self._may_return_empty = any(e.mayReturnEmpty for e in self.exprs)
self.skipWhitespace = all(e.skipWhitespace for e in self.exprs)
# pre-skip whitespace here only if every alternative would have
# done so itself, matching the condition used in parseImpl below
self.callPreparse = all(e.callPreparse for e in self.exprs)
else:
self._may_return_empty = True
@@ -4863,6 +4866,10 @@ class MatchFirst(ParseExpression):
if self.exprs:
self._may_return_empty = any(e.mayReturnEmpty for e in self.exprs)
self.skipWhitespace = all(e.skipWhitespace for e in self.exprs)
# pre-skip whitespace here only if every alternative would have
# done so itself; an alternative starting with White() must not
# lose the whitespace it needs to see
self.callPreparse = all(e.callPreparse for e in self.exprs)
else:
self._may_return_empty = True