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