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 | ed8e23e | 2016-09-27 08:18:22 -0700 | [diff] [blame] | 16 | ## Usage with Macros 1.1 |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 17 | |
David Tolnay | 69b538e | 2016-09-23 19:59:48 -0700 | [diff] [blame] | 18 | ```toml |
| 19 | [dependencies] |
David Tolnay | a3faf25 | 2016-09-27 09:52:30 -0700 | [diff] [blame] | 20 | syn = "0.8" |
| 21 | quote = "0.2" |
David Tolnay | 69b538e | 2016-09-23 19:59:48 -0700 | [diff] [blame] | 22 | |
| 23 | [lib] |
| 24 | rustc-macro = true |
| 25 | ``` |
| 26 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 27 | ```rust |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 28 | #![feature(rustc_macro, rustc_macro_lib)] |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 29 | |
| 30 | extern crate rustc_macro; |
David Tolnay | b4c6326 | 2016-09-23 20:03:06 -0700 | [diff] [blame] | 31 | use rustc_macro::TokenStream; |
| 32 | |
| 33 | extern crate syn; |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 34 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 35 | #[macro_use] |
| 36 | extern crate quote; |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 37 | |
David Tolnay | b4c6326 | 2016-09-23 20:03:06 -0700 | [diff] [blame] | 38 | #[rustc_macro_derive(MyMacro)] |
| 39 | pub fn my_macro(input: TokenStream) -> TokenStream { |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 40 | let source = input.to_string(); |
| 41 | |
| 42 | // Parse a string of items to an AST |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 43 | let ast = syn::parse_macro_input(&source).unwrap(); |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 44 | |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 45 | // Build the output, possibly using quasi-quotation |
| 46 | let expanded = quote! { |
| 47 | // ... |
| 48 | }; |
| 49 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 50 | // Parse this back to a token stream and return it |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 51 | expanded.to_string().parse().unwrap() |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 52 | } |
| 53 | ``` |
David Tolnay | 6c9f5b6 | 2016-09-13 15:19:22 -0700 | [diff] [blame] | 54 | |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 55 | ## License |
| 56 | |
| 57 | Licensed under either of |
| 58 | |
| 59 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 60 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 61 | |
| 62 | at your option. |
| 63 | |
| 64 | ### Contribution |
| 65 | |
| 66 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 67 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 68 | be dual licensed as above, without any additional terms or conditions. |