示例#1
0
    def _enter_error_reporting(self):
        """
        To correctly report what is found ahead and what is expected we shall:

            - execute all grammar recognizers at the farther position reached
              in the input by the active heads.  This will be part of the error
              report (what is found ahead if anything can be recognized).

            - for all last reducing heads, simulate parsing for each of
              possible lookaheads in the head's state until either SHIFT or
              ACCEPT is successfuly executed.  Collect each possible lookahead
              where this is achieved for reporting.  This will be another part
              of the error report (what is expected).

        """

        self._in_error_reporting = True

        # Start with the last shifted heads sorted by position.
        self._last_shifted_heads.sort(key=lambda h: h.position, reverse=True)
        last_head = self._last_shifted_heads[0]
        farthest_heads = takewhile(lambda h: h.position == last_head.position,
                                   self._last_shifted_heads)

        self._tokens_ahead = self._get_all_possible_tokens_ahead(last_head)

        self._active_heads_per_symbol = {}
        for head in farthest_heads:
            for possible_lookahead in head.state.actions.keys():
                h = head.for_token(
                    Token(possible_lookahead, [], position=head.position))
                self._active_heads_per_symbol.setdefault(
                    possible_lookahead, {})[h.state.state_id] = h
 def custom_recovery(head, error):
     # Here we will introduce missing operation token
     head.token_ahead = Token(g.get_terminal('-'),
                              '-',
                              head.position,
                              length=0)
     return True
示例#3
0
 def custom_recovery(parser, input_str, position, symbols):
     # Here we will introduce missing operation token
     return error, None, Token(g.get_terminal('-'), '-', 0)
 def custom_recovery(context, error):
     # Here we will introduce missing operation token
     return Token(g.get_terminal('-'), '-', 0), None