David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 1 | Nom parser for Rust items |
| 2 | ========================= |
| 3 | |
David Tolnay | ac9953b | 2016-09-07 08:37:12 -0700 | [diff] [blame] | 4 | [](https://travis-ci.org/dtolnay/syn) |
| 5 | [](https://crates.io/crates/syn) |
David Tolnay | 50e6220 | 2016-09-12 09:49:49 -0700 | [diff] [blame] | 6 | [](https://dtolnay.github.io/syn/syn/) |
David Tolnay | ac9953b | 2016-09-07 08:37:12 -0700 | [diff] [blame] | 7 | |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 8 | Parse Rust structs and enums without a Syntex dependency, intended for use with |
| 9 | [Macros 1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md). |
| 10 | |
David Tolnay | f939f35 | 2016-09-11 18:00:09 -0700 | [diff] [blame] | 11 | Designed for fast compile time. |
| 12 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 13 | - Compile time for `syn` (from scratch including all dependencies): **4 seconds** |
David Tolnay | f939f35 | 2016-09-11 18:00:09 -0700 | [diff] [blame] | 14 | - Compile time for the `syntex`/`quasi`/`aster` stack: **60+ seconds** |
| 15 | |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 16 | ```toml |
| 17 | [dependencies] |
David Tolnay | 7b3973e | 2016-09-11 17:46:52 -0700 | [diff] [blame] | 18 | syn = "0.5" |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 19 | ``` |
| 20 | |
| 21 | ```rust |
David Tolnay | a097608 | 2016-09-07 08:24:28 -0700 | [diff] [blame] | 22 | extern crate syn; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 23 | |
| 24 | let raw = " |
David Tolnay | 99ef8c9 | 2016-09-04 11:31:37 -0700 | [diff] [blame] | 25 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 26 | pub struct Item { |
| 27 | pub ident: Ident, |
David Tolnay | 99ef8c9 | 2016-09-04 11:31:37 -0700 | [diff] [blame] | 28 | pub vis: Visibility, |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 29 | pub attrs: Vec<Attribute>, |
David Tolnay | 99ef8c9 | 2016-09-04 11:31:37 -0700 | [diff] [blame] | 30 | pub generics: Generics, |
| 31 | pub body: Body, |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 32 | } |
| 33 | "; |
| 34 | |
David Tolnay | b0a2c3a | 2016-09-11 17:48:11 -0700 | [diff] [blame] | 35 | let ast = syn::parse_item(raw).unwrap(); |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 36 | ``` |
| 37 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 38 | ## Usage with [Macros 1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md) |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 39 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 40 | ```rust |
| 41 | // lib.rs |
| 42 | #![crate_type = "rustc-macro"] |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 43 | #![feature(rustc_macro, rustc_macro_lib)] |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 44 | |
| 45 | extern crate rustc_macro; |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 46 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 47 | #[macro_use] |
| 48 | extern crate quote; |
| 49 | extern crate syn; |
| 50 | |
| 51 | use rustc_macro::TokenStream; |
| 52 | |
| 53 | #[rustc_macro_derive(SpecialItem)] |
| 54 | pub fn special_item(input: TokenStream) -> TokenStream { |
| 55 | let source = input.to_string(); |
| 56 | |
| 57 | // Parse a string of items to an AST |
| 58 | let ast = syn::parse_item(&source).unwrap(); |
| 59 | |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 60 | // Build the output, possibly using quasi-quotation |
| 61 | let expanded = quote! { |
| 62 | // ... |
| 63 | }; |
| 64 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 65 | // Parse this back to a token stream and return it |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 66 | expanded.to_string().parse().unwrap() |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 67 | } |
| 68 | ``` |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 69 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 70 | ```toml |
| 71 | # Cargo.toml |
| 72 | # ... |
| 73 | [dependencies] |
| 74 | syn = "0.5" |
| 75 | quote = "0.1" |
| 76 | |
| 77 | [lib] |
| 78 | rustc-macro = true |
| 79 | ``` |
| 80 | |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 81 | ## License |
| 82 | |
| 83 | Licensed under either of |
| 84 | |
| 85 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 86 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 87 | |
| 88 | at your option. |
| 89 | |
| 90 | ### Contribution |
| 91 | |
| 92 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 93 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 94 | be dual licensed as above, without any additional terms or conditions. |