Rewrite the AST to be a bit more user-friendly
This commit is a relatively large rewrite of the AST that `syn` exposes. The
main change is to expose enums-of-structs rather than
enums-with-huge-tuple-variants. The best example of this is `ItemKind::Fn` which
changed from:
enum ItemKind {
Fn(Box<FnDecl>, Unsafety, Constness, Option<Abi>, Generics, Box<Block>),
...
}
to
enum ItemKind {
Fn(ItemFn),
...
}
struct ItemFn {
decl: Box<FnDecl>,
unsafety: Unsafety,
constness: Constness,
abi: Option<Abi>,
generics: Generics,
block: Box<Block>,
}
This change serves a few purposes:
* It's now much easier to add fields to each variant of the ast, ast struct
fields tend to be "by default ignored" in most contexts.
* It's much easier to document what each field is, as each field can have
dedicated documentation.
* There's now canonicalized names for each field (the name of the field) which
can help match `match` statements more consistent across a codebase.
A downside of this representation is that it can be a little more verbose to
work with in `match` statements and during constructions. Overall though I'd
feel at least that the readability improved significantly despite the extra
words required to do various operations.
Closes #136
diff --git a/src/op.rs b/src/op.rs
index d4b0bc7..cac1741 100644
--- a/src/op.rs
+++ b/src/op.rs
@@ -1,51 +1,55 @@
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum BinOp {
- /// The `+` operator (addition)
- Add,
- /// The `-` operator (subtraction)
- Sub,
- /// The `*` operator (multiplication)
- Mul,
- /// The `/` operator (division)
- Div,
- /// The `%` operator (modulus)
- Rem,
- /// The `&&` operator (logical and)
- And,
- /// The `||` operator (logical or)
- Or,
- /// The `^` operator (bitwise xor)
- BitXor,
- /// The `&` operator (bitwise and)
- BitAnd,
- /// The `|` operator (bitwise or)
- BitOr,
- /// The `<<` operator (shift left)
- Shl,
- /// The `>>` operator (shift right)
- Shr,
- /// The `==` operator (equality)
- Eq,
- /// The `<` operator (less than)
- Lt,
- /// The `<=` operator (less than or equal to)
- Le,
- /// The `!=` operator (not equal to)
- Ne,
- /// The `>=` operator (greater than or equal to)
- Ge,
- /// The `>` operator (greater than)
- Gt,
+ast_enum! {
+ #[derive(Copy)]
+ pub enum BinOp {
+ /// The `+` operator (addition)
+ Add,
+ /// The `-` operator (subtraction)
+ Sub,
+ /// The `*` operator (multiplication)
+ Mul,
+ /// The `/` operator (division)
+ Div,
+ /// The `%` operator (modulus)
+ Rem,
+ /// The `&&` operator (logical and)
+ And,
+ /// The `||` operator (logical or)
+ Or,
+ /// The `^` operator (bitwise xor)
+ BitXor,
+ /// The `&` operator (bitwise and)
+ BitAnd,
+ /// The `|` operator (bitwise or)
+ BitOr,
+ /// The `<<` operator (shift left)
+ Shl,
+ /// The `>>` operator (shift right)
+ Shr,
+ /// The `==` operator (equality)
+ Eq,
+ /// The `<` operator (less than)
+ Lt,
+ /// The `<=` operator (less than or equal to)
+ Le,
+ /// The `!=` operator (not equal to)
+ Ne,
+ /// The `>=` operator (greater than or equal to)
+ Ge,
+ /// The `>` operator (greater than)
+ Gt,
+ }
}
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum UnOp {
- /// The `*` operator for dereferencing
- Deref,
- /// The `!` operator for logical inversion
- Not,
- /// The `-` operator for negation
- Neg,
+ast_enum! {
+ #[derive(Copy)]
+ pub enum UnOp {
+ /// The `*` operator for dereferencing
+ Deref,
+ /// The `!` operator for logical inversion
+ Not,
+ /// The `-` operator for negation
+ Neg,
+ }
}
#[cfg(feature = "parsing")]