Closed anki-code closed 4 years ago
The xonsh parser produces its own AST. Looking at the source code, it seems to use enough Python that its AST is likely an extension of Python's AST, with extra tokens and nodes. You'd need to fork asttokens
and make it aware of these addititions, for it to be able to parse xonsh AST. It's hard to say how much work that would be. It might be easy for someone familiar with xonsh internals, but hard for everyone else :)
Got it! Thanks!
Just to add to what @dsagal said, asttokens has to specially handle many kinds of nodes whose tokens are not correctly inferred by the initial generic algorithm. For example in the traceback it says it was trying to specially handle an attribute node and failed because it couldn't find a dot. Does xonsh have a different concept of Attribute
in the AST? Either way, you would probably need to add special cases for some (probably not all) of the additional nodes found in xonsh.
FWIW xonsh produces a plain-old Python AST with no additional nodes.
I tried to walk the tree:
And got:
Here I see no any fields with source position information. It means there is no data about it or the place where I'm searching is wrong?
Oh, I see, we can just avoid trying to specially handle an attribute node. I'm working on PR.
Here I see no any fields with source position information. It means there is no data about it or the place where I'm searching is wrong?
It's not present in ast.iter_fields(node)
, but the attributes are there. You can find them in the __dict__
. Or you can use ast.dump(node, include_attributes=True, indent=2)
, but you will need Python 3.9 for indent=2
.
Thank you for you help and fast responses! We discussed the questions in the PR and I have info to continue thinking.
Hello! Thank you for asttokens!
I'm wondering can I use asttokens with xonsh shell syntax tree that is a superset of Python?
For example I replaced tree to xonsh and it works with pure python:
Result:
The token positions - it's what I need.
But when I set:
I've got traceback:
Could you please help or advice can I achieve token positions using asttokens and xonsh shell ast parser? If no could you point out to another way?
Thanks!