Skip to content
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

Generate rules from Python AST? #72

Closed
void-main opened this issue Dec 19, 2022 · 2 comments
Closed

Generate rules from Python AST? #72

void-main opened this issue Dec 19, 2022 · 2 comments

Comments

@void-main
Copy link

Thanks for building such a great lib on Python AST! However, writing asserts in rules is pretty tedious, I wonder if it's possible to parse a target python AST, and generate the rules automatically?

Also, another question is, now that the contract is based on a single ast.Node, is it possible to do a partial replacement with multiple statements (spans multiple ast.Nodes)?

For example, how do I write the rule to replace all matches of the following 2 statements:

    boxes[:, 0] = boxes_xywhr[:, 0] - half_w
    boxes[:, 1] = boxes_xywhr[:, 1] - half_h

Do I have to do it in a larger scope node (say ast.FunctionDef)? Or is it possible to get some UD-Chains based on the current ast.Node?

@MementoRC
Copy link
Contributor

@void-main I am just a beginner using this package! The way I do these is by iterating the body of say a FunctionDef, find the match, clone and make the changes, then yield Replace(..., ...) - It seems to work. Note that I look for the minimal change, for example if I just want to change say 1 is your example, I make sure that I get to that Constant() and only change it, this way I think it minimizes the MaybeOverlapping

@isidentical
Copy link
Owner

However, writing asserts in rules is pretty tedious, I wonder if it's possible to parse a target python AST, and generate the rules automatically?

One example I can think of is @lmmx's refactory which uses Refactor as a backend and takes the input search form which can be later compiled down to refactor expression. I can't see why this won't be possible as well, as long as some one is interested in writing it down :)

Also, another question is, now that the contract is based on a single ast.Node, is it possible to do a partial replacement with multiple statements (spans multiple ast.Nodes)?

As @MementoRC pointed, indeed it is a use case which is supported but needs a bit more precision to avoid overlaps. This is called Chained or composite actions in our docs, which includes a few more examples. It is still an experimental feature, so would love to hear your feedback as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants