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.

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