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.

77 lines
2.3 KiB

  1. from .grammar_analysis import is_terminal
  2. class Callback(object):
  3. pass
  4. def create_expand1_tree_builder_function(tree_builder):
  5. def f(children):
  6. if len(children) == 1:
  7. return children[0]
  8. else:
  9. return tree_builder(children)
  10. return f
  11. def create_rule_handler(expansion, usermethod):
  12. to_include = [(i, sym.startswith('_')) for i, sym in enumerate(expansion)
  13. if not (is_terminal(sym) and sym.startswith('_'))]
  14. def _build_ast(match):
  15. children = []
  16. for i, to_expand in to_include:
  17. if to_expand:
  18. children += match[i].children
  19. else:
  20. children.append(match[i])
  21. return usermethod(children)
  22. return _build_ast
  23. class ParseTreeBuilder:
  24. def __init__(self, tree_class):
  25. self.tree_class = tree_class
  26. def _create_tree_builder_function(self, name):
  27. tree_class = self.tree_class
  28. def f(children):
  29. return tree_class(name, children)
  30. return f
  31. def create_tree_builder(self, rules, transformer):
  32. callback = Callback()
  33. new_rules = []
  34. for origin, expansions in rules.items():
  35. expand1 = origin.startswith('?')
  36. _origin = origin.lstrip('?*')
  37. for expansion, alias in expansions:
  38. if alias and origin.startswith('_'):
  39. raise Exception("Rule %s is marked for expansion (it starts with an underscore) and isn't allowed to have aliases" % origin)
  40. if alias:
  41. alias = alias.lstrip('*')
  42. _alias = 'autoalias_%s_%s' % (_origin, '_'.join(expansion))
  43. try:
  44. f = transformer._get_func(alias or _origin)
  45. except AttributeError:
  46. if alias:
  47. f = self._create_tree_builder_function(alias)
  48. else:
  49. f = self._create_tree_builder_function(_origin)
  50. if expand1:
  51. f = create_expand1_tree_builder_function(f)
  52. alias_handler = create_rule_handler(expansion, f)
  53. assert not hasattr(callback, _alias)
  54. setattr(callback, _alias, alias_handler)
  55. new_rules.append(( _origin, expansion, _alias ))
  56. return new_rules, callback