-
-
Notifications
You must be signed in to change notification settings - Fork 414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect start_pos / end_pos in the tree #1406
Comments
@erezsh We really need to change it back so that |
Hi @MegaIng |
@MegaIng Yeah, makes sense. The tokens of the parsed grammar aren't relevant to the output tree. |
Not sure what do you mean by that, I'm relatively new to Lark. But trying to access start_pos / end_pos of tokens is my attempt to bring comments into the tree that were ignored on parsing stage. I've done some research and it looks like there isn't a way to easily do that. I'm parsing a Pascal-like language and must use LALR parser because of its speed: my usual input is roughly 350 mb of source code and Earley works unacceptably slow. Tried to rewrite my grammar to not ignore the code but that's probably not possible with LALR. So in my case having tokens with information on where they were found in the input is the last hope of bringing in the comments. |
@munching We aren't talking about your issue directly. If lark behaved correctly, you would have gotten an For collecting comments we normally suggest terminal callbacks, but that doesn't easily bring it into the tree, that is a bit of extra work. |
@munching You might find this useful: https://lark-parser.readthedocs.io/en/latest/recipes.html#collect-all-comments-with-lexer-callbacks |
@erezsh @MegaIng |
There is some code that I wrote once that did something like it. Maybe I should clean it up and add it to Lark, as a utility function. I don't know if it will be helpful for you, but this is the code: def assign_comments(tree, comments):
nodes_by_line = classify(tree.iter_subtrees(), lambda t: getattr(t.meta, 'line', None))
nodes_by_line.pop(None,None)
rightmost_nodes = {line: max(nodes, key=lambda n: n.meta.column) for line, nodes in nodes_by_line.items()}
leftmost_nodes = {line: min(nodes, key=lambda n: (n.meta.column, -(n.meta.end_pos - n.meta.start_pos))) for line, nodes in nodes_by_line.items()}
for c in comments:
if c.line == c.end_line:
n = rightmost_nodes[c.end_line]
assert not hasattr(n.meta, 'inline_comment')
n.meta.inline_comment = c
else:
if c.end_line not in leftmost_nodes:
# Probably past the end of the file
# XXX verify this is the case
continue
n = leftmost_nodes[c.end_line]
header_comments = getattr(n.meta, 'header_comments', [])
n.meta.header_comments = header_comments + [c] P.S. |
Hi,
I'd like to report a possible bug with token's attributes start_pos, end_pos and line.
Below is an example to reproduce.
Grammar:
Input:
myvar1 = myvar2
Python code:
I get the following output which is obviously incorrect:
The input has only one line and 15 symbols total. Does this look like a bug or am I misusing attributes?
Thank you
The text was updated successfully, but these errors were encountered: