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.

232 lines
6.7 KiB

  1. try:
  2. from future_builtins import filter
  3. except ImportError:
  4. pass
  5. from copy import deepcopy
  6. ###{standalone
  7. from collections import OrderedDict
  8. class Meta:
  9. def __init__(self):
  10. self.empty = True
  11. class Tree(object):
  12. """The main tree class.
  13. Creates a new tree, and stores "data" and "children" in attributes of the same name.
  14. Trees can be hashed and compared.
  15. Parameters:
  16. data: The name of the rule or alias
  17. children: List of matched sub-rules and terminals
  18. meta: Line & Column numbers (if ``propagate_positions`` is enabled).
  19. meta attributes: line, column, start_pos, end_line, end_column, end_pos
  20. """
  21. def __init__(self, data, children, meta=None):
  22. self.data = data
  23. self.children = children
  24. self._meta = meta
  25. @property
  26. def meta(self):
  27. if self._meta is None:
  28. self._meta = Meta()
  29. return self._meta
  30. def __repr__(self):
  31. return 'Tree(%r, %r)' % (self.data, self.children)
  32. def _pretty_label(self):
  33. return self.data
  34. def _pretty(self, level, indent_str):
  35. if len(self.children) == 1 and not isinstance(self.children[0], Tree):
  36. return [indent_str*level, self._pretty_label(), '\t', '%s' % (self.children[0],), '\n']
  37. l = [indent_str*level, self._pretty_label(), '\n']
  38. for n in self.children:
  39. if isinstance(n, Tree):
  40. l += n._pretty(level+1, indent_str)
  41. else:
  42. l += [indent_str*(level+1), '%s' % (n,), '\n']
  43. return l
  44. def pretty(self, indent_str=' '):
  45. """Returns an indented string representation of the tree.
  46. Great for debugging.
  47. """
  48. return ''.join(self._pretty(0, indent_str))
  49. def __eq__(self, other):
  50. try:
  51. return self.data == other.data and self.children == other.children
  52. except AttributeError:
  53. return False
  54. def __ne__(self, other):
  55. return not (self == other)
  56. def __hash__(self):
  57. return hash((self.data, tuple(self.children)))
  58. def iter_subtrees(self):
  59. """Depth-first iteration.
  60. Iterates over all the subtrees, never returning to the same node twice (Lark's parse-tree is actually a DAG).
  61. """
  62. queue = [self]
  63. subtrees = OrderedDict()
  64. for subtree in queue:
  65. subtrees[id(subtree)] = subtree
  66. queue += [c for c in reversed(subtree.children)
  67. if isinstance(c, Tree) and id(c) not in subtrees]
  68. del queue
  69. return reversed(list(subtrees.values()))
  70. def find_pred(self, pred):
  71. """Returns all nodes of the tree that evaluate pred(node) as true."""
  72. return filter(pred, self.iter_subtrees())
  73. def find_data(self, data):
  74. """Returns all nodes of the tree whose data equals the given data."""
  75. return self.find_pred(lambda t: t.data == data)
  76. ###}
  77. def expand_kids_by_index(self, *indices):
  78. """Expand (inline) children at the given indices"""
  79. for i in sorted(indices, reverse=True): # reverse so that changing tail won't affect indices
  80. kid = self.children[i]
  81. self.children[i:i+1] = kid.children
  82. def expand_kids_by_data(self, *data_values):
  83. """Expand (inline) children with any of the given data values. Returns True if anything changed"""
  84. changed = False
  85. for i in range(len(self.children)-1, -1, -1):
  86. child = self.children[i]
  87. if isinstance(child, Tree) and child.data in data_values:
  88. self.children[i:i+1] = child.children
  89. changed = True
  90. return changed
  91. def scan_values(self, pred):
  92. """Return all values in the tree that evaluate pred(value) as true.
  93. This can be used to find all the tokens in the tree.
  94. Example:
  95. >>> all_tokens = tree.scan_values(lambda v: isinstance(v, Token))
  96. """
  97. for c in self.children:
  98. if isinstance(c, Tree):
  99. for t in c.scan_values(pred):
  100. yield t
  101. else:
  102. if pred(c):
  103. yield c
  104. def iter_subtrees_topdown(self):
  105. """Breadth-first iteration.
  106. Iterates over all the subtrees, return nodes in order like pretty() does.
  107. """
  108. stack = [self]
  109. while stack:
  110. node = stack.pop()
  111. if not isinstance(node, Tree):
  112. continue
  113. yield node
  114. for n in reversed(node.children):
  115. stack.append(n)
  116. def __deepcopy__(self, memo):
  117. return type(self)(self.data, deepcopy(self.children, memo), meta=self._meta)
  118. def copy(self):
  119. return type(self)(self.data, self.children)
  120. def set(self, data, children):
  121. self.data = data
  122. self.children = children
  123. # XXX Deprecated! Here for backwards compatibility <0.6.0
  124. @property
  125. def line(self):
  126. return self.meta.line
  127. @property
  128. def column(self):
  129. return self.meta.column
  130. @property
  131. def end_line(self):
  132. return self.meta.end_line
  133. @property
  134. def end_column(self):
  135. return self.meta.end_column
  136. class SlottedTree(Tree):
  137. __slots__ = 'data', 'children', 'rule', '_meta'
  138. def pydot__tree_to_png(tree, filename, rankdir="LR", **kwargs):
  139. graph = pydot__tree_to_graph(tree, rankdir, **kwargs)
  140. graph.write_png(filename)
  141. def pydot__tree_to_dot(tree, filename, rankdir="LR", **kwargs):
  142. graph = pydot__tree_to_graph(tree, rankdir, **kwargs)
  143. graph.write(filename)
  144. def pydot__tree_to_graph(tree, rankdir="LR", **kwargs):
  145. """Creates a colorful image that represents the tree (data+children, without meta)
  146. Possible values for `rankdir` are "TB", "LR", "BT", "RL", corresponding to
  147. directed graphs drawn from top to bottom, from left to right, from bottom to
  148. top, and from right to left, respectively.
  149. `kwargs` can be any graph attribute (e. g. `dpi=200`). For a list of
  150. possible attributes, see https://www.graphviz.org/doc/info/attrs.html.
  151. """
  152. import pydot
  153. graph = pydot.Dot(graph_type='digraph', rankdir=rankdir, **kwargs)
  154. i = [0]
  155. def new_leaf(leaf):
  156. node = pydot.Node(i[0], label=repr(leaf))
  157. i[0] += 1
  158. graph.add_node(node)
  159. return node
  160. def _to_pydot(subtree):
  161. color = hash(subtree.data) & 0xffffff
  162. color |= 0x808080
  163. subnodes = [_to_pydot(child) if isinstance(child, Tree) else new_leaf(child)
  164. for child in subtree.children]
  165. node = pydot.Node(i[0], style="filled", fillcolor="#%x" % color, label=subtree.data)
  166. i[0] += 1
  167. graph.add_node(node)
  168. for subnode in subnodes:
  169. graph.add_edge(pydot.Edge(node, subnode))
  170. return node
  171. _to_pydot(tree)
  172. return graph