-
Notifications
You must be signed in to change notification settings - Fork 30
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
A macro to generating AST with python grammar #55
Comments
I'm struggling to understand what's happening in
even with the example. I'm also vary of macros because most Rust tooling breaks down (formatting, autocompletion) and can be difficult to understand if you haven't used the macro before. Have you considered alternative mutation APIs? E.g. Rome has two APIs:
This allows, in combination, to write |
It turns a python snippet to python ast with given values. It has very different user experience to factories. Starting from simpler example &python_ast! {
"def new_function(a, b, c): pass"
} will be parsed to
The factory will be somewhere between it, but it will be more close to the latter. Simple value example let new_function_name = Identifier::new("overriding_name");
&python_ast! {
Vars { new_function_name },
"def new_function_name(a, b, c): pass"
} will turned into
By looking in every node and checking its identifier name More complex one, similar to the original example. let args: Vec<_> = ["a", "b", "c"].iter().map(|name| Identifier::new(name));
let values = HashMap::new();
values.insert("a", 10);
values.insert("b", 20);
values.insert("c", 30);
&python_ast! {
Vars { args, values },
r#"
def new_function(*args):
return { **values }
#"
} will be originally parsed to:
and then folded to
More with comprehension let args: Vec<_> = ["a", "b", "c"].iter().map(str::to_owned).collect();
&python_ast! {
Vars { args },
r#"
def new_function(*args):
return [f"prefixed_{arg}" for arg in args]
#"
} originally turns into
and then folded to
|
Unlike other macros, the linked One good thing about it is |
Inspired by https://docs.rs/pmutil/latest/pmutil/macro.smart_quote.html
e.g.
can be rewritten to:
The grammar will be limited to legal python codes to leverage python parser.
The text was updated successfully, but these errors were encountered: