Browse Source

Added docs (Spurred by issue #510)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.8.3
Erez Sh 5 years ago
parent
commit
3b901a06b0
2 changed files with 22 additions and 0 deletions
  1. +16
    -0
      docs/recipes.md
  2. +6
    -0
      lark/exceptions.py

+ 16
- 0
docs/recipes.md View File

@@ -129,3 +129,19 @@ This prints out:
y d

While convenient, this should be used carefully, as highly ambiguous trees will soon create an exponential explosion of such unambiguous derivations.


## Keeping track of parents when visiting

The following visitor assigns a `parent` attribute for every node in the tree.

If your tree nodes aren't unique (if there is a shared Tree instance), the assert will fail.

```python
class Parent(Visitor):
def visit(self, tree):
for subtree in tree.children:
if isinstance(subtree, Tree):
assert not hasattr(subtree, 'parent')
subtree.parent = tree
```

+ 6
- 0
lark/exceptions.py View File

@@ -97,6 +97,12 @@ class UnexpectedToken(ParseError, UnexpectedInput):
super(UnexpectedToken, self).__init__(message)

class VisitError(LarkError):
"""VisitError is raised when visitors are interrupted by an exception

It provides the following attributes for inspection:
- obj: the tree node or token it was processing when the exception was raised
- orig_exc: the exception that cause it to fail
"""
def __init__(self, rule, obj, orig_exc):
self.obj = obj
self.orig_exc = orig_exc


Loading…
Cancel
Save