lark-parser / lark

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.
MIT License
4.75k stars 401 forks source link

propagate_positions isn't well documented #1293

Closed ningit closed 1 year ago

ningit commented 1 year ago

Describe the bug

The keyword argument propagate_positions of Lark constructor is not obeyed. The attributes line, column, end_line, and end_column are not available in the Tree objects returned by Lark.parse.

To Reproduce

In the following code, the last two lines produce an AttributeError: 'Tree' object has no attribute 'line', while they should return 1.

import lark

grammar = '''
    %import common.WS
    %ignore WS

    %import common.INT -> NUMBER

    start: expr

    expr: NUMBER
        | NUMBER "+" NUMBER
'''

parser = lark.Lark(grammar, parser='lalr', propagate_positions=True)
tree = parser.parse('1 + 2')

print(tree.children[0].children[0].line)
print(tree.children[0].line)
print(tree.line)
erezsh commented 1 year ago

It's tree.meta

On Wed, 28 Jun 2023 at 21:31, ningit @.***> wrote:

Describe the bug

The keyword argument propagate_positions of Lark https://lark-parser.readthedocs.io/en/latest/classes.html#lark.Lark constructor is not obeyed. The attributes line, column, end_line, and end_column are not available in the Tree objects returned by Lark.parse.

To Reproduce

In the following code, the last two lines produce an AttributeError: 'Tree' object has no attribute 'line', while they should return 1.

import lark grammar = ''' %import common.WS %ignore WS %import common.INT -> NUMBER start: expr expr: NUMBER | NUMBER "+" NUMBER''' parser = lark.Lark(grammar, parser='lalr', propagate_positions=True)tree = parser.parse('1 + 2') print(tree.children[0].children[0].line)print(tree.children[0].line)print(tree.line)

— Reply to this email directly, view it on GitHub https://github.com/lark-parser/lark/issues/1293, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFSSSD4QKACQRQG45TXHXTXNR2ARANCNFSM6AAAAAAZXQB4NU . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ningit commented 1 year ago

Many thanks and sorry. Now I see this is mentioned in the documentation for the constructor of the Tree class.

Anyhow, making explicit that these attributes are available through meta in the documentation of the propagate_positions option itself may avoid confusion and help users.

erezsh commented 1 year ago

@ningit I added a PR https://github.com/lark-parser/lark/pull/1294

ningit commented 1 year ago

Great, it is now clearer. I think the PR solves the issue.

erezsh commented 1 year ago

Great! Added even more explanations too.