Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 1 | #![cfg(all(feature = "full", feature = "fold"))] |
Nika Layzell | a2a1a4a | 2017-11-19 11:33:17 -0500 | [diff] [blame] | 2 | #![feature(rustc_private)] |
| 3 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 4 | //! The tests in this module do the following: |
| 5 | //! |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 6 | //! 1. Parse a given expression in both `syn` and `libsyntax`. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 7 | //! 2. Fold over the expression adding brackets around each subexpression (with |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 8 | //! some complications - see the `syn_brackets` and `libsyntax_brackets` |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 9 | //! methods). |
| 10 | //! 3. Serialize the `syn` expression back into a string, and re-parse it with |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 11 | //! `libsyntax`. |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 12 | //! 4. Respan all of the expressions, replacing the spans with the default spans. |
| 13 | //! 5. Compare the expressions with one another, if they are not equal fail. |
| 14 | |
| 15 | #[macro_use] |
| 16 | extern crate quote; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 17 | extern crate rayon; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 18 | extern crate syn; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 19 | extern crate syntax; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 20 | extern crate walkdir; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 21 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 22 | use rayon::iter::{IntoParallelIterator, ParallelIterator}; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 23 | use syntax::ast; |
| 24 | use syntax::ptr::P; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 25 | use walkdir::{DirEntry, WalkDir, WalkDirIterator}; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 26 | |
| 27 | use std::fs::File; |
| 28 | use std::io::Read; |
David Tolnay | 3eaf7d8 | 2017-12-17 23:14:52 -0800 | [diff] [blame] | 29 | use std::process; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 30 | use std::sync::atomic::{AtomicUsize, Ordering}; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 31 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 32 | use common::{parse, respan}; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 33 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 34 | #[macro_use] |
David Tolnay | dd12556 | 2017-12-31 02:16:22 -0500 | [diff] [blame] | 35 | mod macros; |
| 36 | |
| 37 | #[allow(dead_code)] |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 38 | mod common; |
| 39 | |
| 40 | /// Test some pre-set expressions chosen by us. |
| 41 | #[test] |
| 42 | fn test_simple_precedence() { |
| 43 | const EXPRS: &[&str] = &[ |
| 44 | "1 + 2 * 3 + 4", |
| 45 | "1 + 2 * ( 3 + 4 )", |
| 46 | "{ for i in r { } *some_ptr += 1; }", |
| 47 | "{ loop { break 5; } }", |
| 48 | "{ if true { () }.mthd() }", |
Nika Layzell | 3aa0dc7 | 2017-12-04 13:41:28 -0500 | [diff] [blame] | 49 | "{ for i in unsafe { 20 } { } }", |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 50 | ]; |
| 51 | |
| 52 | let mut failed = 0; |
| 53 | |
| 54 | for input in EXPRS { |
| 55 | let expr = if let Some(expr) = parse::syn_expr(input) { |
| 56 | expr |
| 57 | } else { |
| 58 | failed += 1; |
| 59 | continue; |
| 60 | }; |
| 61 | |
| 62 | let pf = match test_expressions(vec![expr]) { |
| 63 | (1, 0) => "passed", |
| 64 | (0, 1) => { |
| 65 | failed += 1; |
| 66 | "failed" |
| 67 | } |
| 68 | _ => unreachable!(), |
| 69 | }; |
| 70 | errorf!("=== {}: {}\n", input, pf); |
| 71 | } |
| 72 | |
| 73 | if failed > 0 { |
| 74 | panic!("Failed {} tests", failed); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Test expressions from rustc, like in `test_round_trip`. |
| 79 | #[test] |
| 80 | fn test_rustc_precedence() { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 81 | common::check_min_stack(); |
Alex Crichton | 8637477 | 2017-07-07 20:39:28 -0700 | [diff] [blame] | 82 | common::clone_rust(); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 83 | let abort_after = common::abort_after(); |
| 84 | if abort_after == 0 { |
| 85 | panic!("Skipping all precedence tests"); |
| 86 | } |
| 87 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 88 | let passed = AtomicUsize::new(0); |
| 89 | let failed = AtomicUsize::new(0); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 90 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 91 | WalkDir::new("tests/rust") |
| 92 | .sort_by(|a, b| a.cmp(b)) |
| 93 | .into_iter() |
| 94 | .filter_entry(common::base_dir_filter) |
| 95 | .collect::<Result<Vec<DirEntry>, walkdir::Error>>() |
| 96 | .unwrap() |
| 97 | .into_par_iter() |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 98 | .for_each(|entry| { |
| 99 | let path = entry.path(); |
| 100 | if path.is_dir() { |
| 101 | return; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 102 | } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 103 | |
| 104 | // Our version of `libsyntax` can't parse this tests |
| 105 | if path.to_str() |
| 106 | .unwrap() |
| 107 | .ends_with("optional_comma_in_match_arm.rs") |
| 108 | { |
| 109 | return; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 110 | } |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 111 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 112 | let mut file = File::open(path).unwrap(); |
| 113 | let mut content = String::new(); |
| 114 | file.read_to_string(&mut content).unwrap(); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 115 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 116 | let (l_passed, l_failed) = match syn::parse_file(&content) { |
| 117 | Ok(file) => { |
| 118 | let exprs = collect_exprs(file); |
| 119 | test_expressions(exprs) |
| 120 | } |
| 121 | Err(msg) => { |
| 122 | errorf!("syn failed to parse\n{:?}\n", msg); |
| 123 | (0, 1) |
| 124 | } |
| 125 | }; |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 126 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 127 | errorf!( |
| 128 | "=== {}: {} passed | {} failed\n", |
| 129 | path.display(), |
| 130 | l_passed, |
| 131 | l_failed |
| 132 | ); |
| 133 | |
| 134 | passed.fetch_add(l_passed, Ordering::SeqCst); |
| 135 | let prev_failed = failed.fetch_add(l_failed, Ordering::SeqCst); |
| 136 | |
| 137 | if prev_failed + l_failed >= abort_after { |
| 138 | process::exit(1); |
| 139 | } |
| 140 | }); |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 141 | |
| 142 | let passed = passed.load(Ordering::SeqCst); |
| 143 | let failed = failed.load(Ordering::SeqCst); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 144 | |
| 145 | errorf!("\n===== Precedence Test Results =====\n"); |
| 146 | errorf!("{} passed | {} failed\n", passed, failed); |
| 147 | |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 148 | if failed > 0 { |
| 149 | panic!("{} failures", failed); |
| 150 | } |
| 151 | } |
| 152 | |
David Tolnay | ee97dbf | 2017-11-19 14:24:38 -0800 | [diff] [blame] | 153 | fn test_expressions(exprs: Vec<syn::Expr>) -> (usize, usize) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 154 | let mut passed = 0; |
| 155 | let mut failed = 0; |
| 156 | |
| 157 | for expr in exprs { |
| 158 | let raw = quote!(#expr).to_string(); |
| 159 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 160 | let libsyntax_ast = if let Some(e) = libsyntax_parse_and_rewrite(&raw) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 161 | e |
| 162 | } else { |
| 163 | failed += 1; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 164 | errorf!("\nFAIL - libsyntax failed to parse raw\n"); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 165 | continue; |
| 166 | }; |
| 167 | |
| 168 | let syn_expr = syn_brackets(expr); |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 169 | let syn_ast = if let Some(e) = parse::libsyntax_expr("e!(#syn_expr).to_string()) { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 170 | e |
| 171 | } else { |
| 172 | failed += 1; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 173 | errorf!("\nFAIL - libsyntax failed to parse bracketed\n"); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 174 | continue; |
| 175 | }; |
| 176 | |
| 177 | let syn_ast = respan::respan_expr(syn_ast); |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 178 | let libsyntax_ast = respan::respan_expr(libsyntax_ast); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 179 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 180 | if syn_ast == libsyntax_ast { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 181 | passed += 1; |
| 182 | } else { |
| 183 | failed += 1; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 184 | errorf!("\nFAIL\n{:?}\n!=\n{:?}\n", syn_ast, libsyntax_ast); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | (passed, failed) |
| 189 | } |
| 190 | |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 191 | fn libsyntax_parse_and_rewrite(input: &str) -> Option<P<ast::Expr>> { |
David Tolnay | 3cede94 | 2017-12-26 12:29:24 -0500 | [diff] [blame] | 192 | parse::libsyntax_expr(input).and_then(libsyntax_brackets) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /// Wrap every expression which is not already wrapped in parens with parens, to |
| 196 | /// reveal the precidence of the parsed expressions, and produce a stringified form |
| 197 | /// of the resulting expression. |
| 198 | /// |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 199 | /// This method operates on libsyntax objects. |
| 200 | fn libsyntax_brackets(libsyntax_expr: P<ast::Expr>) -> Option<P<ast::Expr>> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 201 | use syntax::ast::{Expr, ExprKind, Field, Mac, Pat, Stmt, StmtKind, Ty}; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 202 | use syntax::fold::{self, Folder}; |
| 203 | use syntax::util::ThinVec; |
| 204 | use syntax::util::small_vector::SmallVector; |
| 205 | use syntax::ext::quote::rt::DUMMY_SP; |
| 206 | use syntax::codemap; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 207 | |
| 208 | fn expr(node: ExprKind) -> P<Expr> { |
| 209 | P(Expr { |
| 210 | id: ast::DUMMY_NODE_ID, |
| 211 | node: node, |
| 212 | span: DUMMY_SP, |
| 213 | attrs: ThinVec::new(), |
| 214 | }) |
| 215 | } |
| 216 | |
| 217 | struct BracketsFolder { |
| 218 | failed: bool, |
| 219 | }; |
| 220 | impl Folder for BracketsFolder { |
| 221 | fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 222 | e.map(|e| Expr { |
| 223 | node: match e.node { |
| 224 | ExprKind::Paren(inner) => { |
| 225 | ExprKind::Paren(inner.map(|e| fold::noop_fold_expr(e, self))) |
| 226 | } |
| 227 | ExprKind::If(..) | ExprKind::Block(..) | ExprKind::IfLet(..) => { |
| 228 | return fold::noop_fold_expr(e, self); |
| 229 | } |
| 230 | node => ExprKind::Paren(expr(node).map(|e| fold::noop_fold_expr(e, self))), |
| 231 | }, |
| 232 | ..e |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 233 | }) |
| 234 | } |
| 235 | |
| 236 | fn fold_field(&mut self, f: Field) -> Field { |
| 237 | Field { |
| 238 | ident: codemap::respan(f.ident.span, self.fold_ident(f.ident.node)), |
| 239 | expr: if f.is_shorthand { |
| 240 | f.expr.map(|e| fold::noop_fold_expr(e, self)) |
| 241 | } else { |
| 242 | self.fold_expr(f.expr) |
| 243 | }, |
| 244 | span: self.new_span(f.span), |
| 245 | is_shorthand: f.is_shorthand, |
| 246 | attrs: fold::fold_thin_attrs(f.attrs, self), |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // We don't want to look at expressions that might appear in patterns or |
| 251 | // types yet. We'll look into comparing those in the future. For now |
| 252 | // focus on expressions appearing in other places. |
| 253 | fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> { |
| 254 | pat |
| 255 | } |
| 256 | |
| 257 | fn fold_ty(&mut self, ty: P<Ty>) -> P<Ty> { |
| 258 | ty |
| 259 | } |
| 260 | |
| 261 | fn fold_stmt(&mut self, stmt: Stmt) -> SmallVector<Stmt> { |
| 262 | let node = match stmt.node { |
| 263 | // Don't wrap toplevel expressions in statements. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 264 | StmtKind::Expr(e) => StmtKind::Expr(e.map(|e| fold::noop_fold_expr(e, self))), |
| 265 | StmtKind::Semi(e) => StmtKind::Semi(e.map(|e| fold::noop_fold_expr(e, self))), |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 266 | s => s, |
| 267 | }; |
| 268 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 269 | SmallVector::one(Stmt { node: node, ..stmt }) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | fn fold_mac(&mut self, mac: Mac) -> Mac { |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 273 | // By default when folding over macros, libsyntax panics. This is |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 274 | // because it's usually not what you want, you want to run after |
| 275 | // macro expansion. We do want to do that (syn doesn't do macro |
| 276 | // expansion), so we implement fold_mac to just return the macro |
| 277 | // unchanged. |
| 278 | mac |
| 279 | } |
| 280 | } |
| 281 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 282 | let mut folder = BracketsFolder { failed: false }; |
David Tolnay | cfa5cc0 | 2017-11-13 01:05:11 -0800 | [diff] [blame] | 283 | let e = folder.fold_expr(libsyntax_expr); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 284 | if folder.failed { |
| 285 | None |
| 286 | } else { |
| 287 | Some(e) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /// Wrap every expression which is not already wrapped in parens with parens, to |
| 292 | /// reveal the precidence of the parsed expressions, and produce a stringified form |
| 293 | /// of the resulting expression. |
| 294 | fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr { |
| 295 | use syn::*; |
| 296 | use syn::fold::*; |
| 297 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 298 | fn paren(folder: &mut BracketsFolder, mut node: Expr) -> Expr { |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 299 | let attrs = node.replace_attrs(Vec::new()); |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 300 | Expr::Paren(ExprParen { |
| 301 | attrs: attrs, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 302 | expr: Box::new(fold_expr( |
| 303 | folder, |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 304 | node, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 305 | )), |
David Tolnay | 42eaae1 | 2017-12-26 23:05:18 -0500 | [diff] [blame] | 306 | paren_token: token::Paren::default(), |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 307 | }) |
| 308 | } |
| 309 | |
| 310 | struct BracketsFolder; |
| 311 | impl Folder for BracketsFolder { |
| 312 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 313 | match expr { |
| 314 | Expr::Group(_) => unreachable!(), |
| 315 | Expr::Paren(p) => paren(self, *p.expr), |
| 316 | Expr::If(..) |
| 317 | | Expr::Unsafe(..) |
| 318 | | Expr::Block(..) |
| 319 | | Expr::IfLet(..) => { |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame^] | 320 | fold_expr(self, expr) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 321 | } |
| 322 | node => paren(self, node), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 323 | } |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | fn fold_stmt(&mut self, stmt: Stmt) -> Stmt { |
| 327 | match stmt { |
| 328 | // Don't wrap toplevel expressions in statements. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 329 | Stmt::Expr(e) => Stmt::Expr(Box::new(fold_expr(self, *e))), |
| 330 | Stmt::Semi(e, semi) => Stmt::Semi(Box::new(fold_expr(self, *e)), semi), |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 331 | s => s, |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // We don't want to look at expressions that might appear in patterns or |
| 336 | // types yet. We'll look into comparing those in the future. For now |
| 337 | // focus on expressions appearing in other places. |
| 338 | fn fold_pat(&mut self, pat: Pat) -> Pat { |
| 339 | pat |
| 340 | } |
| 341 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 342 | fn fold_type(&mut self, ty: Type) -> Type { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 343 | ty |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | let mut folder = BracketsFolder; |
| 348 | folder.fold_expr(syn_expr) |
| 349 | } |
| 350 | |
| 351 | /// Walk through a crate collecting all expressions we can find in it. |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 352 | fn collect_exprs(file: syn::File) -> Vec<syn::Expr> { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 353 | use syn::*; |
| 354 | use syn::fold::*; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 355 | use syn::delimited::Delimited; |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 356 | |
| 357 | struct CollectExprsFolder(Vec<Expr>); |
| 358 | impl Folder for CollectExprsFolder { |
| 359 | fn fold_expr(&mut self, expr: Expr) -> Expr { |
| 360 | self.0.push(expr); |
| 361 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 362 | Expr::Tuple(ExprTuple { |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 363 | attrs: vec![], |
David Tolnay | 2a86fdd | 2017-12-28 23:34:28 -0500 | [diff] [blame] | 364 | elems: Delimited::new(), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 365 | paren_token: token::Paren::default(), |
| 366 | }) |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | let mut folder = CollectExprsFolder(vec![]); |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 371 | folder.fold_file(file); |
Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 372 | folder.0 |
| 373 | } |