This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

119 řádky
2.9 KiB

  1. Transformers & Visitors
  2. =======================
  3. Transformers & Visitors provide a convenient interface to process the
  4. parse-trees that Lark returns.
  5. They are used by inheriting from the correct class (visitor or transformer),
  6. and implementing methods corresponding to the rule you wish to process. Each
  7. method accepts the children as an argument. That can be modified using the
  8. ``v_args`` decorator, which allows one to inline the arguments (akin to ``*args``),
  9. or add the tree ``meta`` property as an argument.
  10. See: `visitors.py`_
  11. .. _visitors.py: https://github.com/lark-parser/lark/blob/master/lark/visitors.py
  12. Visitor
  13. -------
  14. Visitors visit each node of the tree, and run the appropriate method on it according to the node's data.
  15. They work bottom-up, starting with the leaves and ending at the root of the tree.
  16. There are two classes that implement the visitor interface:
  17. - ``Visitor``: Visit every node (without recursion)
  18. - ``Visitor_Recursive``: Visit every node using recursion. Slightly faster.
  19. Example:
  20. ::
  21. class IncreaseAllNumbers(Visitor):
  22. def number(self, tree):
  23. assert tree.data == "number"
  24. tree.children[0] += 1
  25. IncreaseAllNumbers().visit(parse_tree)
  26. .. autoclass:: lark.visitors.Visitor
  27. :members: visit, visit_topdown, __default__
  28. .. autoclass:: lark.visitors.Visitor_Recursive
  29. :members: visit, visit_topdown, __default__
  30. Interpreter
  31. -----------
  32. .. autoclass:: lark.visitors.Interpreter
  33. Example:
  34. ::
  35. class IncreaseSomeOfTheNumbers(Interpreter):
  36. def number(self, tree):
  37. tree.children[0] += 1
  38. def skip(self, tree):
  39. # skip this subtree. don't change any number node inside it.
  40. pass
  41. IncreaseSomeOfTheNumbers().visit(parse_tree)
  42. Transformer
  43. -----------
  44. .. autoclass:: lark.visitors.Transformer
  45. :members: transform, __default__, __default_token__, __mul__
  46. Example:
  47. ::
  48. from lark import Tree, Transformer
  49. class EvalExpressions(Transformer):
  50. def expr(self, args):
  51. return eval(args[0])
  52. t = Tree('a', [Tree('expr', ['1+2'])])
  53. print(EvalExpressions().transform( t ))
  54. # Prints: Tree(a, [3])
  55. Example:
  56. ::
  57. class T(Transformer):
  58. INT = int
  59. NUMBER = float
  60. def NAME(self, name):
  61. return lookup_dict.get(name, name)
  62. T(visit_tokens=True).transform(tree)
  63. .. autoclass:: lark.visitors.Transformer_NonRecursive
  64. .. autoclass:: lark.visitors.Transformer_InPlace
  65. .. autoclass:: lark.visitors.Transformer_InPlaceRecursive
  66. v_args
  67. ------
  68. .. autofunction:: lark.visitors.v_args
  69. merge_transformers
  70. ------------------
  71. .. autofunction:: lark.visitors.merge_transformers
  72. Discard
  73. -------
  74. .. autoclass:: lark.visitors.Discard
  75. VisitError
  76. ----------
  77. .. autoclass:: lark.exceptions.VisitError