Skip to content

Commit

Permalink
handle all kinds of parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zhudotexe committed Mar 24, 2020
1 parent fa3e11d commit ab66dde
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions d20/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)]
Expand Down

0 comments on commit ab66dde

Please sign in to comment.