question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

propagate_positions doesn't work with all kinds of `tree_class`

See original GitHub issue

Consider 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 of MyTree

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:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Aug 4, 2021

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.

0reactions
MegaIngcommented, Aug 4, 2021

So what is your suggestion? I am relatively sure that when you are not using PropagatePositions, a tree class that does not inherit from Tree works.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found