-
Notifications
You must be signed in to change notification settings - Fork 109
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
Document the refactoring API #165
Comments
True. We should do that. Note that this method is not going away in the future, the documentation is just not here yet. Happy to have others chime in and write it. |
If I end up trying to use parso for the source transformation stuff that I'm thinking about I can look at writing the docs. I assume Jedi has the best examples of its use? |
Probably. :) |
Looking at the code it seems the |
Understandable! What do you mean by |
To give context, this is for my syntactic sugar blog posts that I have been writing. One of the things I'm going to tackle is rewriting for a in b:
c becomes: _iterator = iter(b)
_done = False
while not _done:
try:
a = next(_iterator)
except StopIteration:
_done = True
else:
c As you can see, the single I'm at the point where no one seems to support this and I'm probably just going to have an API where I construct the nodes manually and it is up to people to manually patch their parse trees (it at least makes the coding easier for me 😁 ). |
If the parso API is not enough for you, I would be really interested what an API would be enough for something like this. I mean parso's API can do this. It's just a bit annoying. You can essentially do a (pseudocode):
This should be perfectly fine. Of course then there's the details if you want to get it right. The suite needs to be indented and a for loop can also have an |
It looks like the thing that wasn't obvious to me was I was going to have to blank out all the parts of the old statement, construct a new statement as pure text, and then let parso figure out that one of the new things inserted is a slew of new statements. You can see what I'm doing now at https://github.com/brettcannon/desugar/blob/master/desugar/syntax.py. I could probably switch to parso since I ended up just returning a list of nodes to make things work out and I could use |
@brettcannon if you actually don't care about comments etc, you could possibly use |
@isidentical as one of the co-authors of the ast module, I almost did that. 😁 But I was trying to avoid being quite that destructive in the transformation in case people actually wanted to use the results in code and thus didn't want to lose comments. |
It worked ~decent enough (not perfectly) for me, only with an extra collection of comments from the source code (iterating all tokens, fetching COMMENTs within the given range of line numbers) + specialized replace over the only changed lines in case of you are interested. |
I'm starting to look for a library to do source transformations and I think parso could handle it via its
parso.Grammar.refactor()
method, but there's not docs on how to use it.The text was updated successfully, but these errors were encountered: