From bda9425ce9e45d4de45b2725f6cb3f767dd5255f Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Thu, 25 Jun 2015 13:43:00 -0500 Subject: [PATCH] Print better error message for lexer error on match expression --- loopy/context_matching.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/loopy/context_matching.py b/loopy/context_matching.py index cc375c438..61203ece2 100644 --- a/loopy/context_matching.py +++ b/loopy/context_matching.py @@ -251,11 +251,21 @@ def parse_match(expr_str): return left_query - from pytools.lex import LexIterator, lex - pstate = LexIterator( - [(tag, s, idx, matchobj) - for (tag, s, idx, matchobj) in lex(_LEX_TABLE, expr_str, match_objects=True) - if tag is not _whitespace], expr_str) + from pytools.lex import LexIterator, lex, InvalidTokenError + try: + pstate = LexIterator( + [(tag, s, idx, matchobj) + for (tag, s, idx, matchobj) in lex(_LEX_TABLE, expr_str, + match_objects=True) + if tag is not _whitespace], expr_str) + except InvalidTokenError as e: + from loopy.diagnostic import LoopyError + raise LoopyError( + "invalid match expression: '{match_expr}' ({err_type}: {err_str})" + .format( + match_expr=expr_str, + err_type=type(e).__name__, + err_str=str(e))) if pstate.is_at_end(): pstate.raise_parse_error("unexpected end of input") -- GitLab