pest-parser / ast

Apache License 2.0
80 stars 15 forks source link

#[pest_ast(rule(Rule::type)] fails since `type` is a reserved rust keyword. #31

Open oxcrow opened 1 month ago

oxcrow commented 1 month ago

Hi everyone,

pest and pest-derived seem to accept grammar that has rust's reserved keywords, such as type, struct, mod etc.

However, pest-ast fails.

#[derive(Debug, Clone, FromPest)]
#[pest_ast(rule(Rule::type))] // <<< Causes issue, and fails to compile, since type is a reserved keyword
pub enum Type {
    Int(Int),
    Id(Id),
}

Can this be fixed?

Thanks!

oxcrow commented 1 month ago

I might have fixed it.

This is a rust specific issue, and it can be resolved using r#type. i.e

#[derive(Debug, Clone, FromPest)]
#[pest_ast(rule(Rule::r#type))] // <<< using r#type tells rust to not treat this as a keyword
pub enum Type {
    Int(Int),
    Id(Id),
}

This compiles correctly, although I need to do more tests to verify if everything is working correctly.

I don't think pest, or pest-ast created this issue, but documenting this issue maybe helpful for future users.

And it might be possible for pest maintainers to pattern match on the procedural macro attributes, and handle these cases internally by using r#ident.

It is little bit more extra work for pest maintainers, but might provide a more comfortable experience for future users.

I need to do more tests, to see if this works correctly.

Thanks.