Browse Source

Fixed documentation bug: Issue #625

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.10.0
Erez Sh 4 years ago
parent
commit
69adc221c5
2 changed files with 24 additions and 1 deletions
  1. +23
    -0
      docs/visitors.md
  2. +1
    -1
      lark/visitors.py

+ 23
- 0
docs/visitors.md View File

@@ -28,6 +28,29 @@ There are two classes that implement the visitor interface:


* Visitor_Recursive - Visit every node using recursion. Slightly faster. * Visitor_Recursive - Visit every node using recursion. Slightly faster.


### Interpreter

The interpreter walks the tree starting at the root (top-down).

For each node, it calls the method corresponding with its `data` attribute.

Unlike Transformer and Visitor, the Interpreter doesn't automatically visit its sub-branches.
The user has to explicitly call `visit`, `visit_children`, or use the `@visit_children_decor`.
This allows the user to implement branching and loops.

**Example:**
```python
class IncreaseSomeOfTheNumbers(Interpreter):
def number(self, tree):
tree.children[0] += 1

def skip(self, tree):
# skip this subtree. don't change any number node inside it.
pass

IncreaseSomeOfTheNumbers().visit(parse_tree)
```

### Transformers ### Transformers


Transformers visit each node of the tree, and run the appropriate method on it according to the node's data. Transformers visit each node of the tree, and run the appropriate method on it according to the node's data.


+ 1
- 1
lark/visitors.py View File

@@ -267,7 +267,7 @@ class Interpreter(_Decoratable):
Calls its methods (provided by user via inheritance) according to tree.data Calls its methods (provided by user via inheritance) according to tree.data


Unlike Transformer and Visitor, the Interpreter doesn't automatically visit its sub-branches. Unlike Transformer and Visitor, the Interpreter doesn't automatically visit its sub-branches.
The user has to explicitly call visit_children, or use the @visit_children_decor
The user has to explicitly call visit, visit_children, or use the @visit_children_decor
""" """


def visit(self, tree): def visit(self, tree):


Loading…
Cancel
Save