-
Notifications
You must be signed in to change notification settings - Fork 16
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
Generating the ast #8
Comments
I think it's best to structure this in a way that it's expected that the user take the output and commit it to their repository to be maintained by hand in the future. I don't think the goal should ever be to make a good, semantic AST, but rather to make the "obvious" translation and provide a quick starting point for projects that have designed their grammar already. |
What are your thoughts on translating |
This should already work manually, and is definitely what I'd consider the "canonical" translation that such a tool should generate. They're powered solely by provided implementations in from-pest: |
I have more questions :)
becomes struct one_char {
content: char,
} Am I right about this?
would turn into a struct number {
digit: Vec<char>,
} I probably intended |
Generally, I think that a sensible default for atoms would be to store a The problem with // number = @{ ASCII_DIGIT* }
struct number(u16); is that the rule matches way too big numbers, so converting it either means panicking or manually plumbing a So, this is a lot less "this is the obvious way to handle it" than the other proposals for the generated AST, but a potential sketch:
Here's a potential by-hand translation of a small number of rules:Grammar:
AST: struct a<'pest>(
#[pest_ast(outer)] Span<'pest>,
);
struct b<'pest>(
#[pest_ast(outer)] Span<'pest>,
);
struct c<'pest>(
#[pest_ast(outer)] Span<'pest>,
);
struct number<'pest>(
#[pest_ast(outer)] Span<'pest>,
);
struct any<'pest>(
#[pest_ast(outer)] Span<'pest>,
);
struct seq<'pest>(
#[pest_ast(outer)] Span<'pest>,
a<'pest>,
b<'pest>,
c<'pest>,
);
enum choice<'pest>{
struct _1(a<'pest>),
struct _2(b<'pest>),
struct _3(c<'pest>),
}
struct compound_seq<'pest>(
#[pest_ast(outer)] Span<'pest>,
a<'pest>,
enum _2 {
struct _1(b<'pest>),
struct _2(c<'pest>),
},
);
enum compound_choice<'pest>{
struct _1(
#[pest_ast(outer)] Span<'pest>,
a<'pest>,
b<'pest>,
),
struct _2(
#[pest_ast(outer)] Span<'pest>,
b<'pest>,
c<'pest>,
),
}
struct assign<'pest>(
#[pest_ast(outer)] Span<'pest>,
enum _1 {
struct _1(a<'pest>),
struct _2(b<'pest>),
struct _3(c<'pest>),
},
number<'pest>,
);
struct assigns<'pest>(
#[pest_ast(outer)] Span<'pest>,
Vec<struct _1(assign<'pest>)>,
assign<'pest>,
); |
I think having the AST take any lifetime is a flawed approach, since it adds a lot of complication in handling it. The first version of pest was designed with an owned |
Hey folks, I started https://github.com/killercup/pest-ast-generator for fun a few days ago and just now saw this issue. The approach I took is very simple -- the goal was to get rid of a bunch of struct I needed to write, not to support every edge case. |
This is a fun one.
Even with
#[derive(FromPest)]
, the initial creation of the ast structures is fairly rote.It would be really cool if we could take a
.pest
file and create a working (though not necessarily ideal) module of ast structures.Basic shapes
becomes
becomes
becomes
Vec<a>
becomes
Vec<a>
becomes
Option<a>
Please, ping me on Gitter or on Discord if you're interested in attacking this. It'll be fun, but somewhat involved.
The text was updated successfully, but these errors were encountered: