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.

96 lines
3.0 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, token.type == '$END')
  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 copy(self):
  29. return copy(self)
  30. def __eq__(self, other):
  31. if not isinstance(other, ParserPuppet):
  32. return False
  33. return self.parser_state == other.parser_state and self.lexer_state == other.lexer_state
  34. def as_immutable(self):
  35. p = copy(self)
  36. return ImmutableParserPuppet(p.parser, p.parser_state, p.lexer_state)
  37. def pretty(self):
  38. """Print the output of ``choices()`` in a way that's easier to read."""
  39. out = ["Puppet choices:"]
  40. for k, v in self.choices().items():
  41. out.append('\t- %s -> %s' % (k, v))
  42. out.append('stack size: %s' % len(self.parser_state.state_stack))
  43. return '\n'.join(out)
  44. def choices(self):
  45. """Returns a dictionary of token types, matched to their action in the parser.
  46. Only returns token types that are accepted by the current state.
  47. Updated by ``feed_token()``.
  48. """
  49. return self.parser_state.parse_conf.parse_table.states[self.parser_state.position]
  50. def accepts(self):
  51. accepts = set()
  52. for t in self.choices():
  53. if t.isupper(): # is terminal?
  54. new_puppet = copy(self)
  55. try:
  56. new_puppet.feed_token(Token(t, ''))
  57. except UnexpectedToken:
  58. pass
  59. else:
  60. accepts.add(t)
  61. return accepts
  62. def resume_parse(self):
  63. """Resume parsing from the current puppet state."""
  64. return self.parser.parse_from_state(self.parser_state)
  65. class ImmutableParserPuppet(ParserPuppet):
  66. result = None
  67. def __hash__(self):
  68. return hash((self.parser_state, self.lexer_state))
  69. def feed_token(self, token):
  70. c = copy(self)
  71. c.result = ParserPuppet.feed_token(c, token)
  72. return c