This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

791 linhas
30 KiB

  1. """"This module implements an SPPF implementation
  2. This is used as the primary output mechanism for the Earley parser
  3. in order to store complex ambiguities.
  4. Full reference and more details is here:
  5. http://www.bramvandersanden.com/post/2014/06/shared-packed-parse-forest/
  6. """
  7. from random import randint
  8. from math import isinf
  9. from collections import deque
  10. from operator import attrgetter
  11. from importlib import import_module
  12. from functools import partial
  13. from ..parse_tree_builder import AmbiguousIntermediateExpander
  14. from ..visitors import Discard
  15. from ..lexer import Token
  16. from ..utils import logger
  17. from ..tree import Tree
  18. class ForestNode(object):
  19. pass
  20. class SymbolNode(ForestNode):
  21. """
  22. A Symbol Node represents a symbol (or Intermediate LR0).
  23. Symbol nodes are keyed by the symbol (s). For intermediate nodes
  24. s will be an LR0, stored as a tuple of (rule, ptr). For completed symbol
  25. nodes, s will be a string representing the non-terminal origin (i.e.
  26. the left hand side of the rule).
  27. The children of a Symbol or Intermediate Node will always be Packed Nodes;
  28. with each Packed Node child representing a single derivation of a production.
  29. Hence a Symbol Node with a single child is unambiguous.
  30. :ivar s: A Symbol, or a tuple of (rule, ptr) for an intermediate node.
  31. :ivar start: The index of the start of the substring matched by this
  32. symbol (inclusive).
  33. :ivar end: The index of the end of the substring matched by this
  34. symbol (exclusive).
  35. :ivar is_intermediate: True if this node is an intermediate node.
  36. :ivar priority: The priority of the node's symbol.
  37. """
  38. __slots__ = ('s', 'start', 'end', '_children', 'paths', 'paths_loaded', 'priority', 'is_intermediate', '_hash')
  39. def __init__(self, s, start, end):
  40. self.s = s
  41. self.start = start
  42. self.end = end
  43. self._children = set()
  44. self.paths = set()
  45. self.paths_loaded = False
  46. ### We use inf here as it can be safely negated without resorting to conditionals,
  47. # unlike None or float('NaN'), and sorts appropriately.
  48. self.priority = float('-inf')
  49. self.is_intermediate = isinstance(s, tuple)
  50. self._hash = hash((self.s, self.start, self.end))
  51. def add_family(self, lr0, rule, start, left, right):
  52. self._children.add(PackedNode(self, lr0, rule, start, left, right))
  53. def add_path(self, transitive, node):
  54. self.paths.add((transitive, node))
  55. def load_paths(self):
  56. for transitive, node in self.paths:
  57. if transitive.next_titem is not None:
  58. vn = SymbolNode(transitive.next_titem.s, transitive.next_titem.start, self.end)
  59. vn.add_path(transitive.next_titem, node)
  60. self.add_family(transitive.reduction.rule.origin, transitive.reduction.rule, transitive.reduction.start, transitive.reduction.node, vn)
  61. else:
  62. self.add_family(transitive.reduction.rule.origin, transitive.reduction.rule, transitive.reduction.start, transitive.reduction.node, node)
  63. self.paths_loaded = True
  64. @property
  65. def is_ambiguous(self):
  66. """Returns True if this node is ambiguous."""
  67. return len(self.children) > 1
  68. @property
  69. def children(self):
  70. """Returns a list of this node's children sorted from greatest to
  71. least priority."""
  72. if not self.paths_loaded: self.load_paths()
  73. return sorted(self._children, key=attrgetter('sort_key'))
  74. def __iter__(self):
  75. return iter(self._children)
  76. def __eq__(self, other):
  77. if not isinstance(other, SymbolNode):
  78. return False
  79. return self is other or (type(self.s) == type(other.s) and self.s == other.s and self.start == other.start and self.end is other.end)
  80. def __hash__(self):
  81. return self._hash
  82. def __repr__(self):
  83. if self.is_intermediate:
  84. rule = self.s[0]
  85. ptr = self.s[1]
  86. before = ( expansion.name for expansion in rule.expansion[:ptr] )
  87. after = ( expansion.name for expansion in rule.expansion[ptr:] )
  88. symbol = "{} ::= {}* {}".format(rule.origin.name, ' '.join(before), ' '.join(after))
  89. else:
  90. symbol = self.s.name
  91. return "({}, {}, {}, {})".format(symbol, self.start, self.end, self.priority)
  92. class PackedNode(ForestNode):
  93. """
  94. A Packed Node represents a single derivation in a symbol node.
  95. :ivar rule: The rule associated with this node.
  96. :ivar parent: The parent of this node.
  97. :ivar left: The left child of this node. ``None`` if one does not exist.
  98. :ivar right: The right child of this node. ``None`` if one does not exist.
  99. :ivar priority: The priority of this node.
  100. """
  101. __slots__ = ('parent', 's', 'rule', 'start', 'left', 'right', 'priority', '_hash')
  102. def __init__(self, parent, s, rule, start, left, right):
  103. self.parent = parent
  104. self.s = s
  105. self.start = start
  106. self.rule = rule
  107. self.left = left
  108. self.right = right
  109. self.priority = float('-inf')
  110. self._hash = hash((self.left, self.right))
  111. @property
  112. def is_empty(self):
  113. return self.left is None and self.right is None
  114. @property
  115. def sort_key(self):
  116. """
  117. Used to sort PackedNode children of SymbolNodes.
  118. A SymbolNode has multiple PackedNodes if it matched
  119. ambiguously. Hence, we use the sort order to identify
  120. the order in which ambiguous children should be considered.
  121. """
  122. return self.is_empty, -self.priority, self.rule.order
  123. @property
  124. def children(self):
  125. """Returns a list of this node's children."""
  126. return [x for x in [self.left, self.right] if x is not None]
  127. def __iter__(self):
  128. yield self.left
  129. yield self.right
  130. def __eq__(self, other):
  131. if not isinstance(other, PackedNode):
  132. return False
  133. return self is other or (self.left == other.left and self.right == other.right)
  134. def __hash__(self):
  135. return self._hash
  136. def __repr__(self):
  137. if isinstance(self.s, tuple):
  138. rule = self.s[0]
  139. ptr = self.s[1]
  140. before = ( expansion.name for expansion in rule.expansion[:ptr] )
  141. after = ( expansion.name for expansion in rule.expansion[ptr:] )
  142. symbol = "{} ::= {}* {}".format(rule.origin.name, ' '.join(before), ' '.join(after))
  143. else:
  144. symbol = self.s.name
  145. return "({}, {}, {}, {})".format(symbol, self.start, self.priority, self.rule.order)
  146. class ForestVisitor(object):
  147. """
  148. An abstract base class for building forest visitors.
  149. This class performs a controllable depth-first walk of an SPPF.
  150. The visitor will not enter cycles and will backtrack if one is encountered.
  151. Subclasses are notified of cycles through the ``on_cycle`` method.
  152. Behavior for visit events is defined by overriding the
  153. ``visit*node*`` functions.
  154. The walk is controlled by the return values of the ``visit*node_in``
  155. methods. Returning a node(s) will schedule them to be visited. The visitor
  156. will begin to backtrack if no nodes are returned.
  157. :ivar single_visit: If ``True``, non-Token nodes will only be visited once.
  158. """
  159. def __init__(self, single_visit=False):
  160. self.single_visit = single_visit
  161. def visit_token_node(self, node):
  162. """Called when a ``Token`` is visited. ``Token`` nodes are always leaves."""
  163. pass
  164. def visit_symbol_node_in(self, node):
  165. """Called when a symbol node is visited. Nodes that are returned
  166. will be scheduled to be visited. If ``visit_intermediate_node_in``
  167. is not implemented, this function will be called for intermediate
  168. nodes as well."""
  169. pass
  170. def visit_symbol_node_out(self, node):
  171. """Called after all nodes returned from a corresponding ``visit_symbol_node_in``
  172. call have been visited. If ``visit_intermediate_node_out``
  173. is not implemented, this function will be called for intermediate
  174. nodes as well."""
  175. pass
  176. def visit_packed_node_in(self, node):
  177. """Called when a packed node is visited. Nodes that are returned
  178. will be scheduled to be visited. """
  179. pass
  180. def visit_packed_node_out(self, node):
  181. """Called after all nodes returned from a corresponding ``visit_packed_node_in``
  182. call have been visited."""
  183. pass
  184. def on_cycle(self, node, path):
  185. """Called when a cycle is encountered.
  186. :param node: The node that causes a cycle.
  187. :param path: The list of nodes being visited: nodes that have been
  188. entered but not exited. The first element is the root in a forest
  189. visit, and the last element is the node visited most recently.
  190. ``path`` should be treated as read-only.
  191. """
  192. pass
  193. def get_cycle_in_path(self, node, path):
  194. """A utility function for use in ``on_cycle`` to obtain a slice of
  195. ``path`` that only contains the nodes that make up the cycle."""
  196. index = len(path) - 1
  197. while id(path[index]) != id(node):
  198. index -= 1
  199. return path[index:]
  200. def visit(self, root):
  201. # Visiting is a list of IDs of all symbol/intermediate nodes currently in
  202. # the stack. It serves two purposes: to detect when we 'recurse' in and out
  203. # of a symbol/intermediate so that we can process both up and down. Also,
  204. # since the SPPF can have cycles it allows us to detect if we're trying
  205. # to recurse into a node that's already on the stack (infinite recursion).
  206. visiting = set()
  207. # set of all nodes that have been visited
  208. visited = set()
  209. # a list of nodes that are currently being visited
  210. # used for the `on_cycle` callback
  211. path = []
  212. # We do not use recursion here to walk the Forest due to the limited
  213. # stack size in python. Therefore input_stack is essentially our stack.
  214. input_stack = deque([root])
  215. # It is much faster to cache these as locals since they are called
  216. # many times in large parses.
  217. vpno = getattr(self, 'visit_packed_node_out')
  218. vpni = getattr(self, 'visit_packed_node_in')
  219. vsno = getattr(self, 'visit_symbol_node_out')
  220. vsni = getattr(self, 'visit_symbol_node_in')
  221. vino = getattr(self, 'visit_intermediate_node_out', vsno)
  222. vini = getattr(self, 'visit_intermediate_node_in', vsni)
  223. vtn = getattr(self, 'visit_token_node')
  224. oc = getattr(self, 'on_cycle')
  225. while input_stack:
  226. current = next(reversed(input_stack))
  227. try:
  228. next_node = next(current)
  229. except StopIteration:
  230. input_stack.pop()
  231. continue
  232. except TypeError:
  233. ### If the current object is not an iterator, pass through to Token/SymbolNode
  234. pass
  235. else:
  236. if next_node is None:
  237. continue
  238. if id(next_node) in visiting:
  239. oc(next_node, path)
  240. continue
  241. input_stack.append(next_node)
  242. continue
  243. if not isinstance(current, ForestNode):
  244. vtn(current)
  245. input_stack.pop()
  246. continue
  247. current_id = id(current)
  248. if current_id in visiting:
  249. if isinstance(current, PackedNode):
  250. vpno(current)
  251. elif current.is_intermediate:
  252. vino(current)
  253. else:
  254. vsno(current)
  255. input_stack.pop()
  256. path.pop()
  257. visiting.remove(current_id)
  258. visited.add(current_id)
  259. elif self.single_visit and current_id in visited:
  260. input_stack.pop()
  261. else:
  262. visiting.add(current_id)
  263. path.append(current)
  264. if isinstance(current, PackedNode):
  265. next_node = vpni(current)
  266. elif current.is_intermediate:
  267. next_node = vini(current)
  268. else:
  269. next_node = vsni(current)
  270. if next_node is None:
  271. continue
  272. if not isinstance(next_node, ForestNode) and \
  273. not isinstance(next_node, Token):
  274. next_node = iter(next_node)
  275. elif id(next_node) in visiting:
  276. oc(next_node, path)
  277. continue
  278. input_stack.append(next_node)
  279. class ForestTransformer(ForestVisitor):
  280. """The base class for a bottom-up forest transformation. Most users will
  281. want to use ``TreeForestTransformer`` instead as it has a friendlier
  282. interface and covers most use cases.
  283. Transformations are applied via inheritance and overriding of the
  284. ``transform*node`` methods.
  285. ``transform_token_node`` receives a ``Token`` as an argument.
  286. All other methods receive the node that is being transformed and
  287. a list of the results of the transformations of that node's children.
  288. The return value of these methods are the resulting transformations.
  289. If ``Discard`` is raised in a node's transformation, no data from that node
  290. will be passed to its parent's transformation.
  291. """
  292. def __init__(self):
  293. super(ForestTransformer, self).__init__()
  294. # results of transformations
  295. self.data = dict()
  296. # used to track parent nodes
  297. self.node_stack = deque()
  298. def transform(self, root):
  299. """Perform a transformation on an SPPF."""
  300. self.node_stack.append('result')
  301. self.data['result'] = []
  302. self.visit(root)
  303. assert len(self.data['result']) <= 1
  304. if self.data['result']:
  305. return self.data['result'][0]
  306. def transform_symbol_node(self, node, data):
  307. """Transform a symbol node."""
  308. return node
  309. def transform_intermediate_node(self, node, data):
  310. """Transform an intermediate node."""
  311. return node
  312. def transform_packed_node(self, node, data):
  313. """Transform a packed node."""
  314. return node
  315. def transform_token_node(self, node):
  316. """Transform a ``Token``."""
  317. return node
  318. def visit_symbol_node_in(self, node):
  319. self.node_stack.append(id(node))
  320. self.data[id(node)] = []
  321. return node.children
  322. def visit_packed_node_in(self, node):
  323. self.node_stack.append(id(node))
  324. self.data[id(node)] = []
  325. return node.children
  326. def visit_token_node(self, node):
  327. try:
  328. transformed = self.transform_token_node(node)
  329. except Discard:
  330. pass
  331. else:
  332. self.data[self.node_stack[-1]].append(transformed)
  333. def visit_symbol_node_out(self, node):
  334. self.node_stack.pop()
  335. try:
  336. transformed = self.transform_symbol_node(node, self.data[id(node)])
  337. except Discard:
  338. pass
  339. else:
  340. self.data[self.node_stack[-1]].append(transformed)
  341. finally:
  342. del self.data[id(node)]
  343. def visit_intermediate_node_out(self, node):
  344. self.node_stack.pop()
  345. try:
  346. transformed = self.transform_intermediate_node(node, self.data[id(node)])
  347. except Discard:
  348. pass
  349. else:
  350. self.data[self.node_stack[-1]].append(transformed)
  351. finally:
  352. del self.data[id(node)]
  353. def visit_packed_node_out(self, node):
  354. self.node_stack.pop()
  355. try:
  356. transformed = self.transform_packed_node(node, self.data[id(node)])
  357. except Discard:
  358. pass
  359. else:
  360. self.data[self.node_stack[-1]].append(transformed)
  361. finally:
  362. del self.data[id(node)]
  363. class ForestSumVisitor(ForestVisitor):
  364. """
  365. A visitor for prioritizing ambiguous parts of the Forest.
  366. This visitor is used when support for explicit priorities on
  367. rules is requested (whether normal, or invert). It walks the
  368. forest (or subsets thereof) and cascades properties upwards
  369. from the leaves.
  370. It would be ideal to do this during parsing, however this would
  371. require processing each Earley item multiple times. That's
  372. a big performance drawback; so running a forest walk is the
  373. lesser of two evils: there can be significantly more Earley
  374. items created during parsing than there are SPPF nodes in the
  375. final tree.
  376. """
  377. def __init__(self):
  378. super(ForestSumVisitor, self).__init__(single_visit=True)
  379. def visit_packed_node_in(self, node):
  380. yield node.left
  381. yield node.right
  382. def visit_symbol_node_in(self, node):
  383. return iter(node.children)
  384. def visit_packed_node_out(self, node):
  385. priority = node.rule.options.priority if not node.parent.is_intermediate and node.rule.options.priority else 0
  386. priority += getattr(node.right, 'priority', 0)
  387. priority += getattr(node.left, 'priority', 0)
  388. node.priority = priority
  389. def visit_symbol_node_out(self, node):
  390. node.priority = max(child.priority for child in node.children)
  391. class PackedData():
  392. """Used in transformationss of packed nodes to distinguish the data
  393. that comes from the left child and the right child.
  394. """
  395. class _NoData():
  396. pass
  397. NO_DATA = _NoData()
  398. def __init__(self, node, data):
  399. self.left = self.NO_DATA
  400. self.right = self.NO_DATA
  401. if data:
  402. if node.left is not None:
  403. self.left = data[0]
  404. if len(data) > 1:
  405. self.right = data[1]
  406. else:
  407. self.right = data[0]
  408. class ForestToParseTree(ForestTransformer):
  409. """Used by the earley parser when ambiguity equals 'resolve' or
  410. 'explicit'. Transforms an SPPF into an (ambiguous) parse tree.
  411. tree_class: The tree class to use for construction
  412. callbacks: A dictionary of rules to functions that output a tree
  413. prioritizer: A ``ForestVisitor`` that manipulates the priorities of
  414. ForestNodes
  415. resolve_ambiguity: If True, ambiguities will be resolved based on
  416. priorities. Otherwise, `_ambig` nodes will be in the resulting
  417. tree.
  418. use_cache: If True, the results of packed node transformations will be
  419. cached.
  420. """
  421. def __init__(self, tree_class=Tree, callbacks=dict(), prioritizer=ForestSumVisitor(), resolve_ambiguity=True, use_cache=True):
  422. super(ForestToParseTree, self).__init__()
  423. self.tree_class = tree_class
  424. self.callbacks = callbacks
  425. self.prioritizer = prioritizer
  426. self.resolve_ambiguity = resolve_ambiguity
  427. self._use_cache = use_cache
  428. self._cache = {}
  429. self._on_cycle_retreat = False
  430. self._cycle_node = None
  431. self._successful_visits = set()
  432. def visit(self, root):
  433. if self.prioritizer:
  434. self.prioritizer.visit(root)
  435. super(ForestToParseTree, self).visit(root)
  436. self._cache = {}
  437. def on_cycle(self, node, path):
  438. logger.debug("Cycle encountered in the SPPF at node: %s. "
  439. "As infinite ambiguities cannot be represented in a tree, "
  440. "this family of derivations will be discarded.", node)
  441. self._cycle_node = node
  442. self._on_cycle_retreat = True
  443. def _check_cycle(self, node):
  444. if self._on_cycle_retreat:
  445. if id(node) == id(self._cycle_node) or id(node) in self._successful_visits:
  446. self._cycle_node = None
  447. self._on_cycle_retreat = False
  448. return
  449. raise Discard()
  450. def _collapse_ambig(self, children):
  451. new_children = []
  452. for child in children:
  453. if hasattr(child, 'data') and child.data == '_ambig':
  454. new_children += child.children
  455. else:
  456. new_children.append(child)
  457. return new_children
  458. def _call_rule_func(self, node, data):
  459. # called when transforming children of symbol nodes
  460. # data is a list of trees or tokens that correspond to the
  461. # symbol's rule expansion
  462. return self.callbacks[node.rule](data)
  463. def _call_ambig_func(self, node, data):
  464. # called when transforming a symbol node
  465. # data is a list of trees where each tree's data is
  466. # equal to the name of the symbol or one of its aliases.
  467. if len(data) > 1:
  468. return self.tree_class('_ambig', data)
  469. elif data:
  470. return data[0]
  471. raise Discard()
  472. def transform_symbol_node(self, node, data):
  473. if id(node) not in self._successful_visits:
  474. raise Discard()
  475. self._check_cycle(node)
  476. self._successful_visits.remove(id(node))
  477. data = self._collapse_ambig(data)
  478. return self._call_ambig_func(node, data)
  479. def transform_intermediate_node(self, node, data):
  480. if id(node) not in self._successful_visits:
  481. raise Discard()
  482. self._check_cycle(node)
  483. self._successful_visits.remove(id(node))
  484. if len(data) > 1:
  485. children = [self.tree_class('_inter', c) for c in data]
  486. return self.tree_class('_iambig', children)
  487. return data[0]
  488. def transform_packed_node(self, node, data):
  489. self._check_cycle(node)
  490. if self.resolve_ambiguity and id(node.parent) in self._successful_visits:
  491. raise Discard()
  492. if self._use_cache and id(node) in self._cache:
  493. return self._cache[id(node)]
  494. children = []
  495. assert len(data) <= 2
  496. data = PackedData(node, data)
  497. if data.left is not PackedData.NO_DATA:
  498. if node.left.is_intermediate and isinstance(data.left, list):
  499. children += data.left
  500. else:
  501. children.append(data.left)
  502. if data.right is not PackedData.NO_DATA:
  503. children.append(data.right)
  504. if node.parent.is_intermediate:
  505. return self._cache.setdefault(id(node), children)
  506. return self._cache.setdefault(id(node), self._call_rule_func(node, children))
  507. def visit_symbol_node_in(self, node):
  508. super(ForestToParseTree, self).visit_symbol_node_in(node)
  509. if self._on_cycle_retreat:
  510. return
  511. return node.children
  512. def visit_packed_node_in(self, node):
  513. self._on_cycle_retreat = False
  514. to_visit = super(ForestToParseTree, self).visit_packed_node_in(node)
  515. if not self.resolve_ambiguity or id(node.parent) not in self._successful_visits:
  516. if not self._use_cache or id(node) not in self._cache:
  517. return to_visit
  518. def visit_packed_node_out(self, node):
  519. super(ForestToParseTree, self).visit_packed_node_out(node)
  520. if not self._on_cycle_retreat:
  521. self._successful_visits.add(id(node.parent))
  522. def handles_ambiguity(func):
  523. """Decorator for methods of subclasses of ``TreeForestTransformer``.
  524. Denotes that the method should receive a list of transformed derivations."""
  525. func.handles_ambiguity = True
  526. return func
  527. class TreeForestTransformer(ForestToParseTree):
  528. """A ``ForestTransformer`` with a tree ``Transformer``-like interface.
  529. By default, it will construct a tree.
  530. Methods provided via inheritance are called based on the rule/symbol
  531. names of nodes in the forest.
  532. Methods that act on rules will receive a list of the results of the
  533. transformations of the rule's children. By default, trees and tokens.
  534. Methods that act on tokens will receive a token.
  535. Alternatively, methods that act on rules may be annotated with
  536. ``handles_ambiguity``. In this case, the function will receive a list
  537. of all the transformations of all the derivations of the rule.
  538. By default, a list of trees where each tree.data is equal to the
  539. rule name or one of its aliases.
  540. Non-tree transformations are made possible by override of
  541. ``__default__``, ``__default_token__``, and ``__default_ambig__``.
  542. .. note::
  543. Tree shaping features such as inlined rules and token filtering are
  544. not built into the transformation. Positions are also not
  545. propagated.
  546. :param tree_class: The tree class to use for construction
  547. :param prioritizer: A ``ForestVisitor`` that manipulates the priorities of
  548. nodes in the SPPF.
  549. :param resolve_ambiguity: If True, ambiguities will be resolved based on
  550. priorities.
  551. :param use_cache: If True, caches the results of some transformations,
  552. potentially improving performance when ``resolve_ambiguity==False``.
  553. Only use if you know what you are doing: i.e. All transformation
  554. functions are pure and referentially transparent.
  555. """
  556. def __init__(self, tree_class=Tree, prioritizer=ForestSumVisitor(), resolve_ambiguity=True, use_cache=False):
  557. super(TreeForestTransformer, self).__init__(tree_class, dict(), prioritizer, resolve_ambiguity, use_cache)
  558. def __default__(self, name, data):
  559. """Default operation on tree (for override).
  560. Returns a tree with name with data as children.
  561. """
  562. return self.tree_class(name, data)
  563. def __default_ambig__(self, name, data):
  564. """Default operation on ambiguous rule (for override).
  565. Wraps data in an '_ambig_' node if it contains more than
  566. one element.
  567. """
  568. if len(data) > 1:
  569. return self.tree_class('_ambig', data)
  570. elif data:
  571. return data[0]
  572. raise Discard()
  573. def __default_token__(self, node):
  574. """Default operation on ``Token`` (for override).
  575. Returns ``node``.
  576. """
  577. return node
  578. def transform_token_node(self, node):
  579. return getattr(self, node.type, self.__default_token__)(node)
  580. def _call_rule_func(self, node, data):
  581. name = node.rule.alias or node.rule.options.template_source or node.rule.origin.name
  582. user_func = getattr(self, name, self.__default__)
  583. if user_func == self.__default__ or hasattr(user_func, 'handles_ambiguity'):
  584. user_func = partial(self.__default__, name)
  585. if not self.resolve_ambiguity:
  586. wrapper = partial(AmbiguousIntermediateExpander, self.tree_class)
  587. user_func = wrapper(user_func)
  588. return user_func(data)
  589. def _call_ambig_func(self, node, data):
  590. name = node.s.name
  591. user_func = getattr(self, name, self.__default_ambig__)
  592. if user_func == self.__default_ambig__ or not hasattr(user_func, 'handles_ambiguity'):
  593. user_func = partial(self.__default_ambig__, name)
  594. return user_func(data)
  595. class ForestToPyDotVisitor(ForestVisitor):
  596. """
  597. A Forest visitor which writes the SPPF to a PNG.
  598. The SPPF can get really large, really quickly because
  599. of the amount of meta-data it stores, so this is probably
  600. only useful for trivial trees and learning how the SPPF
  601. is structured.
  602. """
  603. def __init__(self, rankdir="TB"):
  604. super(ForestToPyDotVisitor, self).__init__(single_visit=True)
  605. self.pydot = import_module('pydot')
  606. self.graph = self.pydot.Dot(graph_type='digraph', rankdir=rankdir)
  607. def visit(self, root, filename):
  608. super(ForestToPyDotVisitor, self).visit(root)
  609. try:
  610. self.graph.write_png(filename)
  611. except FileNotFoundError as e:
  612. logger.error("Could not write png: ", e)
  613. def visit_token_node(self, node):
  614. graph_node_id = str(id(node))
  615. graph_node_label = "\"{}\"".format(node.value.replace('"', '\\"'))
  616. graph_node_color = 0x808080
  617. graph_node_style = "\"filled,rounded\""
  618. graph_node_shape = "diamond"
  619. graph_node = self.pydot.Node(graph_node_id, style=graph_node_style, fillcolor="#{:06x}".format(graph_node_color), shape=graph_node_shape, label=graph_node_label)
  620. self.graph.add_node(graph_node)
  621. def visit_packed_node_in(self, node):
  622. graph_node_id = str(id(node))
  623. graph_node_label = repr(node)
  624. graph_node_color = 0x808080
  625. graph_node_style = "filled"
  626. graph_node_shape = "diamond"
  627. graph_node = self.pydot.Node(graph_node_id, style=graph_node_style, fillcolor="#{:06x}".format(graph_node_color), shape=graph_node_shape, label=graph_node_label)
  628. self.graph.add_node(graph_node)
  629. yield node.left
  630. yield node.right
  631. def visit_packed_node_out(self, node):
  632. graph_node_id = str(id(node))
  633. graph_node = self.graph.get_node(graph_node_id)[0]
  634. for child in [node.left, node.right]:
  635. if child is not None:
  636. child_graph_node_id = str(id(child))
  637. child_graph_node = self.graph.get_node(child_graph_node_id)[0]
  638. self.graph.add_edge(self.pydot.Edge(graph_node, child_graph_node))
  639. else:
  640. #### Try and be above the Python object ID range; probably impl. specific, but maybe this is okay.
  641. child_graph_node_id = str(randint(100000000000000000000000000000,123456789012345678901234567890))
  642. child_graph_node_style = "invis"
  643. child_graph_node = self.pydot.Node(child_graph_node_id, style=child_graph_node_style, label="None")
  644. child_edge_style = "invis"
  645. self.graph.add_node(child_graph_node)
  646. self.graph.add_edge(self.pydot.Edge(graph_node, child_graph_node, style=child_edge_style))
  647. def visit_symbol_node_in(self, node):
  648. graph_node_id = str(id(node))
  649. graph_node_label = repr(node)
  650. graph_node_color = 0x808080
  651. graph_node_style = "\"filled\""
  652. if node.is_intermediate:
  653. graph_node_shape = "ellipse"
  654. else:
  655. graph_node_shape = "rectangle"
  656. graph_node = self.pydot.Node(graph_node_id, style=graph_node_style, fillcolor="#{:06x}".format(graph_node_color), shape=graph_node_shape, label=graph_node_label)
  657. self.graph.add_node(graph_node)
  658. return iter(node.children)
  659. def visit_symbol_node_out(self, node):
  660. graph_node_id = str(id(node))
  661. graph_node = self.graph.get_node(graph_node_id)[0]
  662. for child in node.children:
  663. child_graph_node_id = str(id(child))
  664. child_graph_node = self.graph.get_node(child_graph_node_id)[0]
  665. self.graph.add_edge(self.pydot.Edge(graph_node, child_graph_node))