propagate_positions doesn't work with all kinds of `tree_class`
See original GitHub issueConsider this snippet:
from lark import Lark, Meta
# just for the sake of demonstration
class MyTree:
def __init__(self, data, children):
self.data = data
self.children = children
self.meta = Meta()
parser = Lark.open(
grammar_filename="foobar.lark",
propagate_positions=True,
tree_class=MyTree
)
Due to the isinstance(res, Tree)
in implementation of PropagatePositions.__call__
:
class PropagatePositions:
...
def __call__(self, children):
res = self.node_builder(children)
if isinstance(res, Tree):
...
# algorithm impl
...
return res
when MyTree
is not derived from Tree
(and it might be so for some reason) PropagatePositions
won’t do anything useful. Currently the available workarounds I’m aware of are:
- do it yourself with
transformer
supplied to parser but only when grammar has no inlined terminals - monkey patching
PropagatePositions.__call__
with alternative implementation aware ofMyTree
I’m not sure what would be the best way forward since PropagatePositions in some cases would have to be aware of tree_class
layout. For instance (general idea since I’m not allowed to provide concrete example):
class MyTree:
# let's say you don't need all the details of Meta and you'd like to have all the data flat in MyTree
def __init__(self, data, children, start=None, end=None)
self.data = data
self.children = children
self.start = start
self.end = end
class MyPropagatePositions:
...
def __call__(self, children):
...
if isinstance(res, MyTree):
...
# just start position part
if isinstance(child, MyTree):
res.start = child.start
elif isinstance(child, Token):
res.start = child.pos_in_stream
This in turn would require yet another option to Lark
in case user would like to roll out their own implementation. The alternative solution would be requiring that MyTree
derives from lark.Tree
but it might be too limiting.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Everything You Need to Know About Growing Trees From Seeds
Generally speaking, there are two ways to grow tree seeds: the natural way, which often includes sowing the seeds outside in autumn, or...
Read more >Efficient and easy segment trees - Codeforces
push propagates changes from all the parents of a given node down the tree starting from the root. This parents are exactly the...
Read more >Bent into shape: The rules of tree form - Knowable Magazine
A common test for whether a plant's gravity-perception machinery is working is to lay the plant on its side. If it knows up...
Read more >How can I implement a tree in Python? - Stack Overflow
There isn't any builtin data structure for generic trees in Python, but it's easily implemented with classes. class Tree(object): "Generic tree node.
Read more >Botanist vs. Horticulturist: What's the Difference Between Them?
What Kinds of Jobs Do Botanists Have? Plant Scientist Studying Tree. Botanists can have a wide range of different career options.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The parser expects certain behaviors from the tree class. PropagatePositions expects a meta attribute.
Giving the users complete freedom isn’t the best idea, probably.
I’ll take your comment about mentioning it in the documentation. I’ll also add a subclass test with a proper error, to help prevent future confusion.
So what is your suggestion? I am relatively sure that when you are not using
PropagatePositions
, a tree class that does not inherit fromTree
works.