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.

274 lines
8.2 KiB

  1. from functools import wraps
  2. from .utils import smart_decorator
  3. from .tree import Tree
  4. from .exceptions import VisitError, GrammarError
  5. ###{standalone
  6. from inspect import getmembers, getmro
  7. class Discard(Exception):
  8. pass
  9. # Transformers
  10. class Transformer:
  11. """Visits the tree recursively, starting with the leaves and finally the root (bottom-up)
  12. Calls its methods (provided by user via inheritance) according to tree.data
  13. The returned value replaces the old one in the structure.
  14. Can be used to implement map or reduce.
  15. """
  16. def _call_userfunc(self, tree, new_children=None):
  17. # Assumes tree is already transformed
  18. children = new_children if new_children is not None else tree.children
  19. try:
  20. f = getattr(self, tree.data)
  21. except AttributeError:
  22. return self.__default__(tree.data, children, tree.meta)
  23. else:
  24. try:
  25. if getattr(f, 'meta', False):
  26. return f(children, tree.meta)
  27. elif getattr(f, 'inline', False):
  28. return f(*children)
  29. elif getattr(f, 'whole_tree', False):
  30. if new_children is not None:
  31. raise NotImplementedError("Doesn't work with the base Transformer class")
  32. return f(tree)
  33. else:
  34. return f(children)
  35. except (GrammarError, Discard):
  36. raise
  37. except Exception as e:
  38. raise VisitError(tree, e)
  39. def _transform_children(self, children):
  40. for c in children:
  41. try:
  42. yield self._transform_tree(c) if isinstance(c, Tree) else c
  43. except Discard:
  44. pass
  45. def _transform_tree(self, tree):
  46. children = list(self._transform_children(tree.children))
  47. return self._call_userfunc(tree, children)
  48. def transform(self, tree):
  49. return self._transform_tree(tree)
  50. def __mul__(self, other):
  51. return TransformerChain(self, other)
  52. def __default__(self, data, children, meta):
  53. "Default operation on tree (for override)"
  54. return Tree(data, children, meta)
  55. @classmethod
  56. def _apply_decorator(cls, decorator, **kwargs):
  57. mro = getmro(cls)
  58. assert mro[0] is cls
  59. libmembers = {name for _cls in mro[1:] for name, _ in getmembers(_cls)}
  60. for name, value in getmembers(cls):
  61. # Make sure the function isn't inherited (unless it's overwritten)
  62. if name.startswith('_') or (name in libmembers and name not in cls.__dict__):
  63. continue
  64. if not callable(cls.__dict__[name]):
  65. continue
  66. # Skip if v_args already applied (at the function level)
  67. if hasattr(cls.__dict__[name], 'vargs_applied'):
  68. continue
  69. static = isinstance(cls.__dict__[name], (staticmethod, classmethod))
  70. setattr(cls, name, decorator(value, static=static, **kwargs))
  71. return cls
  72. class InlineTransformer(Transformer): # XXX Deprecated
  73. def _call_userfunc(self, tree, new_children=None):
  74. # Assumes tree is already transformed
  75. children = new_children if new_children is not None else tree.children
  76. try:
  77. f = getattr(self, tree.data)
  78. except AttributeError:
  79. return self.__default__(tree.data, children, tree.meta)
  80. else:
  81. return f(*children)
  82. class TransformerChain(object):
  83. def __init__(self, *transformers):
  84. self.transformers = transformers
  85. def transform(self, tree):
  86. for t in self.transformers:
  87. tree = t.transform(tree)
  88. return tree
  89. def __mul__(self, other):
  90. return TransformerChain(*self.transformers + (other,))
  91. class Transformer_InPlace(Transformer):
  92. "Non-recursive. Changes the tree in-place instead of returning new instances"
  93. def _transform_tree(self, tree): # Cancel recursion
  94. return self._call_userfunc(tree)
  95. def transform(self, tree):
  96. for subtree in tree.iter_subtrees():
  97. subtree.children = list(self._transform_children(subtree.children))
  98. return self._transform_tree(tree)
  99. class Transformer_InPlaceRecursive(Transformer):
  100. "Recursive. Changes the tree in-place instead of returning new instances"
  101. def _transform_tree(self, tree):
  102. tree.children = list(self._transform_children(tree.children))
  103. return self._call_userfunc(tree)
  104. # Visitors
  105. class VisitorBase:
  106. def _call_userfunc(self, tree):
  107. return getattr(self, tree.data, self.__default__)(tree)
  108. def __default__(self, tree):
  109. "Default operation on tree (for override)"
  110. return tree
  111. class Visitor(VisitorBase):
  112. """Bottom-up visitor, non-recursive
  113. Visits the tree, starting with the leaves and finally the root (bottom-up)
  114. Calls its methods (provided by user via inheritance) according to tree.data
  115. """
  116. def visit(self, tree):
  117. for subtree in tree.iter_subtrees():
  118. self._call_userfunc(subtree)
  119. return tree
  120. class Visitor_Recursive(VisitorBase):
  121. """Bottom-up visitor, recursive
  122. Visits the tree, starting with the leaves and finally the root (bottom-up)
  123. Calls its methods (provided by user via inheritance) according to tree.data
  124. """
  125. def visit(self, tree):
  126. for child in tree.children:
  127. if isinstance(child, Tree):
  128. self.visit(child)
  129. f = getattr(self, tree.data, self.__default__)
  130. f(tree)
  131. return tree
  132. def visit_children_decor(func):
  133. "See Interpreter"
  134. @wraps(func)
  135. def inner(cls, tree):
  136. values = cls.visit_children(tree)
  137. return func(cls, values)
  138. return inner
  139. class Interpreter:
  140. """Top-down visitor, recursive
  141. Visits the tree, starting with the root and finally the leaves (top-down)
  142. Calls its methods (provided by user via inheritance) according to tree.data
  143. Unlike Transformer and Visitor, the Interpreter doesn't automatically visit its sub-branches.
  144. The user has to explicitly call visit_children, or use the @visit_children_decor
  145. """
  146. def visit(self, tree):
  147. return getattr(self, tree.data)(tree)
  148. def visit_children(self, tree):
  149. return [self.visit(child) if isinstance(child, Tree) else child
  150. for child in tree.children]
  151. def __getattr__(self, name):
  152. return self.__default__
  153. def __default__(self, tree):
  154. return self.visit_children(tree)
  155. # Decorators
  156. def _apply_decorator(obj, decorator, **kwargs):
  157. try:
  158. _apply = obj._apply_decorator
  159. except AttributeError:
  160. return decorator(obj, **kwargs)
  161. else:
  162. return _apply(decorator, **kwargs)
  163. def _inline_args__func(func):
  164. @wraps(func)
  165. def create_decorator(_f, with_self):
  166. if with_self:
  167. def f(self, children):
  168. return _f(self, *children)
  169. else:
  170. def f(self, children):
  171. return _f(*children)
  172. return f
  173. return smart_decorator(func, create_decorator)
  174. def inline_args(obj): # XXX Deprecated
  175. return _apply_decorator(obj, _inline_args__func)
  176. def _visitor_args_func_dec(func, inline=False, meta=False, whole_tree=False, static=False):
  177. assert [whole_tree, meta, inline].count(True) <= 1
  178. def create_decorator(_f, with_self):
  179. if with_self:
  180. def f(self, *args, **kwargs):
  181. return _f(self, *args, **kwargs)
  182. else:
  183. def f(self, *args, **kwargs):
  184. return _f(*args, **kwargs)
  185. return f
  186. if static:
  187. f = wraps(func)(create_decorator(func, False))
  188. else:
  189. f = smart_decorator(func, create_decorator)
  190. f.vargs_applied = True
  191. f.inline = inline
  192. f.meta = meta
  193. f.whole_tree = whole_tree
  194. return f
  195. def v_args(inline=False, meta=False, tree=False):
  196. "A convenience decorator factory, for modifying the behavior of user-supplied visitor methods"
  197. if [tree, meta, inline].count(True) > 1:
  198. raise ValueError("Visitor functions can either accept tree, or meta, or be inlined. These cannot be combined.")
  199. def _visitor_args_dec(obj):
  200. return _apply_decorator(obj, _visitor_args_func_dec, inline=inline, meta=meta, whole_tree=tree)
  201. return _visitor_args_dec
  202. ###}