From 6a6c1535ea98e2f84a479cd6873dee553fee92c3 Mon Sep 17 00:00:00 2001 From: Paul McGuire Date: Sat, 29 Sep 2018 13:55:54 -0500 Subject: tighten up error checking in Regex.sub() --- pyparsing.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'pyparsing.py') diff --git a/pyparsing.py b/pyparsing.py index cdbf398..3ea7660 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -2846,7 +2846,23 @@ class Regex(Token): Return Regex with an attached parse action to transform the parsed result as if called using C{re.sub(expr, repl, string)}. """ - return self.addParseAction(lambda s, l, t: self.re.sub(repl, t[0])) + if self.asGroupList: + warnings.warn("cannot use sub() with Regex(asGroupList=True)", + SyntaxWarning, stacklevel=2) + raise SyntaxError() + + if self.asMatch and callable(repl): + warnings.warn("cannot use sub() with a callable with Regex(asMatch=True)", + SyntaxWarning, stacklevel=2) + raise SyntaxError() + + if self.asMatch: + def pa(tokens): + return tokens[0].expand(repl) + else: + def pa(tokens): + return self.re.sub(repl, tokens[0]) + return self.addParseAction(pa) class QuotedString(Token): r""" -- cgit v1.2.1