Browse Source

Improved Earley error on EOF (Issue #457)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.8
Erez Sh 5 years ago
parent
commit
0a4530b942
3 changed files with 14 additions and 5 deletions
  1. +8
    -0
      lark/exceptions.py
  2. +4
    -4
      lark/parsers/earley.py
  3. +2
    -1
      lark/parsers/xearley.py

+ 8
- 0
lark/exceptions.py View File

@@ -13,6 +13,14 @@ class ParseError(LarkError):
class LexError(LarkError):
pass

class UnexpectedEOF(ParseError):
def __init__(self, expected):
self.expected = expected

message = ("Unexpected end-of-input. Expected one of: \n\t* %s\n" % '\n\t* '.join(x.name for x in self.expected))
super(UnexpectedEOF, self).__init__(message)


class UnexpectedInput(LarkError):
pos_in_stream = None



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

@@ -14,7 +14,7 @@ import logging
from collections import deque

from ..visitors import Transformer_InPlace, v_args
from ..exceptions import ParseError, UnexpectedToken
from ..exceptions import UnexpectedEOF, UnexpectedToken
from .grammar_analysis import GrammarAnalyzer
from ..grammar import NonTerminal
from .earley_common import Item, TransitiveItem
@@ -270,6 +270,7 @@ class Parser:

## Column is now the final column in the parse.
assert i == len(columns)-1
return to_scan

def parse(self, stream, start):
assert start, start
@@ -288,7 +289,7 @@ class Parser:
else:
columns[0].add(item)

self._parse(stream, columns, to_scan, start_symbol)
to_scan = self._parse(stream, columns, to_scan, start_symbol)

# If the parse was successful, the start
# symbol should have been completed in the last step of the Earley cycle, and will be in
@@ -306,8 +307,7 @@ class Parser:

if not solutions:
expected_tokens = [t.expect for t in to_scan]
# raise ParseError('Incomplete parse: Could not find a solution to input')
raise ParseError('Unexpected end of input! Expecting a terminal of: %s' % expected_tokens)
raise UnexpectedEOF(expected_tokens)
elif len(solutions) > 1:
assert False, 'Earley should not generate multiple start symbol items!'



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

@@ -146,4 +146,5 @@ class Parser(BaseParser):
self.predict_and_complete(i, to_scan, columns, transitives)

## Column is now the final column in the parse.
assert i == len(columns)-1
assert i == len(columns)-1
return to_scan

Loading…
Cancel
Save