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 | ab09d46 | 2016-09-23 19:27:54 -0700 | [diff] [blame] | 18 | syn = "0.6" |
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 | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 35 | let ast = syn::parse_macro_input(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 | |
David Tolnay | 69b538e | 2016-09-23 19:59:48 -0700 | [diff] [blame^] | 40 | ```toml |
| 41 | [dependencies] |
| 42 | syn = "0.6" |
| 43 | quote = "0.1" |
| 44 | |
| 45 | [lib] |
| 46 | rustc-macro = true |
| 47 | ``` |
| 48 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 49 | ```rust |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 50 | #![crate_type = "rustc-macro"] |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 51 | #![feature(rustc_macro, rustc_macro_lib)] |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 52 | |
| 53 | extern crate rustc_macro; |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 54 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 55 | #[macro_use] |
| 56 | extern crate quote; |
| 57 | extern crate syn; |
| 58 | |
| 59 | use rustc_macro::TokenStream; |
| 60 | |
| 61 | #[rustc_macro_derive(SpecialItem)] |
| 62 | pub fn special_item(input: TokenStream) -> TokenStream { |
| 63 | let source = input.to_string(); |
| 64 | |
| 65 | // Parse a string of items to an AST |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 66 | let ast = syn::parse_macro_input(&source).unwrap(); |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 67 | |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 68 | // Build the output, possibly using quasi-quotation |
| 69 | let expanded = quote! { |
| 70 | // ... |
| 71 | }; |
| 72 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 73 | // Parse this back to a token stream and return it |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 74 | expanded.to_string().parse().unwrap() |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 75 | } |
| 76 | ``` |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 77 | |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 78 | ## License |
| 79 | |
| 80 | Licensed under either of |
| 81 | |
| 82 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 83 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 84 | |
| 85 | at your option. |
| 86 | |
| 87 | ### Contribution |
| 88 | |
| 89 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 90 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 91 | be dual licensed as above, without any additional terms or conditions. |