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
+19 -18
View File
@@ -35,6 +35,12 @@ STATEMENT_KEYWORDS = 'assert', 'del', 'global', 'nonlocal', 'raise', \
_loaded_grammars = {}
class ParseError(Exception):
"""
Signals you that the code you fed the Parser was not correct Python code.
"""
def load_grammar(file='grammar3.4'):
# For now we only support two different Python syntax versions: The latest
# Python 3 and Python 2. This may change.
@@ -111,9 +117,6 @@ class Parser(object):
'lambdef_nocond': pt.Lambda,
}
class ParserError(Exception):
pass
def __init__(self, grammar, source, start, tokenizer=None):
start_number = grammar.symbol2number[start]
@@ -136,20 +139,18 @@ class Parser(object):
self.error_recovery, start_number)
if tokenizer is None:
tokenizer = tokenize.source_tokens(source)
try:
self._parsed = p.parse(self._tokenize(tokenizer))
except Parser.ParserError:
self._parsed = None
else:
if start == 'file_input' != self._parsed.type:
# If there's only one statement, we get back a non-module. That's
# not what we want, we want a module, so we add it here:
self._parsed = self.convert_node(grammar,
grammar.symbol2number['file_input'],
[self._parsed])
if added_newline:
self.remove_last_newline()
self._parsed = p.parse(self._tokenize(tokenizer))
if start == 'file_input' != self._parsed.type:
# If there's only one statement, we get back a non-module. That's
# not what we want, we want a module, so we add it here:
self._parsed = self.convert_node(grammar,
grammar.symbol2number['file_input'],
[self._parsed])
if added_newline:
self.remove_last_newline()
def get_parsed_node(self):
return self._parsed
@@ -157,13 +158,13 @@ class Parser(object):
def _tokenize(self, tokenizer):
for typ, value, start_pos, prefix in tokenizer:
if typ == ERRORTOKEN:
raise Parser.ParserError
raise ParseError
elif typ == OP:
typ = token.opmap[value]
yield typ, value, prefix, start_pos
def error_recovery(self, *args, **kwargs):
raise Parser.ParserError
raise ParseError
def convert_node(self, grammar, type, children):
"""
+13 -9
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)