From d9264609f2c013d70de2adf03d03dd9cb4b5c493 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 22 Jun 2018 01:56:29 +0200 Subject: [PATCH] Get quite a bit of the error recovery working --- parso/parser.py | 2 +- parso/python/parser.py | 37 ++++++++++++------------------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/parso/parser.py b/parso/parser.py index 600ed79..80ea7b2 100644 --- a/parso/parser.py +++ b/parso/parser.py @@ -55,7 +55,7 @@ class BaseParser(object): del self.pgen_parser 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): if self._error_recovery: raise NotImplementedError("Error Recovery is not implemented") diff --git a/parso/python/parser.py b/parso/python/parser.py index 16748bc..f05f1a8 100644 --- a/parso/python/parser.py +++ b/parso/python/parser.py @@ -134,14 +134,14 @@ class Parser(BaseParser): 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): def get_nonterminal_and_nodes(stack): for dfa, state, (type_, nodes) in stack: nonterminal = pgen_grammar.number2nonterminal[type_] yield nonterminal, nodes - tos_nodes = stack.get_tos_nodes() + tos_nodes = stack[-1].nodes if tos_nodes: last_leaf = tos_nodes[-1].get_last_leaf() else: @@ -164,32 +164,19 @@ class Parser(BaseParser): # error recovery. #print('x', pprint.pprint(stack)) ilabel = token_to_ilabel(pgen_grammar, NEWLINE, value) - - dfa, state, (type_, nodes) = stack[-1] - nonterminal = pgen_grammar.number2nonterminal[type_] - states, first = dfa - arcs = states[state] - # Look for a state with this label - for i, newstate in arcs: - 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) - 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_] + try: + plan = stack[-1].dfa.ilabel_to_plan[ilabel] + except KeyError: + pass + else: + if plan.next_dfa.is_final and not plan.dfa_pushes: + stack[-1].dfa = plan.next_dfa + add_token_callback(typ, value, start_pos, prefix) + return if not 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) def current_suite(stack):