Browse Source

Added 'considered_rules' to exceptions, to help users debug

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.5
Erez Shinan 6 years ago
parent
commit
327cca8c00
4 changed files with 6 additions and 4 deletions
  1. +2
    -1
      lark/common.py
  2. +2
    -1
      lark/lexer.py
  3. +1
    -1
      lark/parsers/earley.py
  4. +1
    -1
      lark/parsers/xearley.py

+ 2
- 1
lark/common.py View File

@@ -17,11 +17,12 @@ class ParseError(Exception):
pass

class UnexpectedToken(ParseError):
def __init__(self, token, expected, seq, index):
def __init__(self, token, expected, seq, index, considered_rules=None):
self.token = token
self.expected = expected
self.line = getattr(token, 'line', '?')
self.column = getattr(token, 'column', '?')
self.considered_rules = considered_rules

try:
context = ' '.join(['%r(%s)' % (t.value, t.type) for t in seq[index:index+5]])


+ 2
- 1
lark/lexer.py View File

@@ -10,7 +10,7 @@ class LexError(Exception):
pass

class UnexpectedInput(LexError):
def __init__(self, seq, lex_pos, line, column, allowed=None):
def __init__(self, seq, lex_pos, line, column, allowed=None, considered_rules=None):
context = seq[lex_pos:lex_pos+5]
message = "No token defined for: '%s' in %r at line %d col %d" % (seq[lex_pos], context, line, column)
if allowed:
@@ -22,6 +22,7 @@ class UnexpectedInput(LexError):
self.column = column
self.context = context
self.allowed = allowed
self.considered_rules = considered_rules

class Token(Str):
def __new__(cls, type_, value, pos_in_stream=None, line=None, column=None):


+ 1
- 1
lark/parsers/earley.py View File

@@ -197,7 +197,7 @@ class Parser:

if not next_set:
expect = {i.expect for i in column.to_scan}
raise UnexpectedToken(token, expect, stream, i)
raise UnexpectedToken(token, expect, stream, set(column.to_scan))

return next_set



+ 1
- 1
lark/parsers/xearley.py View File

@@ -112,7 +112,7 @@ class Parser:
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})
raise UnexpectedInput(stream, i, text_line, text_column, {item.expect for item in to_scan}, set(to_scan))

return next_set



Loading…
Cancel
Save