Selaa lähdekoodia

transform token doc

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.8.0
Hao Wu 4 vuotta sitten
vanhempi
commit
10a09ebba8
1 muutettua tiedostoa jossa 13 lisäystä ja 0 poistoa
  1. +13
    -0
      docs/transfromer_and_vistor.md

+ 13
- 0
docs/transfromer_and_vistor.md Näytä tiedosto

@@ -40,6 +40,7 @@ Because nodes are reduced from leaf to root, at any point the callbacks may assu

Transformers can be chained into a new transformer by using multiplication.


**Example:**
```python
from lark import Tree, Transformer
@@ -54,6 +55,18 @@ print(EvalExpressions().transform( t ))
# Prints: Tree(a, [3])
```

By default, transformer works only on rules, `visit_tokens=True` will make transfomer process tokens. This is handy in parsing simple token, such as turn `INT` to `int`, `NUMBER` to `float`. etc.

```python
class T(Transformer):
INT = int # same with def INT(self, tok): int(tok)
NUMBER = float # same with def INT(self, tok): int(tok)
def NAME(self, name):
return lookup_dict.get(name, name)

T(visit_tokens=True).transform(tree)
```

Here are the classes that implement the transformer interface:



Ladataan…
Peruuta
Tallenna