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.

158 lines
5.8 KiB

  1. "This module implements an experimental Earley Parser with a dynamic lexer"
  2. # The parser uses a parse-forest to keep track of derivations and ambiguations.
  3. # When the parse ends successfully, a disambiguation stage resolves all ambiguity
  4. # (right now ambiguity resolution is not developed beyond the needs of lark)
  5. # Afterwards the parse tree is reduced (transformed) according to user callbacks.
  6. # I use the no-recursion version of Transformer and Visitor, because the tree might be
  7. # deeper than Python's recursion limit (a bit absurd, but that's life)
  8. #
  9. # The algorithm keeps track of each state set, using a corresponding Column instance.
  10. # Column keeps track of new items using NewsList instances.
  11. #
  12. # Instead of running a lexer beforehand, or using a costy char-by-char method, this parser
  13. # uses regular expressions by necessity, achieving high-performance while maintaining all of
  14. # Earley's power in parsing any CFG.
  15. #
  16. #
  17. # Author: Erez Shinan (2017)
  18. # Email : erezshin@gmail.com
  19. from collections import defaultdict
  20. from ..common import ParseError, UnexpectedToken, Terminal
  21. from ..lexer import Token, UnexpectedInput
  22. from ..tree import Tree
  23. from .grammar_analysis import GrammarAnalyzer
  24. from .earley import ApplyCallbacks, Item, Column
  25. class Parser:
  26. def __init__(self, rules, start_symbol, callback, resolve_ambiguity=None, ignore=()):
  27. self.analysis = GrammarAnalyzer(rules, start_symbol)
  28. self.start_symbol = start_symbol
  29. self.resolve_ambiguity = resolve_ambiguity
  30. self.ignore = list(ignore)
  31. self.postprocess = {}
  32. self.predictions = {}
  33. self.FIRST = {}
  34. for rule in self.analysis.rules:
  35. if rule.origin != '$root': # XXX kinda ugly
  36. a = rule.alias
  37. self.postprocess[rule] = a if callable(a) else (a and getattr(callback, a))
  38. self.predictions[rule.origin] = [x.rule for x in self.analysis.expand_rule(rule.origin)]
  39. self.FIRST[rule.origin] = self.analysis.FIRST[rule.origin]
  40. def parse(self, stream, start_symbol=None):
  41. # Define parser functions
  42. start_symbol = start_symbol or self.start_symbol
  43. delayed_matches = defaultdict(list)
  44. text_line = 1
  45. text_column = 0
  46. def predict(nonterm, column):
  47. assert not isinstance(nonterm, Terminal), nonterm
  48. return [Item(rule, 0, column, None) for rule in self.predictions[nonterm]]
  49. def complete(item):
  50. name = item.rule.origin
  51. return [i.advance(item.tree) for i in item.start.to_predict if i.expect == name]
  52. def predict_and_complete(column):
  53. while True:
  54. to_predict = {x.expect for x in column.to_predict.get_news()
  55. if x.ptr} # if not part of an already predicted batch
  56. to_reduce = column.to_reduce.get_news()
  57. if not (to_predict or to_reduce):
  58. break
  59. for nonterm in to_predict:
  60. column.add( predict(nonterm, column) )
  61. for item in to_reduce:
  62. new_items = list(complete(item))
  63. for new_item in new_items:
  64. if new_item.similar(item):
  65. raise ParseError('Infinite recursion detected! (rule %s)' % new_item.rule)
  66. column.add(new_items)
  67. def scan(i, token, column):
  68. to_scan = column.to_scan.get_news()
  69. for x in self.ignore:
  70. m = x.match(stream, i)
  71. if m:
  72. delayed_matches[m.end()] += set(to_scan)
  73. delayed_matches[m.end()] += set(column.to_reduce)
  74. # TODO add partial matches for ignore too?
  75. # s = m.group(0)
  76. # for j in range(1, len(s)):
  77. # m = x.match(s[:-j])
  78. # if m:
  79. # delayed_matches[m.end()] += to_scan
  80. for item in to_scan:
  81. m = item.expect.match(stream, i)
  82. if m:
  83. t = Token(item.expect.name, m.group(0), i, text_line, text_column)
  84. delayed_matches[m.end()].append(item.advance(t))
  85. s = m.group(0)
  86. for j in range(1, len(s)):
  87. m = item.expect.match(s[:-j])
  88. if m:
  89. delayed_matches[m.end()].append(item.advance(m.group(0)))
  90. next_set = Column(i+1, self.FIRST)
  91. next_set.add(delayed_matches[i+1])
  92. del delayed_matches[i+1] # No longer needed, so unburden memory
  93. if not next_set and not delayed_matches:
  94. raise UnexpectedInput(stream, i, text_line, text_column, to_scan)
  95. return next_set
  96. # Main loop starts
  97. column0 = Column(0, self.FIRST)
  98. column0.add(predict(start_symbol, column0))
  99. column = column0
  100. for i, token in enumerate(stream):
  101. predict_and_complete(column)
  102. column = scan(i, token, column)
  103. if token == '\n':
  104. text_line += 1
  105. text_column = 1
  106. else:
  107. text_column += 1
  108. predict_and_complete(column)
  109. # Parse ended. Now build a parse tree
  110. solutions = [n.tree for n in column.to_reduce
  111. if n.rule.origin==start_symbol and n.start is column0]
  112. if not solutions:
  113. expected_tokens = [t.expect.name for t in column.to_scan]
  114. raise ParseError('Unexpected end of input! Expecting a terminal of: %s' % expected_tokens)
  115. elif len(solutions) == 1:
  116. tree = solutions[0]
  117. else:
  118. tree = Tree('_ambig', solutions)
  119. if self.resolve_ambiguity:
  120. tree = self.resolve_ambiguity(tree)
  121. return ApplyCallbacks(self.postprocess).transform(tree)