This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.7 KiB

  1. # This module provide a LALR puppet, which is used to debugging and error handling
  2. from copy import copy
  3. from .lalr_analysis import Shift, Reduce
  4. from .. import Token
  5. from ..exceptions import UnexpectedToken
  6. class ParserPuppet(object):
  7. """ParserPuppet gives you advanced control over error handling when parsing with LALR.
  8. For a simpler, more streamlined interface, see the ``on_error`` argument to ``Lark.parse()``.
  9. """
  10. def __init__(self, parser, parser_state, lexer_state):
  11. self.parser = parser
  12. self.parser_state = parser_state
  13. self.lexer_state = lexer_state
  14. def feed_token(self, token):
  15. """Feed the parser with a token, and advance it to the next state, as if it received it from the lexer.
  16. Note that ``token`` has to be an instance of ``Token``.
  17. """
  18. return self.parser_state.feed_token(token)
  19. def __copy__(self):
  20. """Create a new puppet with a separate state.
  21. Calls to feed_token() won't affect the old puppet, and vice-versa.
  22. """
  23. return type(self)(
  24. self.parser,
  25. copy(self.parser_state),
  26. copy(self.lexer_state),
  27. )
  28. def __eq__(self, other):
  29. if not isinstance(other, ParserPuppet):
  30. return False
  31. return self.parser_state == other.parser_state and self.lexer_state == other.lexer_state
  32. # TODO Provide with an immutable puppet instance
  33. # def __hash__(self):
  34. # return hash((self.parser_state, self.lexer_state))
  35. def pretty(self):
  36. """Print the output of ``choices()`` in a way that's easier to read."""
  37. out = ["Puppet choices:"]
  38. for k, v in self.choices().items():
  39. out.append('\t- %s -> %s' % (k, v))
  40. out.append('stack size: %s' % len(self.parser_state.state_stack))
  41. return '\n'.join(out)
  42. def choices(self):
  43. """Returns a dictionary of token types, matched to their action in the parser.
  44. Only returns token types that are accepted by the current state.
  45. Updated by ``feed_token()``.
  46. """
  47. return self.parser_state.parse_conf.parse_table.states[self.parser_state.position]
  48. def accepts(self):
  49. accepts = set()
  50. for t in self.choices():
  51. if t.isupper(): # is terminal?
  52. new_puppet = copy(self)
  53. try:
  54. new_puppet.feed_token(Token(t, ''))
  55. except UnexpectedToken:
  56. pass
  57. else:
  58. accepts.add(t)
  59. return accepts
  60. def resume_parse(self):
  61. """Resume parsing from the current puppet state."""
  62. return self.parser.parse_from_state(self.parser_state)