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.

108 lines
3.3 KiB

  1. """This module implements a LALR(1) Parser
  2. """
  3. # Author: Erez Shinan (2017)
  4. # Email : erezshin@gmail.com
  5. from ..exceptions import UnexpectedToken
  6. from ..lexer import Token
  7. from ..utils import Enumerator, Serialize
  8. from .lalr_analysis import LALR_Analyzer, Shift, IntParseTable
  9. ###{standalone
  10. class LALR_Parser(object):
  11. def __init__(self, parser_conf, debug=False):
  12. assert all(r.options is None or r.options.priority is None
  13. for r in parser_conf.rules), "LALR doesn't yet support prioritization"
  14. analysis = LALR_Analyzer(parser_conf, debug=debug)
  15. analysis.compute_lookahead()
  16. callbacks = parser_conf.callbacks
  17. self._parse_table = analysis.parse_table
  18. self.parser_conf = parser_conf
  19. self.parser = _Parser(analysis.parse_table, callbacks)
  20. @classmethod
  21. def deserialize(cls, data, memo, callbacks):
  22. inst = cls.__new__(cls)
  23. inst._parse_table = IntParseTable.deserialize(data, memo)
  24. inst.parser = _Parser(inst._parse_table, callbacks)
  25. return inst
  26. def serialize(self, memo):
  27. return self._parse_table.serialize(memo)
  28. def parse(self, *args):
  29. return self.parser.parse(*args)
  30. class _Parser:
  31. def __init__(self, parse_table, callbacks):
  32. self.states = parse_table.states
  33. self.start_states = parse_table.start_states
  34. self.end_states = parse_table.end_states
  35. self.callbacks = callbacks
  36. def parse(self, seq, start, set_state=None):
  37. token = None
  38. stream = iter(seq)
  39. states = self.states
  40. start_state = self.start_states[start]
  41. end_state = self.end_states[start]
  42. state_stack = [start_state]
  43. value_stack = []
  44. if set_state: set_state(start_state)
  45. def get_action(token):
  46. state = state_stack[-1]
  47. try:
  48. return states[state][token.type]
  49. except KeyError:
  50. expected = [s for s in states[state].keys() if s.isupper()]
  51. raise UnexpectedToken(token, expected, state=state)
  52. def reduce(rule):
  53. size = len(rule.expansion)
  54. if size:
  55. s = value_stack[-size:]
  56. del state_stack[-size:]
  57. del value_stack[-size:]
  58. else:
  59. s = []
  60. value = self.callbacks[rule](s)
  61. _action, new_state = states[state_stack[-1]][rule.origin.name]
  62. assert _action is Shift
  63. state_stack.append(new_state)
  64. value_stack.append(value)
  65. # Main LALR-parser loop
  66. for token in stream:
  67. while True:
  68. action, arg = get_action(token)
  69. assert arg != end_state
  70. if action is Shift:
  71. state_stack.append(arg)
  72. value_stack.append(token)
  73. if set_state: set_state(arg)
  74. break # next token
  75. else:
  76. reduce(arg)
  77. token = Token.new_borrow_pos('$END', '', token) if token else Token('$END', '', 0, 1, 1)
  78. while True:
  79. _action, arg = get_action(token)
  80. if _action is Shift:
  81. assert arg == end_state
  82. val ,= value_stack
  83. return val
  84. else:
  85. reduce(arg)
  86. ###}