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.

62 lines
1.6 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 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. .. autoclass:: lark.visitors.Visitor_Recursive
  28. Interpreter
  29. -----------
  30. .. autoclass:: lark.visitors.Interpreter
  31. Transformer
  32. -----------
  33. .. autoclass:: lark.visitors.Transformer
  34. :members: __default__, __default_token__
  35. v_args
  36. ------
  37. .. autofunction:: lark.visitors.v_args
  38. Discard
  39. -------
  40. .. autoclass:: lark.visitors.Discard