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/lit.rs b/src/lit.rs
index 81be285..4811485 100644
--- a/src/lit.rs
+++ b/src/lit.rs
@@ -1,32 +1,34 @@
-/// Literal kind.
-///
-/// E.g. `"foo"`, `42`, `12.34` or `bool`
-#[derive(Debug, Clone, Eq, PartialEq, Hash)]
-pub enum Lit {
- /// A string literal (`"foo"`)
- Str(String, StrStyle),
- /// A byte string (`b"foo"`)
- ByteStr(Vec<u8>, StrStyle),
- /// A byte char (`b'f'`)
- Byte(u8),
- /// A character literal (`'a'`)
- Char(char),
- /// An integer literal (`1`)
- Int(u64, IntTy),
- /// A float literal (`1f64` or `1E10f64` or `1.0E10`)
- Float(String, FloatTy),
- /// A boolean literal
- Bool(bool),
+ast_enum! {
+ /// Literal kind.
+ ///
+ /// E.g. `"foo"`, `42`, `12.34` or `bool`
+ pub enum Lit {
+ /// A string literal (`"foo"`)
+ Str(String, StrStyle),
+ /// A byte string (`b"foo"`)
+ ByteStr(Vec<u8>, StrStyle),
+ /// A byte char (`b'f'`)
+ Byte(u8),
+ /// A character literal (`'a'`)
+ Char(char),
+ /// An integer literal (`1`)
+ Int(u64, IntTy),
+ /// A float literal (`1f64` or `1E10f64` or `1.0E10`)
+ Float(String, FloatTy),
+ /// A boolean literal
+ Bool(bool),
+ }
}
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum StrStyle {
- /// A regular string, like `"foo"`
- Cooked,
- /// A raw string, like `r##"foo"##`
- ///
- /// The uint is the number of `#` symbols used
- Raw(usize),
+ast_enum! {
+ pub enum StrStyle {
+ /// A regular string, like `"foo"`
+ Cooked,
+ /// A raw string, like `r##"foo"##`
+ ///
+ /// The uint is the number of `#` symbols used
+ Raw(usize),
+ }
}
impl From<String> for Lit {
@@ -65,26 +67,30 @@
}
}
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum IntTy {
- Isize,
- I8,
- I16,
- I32,
- I64,
- Usize,
- U8,
- U16,
- U32,
- U64,
- Unsuffixed,
+ast_enum! {
+ #[derive(Copy)]
+ pub enum IntTy {
+ Isize,
+ I8,
+ I16,
+ I32,
+ I64,
+ Usize,
+ U8,
+ U16,
+ U32,
+ U64,
+ Unsuffixed,
+ }
}
-#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
-pub enum FloatTy {
- F32,
- F64,
- Unsuffixed,
+ast_enum! {
+ #[derive(Copy)]
+ pub enum FloatTy {
+ F32,
+ F64,
+ Unsuffixed,
+ }
}
macro_rules! impl_from_for_lit {