Skip to content

Commit

Permalink
Improved error messages (as pointed out in issue #181)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Jul 11, 2018
1 parent 0240e1c commit 1247a8c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions lark/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ def match_examples(self, parse_fn, examples):

class UnexpectedCharacters(LexError, UnexpectedInput):
def __init__(self, seq, lex_pos, line, column, allowed=None, considered_tokens=None, state=None):
context = seq[lex_pos:lex_pos+10]
message = "No token defined for '%s' in %r at line %d col %d" % (seq[lex_pos], context, line, column)
if allowed:
message += '\n\nExpecting: %s\n' % allowed

super(UnexpectedCharacters, self).__init__(message)
message = "No terminal defined for '%s' at line %d col %d" % (seq[lex_pos], line, column)

self.line = line
self.column = column
self.context = context
self.allowed = allowed
self.considered_tokens = considered_tokens
self.pos_in_stream = lex_pos
self.state = state

message += '\n\n' + self.get_context(seq)
if allowed:
message += '\nExpecting: %s\n' % allowed

super(UnexpectedCharacters, self).__init__(message)



class UnexpectedToken(ParseError, UnexpectedInput):
def __init__(self, token, expected, considered_rules=None, state=None):
Expand Down
2 changes: 1 addition & 1 deletion lark/load_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def pattern(self, p):
self.token_reverse[p] = tokendef
self.tokens.append(tokendef)

return Terminal(Token('TERMINAL', token_name, -1), filter_out=isinstance(p, PatternStr))
return Terminal(token_name, filter_out=isinstance(p, PatternStr))


def _rfind(s, choices):
Expand Down
4 changes: 2 additions & 2 deletions lark/parsers/xearley.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from collections import defaultdict

from ..exceptions import ParseError, UnexpectedInput
from ..exceptions import ParseError, UnexpectedCharacters
from ..lexer import Token
from ..tree import Tree
from .grammar_analysis import GrammarAnalyzer
Expand Down Expand Up @@ -116,7 +116,7 @@ def scan(i, column):
del delayed_matches[i+1] # No longer needed, so unburden memory

if not next_set and not delayed_matches:
raise UnexpectedInput(stream, i, text_line, text_column, {item.expect for item in to_scan}, set(to_scan))
raise UnexpectedCharacters(stream, i, text_line, text_column, {item.expect for item in to_scan}, set(to_scan))

return next_set

Expand Down

0 comments on commit 1247a8c

Please sign in to comment.