summaryrefslogtreecommitdiff
path: root/pyparsing.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2018-09-29 13:55:54 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2018-09-29 13:55:54 -0500
commit6a6c1535ea98e2f84a479cd6873dee553fee92c3 (patch)
treed0396b6ce9194b5f71b4d6ae8b3060ce94563d38 /pyparsing.py
parentfe47c549f0662f068808143dee2811a69d4034c9 (diff)
downloadpyparsing-git-6a6c1535ea98e2f84a479cd6873dee553fee92c3.tar.gz
tighten up error checking in Regex.sub()
Diffstat (limited to 'pyparsing.py')
-rw-r--r--pyparsing.py18
1 files changed, 17 insertions, 1 deletions
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"""