mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-11 07:01:59 +08:00
Get quite a bit of the error recovery working
This commit is contained in:
@@ -55,7 +55,7 @@ class BaseParser(object):
|
|||||||
del self.pgen_parser
|
del self.pgen_parser
|
||||||
return node
|
return node
|
||||||
|
|
||||||
def error_recovery(self, pgen_grammar, stack, arcs, typ, value, start_pos, prefix,
|
def error_recovery(self, pgen_grammar, stack, typ, value, start_pos, prefix,
|
||||||
add_token_callback):
|
add_token_callback):
|
||||||
if self._error_recovery:
|
if self._error_recovery:
|
||||||
raise NotImplementedError("Error Recovery is not implemented")
|
raise NotImplementedError("Error Recovery is not implemented")
|
||||||
|
|||||||
@@ -134,14 +134,14 @@ class Parser(BaseParser):
|
|||||||
|
|
||||||
return self._leaf_map.get(type, tree.Operator)(value, start_pos, prefix)
|
return self._leaf_map.get(type, tree.Operator)(value, start_pos, prefix)
|
||||||
|
|
||||||
def error_recovery(self, pgen_grammar, stack, arcs, typ, value, start_pos, prefix,
|
def error_recovery(self, pgen_grammar, stack, typ, value, start_pos, prefix,
|
||||||
add_token_callback):
|
add_token_callback):
|
||||||
def get_nonterminal_and_nodes(stack):
|
def get_nonterminal_and_nodes(stack):
|
||||||
for dfa, state, (type_, nodes) in stack:
|
for dfa, state, (type_, nodes) in stack:
|
||||||
nonterminal = pgen_grammar.number2nonterminal[type_]
|
nonterminal = pgen_grammar.number2nonterminal[type_]
|
||||||
yield nonterminal, nodes
|
yield nonterminal, nodes
|
||||||
|
|
||||||
tos_nodes = stack.get_tos_nodes()
|
tos_nodes = stack[-1].nodes
|
||||||
if tos_nodes:
|
if tos_nodes:
|
||||||
last_leaf = tos_nodes[-1].get_last_leaf()
|
last_leaf = tos_nodes[-1].get_last_leaf()
|
||||||
else:
|
else:
|
||||||
@@ -164,32 +164,19 @@ class Parser(BaseParser):
|
|||||||
# error recovery.
|
# error recovery.
|
||||||
#print('x', pprint.pprint(stack))
|
#print('x', pprint.pprint(stack))
|
||||||
ilabel = token_to_ilabel(pgen_grammar, NEWLINE, value)
|
ilabel = token_to_ilabel(pgen_grammar, NEWLINE, value)
|
||||||
|
try:
|
||||||
dfa, state, (type_, nodes) = stack[-1]
|
plan = stack[-1].dfa.ilabel_to_plan[ilabel]
|
||||||
nonterminal = pgen_grammar.number2nonterminal[type_]
|
except KeyError:
|
||||||
states, first = dfa
|
pass
|
||||||
arcs = states[state]
|
else:
|
||||||
# Look for a state with this label
|
if plan.next_dfa.is_final and not plan.dfa_pushes:
|
||||||
for i, newstate in arcs:
|
stack[-1].dfa = plan.next_dfa
|
||||||
if ilabel == i:
|
|
||||||
if nonterminal == 'simple_stmt':
|
|
||||||
# This is basically shifting
|
|
||||||
stack[-1] = (dfa, newstate, (type_, nodes))
|
|
||||||
|
|
||||||
reduce_stack(states, newstate)
|
|
||||||
add_token_callback(typ, value, start_pos, prefix)
|
add_token_callback(typ, value, start_pos, prefix)
|
||||||
return
|
return
|
||||||
# Check if we're at the right point
|
|
||||||
#for nonterminal, nodes in get_nonterminal_and_nodes(stack):
|
|
||||||
# self.pgen_parser._pop()
|
|
||||||
|
|
||||||
#break
|
|
||||||
break
|
|
||||||
#nonterminal = pgen_grammar.number2nonterminal[type_]
|
|
||||||
|
|
||||||
if not self._error_recovery:
|
if not self._error_recovery:
|
||||||
return super(Parser, self).error_recovery(
|
return super(Parser, self).error_recovery(
|
||||||
pgen_grammar, stack, arcs, typ, value, start_pos, prefix,
|
pgen_grammar, stack, typ, value, start_pos, prefix,
|
||||||
add_token_callback)
|
add_token_callback)
|
||||||
|
|
||||||
def current_suite(stack):
|
def current_suite(stack):
|
||||||
|
|||||||
Reference in New Issue
Block a user