From ab66dde4398cf8e5e5d5e724b8852bec81705673 Mon Sep 17 00:00:00 2001 From: Andrew Zhu Date: Tue, 24 Mar 2020 14:43:39 -0700 Subject: [PATCH] handle all kinds of parse errors --- d20/dice.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/d20/dice.py b/d20/dice.py index 66a50e3..4a434d1 100644 --- a/d20/dice.py +++ b/d20/dice.py @@ -183,6 +183,8 @@ def parse(self, expr, allow_comments=False): return self._parse_with_comments(expr) except lark.UnexpectedToken as ut: raise RollSyntaxError(ut.line, ut.column, ut.token, ut.expected) + except lark.UnexpectedCharacters as uc: + raise RollSyntaxError(uc.line, uc.column, expr[uc.pos_in_stream], uc.allowed) def _parse_no_comment(self, expr): # see if this expr is in cache @@ -196,9 +198,9 @@ def _parse_no_comment(self, expr): def _parse_with_comments(self, expr): try: return ast.parser.parse(expr, start='commented_expr') - except lark.UnexpectedToken as ut: + except lark.UnexpectedInput as ui: # if the statement up to the unexpected token ends with an operator, remove that from the end - successful_fragment = expr[:ut.pos_in_stream] + successful_fragment = expr[:ui.pos_in_stream] for op in SetOperator.OPERATIONS: if successful_fragment.endswith(op): successful_fragment = successful_fragment[:-len(op)]