Skip to content

Commit

Permalink
Merge pull request #493
Browse files Browse the repository at this point in the history
* Limit error message overriding when raising exception in Forward; add test for forward exception interception
  • Loading branch information
ksunden committed Jul 29, 2023
1 parent a60ec32 commit 173bc16
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyparsing/core.py
Expand Up @@ -4547,7 +4547,8 @@ def parseImpl(self, instring, loc, doActions=True):
try:
return self.expr._parse(instring, loc, doActions, callPreParse=False)
except ParseBaseException as pbe:
pbe.msg = self.errmsg
if not isinstance(self, Forward) or self.customName is not None:
pbe.msg = self.errmsg
raise
else:
raise ParseException(instring, loc, "No expression defined", self)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_unit.py
Expand Up @@ -9897,6 +9897,29 @@ def modify_upper(self, tokens):
msg=f"unexpected exception line ({exception_line!r})",
)


def testForwardReferenceException(self):
token = pp.Forward()
num = pp.Word(pp.nums)
num.setName("num")
text = pp.Word(pp.alphas)
text.setName("text")
fail = pp.Regex(r"\\[A-Za-z]*")("name")
def parse_fail(s, loc, toks):
raise pp.ParseFatalException(s, loc, f"Unknown symbol: {toks['name']}")
fail.set_parse_action(parse_fail)
token <<= num | text | fail

# If no name is given, do not intercept error messages
with self.assertRaises(pp.ParseFatalException, msg="Unknown symbol: \\fail"):
token.parse_string("\\fail")

# If name is given, do intercept error messages
token.set_name("token")
with self.assertRaises(pp.ParseFatalException, msg="Expected token, found.*"):
token.parse_string("\\fail")


def testMiscellaneousExceptionBits(self):
pp.ParserElement.verbose_stacktrace = True

Expand Down

0 comments on commit 173bc16

Please sign in to comment.