David Tolnay | 6eacaff | 2016-10-23 15:20:23 -0700 | [diff] [blame] | 1 | Nom parser for Rust source code |
| 2 | =============================== |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 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 | 3c0c8c5 | 2016-10-30 20:20:46 -0700 | [diff] [blame^] | 20 | syn = "0.10" |
David Tolnay | f8f4307 | 2016-10-08 14:58:05 -0700 | [diff] [blame] | 21 | quote = "0.3" |
David Tolnay | 69b538e | 2016-09-23 19:59:48 -0700 | [diff] [blame] | 22 | |
| 23 | [lib] |
David Tolnay | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 24 | proc-macro = true |
David Tolnay | 69b538e | 2016-09-23 19:59:48 -0700 | [diff] [blame] | 25 | ``` |
| 26 | |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 27 | ```rust |
David Tolnay | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 28 | #![feature(proc_macro, proc_macro_lib)] |
White-Oak | 82d0db7 | 2016-09-13 21:45:58 +0300 | [diff] [blame] | 29 | |
David Tolnay | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 30 | extern crate proc_macro; |
| 31 | use proc_macro::TokenStream; |
David Tolnay | b4c6326 | 2016-09-23 20:03:06 -0700 | [diff] [blame] | 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 | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 38 | #[proc_macro_derive(MyMacro)] |
David Tolnay | b4c6326 | 2016-09-23 20:03:06 -0700 | [diff] [blame] | 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 | |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 42 | // Parse the string representation 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 | |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 50 | // Parse 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 | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 55 | ## Complete example |
| 56 | |
| 57 | Suppose we have the following simple trait which returns the number of fields in |
| 58 | a struct: |
| 59 | |
| 60 | ```rust |
| 61 | trait NumFields { |
| 62 | fn num_fields() -> usize; |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | A complete Macros 1.1 implementation of `#[derive(NumFields)]` based on `syn` |
| 67 | and [`quote`](https://github.com/dtolnay/quote) looks like this: |
| 68 | |
| 69 | ```rust |
David Tolnay | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 70 | #![feature(proc_macro, proc_macro_lib)] |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 71 | |
David Tolnay | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 72 | extern crate proc_macro; |
| 73 | use proc_macro::TokenStream; |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 74 | |
| 75 | extern crate syn; |
| 76 | |
| 77 | #[macro_use] |
| 78 | extern crate quote; |
| 79 | |
David Tolnay | 45cc849 | 2016-10-08 20:52:03 -0700 | [diff] [blame] | 80 | #[proc_macro_derive(NumFields)] |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 81 | pub fn num_fields(input: TokenStream) -> TokenStream { |
| 82 | let source = input.to_string(); |
| 83 | |
| 84 | // Parse the string representation to an AST |
| 85 | let ast = syn::parse_macro_input(&source).unwrap(); |
| 86 | |
| 87 | // Build the output |
David Tolnay | ed7a508 | 2016-10-30 20:06:29 -0700 | [diff] [blame] | 88 | let expanded = expand_num_fields(&ast); |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 89 | |
David Tolnay | ed7a508 | 2016-10-30 20:06:29 -0700 | [diff] [blame] | 90 | // Return the original input struct unmodified, and the |
| 91 | // generated impl along with it |
| 92 | quote!(#ast #expanded).to_string().parse().unwrap() |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David Tolnay | ed7a508 | 2016-10-30 20:06:29 -0700 | [diff] [blame] | 95 | fn expand_num_fields(ast: &syn::MacroInput) -> quote::Tokens { |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 96 | let n = match ast.body { |
| 97 | syn::Body::Struct(ref data) => data.fields().len(), |
| 98 | syn::Body::Enum(_) => panic!("#[derive(NumFields)] can only be used with structs"), |
| 99 | }; |
| 100 | |
| 101 | // Used in the quasi-quotation below as `#name` |
| 102 | let name = &ast.ident; |
| 103 | |
| 104 | // Helper is provided for handling complex generic types correctly and effortlessly |
| 105 | let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl(); |
| 106 | |
| 107 | quote! { |
David Tolnay | b988b6d | 2016-10-05 00:12:37 -0700 | [diff] [blame] | 108 | // The generated impl |
| 109 | impl #impl_generics ::mycrate::NumFields for #name #ty_generics #where_clause { |
| 110 | fn num_fields() -> usize { |
| 111 | #n |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | ``` |
| 117 | |
David Tolnay | 686f504 | 2016-10-30 19:24:51 -0700 | [diff] [blame] | 118 | ## Optional features |
| 119 | |
| 120 | Syn puts a lot of functionality behind optional features in order to optimize |
| 121 | compile time for the most common use cases. These are the available features and |
| 122 | their effect on compile time. Dependencies are included in the compile times. |
| 123 | |
| 124 | Features | Compile time | Functionality |
| 125 | --- | --- | --- |
| 126 | *(none)* | 1 sec | The data structures representing the AST of Rust structs, enums, and types. |
| 127 | parsing | 4 sec | Parsing Rust source code containing structs and enums into an AST. |
| 128 | printing | 2 sec | Printing an AST of structs and enums as Rust source code. |
| 129 | **parsing, printing** | **4 sec** | **This is the default.** Parsing and printing of Rust structs and enums. This is typically what you want for implementing Macros 1.1 custom derives. |
| 130 | full | 2 sec | The data structures representing the full AST of all possible Rust code. |
| 131 | full, parsing | 7 sec | Parsing any valid Rust source code to an AST. |
| 132 | full, printing | 4 sec | Turning an AST into Rust source code. |
| 133 | full, parsing, printing | 8 sec | Parsing and printing any Rust syntax. |
| 134 | full, parsing, printing, expand | 9 sec | Expansion of custom derives in a file of Rust code. This is typically what you want for expanding custom derives on stable Rust using a build script. |
| 135 | full, parsing, printing, expand, pretty | 60 sec | Expansion of custom derives with pretty-printed output. This is what you want when iterating on or debugging a custom derive, but the pretty printing should be disabled once you get everything working. |
| 136 | |
David Tolnay | ed7a508 | 2016-10-30 20:06:29 -0700 | [diff] [blame] | 137 | ## Custom derives on stable Rust |
| 138 | |
| 139 | Syn supports a way of expanding custom derives from a build script, similar to |
| 140 | what [Serde is able to do with serde_codegen](https://serde.rs/codegen-stable.html). |
| 141 | The advantage of using Syn for this purpose rather than Syntex is much faster |
| 142 | compile time. |
| 143 | |
| 144 | Continuing with the `NumFields` example from above, it can be extended to |
| 145 | support stable Rust like this. One or more custom derives are added to a |
| 146 | [`Registry`](https://dtolnay.github.io/syn/syn/struct.Registry.html), which is |
| 147 | then able to expand those derives in a source file at a particular path and |
| 148 | write the expanded output to a different path. A custom derive is represented by |
| 149 | the [`CustomDerive`](https://dtolnay.github.io/syn/syn/trait.CustomDerive.html) |
| 150 | trait which takes a [`MacroInput`](https://dtolnay.github.io/syn/syn/struct.MacroInput.html) |
| 151 | (either a struct or an enum) and [expands it](https://dtolnay.github.io/syn/syn/struct.Expanded.html) |
| 152 | into zero or more new items and maybe a modified or unmodified instance of the |
| 153 | original input. |
| 154 | |
| 155 | ```rust |
| 156 | pub fn expand_file<S, D>(src: S, dst: D) -> Result<(), String> |
| 157 | where S: AsRef<Path>, |
| 158 | D: AsRef<Path> |
| 159 | { |
| 160 | let mut registry = syn::Registry::new(); |
| 161 | registry.add_derive("NumFields", |input| { |
| 162 | let tokens = expand_num_fields(&input); |
| 163 | let items = syn::parse_items(&tokens.to_string()).unwrap(); |
| 164 | Ok(syn::Expanded { |
| 165 | new_items: items, |
| 166 | original: Some(input), |
| 167 | }) |
| 168 | }); |
| 169 | registry.expand_file(src, dst) |
| 170 | } |
| 171 | ``` |
| 172 | |
| 173 | The codegen can be invoked from a build script as follows. |
| 174 | |
| 175 | ```rust |
| 176 | extern crate your_codegen; |
| 177 | |
| 178 | use std::env; |
| 179 | use std::path::Path; |
| 180 | |
| 181 | fn main() { |
| 182 | let out_dir = env::var_os("OUT_DIR").unwrap(); |
| 183 | |
| 184 | let src = Path::new("src/codegen_types.in.rs"); |
| 185 | let dst = Path::new(&out_dir).join("codegen_types.rs"); |
| 186 | |
| 187 | your_codegen::expand_file(&src, &dst).unwrap(); |
| 188 | } |
| 189 | ``` |
| 190 | |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 191 | ## License |
| 192 | |
| 193 | Licensed under either of |
| 194 | |
| 195 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 196 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 197 | |
| 198 | at your option. |
| 199 | |
| 200 | ### Contribution |
| 201 | |
| 202 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 203 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 204 | be dual licensed as above, without any additional terms or conditions. |