Browse Source

Docs: Add merge_transformer; fix docstrings for sphinx

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.12.0
Erez Sh 3 years ago
parent
commit
d433e30659
4 changed files with 23 additions and 13 deletions
  1. +6
    -1
      docs/visitors.rst
  2. +2
    -0
      examples/composition/eval_csv.py
  3. +2
    -0
      examples/composition/eval_json.py
  4. +13
    -12
      lark/visitors.py

+ 6
- 1
docs/visitors.rst View File

@@ -103,12 +103,17 @@ v_args


.. autofunction:: lark.visitors.v_args .. autofunction:: lark.visitors.v_args


merge_transformers
------------------

.. autofunction:: lark.visitors.merge_transformers

Discard Discard
------- -------


.. autoclass:: lark.visitors.Discard .. autoclass:: lark.visitors.Discard


VisitError VisitError
-------
----------


.. autoclass:: lark.exceptions.VisitError .. autoclass:: lark.exceptions.VisitError

+ 2
- 0
examples/composition/eval_csv.py View File

@@ -1,3 +1,5 @@
"Transformer for evaluating csv.lark"
from lark import Transformer from lark import Transformer
class CsvTreeToPandasDict(Transformer): class CsvTreeToPandasDict(Transformer):


+ 2
- 0
examples/composition/eval_json.py View File

@@ -1,3 +1,5 @@
"Transformer for evaluating json.lark"
from lark import Transformer, v_args from lark import Transformer, v_args
class JsonTreeToJson(Transformer): class JsonTreeToJson(Transformer):


+ 13
- 12
lark/visitors.py View File

@@ -159,7 +159,7 @@ def merge_transformers(base_transformer=None, **transformers_to_merge):
thereby creating some of their rules in a 'namespace'. (i.e with a consitent name prefix) thereby creating some of their rules in a 'namespace'. (i.e with a consitent name prefix)
In this case, the key for the transformer should match the name of the imported grammar. In this case, the key for the transformer should match the name of the imported grammar.


Paramaters:
Parameters:
base_transformer (Transformer, optional): The transformer that all other transformers will be added to. base_transformer (Transformer, optional): The transformer that all other transformers will be added to.
**transformers_to_merge: Keyword arguments, in the form of ``name_prefix = transformer``. **transformers_to_merge: Keyword arguments, in the form of ``name_prefix = transformer``.


@@ -167,21 +167,22 @@ def merge_transformers(base_transformer=None, **transformers_to_merge):
AttributeError: In case of a name collision in the merged methods AttributeError: In case of a name collision in the merged methods


Example: Example:
```python
class TBase(Transformer):
def start(self, children):
return children[0] + 'bar'
::

class TBase(Transformer):
def start(self, children):
return children[0] + 'bar'

class TImportedGrammar(Transformer):
def foo(self, children):
return "foo"


class TImportedGrammar(Transformer):
def foo(self, children):
return "foo"
composed_transformer = merge_transformers(TBase(), imported=TImportedGrammar())


composed_transformer = merge_transformers(TBase(), imported=TImportedGrammar())
t = Tree('start', [ Tree('imported__foo', []) ])


t = Tree('start', [ Tree('imported__foo', []) ])
assert composed_transformer.transform(t) == 'foobar'


assert composed_transformer.transform(t) == 'foobar'
```
""" """
if base_transformer is None: if base_transformer is None:
base_transformer = Transformer() base_transformer = Transformer()


Loading…
Cancel
Save