David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 1 | //! This crate automatically generates the definition of the `Visitor`, |
| 2 | //! `VisitorMut`, and `Folder` traits in `syn` based on the `syn` source. It |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 3 | //! discovers structs and enums declared with the `ast_*` macros and generates |
| 4 | //! the functions for those types. |
| 5 | //! |
| 6 | //! It makes a few assumptions about the target crate: |
| 7 | //! 1. All structs which are discovered must be re-exported in the root of the |
| 8 | //! crate, even if they were declared in a submodule. |
| 9 | //! 2. This code cannot discover submodules which are located in subdirectories |
| 10 | //! - only submodules located in the same directory. |
| 11 | //! 3. The path to `syn` is hardcoded. |
| 12 | |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 13 | #![cfg_attr(feature = "cargo-clippy", allow(redundant_closure))] |
| 14 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 15 | #[macro_use] |
| 16 | extern crate failure; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 17 | extern crate inflections; |
David Tolnay | 2e0dba1 | 2017-12-27 01:54:40 -0500 | [diff] [blame] | 18 | extern crate proc_macro2; |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 19 | #[macro_use] |
| 20 | extern crate quote; |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 21 | #[macro_use] |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 22 | extern crate syn; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 23 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 24 | use quote::{ToTokens, Tokens}; |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 25 | use syn::{Attribute, Body, BodyStruct, DeriveInput, Ident, Item}; |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 26 | use failure::{err_msg, Error}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 27 | |
David Tolnay | 8ed806a | 2017-12-26 01:28:28 -0500 | [diff] [blame] | 28 | use std::io::{Read, Write}; |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 29 | use std::fmt::{self, Debug}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 30 | use std::fs::File; |
| 31 | use std::path::Path; |
| 32 | use std::collections::BTreeMap; |
| 33 | |
| 34 | const SYN_CRATE_ROOT: &str = "../src/lib.rs"; |
| 35 | |
| 36 | const FOLD_SRC: &str = "../src/gen/fold.rs"; |
| 37 | const VISIT_SRC: &str = "../src/gen/visit.rs"; |
| 38 | const VISIT_MUT_SRC: &str = "../src/gen/visit_mut.rs"; |
| 39 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 40 | const IGNORED_MODS: &[&str] = &["fold", "visit", "visit_mut"]; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 41 | |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 42 | const EXTRA_TYPES: &[&str] = &["Ident", "Lifetime", "Lit"]; |
| 43 | |
| 44 | const TERMINAL_TYPES: &[&str] = &["Span"]; |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 45 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 46 | fn path_eq(a: &syn::Path, b: &syn::Path) -> bool { |
| 47 | if a.global() != b.global() || a.segments.len() != b.segments.len() { |
| 48 | return false; |
| 49 | } |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 50 | a.segments |
| 51 | .iter() |
| 52 | .zip(b.segments.iter()) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 53 | .all(|(a, b)| a.item().ident.as_ref() == b.item().ident.as_ref()) |
| 54 | } |
| 55 | |
| 56 | fn get_features(attrs: &[Attribute], mut features: Tokens) -> Tokens { |
| 57 | for attr in attrs { |
| 58 | if path_eq(&attr.path, &"cfg".into()) { |
| 59 | attr.to_tokens(&mut features); |
| 60 | } |
| 61 | } |
| 62 | features |
| 63 | } |
| 64 | |
| 65 | #[derive(Clone)] |
| 66 | pub struct AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 67 | ast: DeriveInput, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 68 | features: Tokens, |
| 69 | // True if this is an ast_enum_of_structs! item with a #full annotation. |
| 70 | eos_full: bool, |
| 71 | } |
| 72 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 73 | impl Debug for AstItem { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 74 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 75 | f.debug_struct("AstItem") |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 76 | .field("ast", &self.ast) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 77 | .field("features", &self.features.to_string()) |
| 78 | .finish() |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // NOTE: BTreeMap is used here instead of HashMap to have deterministic output. |
David Tolnay | c47f842 | 2017-12-28 17:30:46 -0500 | [diff] [blame] | 83 | type Lookup = BTreeMap<Ident, AstItem>; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 84 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 85 | fn load_file<P: AsRef<Path>>(name: P, features: &Tokens, lookup: &mut Lookup) -> Result<(), Error> { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 86 | let name = name.as_ref(); |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 87 | let parent = name.parent().ok_or_else(|| err_msg("no parent path"))?; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 88 | |
| 89 | let mut f = File::open(name)?; |
| 90 | let mut src = String::new(); |
| 91 | f.read_to_string(&mut src)?; |
| 92 | |
| 93 | // Parse the file |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 94 | let file = |
| 95 | syn::parse_file(&src).map_err(|_| format_err!("failed to parse {}", name.display()))?; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 96 | |
| 97 | // Collect all of the interesting AstItems declared in this file or submodules. |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 98 | 'items: for item in file.items { |
| 99 | match item { |
| 100 | Item::Mod(item) => { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 101 | // Don't inspect inline modules. |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 102 | if item.content.is_some() { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 103 | continue; |
| 104 | } |
| 105 | |
| 106 | // We don't want to try to load the generated rust files and |
| 107 | // parse them, so we ignore them here. |
| 108 | for name in IGNORED_MODS { |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 109 | if item.ident.as_ref() == *name { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 110 | continue 'items; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Lookup any #[cfg()] attributes on the module and add them to |
| 115 | // the feature set. |
| 116 | let features = get_features(&item.attrs, features.clone()); |
| 117 | |
| 118 | // Look up the submodule file, and recursively parse it. |
| 119 | // XXX: Only handles same-directory .rs file submodules. |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 120 | let path = parent.join(&format!("{}.rs", item.ident.as_ref())); |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 121 | load_file(path, &features, lookup)?; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 122 | } |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 123 | Item::Macro(item) => { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 124 | // Lookip any #[cfg()] attributes directly on the macro |
| 125 | // invocation, and add them to the feature set. |
| 126 | let features = get_features(&item.attrs, features.clone()); |
| 127 | |
| 128 | // Try to parse the AstItem declaration out of the item. |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 129 | let found = if path_eq(&item.mac.path, &"ast_struct".into()) { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 130 | syn::parse_tokens::<parsing::AstStruct>(item.mac.tts.clone().into_tokens()) |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 131 | .map_err(|_| err_msg("failed to parse ast_struct"))? |
| 132 | .0 |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 133 | } else if path_eq(&item.mac.path, &"ast_enum".into()) { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 134 | syn::parse_tokens::<parsing::AstEnum>(item.mac.tts.clone().into_tokens()) |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 135 | .map_err(|_| err_msg("failed to parse ast_enum"))? |
| 136 | .0 |
David Tolnay | c6b55bc | 2017-11-09 22:48:38 -0800 | [diff] [blame] | 137 | } else if path_eq(&item.mac.path, &"ast_enum_of_structs".into()) { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 138 | syn::parse_tokens::<parsing::AstEnumOfStructs>( |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 139 | item.mac.tts.clone().into_tokens(), |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 140 | ).map_err(|_| err_msg("failed to parse ast_enum_of_structs"))? |
| 141 | .0 |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 142 | } else { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 143 | continue; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | // Record our features on the parsed AstItems. |
| 147 | for mut item in found { |
| 148 | features.to_tokens(&mut item.features); |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 149 | lookup.insert(item.ast.ident, item); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 150 | } |
| 151 | } |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 152 | Item::Struct(item) => { |
| 153 | let ident = item.ident; |
| 154 | if EXTRA_TYPES.contains(&ident.as_ref()) { |
| 155 | lookup.insert(ident, AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 156 | ast: DeriveInput { |
David Tolnay | 4ba63a0 | 2017-12-28 15:53:05 -0500 | [diff] [blame] | 157 | ident: ident, |
| 158 | vis: item.vis, |
| 159 | attrs: item.attrs, |
| 160 | generics: item.generics, |
| 161 | body: Body::Struct(BodyStruct { |
| 162 | data: item.data, |
| 163 | struct_token: item.struct_token, |
| 164 | semi_token: item.semi_token, |
| 165 | }), |
| 166 | }, |
| 167 | features: features.clone(), |
| 168 | eos_full: false, |
| 169 | }); |
| 170 | } |
| 171 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 172 | _ => {} |
| 173 | } |
| 174 | } |
| 175 | Ok(()) |
| 176 | } |
| 177 | |
| 178 | mod parsing { |
| 179 | use super::AstItem; |
| 180 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 181 | use syn::synom::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 182 | use syn::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 183 | use quote::Tokens; |
David Tolnay | 2e0dba1 | 2017-12-27 01:54:40 -0500 | [diff] [blame] | 184 | use proc_macro2::TokenStream; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 185 | |
| 186 | // Parses #full - returns #[cfg(feature = "full")] if it is present, and |
| 187 | // nothing otherwise. |
| 188 | named!(full -> (Tokens, bool), map!(option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 189 | punct!(#) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 190 | id: syn!(Ident) >> |
David Tolnay | 28c5a46 | 2017-12-27 01:59:30 -0500 | [diff] [blame] | 191 | cond_reduce!(id == "full", epsilon!()) >> |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 192 | () |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 193 | )), |s| if s.is_some() { |
| 194 | (quote!(#[cfg(feature = "full")]), true) |
| 195 | } else { |
| 196 | (quote!(), false) |
| 197 | })); |
| 198 | |
David Tolnay | 28c5a46 | 2017-12-27 01:59:30 -0500 | [diff] [blame] | 199 | named!(manual_extra_traits -> (), do_parse!( |
| 200 | punct!(#) >> |
| 201 | id: syn!(Ident) >> |
| 202 | cond_reduce!(id == "manual_extra_traits", epsilon!()) >> |
| 203 | () |
| 204 | )); |
| 205 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 206 | // Parses a simple AstStruct without the `pub struct` prefix. |
| 207 | named!(ast_struct_inner -> AstItem, do_parse!( |
| 208 | id: syn!(Ident) >> |
| 209 | features: full >> |
David Tolnay | 28c5a46 | 2017-12-27 01:59:30 -0500 | [diff] [blame] | 210 | option!(manual_extra_traits) >> |
David Tolnay | 2e0dba1 | 2017-12-27 01:54:40 -0500 | [diff] [blame] | 211 | rest: syn!(TokenStream) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 212 | (AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 213 | ast: parse_tokens::<DeriveInput>(quote! { |
David Tolnay | 2e0dba1 | 2017-12-27 01:54:40 -0500 | [diff] [blame] | 214 | pub struct #id #rest |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 215 | })?, |
| 216 | features: features.0, |
| 217 | eos_full: features.1, |
| 218 | }) |
| 219 | )); |
| 220 | |
| 221 | // ast_struct! parsing |
| 222 | pub struct AstStruct(pub Vec<AstItem>); |
| 223 | impl Synom for AstStruct { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 224 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 225 | many0!(Attribute::parse_outer) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 226 | keyword!(pub) >> |
| 227 | keyword!(struct) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 228 | res: call!(ast_struct_inner) >> |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 229 | (AstStruct(vec![res])) |
| 230 | )); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // ast_enum! parsing |
| 234 | pub struct AstEnum(pub Vec<AstItem>); |
| 235 | impl Synom for AstEnum { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 236 | named!(parse -> Self, map!(syn!(DeriveInput), |x| { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 237 | AstEnum(vec![AstItem { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 238 | ast: x, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 239 | features: quote!(), |
| 240 | eos_full: false, |
| 241 | }]) |
| 242 | })); |
| 243 | } |
| 244 | |
| 245 | // A single variant of an ast_enum_of_structs! |
| 246 | struct EosVariant { |
| 247 | name: Ident, |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 248 | member: Option<Path>, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 249 | inner: Option<AstItem>, |
| 250 | } |
| 251 | named!(eos_variant -> EosVariant, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 252 | many0!(Attribute::parse_outer) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 253 | keyword!(pub) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 254 | variant: syn!(Ident) >> |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 255 | member: option!(map!(parens!(alt!( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 256 | call!(ast_struct_inner) => { |x: AstItem| (Path::from(x.ast.ident), Some(x)) } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 257 | | |
| 258 | syn!(Path) => { |x| (x, None) } |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 259 | )), |x| x.0)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 260 | punct!(,) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 261 | (EosVariant { |
| 262 | name: variant, |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 263 | member: member.clone().map(|x| x.0), |
| 264 | inner: member.map(|x| x.1).unwrap_or_default(), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 265 | }) |
| 266 | )); |
| 267 | |
| 268 | // ast_enum_of_structs! parsing |
| 269 | pub struct AstEnumOfStructs(pub Vec<AstItem>); |
| 270 | impl Synom for AstEnumOfStructs { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 271 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 272 | many0!(Attribute::parse_outer) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 273 | keyword!(pub) >> |
| 274 | keyword!(enum) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 275 | id: syn!(Ident) >> |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 276 | body: braces!(many0!(eos_variant)) >> |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 277 | option!(syn!(Ident)) >> // do_not_generate_to_tokens |
| 278 | ({ |
| 279 | // XXX: This is really gross - we shouldn't have to convert the |
| 280 | // tokens to strings to re-parse them. |
| 281 | let enum_item = { |
| 282 | let variants = body.0.iter().map(|v| { |
| 283 | let name = v.name; |
David Tolnay | fcfb900 | 2017-12-28 22:04:29 -0500 | [diff] [blame] | 284 | match v.member { |
| 285 | Some(ref member) => quote!(#name(#member)), |
| 286 | None => quote!(#name), |
| 287 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 288 | }); |
| 289 | parse_tokens::<DeriveInput>(quote! { |
| 290 | pub enum #id { #(#variants),* } |
| 291 | })? |
| 292 | }; |
| 293 | let mut items = vec![AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 294 | ast: enum_item, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 295 | features: quote!(), |
| 296 | eos_full: false, |
| 297 | }]; |
| 298 | items.extend(body.0.into_iter().filter_map(|v| v.inner)); |
| 299 | AstEnumOfStructs(items) |
| 300 | }) |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame^] | 301 | )); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | mod codegen { |
| 306 | use super::{AstItem, Lookup}; |
| 307 | use syn::*; |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 308 | use syn::delimited::Delimited; |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 309 | use quote::{ToTokens, Tokens}; |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 310 | use std::fmt::{self, Display}; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 311 | |
| 312 | #[derive(Default)] |
| 313 | pub struct State { |
| 314 | pub visit_trait: String, |
| 315 | pub visit_impl: String, |
| 316 | pub visit_mut_trait: String, |
| 317 | pub visit_mut_impl: String, |
| 318 | pub fold_trait: String, |
| 319 | pub fold_impl: String, |
| 320 | } |
| 321 | |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 322 | fn under_name(name: Ident) -> Ident { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 323 | use inflections::Inflect; |
| 324 | name.as_ref().to_snake_case().into() |
| 325 | } |
| 326 | |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 327 | enum RelevantType<'a> { |
| 328 | Box(&'a Type), |
| 329 | Vec(&'a Type), |
| 330 | Delimited(&'a Type), |
| 331 | Option(&'a Type), |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 332 | Tuple(&'a Delimited<Type, Token![,]>), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 333 | Simple(&'a AstItem), |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 334 | Token(Tokens), |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 335 | Pass, |
| 336 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 337 | |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 338 | fn classify<'a>(ty: &'a Type, lookup: &'a Lookup) -> RelevantType<'a> { |
| 339 | match *ty { |
| 340 | Type::Path(TypePath { qself: None, ref path }) => { |
| 341 | let last = path.segments.last().unwrap().into_item(); |
| 342 | match last.ident.as_ref() { |
| 343 | "Box" => RelevantType::Box(first_arg(&last.arguments)), |
| 344 | "Vec" => RelevantType::Vec(first_arg(&last.arguments)), |
| 345 | "Delimited" => RelevantType::Delimited(first_arg(&last.arguments)), |
| 346 | "Option" => RelevantType::Option(first_arg(&last.arguments)), |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 347 | "Brace" | "Bracket" | "Paren" | "Group" => { |
| 348 | RelevantType::Token(last.ident.into_tokens()) |
| 349 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 350 | _ => { |
| 351 | if let Some(item) = lookup.get(&last.ident) { |
| 352 | RelevantType::Simple(item) |
| 353 | } else { |
| 354 | RelevantType::Pass |
| 355 | } |
| 356 | } |
| 357 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 358 | } |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 359 | Type::Tuple(TypeTuple { ref elems, .. }) => { |
| 360 | RelevantType::Tuple(elems) |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 361 | } |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 362 | Type::Macro(TypeMacro { ref mac }) if mac.path.segments.last().unwrap().into_item().ident == "Token" => { |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 363 | RelevantType::Token(mac.into_tokens()) |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 364 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 365 | _ => RelevantType::Pass, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | #[derive(Debug, Eq, PartialEq, Copy, Clone)] |
| 370 | enum Kind { |
| 371 | Visit, |
| 372 | VisitMut, |
| 373 | Fold, |
| 374 | } |
| 375 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 376 | enum Operand { |
| 377 | Borrowed(Tokens), |
| 378 | Owned(Tokens), |
| 379 | } |
| 380 | |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 381 | use self::Operand::*; |
| 382 | use self::Kind::*; |
| 383 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 384 | impl Operand { |
| 385 | fn tokens(&self) -> &Tokens { |
| 386 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 387 | Borrowed(ref n) | Owned(ref n) => n, |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | fn ref_tokens(&self) -> Tokens { |
| 392 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 393 | Borrowed(ref n) => n.clone(), |
| 394 | Owned(ref n) => quote!(&#n), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | fn ref_mut_tokens(&self) -> Tokens { |
| 399 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 400 | Borrowed(ref n) => n.clone(), |
| 401 | Owned(ref n) => quote!(&mut #n), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | fn owned_tokens(&self) -> Tokens { |
| 406 | match *self { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 407 | Borrowed(ref n) => quote!(*#n), |
| 408 | Owned(ref n) => n.clone(), |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | impl Display for Operand { |
| 414 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | 4e52d8a | 2017-12-28 15:54:50 -0500 | [diff] [blame] | 415 | Display::fmt(self.tokens(), formatter) |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 419 | fn first_arg(params: &PathArguments) -> &Type { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 420 | let data = match *params { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 421 | PathArguments::AngleBracketed(ref data) => data, |
| 422 | _ => panic!("Expected at least 1 type argument here"), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 423 | }; |
| 424 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 425 | match **data.args |
| 426 | .first() |
| 427 | .expect("Expected at least 1 type argument here") |
| 428 | .item() |
| 429 | { |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 430 | GenericArgument::Type(ref ty) => ty, |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 431 | _ => panic!("Expected at least 1 type argument here"), |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 432 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | fn simple_visit( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 436 | item: &AstItem, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 437 | kind: Kind, |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 438 | name: &Operand, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 439 | ) -> String { |
| 440 | match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 441 | Visit => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 442 | "_visitor.visit_{under_name}({name})", |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 443 | under_name = under_name(item.ast.ident), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 444 | name = name.ref_tokens(), |
| 445 | ), |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 446 | VisitMut => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 447 | "_visitor.visit_{under_name}_mut({name})", |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 448 | under_name = under_name(item.ast.ident), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 449 | name = name.ref_mut_tokens(), |
| 450 | ), |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 451 | Fold => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 452 | "_visitor.fold_{under_name}({name})", |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 453 | under_name = under_name(item.ast.ident), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 454 | name = name.owned_tokens(), |
| 455 | ), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | fn box_visit( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 460 | elem: &Type, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 461 | lookup: &Lookup, |
| 462 | kind: Kind, |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 463 | name: &Operand, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 464 | ) -> Option<String> { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 465 | let name = name.owned_tokens(); |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 466 | let res = visit(elem, lookup, kind, &Owned(quote!(*#name)))?; |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 467 | Some(match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 468 | Fold => format!("Box::new({})", res), |
| 469 | Visit | VisitMut => res, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 470 | }) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | fn vec_visit( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 474 | elem: &Type, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 475 | lookup: &Lookup, |
| 476 | kind: Kind, |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 477 | name: &Operand, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 478 | ) -> Option<String> { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 479 | let operand = match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 480 | Visit | VisitMut => Borrowed(quote!(it)), |
| 481 | Fold => Owned(quote!(it)), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 482 | }; |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 483 | let val = visit(elem, lookup, kind, &operand)?; |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 484 | Some(match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 485 | Visit => { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 486 | format!( |
| 487 | "for it in {name} {{ {val} }}", |
| 488 | name = name.ref_tokens(), |
| 489 | val = val, |
| 490 | ) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 491 | } |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 492 | VisitMut => { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 493 | format!( |
| 494 | "for it in {name} {{ {val} }}", |
| 495 | name = name.ref_mut_tokens(), |
| 496 | val = val, |
| 497 | ) |
| 498 | } |
| 499 | Fold => format!( |
| 500 | "FoldHelper::lift({name}, |it| {{ {val} }})", |
| 501 | name = name.owned_tokens(), |
| 502 | val = val, |
| 503 | ), |
| 504 | }) |
| 505 | } |
| 506 | |
| 507 | fn delimited_visit( |
| 508 | elem: &Type, |
| 509 | lookup: &Lookup, |
| 510 | kind: Kind, |
| 511 | name: &Operand, |
| 512 | ) -> Option<String> { |
| 513 | let operand = match kind { |
| 514 | Visit | VisitMut => Borrowed(quote!(it)), |
| 515 | Fold => Owned(quote!(it)), |
| 516 | }; |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 517 | let val = visit(elem, lookup, kind, &operand)?; |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 518 | Some(match kind { |
| 519 | Visit => { |
| 520 | format!( |
| 521 | "for el in {name} {{ \ |
| 522 | let it = el.item(); \ |
| 523 | {val} \ |
| 524 | }}", |
| 525 | name = name.ref_tokens(), |
| 526 | val = val, |
| 527 | ) |
| 528 | } |
| 529 | VisitMut => { |
| 530 | format!( |
| 531 | "for mut el in {name} {{ \ |
| 532 | let it = el.item_mut(); \ |
| 533 | {val} \ |
| 534 | }}", |
| 535 | name = name.ref_mut_tokens(), |
| 536 | val = val, |
| 537 | ) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 538 | } |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 539 | Fold => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 540 | "FoldHelper::lift({name}, |it| {{ {val} }})", |
| 541 | name = name.owned_tokens(), |
| 542 | val = val, |
| 543 | ), |
| 544 | }) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 545 | } |
| 546 | |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 547 | fn option_visit( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 548 | elem: &Type, |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 549 | lookup: &Lookup, |
| 550 | kind: Kind, |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 551 | name: &Operand, |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 552 | ) -> Option<String> { |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 553 | let it = match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 554 | Visit | VisitMut => Borrowed(quote!(it)), |
| 555 | Fold => Owned(quote!(it)), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 556 | }; |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 557 | let val = visit(elem, lookup, kind, &it)?; |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 558 | Some(match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 559 | Visit => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 560 | "if let Some(ref it) = {name} {{ {val} }}", |
| 561 | name = name.owned_tokens(), |
| 562 | val = val, |
| 563 | ), |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 564 | VisitMut => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 565 | "if let Some(ref mut it) = {name} {{ {val} }}", |
| 566 | name = name.owned_tokens(), |
| 567 | val = val, |
| 568 | ), |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 569 | Fold => format!( |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 570 | "({name}).map(|it| {{ {val} }})", |
| 571 | name = name.owned_tokens(), |
| 572 | val = val, |
| 573 | ), |
| 574 | }) |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 575 | } |
| 576 | |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 577 | fn tuple_visit( |
| 578 | elems: &Delimited<Type, Token![,]>, |
| 579 | lookup: &Lookup, |
| 580 | kind: Kind, |
| 581 | name: &Operand, |
| 582 | ) -> Option<String> { |
| 583 | let mut code = String::new(); |
| 584 | for (i, elem) in elems.items().enumerate() { |
| 585 | let name = name.tokens(); |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 586 | let i = Index::from(i); |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 587 | let it = Owned(quote!((#name).#i)); |
David Tolnay | 39d0a20 | 2017-12-28 18:19:00 -0500 | [diff] [blame] | 588 | let val = visit(elem, lookup, kind, &it) |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 589 | .unwrap_or_else(|| noop_visit(kind, &it)); |
| 590 | code.push_str(&format!(" {}", val)); |
| 591 | match kind { |
| 592 | Fold => code.push(','), |
| 593 | Visit | VisitMut => code.push(';'), |
| 594 | } |
| 595 | code.push('\n'); |
| 596 | } |
| 597 | if code.is_empty() { |
| 598 | None |
| 599 | } else { |
| 600 | Some(match kind { |
| 601 | Fold => { |
| 602 | format!("(\n{} )", code) |
| 603 | } |
| 604 | Visit | VisitMut => { |
| 605 | format!("\n{} ", code) |
| 606 | } |
| 607 | }) |
| 608 | } |
| 609 | } |
| 610 | |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 611 | fn token_visit(ty: Tokens, kind: Kind, name: &Operand) -> String { |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 612 | match kind { |
| 613 | Fold => format!( |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 614 | "{ty}(tokens_helper(_visitor, &({name}).0))", |
| 615 | ty = ty, |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 616 | name = name.owned_tokens(), |
| 617 | ), |
| 618 | Visit => format!( |
| 619 | "tokens_helper(_visitor, &({name}).0)", |
| 620 | name = name.ref_tokens(), |
| 621 | ), |
| 622 | VisitMut => format!( |
| 623 | "tokens_helper(_visitor, &mut ({name}).0)", |
| 624 | name = name.ref_mut_tokens(), |
| 625 | ), |
| 626 | } |
| 627 | } |
| 628 | |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 629 | fn noop_visit(kind: Kind, name: &Operand) -> String { |
| 630 | match kind { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 631 | Fold => name.owned_tokens().to_string(), |
| 632 | Visit | VisitMut => format!("// Skipped field {}", name), |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | fn visit(ty: &Type, lookup: &Lookup, kind: Kind, name: &Operand) -> Option<String> { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 637 | match classify(ty, lookup) { |
| 638 | RelevantType::Box(elem) => { |
| 639 | box_visit(elem, lookup, kind, name) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 640 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 641 | RelevantType::Vec(elem) => { |
| 642 | vec_visit(elem, lookup, kind, name) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 643 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 644 | RelevantType::Delimited(elem) => { |
| 645 | delimited_visit(elem, lookup, kind, name) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 646 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 647 | RelevantType::Option(elem) => { |
| 648 | option_visit(elem, lookup, kind, name) |
| 649 | } |
David Tolnay | 5c4c0b5 | 2017-12-28 17:58:54 -0500 | [diff] [blame] | 650 | RelevantType::Tuple(elems) => { |
| 651 | tuple_visit(elems, lookup, kind, name) |
| 652 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 653 | RelevantType::Simple(item) => { |
| 654 | let mut res = simple_visit(item, kind, name); |
| 655 | Some(if item.eos_full { |
| 656 | format!("full!({res})", res = res) |
| 657 | } else { |
| 658 | res |
| 659 | }) |
| 660 | } |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 661 | RelevantType::Token(ty) => { |
| 662 | Some(token_visit(ty, kind, name)) |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 663 | } |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 664 | RelevantType::Pass => { |
| 665 | None |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 666 | } |
| 667 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | pub fn generate(state: &mut State, lookup: &Lookup, s: &AstItem) { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 671 | let under_name = under_name(s.ast.ident); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 672 | |
| 673 | state.visit_trait.push_str(&format!( |
| 674 | "{features}\n\ |
Nika Layzell | c86173a | 2017-11-18 13:55:22 -0500 | [diff] [blame] | 675 | fn visit_{under_name}(&mut self, i: &'ast {ty}) {{ \ |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 676 | visit_{under_name}(self, i) \ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 677 | }}\n", |
| 678 | features = s.features, |
| 679 | under_name = under_name, |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 680 | ty = s.ast.ident, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 681 | )); |
| 682 | state.visit_mut_trait.push_str(&format!( |
| 683 | "{features}\n\ |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 684 | fn visit_{under_name}_mut(&mut self, i: &mut {ty}) {{ \ |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 685 | visit_{under_name}_mut(self, i) \ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 686 | }}\n", |
| 687 | features = s.features, |
| 688 | under_name = under_name, |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 689 | ty = s.ast.ident, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 690 | )); |
| 691 | state.fold_trait.push_str(&format!( |
| 692 | "{features}\n\ |
| 693 | fn fold_{under_name}(&mut self, i: {ty}) -> {ty} {{ \ |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 694 | fold_{under_name}(self, i) \ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 695 | }}\n", |
| 696 | features = s.features, |
| 697 | under_name = under_name, |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 698 | ty = s.ast.ident, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 699 | )); |
| 700 | |
| 701 | state.visit_impl.push_str(&format!( |
| 702 | "{features}\n\ |
Nika Layzell | c86173a | 2017-11-18 13:55:22 -0500 | [diff] [blame] | 703 | pub fn visit_{under_name}<'ast, V: Visitor<'ast> + ?Sized>(\ |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 704 | _visitor: &mut V, _i: &'ast {ty}) {{\n", |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 705 | features = s.features, |
| 706 | under_name = under_name, |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 707 | ty = s.ast.ident, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 708 | )); |
| 709 | state.visit_mut_impl.push_str(&format!( |
| 710 | "{features}\n\ |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 711 | pub fn visit_{under_name}_mut<V: VisitorMut + ?Sized>(\ |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 712 | _visitor: &mut V, _i: &mut {ty}) {{\n", |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 713 | features = s.features, |
| 714 | under_name = under_name, |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 715 | ty = s.ast.ident, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 716 | )); |
David Tolnay | d0adf52 | 2017-12-29 01:30:07 -0500 | [diff] [blame] | 717 | let before_fold_impl_len = state.fold_impl.len(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 718 | state.fold_impl.push_str(&format!( |
| 719 | "{features}\n\ |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 720 | pub fn fold_{under_name}<V: Folder + ?Sized>(\ |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 721 | _visitor: &mut V, _i: {ty}) -> {ty} {{\n", |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 722 | features = s.features, |
| 723 | under_name = under_name, |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 724 | ty = s.ast.ident, |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 725 | )); |
| 726 | |
| 727 | // XXX: This part is a disaster - I'm not sure how to make it cleaner though :'( |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 728 | match s.ast.body { |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 729 | Body::Enum(ref e) => { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 730 | let use_decl = format!(" use ::{}::*;\n", s.ast.ident); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 731 | state.visit_impl.push_str(&use_decl); |
| 732 | state.visit_mut_impl.push_str(&use_decl); |
| 733 | state.fold_impl.push_str(&use_decl); |
| 734 | |
| 735 | state.visit_impl.push_str(" match *_i {\n"); |
| 736 | state.visit_mut_impl.push_str(" match *_i {\n"); |
| 737 | state.fold_impl.push_str(" match _i {\n"); |
| 738 | for variant in &e.variants { |
| 739 | let fields: Vec<(&Field, Tokens)> = match variant.item().data { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 740 | VariantData::Struct(..) => panic!("Doesn't support enum struct variants"), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 741 | VariantData::Tuple(ref fields, ..) => { |
| 742 | let binding = format!(" {}(", variant.item().ident); |
| 743 | state.visit_impl.push_str(&binding); |
| 744 | state.visit_mut_impl.push_str(&binding); |
| 745 | state.fold_impl.push_str(&binding); |
| 746 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 747 | let res = fields |
| 748 | .iter() |
| 749 | .enumerate() |
| 750 | .map(|(idx, el)| { |
| 751 | let name = format!("_binding_{}", idx); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 752 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 753 | state.visit_impl.push_str("ref "); |
| 754 | state.visit_mut_impl.push_str("ref mut "); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 755 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 756 | state.visit_impl.push_str(&name); |
| 757 | state.visit_mut_impl.push_str(&name); |
| 758 | state.fold_impl.push_str(&name); |
| 759 | state.visit_impl.push_str(", "); |
| 760 | state.visit_mut_impl.push_str(", "); |
| 761 | state.fold_impl.push_str(", "); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 762 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 763 | let mut tokens = quote!(); |
| 764 | Ident::from(name).to_tokens(&mut tokens); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 765 | |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 766 | (*el.item(), tokens) |
| 767 | }) |
| 768 | .collect(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 769 | |
| 770 | state.visit_impl.push_str(") => {\n"); |
| 771 | state.visit_mut_impl.push_str(") => {\n"); |
| 772 | state.fold_impl.push_str(") => {\n"); |
| 773 | |
| 774 | res |
| 775 | } |
| 776 | VariantData::Unit => { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 777 | state |
| 778 | .visit_impl |
| 779 | .push_str(&format!(" {} => {{ }}\n", variant.item().ident,)); |
| 780 | state |
| 781 | .visit_mut_impl |
| 782 | .push_str(&format!(" {} => {{ }}\n", variant.item().ident,)); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 783 | state.fold_impl.push_str(&format!( |
| 784 | " {0} => {{ {0} }}\n", |
| 785 | variant.item().ident |
| 786 | )); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 787 | continue; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 788 | } |
| 789 | }; |
| 790 | |
| 791 | if fields.is_empty() { |
| 792 | state.visit_impl.push_str(" {}"); |
| 793 | state.visit_mut_impl.push_str(") => {\n"); |
| 794 | state.fold_impl.push_str(") => {\n"); |
| 795 | } |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 796 | state |
| 797 | .fold_impl |
| 798 | .push_str(&format!(" {} (\n", variant.item().ident,)); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 799 | for (field, binding) in fields { |
| 800 | state.visit_impl.push_str(&format!( |
| 801 | " {};\n", |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 802 | visit( |
| 803 | &field.ty, |
| 804 | lookup, |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 805 | Visit, |
| 806 | &Borrowed(binding.clone()) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 807 | ).unwrap_or_else(|| noop_visit( |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 808 | Visit, |
| 809 | &Borrowed(binding.clone()) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 810 | )), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 811 | )); |
| 812 | state.visit_mut_impl.push_str(&format!( |
| 813 | " {};\n", |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 814 | visit( |
| 815 | &field.ty, |
| 816 | lookup, |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 817 | VisitMut, |
| 818 | &Borrowed(binding.clone()) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 819 | ).unwrap_or_else(|| noop_visit( |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 820 | VisitMut, |
| 821 | &Borrowed(binding.clone()) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 822 | )), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 823 | )); |
| 824 | state.fold_impl.push_str(&format!( |
| 825 | " {},\n", |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 826 | visit(&field.ty, lookup, Fold, &Owned(binding.clone())) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 827 | .unwrap_or_else(|| noop_visit( |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 828 | Fold, |
| 829 | &Owned(binding) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 830 | )), |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 831 | )); |
| 832 | } |
| 833 | state.fold_impl.push_str(" )\n"); |
| 834 | |
| 835 | state.visit_impl.push_str(" }\n"); |
| 836 | state.visit_mut_impl.push_str(" }\n"); |
| 837 | state.fold_impl.push_str(" }\n"); |
| 838 | } |
| 839 | state.visit_impl.push_str(" }\n"); |
| 840 | state.visit_mut_impl.push_str(" }\n"); |
| 841 | state.fold_impl.push_str(" }\n"); |
| 842 | } |
| 843 | Body::Struct(ref v) => { |
| 844 | let fields: Vec<(&Field, Tokens)> = match v.data { |
| 845 | VariantData::Struct(ref fields, ..) => { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 846 | state |
| 847 | .fold_impl |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 848 | .push_str(&format!(" {} {{\n", s.ast.ident)); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 849 | fields |
| 850 | .iter() |
| 851 | .map(|el| { |
| 852 | let id = el.item().ident; |
| 853 | (*el.item(), quote!(_i.#id)) |
| 854 | }) |
| 855 | .collect() |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 856 | } |
| 857 | VariantData::Tuple(ref fields, ..) => { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 858 | state |
| 859 | .fold_impl |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 860 | .push_str(&format!(" {} (\n", s.ast.ident)); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 861 | fields |
| 862 | .iter() |
| 863 | .enumerate() |
| 864 | .map(|(idx, el)| { |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 865 | let id = Index::from(idx); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 866 | (*el.item(), quote!(_i.#id)) |
| 867 | }) |
| 868 | .collect() |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 869 | } |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 870 | VariantData::Unit => { |
| 871 | state.fold_impl.push_str(" _i\n"); |
| 872 | vec![] |
| 873 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 874 | }; |
| 875 | |
| 876 | for (field, ref_toks) in fields { |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 877 | let ref_toks = Owned(ref_toks); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 878 | state.visit_impl.push_str(&format!( |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 879 | " {};\n", |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 880 | visit(&field.ty, lookup, Visit, &ref_toks) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 881 | .unwrap_or_else(|| noop_visit( |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 882 | Visit, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 883 | &ref_toks, |
| 884 | )) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 885 | )); |
| 886 | state.visit_mut_impl.push_str(&format!( |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 887 | " {};\n", |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 888 | visit(&field.ty, lookup, VisitMut, &ref_toks) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 889 | .unwrap_or_else(|| noop_visit( |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 890 | VisitMut, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 891 | &ref_toks, |
| 892 | )) |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 893 | )); |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 894 | let fold = visit(&field.ty, lookup, Fold, &ref_toks) |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 895 | .unwrap_or_else(|| noop_visit( |
David Tolnay | 83db927 | 2017-12-28 17:02:31 -0500 | [diff] [blame] | 896 | Fold, |
David Tolnay | 4a91874 | 2017-12-28 16:54:41 -0500 | [diff] [blame] | 897 | &ref_toks, |
| 898 | )); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 899 | if let Some(ref name) = field.ident { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 900 | state |
| 901 | .fold_impl |
| 902 | .push_str(&format!(" {}: {},\n", name, fold)); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 903 | } else { |
| 904 | state.fold_impl.push_str(&format!(" {},\n", fold)); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | match v.data { |
| 909 | VariantData::Struct(..) => state.fold_impl.push_str(" }\n"), |
| 910 | VariantData::Tuple(..) => state.fold_impl.push_str(" )\n"), |
| 911 | VariantData::Unit => {} |
| 912 | }; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // Close the impl body |
| 917 | state.visit_impl.push_str("}\n"); |
| 918 | state.visit_mut_impl.push_str("}\n"); |
| 919 | state.fold_impl.push_str("}\n"); |
David Tolnay | d0adf52 | 2017-12-29 01:30:07 -0500 | [diff] [blame] | 920 | |
| 921 | if s.ast.ident == "Ident" || s.ast.ident == "Lifetime" { |
| 922 | // Discard the generated impl. These have private fields and are |
| 923 | // handwritten. |
| 924 | state.fold_impl.truncate(before_fold_impl_len); |
| 925 | } |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
| 929 | fn main() { |
| 930 | let mut lookup = BTreeMap::new(); |
David Tolnay | ea9ae89 | 2017-12-26 01:44:32 -0500 | [diff] [blame] | 931 | load_file(SYN_CRATE_ROOT, "e!(), &mut lookup).unwrap(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 932 | |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 933 | // Load in any terminal types |
| 934 | for &tt in TERMINAL_TYPES { |
| 935 | use syn::*; |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 936 | lookup.insert( |
| 937 | Ident::from(tt), |
| 938 | AstItem { |
David Tolnay | 3d77218 | 2017-12-28 17:18:53 -0500 | [diff] [blame] | 939 | ast: DeriveInput { |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 940 | ident: Ident::from(tt), |
| 941 | vis: Visibility::Public(VisPublic { |
| 942 | pub_token: Default::default(), |
| 943 | }), |
| 944 | attrs: vec![], |
| 945 | generics: Default::default(), |
| 946 | body: Body::Struct(BodyStruct { |
| 947 | data: VariantData::Unit, |
| 948 | struct_token: Default::default(), |
| 949 | semi_token: None, |
| 950 | }), |
| 951 | }, |
| 952 | features: Default::default(), |
| 953 | eos_full: false, |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 954 | }, |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 955 | ); |
Nika Layzell | efb83ba | 2017-12-19 18:23:55 -0500 | [diff] [blame] | 956 | } |
| 957 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 958 | let mut state = Default::default(); |
| 959 | for s in lookup.values() { |
| 960 | codegen::generate(&mut state, &lookup, s); |
| 961 | } |
| 962 | |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 963 | let full_macro = " |
| 964 | #[cfg(feature = \"full\")] |
| 965 | macro_rules! full { |
| 966 | ($e:expr) => { $e } |
| 967 | } |
| 968 | |
| 969 | #[cfg(not(feature = \"full\"))] |
| 970 | macro_rules! full { |
| 971 | ($e:expr) => { unreachable!() } |
| 972 | } |
| 973 | "; |
| 974 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 975 | let mut fold_file = File::create(FOLD_SRC).unwrap(); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 976 | write!( |
| 977 | fold_file, |
| 978 | "\ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 979 | // THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT |
| 980 | |
| 981 | //! A Folder represents an AST->AST fold; it accepts an AST piece, |
| 982 | //! and returns a piece of the same type. |
| 983 | |
David Tolnay | 0afc9b3 | 2017-12-27 13:38:24 -0500 | [diff] [blame] | 984 | #![cfg_attr(rustfmt, rustfmt_skip)] |
| 985 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 986 | // Unreachable code is generated sometimes without the full feature. |
| 987 | #![allow(unreachable_code)] |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 988 | #![cfg_attr(feature = \"cargo-clippy\", allow(needless_pass_by_value))] |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 989 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 990 | use *; |
David Tolnay | 1e01f9c | 2017-12-28 20:16:19 -0500 | [diff] [blame] | 991 | use token::{{Brace, Bracket, Paren, Group}}; |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 992 | use proc_macro2::Span; |
David Tolnay | f60f426 | 2017-12-28 19:17:58 -0500 | [diff] [blame] | 993 | use gen::helper::fold::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 994 | |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 995 | {full_macro} |
| 996 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 997 | /// AST->AST fold. |
| 998 | /// |
| 999 | /// Each method of the Folder trait is a hook to be potentially overridden. Each |
| 1000 | /// method's default implementation recursively visits the substructure of the |
| 1001 | /// input via the `walk` functions, which perform an \"identity fold\", that |
| 1002 | /// is, they return the same structure that they are given (for example the |
| 1003 | /// `fold_file` method by default calls `fold::walk_file`). |
| 1004 | /// |
| 1005 | /// If you want to ensure that your code handles every variant |
| 1006 | /// explicitly, you need to override each method. (And you also need |
| 1007 | /// to monitor future changes to `Folder` in case a new method with a |
| 1008 | /// new default implementation gets introduced.) |
| 1009 | pub trait Folder {{ |
| 1010 | {fold_trait} |
| 1011 | }} |
| 1012 | |
David Tolnay | d0adf52 | 2017-12-29 01:30:07 -0500 | [diff] [blame] | 1013 | pub fn fold_ident<V: Folder + ?Sized>(_visitor: &mut V, mut _i: Ident) -> Ident {{ |
| 1014 | _i.span = _visitor.fold_span(_i.span); |
| 1015 | _i |
| 1016 | }} |
| 1017 | |
| 1018 | pub fn fold_lifetime<V: Folder + ?Sized>(_visitor: &mut V, mut _i: Lifetime) -> Lifetime {{ |
| 1019 | _i.span = _visitor.fold_span(_i.span); |
| 1020 | _i |
| 1021 | }} |
| 1022 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1023 | {fold_impl} |
| 1024 | ", |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1025 | full_macro = full_macro, |
| 1026 | fold_trait = state.fold_trait, |
| 1027 | fold_impl = state.fold_impl |
| 1028 | ).unwrap(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1029 | |
| 1030 | let mut visit_file = File::create(VISIT_SRC).unwrap(); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1031 | write!( |
| 1032 | visit_file, |
| 1033 | "\ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1034 | // THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT |
| 1035 | |
| 1036 | //! AST walker. Each overridden visit method has full control over what |
| 1037 | //! happens with its node, it can do its own traversal of the node's children, |
| 1038 | //! call `visit::walk_*` to apply the default traversal algorithm, or prevent |
| 1039 | //! deeper traversal by doing nothing. |
| 1040 | |
David Tolnay | 0afc9b3 | 2017-12-27 13:38:24 -0500 | [diff] [blame] | 1041 | #![cfg_attr(rustfmt, rustfmt_skip)] |
| 1042 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 1043 | #![cfg_attr(feature = \"cargo-clippy\", allow(match_same_arms))] |
| 1044 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 1045 | use *; |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 1046 | use proc_macro2::Span; |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 1047 | use gen::helper::visit::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1048 | |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1049 | {full_macro} |
| 1050 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1051 | /// Each method of the Visitor trait is a hook to be potentially |
| 1052 | /// overridden. Each method's default implementation recursively visits |
| 1053 | /// the substructure of the input via the corresponding `walk` method; |
| 1054 | /// e.g. the `visit_mod` method by default calls `visit::walk_mod`. |
| 1055 | /// |
| 1056 | /// If you want to ensure that your code handles every variant |
| 1057 | /// explicitly, you need to override each method. (And you also need |
| 1058 | /// to monitor future changes to `Visitor` in case a new method with a |
| 1059 | /// new default implementation gets introduced.) |
Nika Layzell | c86173a | 2017-11-18 13:55:22 -0500 | [diff] [blame] | 1060 | pub trait Visitor<'ast> {{ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1061 | {visit_trait} |
| 1062 | }} |
| 1063 | |
| 1064 | {visit_impl} |
| 1065 | ", |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1066 | full_macro = full_macro, |
| 1067 | visit_trait = state.visit_trait, |
| 1068 | visit_impl = state.visit_impl |
| 1069 | ).unwrap(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1070 | |
| 1071 | let mut visit_mut_file = File::create(VISIT_MUT_SRC).unwrap(); |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1072 | write!( |
| 1073 | visit_mut_file, |
| 1074 | "\ |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1075 | // THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT |
| 1076 | |
| 1077 | //! AST walker. Each overridden visit method has full control over what |
| 1078 | //! happens with its node, it can do its own traversal of the node's children, |
| 1079 | //! call `visit::walk_*` to apply the default traversal algorithm, or prevent |
| 1080 | //! deeper traversal by doing nothing. |
| 1081 | |
David Tolnay | 0afc9b3 | 2017-12-27 13:38:24 -0500 | [diff] [blame] | 1082 | #![cfg_attr(rustfmt, rustfmt_skip)] |
| 1083 | |
David Tolnay | f0d63bf | 2017-12-26 12:29:47 -0500 | [diff] [blame] | 1084 | #![cfg_attr(feature = \"cargo-clippy\", allow(match_same_arms))] |
| 1085 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 1086 | use *; |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 1087 | use proc_macro2::Span; |
David Tolnay | cc0f037 | 2017-12-28 19:11:04 -0500 | [diff] [blame] | 1088 | use gen::helper::visit_mut::*; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1089 | |
Nika Layzell | 4ab8d6e | 2017-10-26 09:45:49 -0400 | [diff] [blame] | 1090 | {full_macro} |
| 1091 | |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1092 | /// Each method of the VisitorMut trait is a hook to be potentially |
| 1093 | /// overridden. Each method's default implementation recursively visits |
| 1094 | /// the substructure of the input via the corresponding `walk` method; |
| 1095 | /// e.g. the `visit_mod` method by default calls `visit::walk_mod`. |
| 1096 | /// |
| 1097 | /// If you want to ensure that your code handles every variant |
| 1098 | /// explicitly, you need to override each method. (And you also need |
| 1099 | /// to monitor future changes to `VisitorMut` in case a new method with a |
| 1100 | /// new default implementation gets introduced.) |
| 1101 | pub trait VisitorMut {{ |
| 1102 | {visit_mut_trait} |
| 1103 | }} |
| 1104 | |
| 1105 | {visit_mut_impl} |
| 1106 | ", |
David Tolnay | d67fb75 | 2017-12-27 13:50:29 -0500 | [diff] [blame] | 1107 | full_macro = full_macro, |
| 1108 | visit_mut_trait = state.visit_mut_trait, |
| 1109 | visit_mut_impl = state.visit_mut_impl |
| 1110 | ).unwrap(); |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 1111 | } |