1
0
forked from VimPlug/jedi

The parser without error recovery raises an error now if he's not able to parse something.

This commit is contained in:
Dave Halter
2015-12-25 18:53:05 +01:00
parent 6bad5a924b
commit a373e34229
4 changed files with 41 additions and 34 deletions

View File

@@ -18,8 +18,12 @@ how this parsing engine works.
from jedi.parser import tokenize
class ParseError(Exception):
"""Exception to signal the parser is stuck."""
class InternalParseError(Exception):
"""
Exception to signal the parser is stuck and error recovery didn't help.
Basically this shouldn't happen. It's a sign that something is really
wrong.
"""
def __init__(self, msg, type, value, start_pos):
Exception.__init__(self, "%s: type=%r, value=%r, start_pos=%r" %
@@ -38,7 +42,7 @@ class PgenParser(object):
p = Parser(grammar, [converter]) # create instance
p.setup([start]) # prepare for parsing
<for each input token>:
if p.addtoken(...): # parse a token; may raise ParseError
if p.addtoken(...): # parse a token
break
root = p.rootnode # root of abstract syntax tree
@@ -53,10 +57,10 @@ class PgenParser(object):
Parsing is complete when addtoken() returns True; the root of the
abstract syntax tree can then be retrieved from the rootnode
instance variable. When a syntax error occurs, addtoken() raises
the ParseError exception. There is no error recovery; the parser
cannot be used after a syntax error was reported (but it can be
reinitialized by calling setup()).
instance variable. When a syntax error occurs, error_recovery()
is called. There is no error recovery; the parser cannot be used
after a syntax error was reported (but it can be reinitialized by
calling setup()).
"""
@@ -109,7 +113,7 @@ class PgenParser(object):
start_pos, prefix, self.addtoken)
# Add the ENDMARKER again.
if not self.addtoken(type, value, prefix, start_pos):
raise ParseError("incomplete input", type, value, start_pos)
raise InternalParseError("incomplete input", type, value, start_pos)
return self.rootnode
def addtoken(self, type, value, prefix, start_pos):
@@ -162,7 +166,7 @@ class PgenParser(object):
self.pop()
if not self.stack:
# Done parsing, but another token is input
raise ParseError("too much input", type, value, start_pos)
raise InternalParseError("too much input", type, value, start_pos)
else:
self.error_recovery(self.grammar, self.stack, type,
value, start_pos, prefix, self.addtoken)