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.

107 lines
3.3 KiB

  1. from ..utils import compare
  2. from functools import cmp_to_key
  3. from ..tree import Tree, Visitor_NoRecurse
  4. # Standard ambiguity resolver (uses comparison)
  5. #
  6. # Author: Erez Sh
  7. def _compare_rules(rule1, rule2):
  8. if rule1.origin != rule2.origin:
  9. if rule1.options and rule2.options:
  10. if rule1.options.priority is not None and rule2.options.priority is not None:
  11. assert rule1.options.priority != rule2.options.priority, "Priority is the same between both rules: %s == %s" % (rule1, rule2)
  12. return -compare(rule1.options.priority, rule2.options.priority)
  13. return 0
  14. c = compare( len(rule1.expansion), len(rule2.expansion))
  15. if rule1.origin.startswith('__'): # XXX hack! We need to set priority in parser, not here
  16. c = -c
  17. return c
  18. def _compare_drv(tree1, tree2):
  19. if not (isinstance(tree1, Tree) and isinstance(tree2, Tree)):
  20. return -compare(tree1, tree2)
  21. try:
  22. rule1, rule2 = tree1.rule, tree2.rule
  23. except AttributeError:
  24. # Probably trees that don't take part in this parse (better way to distinguish?)
  25. return -compare(tree1, tree2)
  26. # XXX These artifacts can appear due to imperfections in the ordering of Visitor_NoRecurse,
  27. # when confronted with duplicate (same-id) nodes. Fixing this ordering is possible, but would be
  28. # computationally inefficient. So we handle it here.
  29. if tree1.data == '_ambig':
  30. _standard_resolve_ambig(tree1)
  31. if tree2.data == '_ambig':
  32. _standard_resolve_ambig(tree2)
  33. c = _compare_rules(tree1.rule, tree2.rule)
  34. if c:
  35. return c
  36. # rules are "equal", so compare trees
  37. for t1, t2 in zip(tree1.children, tree2.children):
  38. c = _compare_drv(t1, t2)
  39. if c:
  40. return c
  41. return compare(len(tree1.children), len(tree2.children))
  42. def _standard_resolve_ambig(tree):
  43. assert tree.data == '_ambig'
  44. best = min(tree.children, key=cmp_to_key(_compare_drv))
  45. assert best.data == 'drv'
  46. tree.set('drv', best.children)
  47. tree.rule = best.rule # needed for applying callbacks
  48. def standard_resolve_ambig(tree):
  49. for ambig in tree.find_data('_ambig'):
  50. _standard_resolve_ambig(ambig)
  51. return tree
  52. # Anti-score Sum
  53. #
  54. # Author: Uriva (https://github.com/uriva)
  55. def _antiscore_sum_drv(tree):
  56. if not isinstance(tree, Tree):
  57. return 0
  58. # XXX These artifacts can appear due to imperfections in the ordering of Visitor_NoRecurse,
  59. # when confronted with duplicate (same-id) nodes. Fixing this ordering is possible, but would be
  60. # computationally inefficient. So we handle it here.
  61. if tree.data == '_ambig':
  62. _antiscore_sum_resolve_ambig(tree)
  63. try:
  64. priority = tree.rule.options.priority
  65. except AttributeError:
  66. # Probably trees that don't take part in this parse (better way to distinguish?)
  67. priority = None
  68. return (priority or 0) + sum(map(_antiscore_sum_drv, tree.children), 0)
  69. def _antiscore_sum_resolve_ambig(tree):
  70. assert tree.data == '_ambig'
  71. best = min(tree.children, key=_antiscore_sum_drv)
  72. assert best.data == 'drv'
  73. tree.set('drv', best.children)
  74. tree.rule = best.rule # needed for applying callbacks
  75. def antiscore_sum_resolve_ambig(tree):
  76. for ambig in tree.find_data('_ambig'):
  77. _antiscore_sum_resolve_ambig(ambig)
  78. return tree