This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

221 рядки
6.3 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 scan_values(self, pred):
  83. """Return all values in the tree that evaluate pred(value) as true.
  84. This can be used to find all the tokens in the tree.
  85. Example:
  86. >>> all_tokens = tree.scan_values(lambda v: isinstance(v, Token))
  87. """
  88. for c in self.children:
  89. if isinstance(c, Tree):
  90. for t in c.scan_values(pred):
  91. yield t
  92. else:
  93. if pred(c):
  94. yield c
  95. def iter_subtrees_topdown(self):
  96. """Breadth-first iteration.
  97. Iterates over all the subtrees, return nodes in order like pretty() does.
  98. """
  99. stack = [self]
  100. while stack:
  101. node = stack.pop()
  102. if not isinstance(node, Tree):
  103. continue
  104. yield node
  105. for n in reversed(node.children):
  106. stack.append(n)
  107. def __deepcopy__(self, memo):
  108. return type(self)(self.data, deepcopy(self.children, memo), meta=self._meta)
  109. def copy(self):
  110. return type(self)(self.data, self.children)
  111. def set(self, data, children):
  112. self.data = data
  113. self.children = children
  114. # XXX Deprecated! Here for backwards compatibility <0.6.0
  115. @property
  116. def line(self):
  117. return self.meta.line
  118. @property
  119. def column(self):
  120. return self.meta.column
  121. @property
  122. def end_line(self):
  123. return self.meta.end_line
  124. @property
  125. def end_column(self):
  126. return self.meta.end_column
  127. class SlottedTree(Tree):
  128. __slots__ = 'data', 'children', 'rule', '_meta'
  129. def pydot__tree_to_png(tree, filename, rankdir="LR", **kwargs):
  130. graph = pydot__tree_to_graph(tree, rankdir, **kwargs)
  131. graph.write_png(filename)
  132. def pydot__tree_to_dot(tree, filename, rankdir="LR", **kwargs):
  133. graph = pydot__tree_to_graph(tree, rankdir, **kwargs)
  134. graph.write(filename)
  135. def pydot__tree_to_graph(tree, rankdir="LR", **kwargs):
  136. """Creates a colorful image that represents the tree (data+children, without meta)
  137. Possible values for `rankdir` are "TB", "LR", "BT", "RL", corresponding to
  138. directed graphs drawn from top to bottom, from left to right, from bottom to
  139. top, and from right to left, respectively.
  140. `kwargs` can be any graph attribute (e. g. `dpi=200`). For a list of
  141. possible attributes, see https://www.graphviz.org/doc/info/attrs.html.
  142. """
  143. import pydot
  144. graph = pydot.Dot(graph_type='digraph', rankdir=rankdir, **kwargs)
  145. i = [0]
  146. def new_leaf(leaf):
  147. node = pydot.Node(i[0], label=repr(leaf))
  148. i[0] += 1
  149. graph.add_node(node)
  150. return node
  151. def _to_pydot(subtree):
  152. color = hash(subtree.data) & 0xffffff
  153. color |= 0x808080
  154. subnodes = [_to_pydot(child) if isinstance(child, Tree) else new_leaf(child)
  155. for child in subtree.children]
  156. node = pydot.Node(i[0], style="filled", fillcolor="#%x" % color, label=subtree.data)
  157. i[0] += 1
  158. graph.add_node(node)
  159. for subnode in subnodes:
  160. graph.add_edge(pydot.Edge(node, subnode))
  161. return node
  162. _to_pydot(tree)
  163. return graph