summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/patcomp.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/patcomp.py')
-rw-r--r--Lib/lib2to3/patcomp.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/Lib/lib2to3/patcomp.py b/Lib/lib2to3/patcomp.py
index 076fdc1..24251cb 100644
--- a/Lib/lib2to3/patcomp.py
+++ b/Lib/lib2to3/patcomp.py
@@ -14,10 +14,7 @@ __author__ = "Guido van Rossum <guido@python.org>"
import os
# Fairly local imports
-from .pgen2 import driver
-from .pgen2 import literals
-from .pgen2 import token
-from .pgen2 import tokenize
+from .pgen2 import driver, literals, token, tokenize, parse
# Really local imports
from . import pytree
@@ -28,6 +25,10 @@ _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__),
"PatternGrammar.txt")
+class PatternSyntaxError(Exception):
+ pass
+
+
def tokenize_wrapper(input):
"""Tokenizes a string suppressing significant whitespace."""
skip = set((token.NEWLINE, token.INDENT, token.DEDENT))
@@ -54,7 +55,10 @@ class PatternCompiler(object):
def compile_pattern(self, input, debug=False):
"""Compiles a pattern string to a nested pytree.*Pattern object."""
tokens = tokenize_wrapper(input)
- root = self.driver.parse_tokens(tokens, debug=debug)
+ try:
+ root = self.driver.parse_tokens(tokens, debug=debug)
+ except parse.ParseError as e:
+ raise PatternSyntaxError(str(e))
return self.compile_node(root)
def compile_node(self, node):
@@ -139,7 +143,9 @@ class PatternCompiler(object):
value = node.value
if value.isupper():
if value not in TOKEN_MAP:
- raise SyntaxError("Invalid token: %r" % value)
+ raise PatternSyntaxError("Invalid token: %r" % value)
+ if nodes[1:]:
+ raise PatternSyntaxError("Can't have details for token")
return pytree.LeafPattern(TOKEN_MAP[value])
else:
if value == "any":
@@ -147,7 +153,7 @@ class PatternCompiler(object):
elif not value.startswith("_"):
type = getattr(self.pysyms, value, None)
if type is None:
- raise SyntaxError("Invalid symbol: %r" % value)
+ raise PatternSyntaxError("Invalid symbol: %r" % value)
if nodes[1:]: # Details present
content = [self.compile_node(nodes[1].children[1])]
else: