Browse Source

document lark.Visitor

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Sasank Chilamkurthy 4 years ago
parent
commit
168e6db2d1
2 changed files with 34 additions and 3 deletions
  1. +10
    -1
      docs/classes.rst
  2. +24
    -2
      lark/visitors.py

+ 10
- 1
docs/classes.rst View File

@@ -22,4 +22,13 @@ Tree
Token
-----

.. autoclass:: lark.Token
.. autoclass:: lark.Token

Visitor
-------

.. autoclass:: lark.visitors.VisitorBase

.. autoclass:: lark.Visitor

.. autoclass:: lark.visitors.Visitor_Recursive

+ 24
- 2
lark/visitors.py View File

@@ -199,6 +199,27 @@ class Transformer_InPlaceRecursive(Transformer):
# Visitors

class VisitorBase:
"""Visitors visit each node of the tree
Run the appropriate method on it according to the node’s data.
They work bottom-up, starting with the leaves and ending at the root
of the tree.

There are two classes that implement the visitor interface:

- ``Visitor``: Visit every node (without recursion)
- ``Visitor_Recursive``: Visit every node using recursion. Slightly faster.

Example:
::

class IncreaseAllNumbers(Visitor):
def number(self, tree):
assert tree.data == "number"
tree.children[0] += 1

IncreaseAllNumbers().visit(parse_tree)
"""
def _call_userfunc(self, tree):
return getattr(self, tree.data, self.__default__)(tree)

@@ -211,7 +232,7 @@ class VisitorBase:


class Visitor(VisitorBase):
"""Bottom-up visitor, non-recursive
"""Bottom-up visitor, non-recursive.

Visits the tree, starting with the leaves and finally the root (bottom-up)
Calls its methods (provided by user via inheritance) according to tree.data
@@ -227,8 +248,9 @@ class Visitor(VisitorBase):
self._call_userfunc(subtree)
return tree


class Visitor_Recursive(VisitorBase):
"""Bottom-up visitor, recursive
"""Bottom-up visitor, recursive.

Visits the tree, starting with the leaves and finally the root (bottom-up)
Calls its methods (provided by user via inheritance) according to tree.data


Loading…
Cancel
Save