blob: 95a5ba9ae41c71cfc78de7863490a5dccc7ed6a7 [file] [log] [blame] [view]
David Tolnay6eacaff2016-10-23 15:20:23 -07001Nom parser for Rust source code
2===============================
David Tolnay35161ff2016-09-03 11:33:15 -07003
David Tolnayac9953b2016-09-07 08:37:12 -07004[![Build Status](https://api.travis-ci.org/dtolnay/syn.svg?branch=master)](https://travis-ci.org/dtolnay/syn)
5[![Latest Version](https://img.shields.io/crates/v/syn.svg)](https://crates.io/crates/syn)
David Tolnay3150dfb2017-02-19 13:00:54 -08006[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://dtolnay.github.io/syn/syn/)
David Tolnayac9953b2016-09-07 08:37:12 -07007
David Tolnayc088adb2018-01-01 00:26:05 -05008Syn is a parsing library for parsing a stream of Rust tokens into a syntax tree
9of Rust source code.
David Tolnay35161ff2016-09-03 11:33:15 -070010
David Tolnayc088adb2018-01-01 00:26:05 -050011Currently this library is geared toward the [custom derive] use case but
12contains some APIs that may be useful for Rust procedural macros more generally.
David Tolnayf939f352016-09-11 18:00:09 -070013
David Tolnayc088adb2018-01-01 00:26:05 -050014[custom derive]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
David Tolnayf939f352016-09-11 18:00:09 -070015
David Tolnayc088adb2018-01-01 00:26:05 -050016- **Data structures** — Syn provides a complete syntax tree that can represent
17 any valid Rust source code. The syntax tree is rooted at [`syn::File`] which
18 represents a full source file, but there are other entry points that may be
19 useful to procedural macros including [`syn::Item`], [`syn::Expr`] and
20 [`syn::Type`].
David Tolnayfb9f7042016-12-22 12:31:39 -050021
David Tolnayc088adb2018-01-01 00:26:05 -050022- **Custom derives** — Of particular interest to custom derives is
23 [`syn::DeriveInput`] which is any of the three legal input items to a derive
24 macro. An example below shows using this type in a library that can derive
25 implementations of a trait of your own.
26
27- **Parser combinators** — Parsing in Syn is built on a suite of public parser
28 combinator macros that you can use for parsing any token-based syntax you
29 dream up within a `functionlike!(...)` procedural macro. Every syntax tree
30 node defined by Syn is individually parsable and may be used as a building
31 block for custom syntaxes, or you may do it all yourself working from the most
32 primitive tokens.
33
34- **Location information** — Every token parsed by Syn is associated with a
35 `Span` that tracks line and column information back to the source of that
36 token. These spans allow a procedural macro to display detailed error messages
37 pointing to all the right places in the user's code. There is an example of
38 this below.
39
40- **Feature flags** — Functionality is aggressively feature gated so your
41 procedural macros enable only what they need, and do not pay in compile time
42 for all the rest.
43
44[`syn::File`]: https://docs.rs/syn/0.12/syn/struct.File.html
45[`syn::Item`]: https://docs.rs/syn/0.12/syn/enum.Item.html
46[`syn::Expr`]: https://docs.rs/syn/0.12/syn/enum.Expr.html
47[`syn::Type`]: https://docs.rs/syn/0.12/syn/enum.Type.html
48[`syn::DeriveInput`]: https://docs.rs/syn/0.12/syn/struct.DeriveInput.html
49
50If you get stuck with anything involving procedural macros in Rust I am happy to
51provide help even if the issue is not related to Syn. Please file a ticket in
52this repo.
53
54*Version requirement: Syn supports any compiler version back to Rust's very
55first support for procedural macros in Rust 1.15.0. Some features especially
56around error reporting are only available in newer compilers or on the nightly
57channel.*
58
59## Example of a custom derive
60
61The canonical custom derive using Syn looks like this. We write an ordinary Rust
62function tagged with a `proc_macro_derive` attribute and the name of the trait
63we are deriving. Any time that derive appears in the user's code, the Rust
64compiler passes their data structure as tokens into our macro. We get to execute
65arbitrary Rust code to figure out what to do with those tokens, then hand some
66tokens back to the compiler to compile into the user's crate.
67
68[`TokenStream`]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html
David Tolnay6c9f5b62016-09-13 15:19:22 -070069
David Tolnay69b538e2016-09-23 19:59:48 -070070```toml
71[dependencies]
David Tolnayc088adb2018-01-01 00:26:05 -050072syn = "0.12"
73quote = "0.4"
David Tolnay69b538e2016-09-23 19:59:48 -070074
75[lib]
David Tolnay45cc8492016-10-08 20:52:03 -070076proc-macro = true
David Tolnay69b538e2016-09-23 19:59:48 -070077```
78
White-Oak82d0db72016-09-13 21:45:58 +030079```rust
David Tolnay45cc8492016-10-08 20:52:03 -070080extern crate proc_macro;
David Tolnayb4c63262016-09-23 20:03:06 -070081extern crate syn;
David Tolnay6c9f5b62016-09-13 15:19:22 -070082
White-Oak82d0db72016-09-13 21:45:58 +030083#[macro_use]
84extern crate quote;
White-Oak82d0db72016-09-13 21:45:58 +030085
David Tolnayc088adb2018-01-01 00:26:05 -050086use proc_macro::TokenStream;
87use syn::DeriveInput;
88
David Tolnay45cc8492016-10-08 20:52:03 -070089#[proc_macro_derive(MyMacro)]
David Tolnayb4c63262016-09-23 20:03:06 -070090pub fn my_macro(input: TokenStream) -> TokenStream {
David Tolnayc088adb2018-01-01 00:26:05 -050091 // Parse the input tokens into a syntax tree
92 let input: DeriveInput = syn::parse(input).unwrap();
White-Oak82d0db72016-09-13 21:45:58 +030093
David Tolnay6c9f5b62016-09-13 15:19:22 -070094 // Build the output, possibly using quasi-quotation
95 let expanded = quote! {
96 // ...
97 };
98
David Tolnayc088adb2018-01-01 00:26:05 -050099 // Hand the output tokens back to the compiler
100 expanded.into()
White-Oak82d0db72016-09-13 21:45:58 +0300101}
102```
David Tolnay6c9f5b62016-09-13 15:19:22 -0700103
David Tolnayc088adb2018-01-01 00:26:05 -0500104The [`heapsize`] example directory shows a complete working Macros 1.1
105implementation of a custom derive. It works on any Rust compiler \>=1.15.0. The
106example derives a `HeapSize` trait which computes an estimate of the amount of
107heap memory owned by a value.
David Tolnayb988b6d2016-10-05 00:12:37 -0700108
David Tolnayc088adb2018-01-01 00:26:05 -0500109[`heapsize`]: examples/heapsize
David Tolnayb988b6d2016-10-05 00:12:37 -0700110
111```rust
David Tolnayc088adb2018-01-01 00:26:05 -0500112pub trait HeapSize {
113 /// Total number of bytes of heap memory owned by `self`.
114 fn heap_size_of_children(&self) -> usize;
David Tolnayb988b6d2016-10-05 00:12:37 -0700115}
116```
117
David Tolnayc088adb2018-01-01 00:26:05 -0500118The custom derive allows users to write `#[derive(HeapSize)]` on data structures
119in their program.
David Tolnayb988b6d2016-10-05 00:12:37 -0700120
121```rust
David Tolnayc088adb2018-01-01 00:26:05 -0500122#[derive(HeapSize)]
123struct Demo<'a, T: ?Sized> {
124 a: Box<T>,
125 b: u8,
126 c: &'a str,
127 d: String,
David Tolnayb988b6d2016-10-05 00:12:37 -0700128}
129```
130
David Tolnayc088adb2018-01-01 00:26:05 -0500131## Spans and error reporting
David Tolnayc2263f32017-03-09 19:20:52 -0800132
David Tolnayc088adb2018-01-01 00:26:05 -0500133The [`heapsize2`] example directory is an extension of the `heapsize` example
134that demonstrates some of the hygiene and error reporting properties of Macros
1352.0. This example currently requires a nightly Rust compiler \>=1.24.0-nightly
136but we are working to stabilize all of the APIs involved.
David Tolnayc2263f32017-03-09 19:20:52 -0800137
David Tolnayc088adb2018-01-01 00:26:05 -0500138[`heapsize2`]: examples/heapsize2
David Tolnay736829a2016-12-22 15:55:53 -0500139
David Tolnayc088adb2018-01-01 00:26:05 -0500140The token-based procedural macro API provides great control over where the
141compiler's error messages are displayed in user code. Consider the error the
142user sees if one of their field types does not implement `HeapSize`.
David Tolnay736829a2016-12-22 15:55:53 -0500143
144```rust
David Tolnayc088adb2018-01-01 00:26:05 -0500145#[derive(HeapSize)]
146struct Broken {
147 ok: String,
148 bad: std::thread::Thread,
David Tolnay736829a2016-12-22 15:55:53 -0500149}
150```
151
David Tolnayc088adb2018-01-01 00:26:05 -0500152In the Macros 1.1 string-based procedural macro world, the resulting error would
153point unhelpfully to the invocation of the derive macro and not to the actual
154problematic field.
155
156```
157error[E0599]: no method named `heap_size_of_children` found for type `std::thread::Thread` in the current scope
158 --> src/main.rs:4:10
159 |
1604 | #[derive(HeapSize)]
161 | ^^^^^^^^
162```
163
164By tracking span information all the way through the expansion of a procedural
165macro as shown in the `heapsize2` example, token-based macros in Syn are able to
166trigger errors that directly pinpoint the source of the problem.
167
168```
169error[E0277]: the trait bound `std::thread::Thread: HeapSize` is not satisfied
170 --> src/main.rs:7:5
171 |
1727 | bad: std::thread::Thread,
173 | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HeapSize` is not implemented for `std::thread::Thread`
174```
175
176## Parsing a custom syntax using combinators
177
178The [`lazy-static`] example directory shows the implementation of a
179`functionlike!(...)` procedural macro in which the input tokens are parsed using
180[`nom`]-style parser combinators.
181
182[`lazy-static`]: examples/lazy-static
183[`nom`]: https://github.com/Geal/nom
184
185The example reimplements the popular `lazy_static` crate from crates.io as a
186procedural macro.
187
188```
189lazy_static! {
190 static ref USERNAME: Regex = Regex::new("^[a-z0-9_-]{3,16}$").unwrap();
191}
192```
193
194The implementation shows how to trigger custom warnings and error messages on
195the macro input.
196
197```
198warning: come on, pick a more creative name
199 --> src/main.rs:10:16
200 |
20110 | static ref FOO: String = "lazy_static".to_owned();
202 | ^^^
203```
204
David Tolnay941c0922016-12-22 16:06:27 -0500205## Debugging
206
207When developing a procedural macro it can be helpful to look at what the
208generated code looks like. Use `cargo rustc -- -Zunstable-options
David Tolnayc088adb2018-01-01 00:26:05 -0500209--pretty=expanded` or the [`cargo expand`] subcommand.
210
211[`cargo expand`]: https://github.com/dtolnay/cargo-expand
David Tolnay941c0922016-12-22 16:06:27 -0500212
213To show the expanded code for some crate that uses your procedural macro, run
214`cargo expand` from that crate. To show the expanded code for one of your own
215test cases, run `cargo expand --test the_test_case` where the last argument is
216the name of the test file without the `.rs` extension.
217
David Tolnay3bfbd542017-01-16 14:57:53 -0800218This write-up by Brandon W Maister discusses debugging in more detail:
David Tolnayc088adb2018-01-01 00:26:05 -0500219[Debugging Rust's new Custom Derive system][debugging].
220
221[debugging]: https://quodlibetor.github.io/posts/debugging-rusts-new-custom-derive-system/
David Tolnay3bfbd542017-01-16 14:57:53 -0800222
David Tolnay686f5042016-10-30 19:24:51 -0700223## Optional features
224
225Syn puts a lot of functionality behind optional features in order to optimize
David Tolnayc088adb2018-01-01 00:26:05 -0500226compile time for the most common use cases. The following features are
227available.
David Tolnay686f5042016-10-30 19:24:51 -0700228
David Tolnayc088adb2018-01-01 00:26:05 -0500229- **`derive`** *(enabled by default)* — Data structures for representing the
230 possible input to a custom derive, including structs and enums and types.
231- **`full`** — Data structures for representing the syntax tree of all valid
232 Rust source code, including items and expressions.
233- **`parsing`** *(enabled by default)* — Ability to parse input tokens into a
234 syntax tree node of a chosen type.
235- **`printing`** *(enabled by default)* — Ability to print a syntax tree node as
236 tokens of Rust source code.
237- **`visit`** — Trait for traversing a syntax tree.
238- **`visit-mut`** — Trait for traversing and mutating in place a syntax tree.
239- **`fold`** — Trait for transforming an owned syntax tree.
240- **`clone-impls`** *(enabled by default)* — Clone impls for all syntax tree
241 types.
242- **`extra-traits`** — Debug, Eq, PartialEq, Hash impls for all syntax tree
243 types.
244
245## Nightly features
246
247By default Syn uses the [`proc-macro2`] crate to emulate the nightly compiler's
248procedural macro API in a stable way that works all the way back to Rust 1.15.0.
249This shim makes it possible to write code without regard for whether the current
250compiler version supports the features we use.
251
252[`proc-macro2`]: https://github.com/alexcrichton/proc-macro2
253
254On a nightly compiler, to eliminate the stable shim and use the compiler's
255`proc-macro` directly, add `proc-macro2` to your Cargo.toml and set its
256`"nightly"` feature which bypasses the stable shim.
257
258```toml
259[dependencies]
260syn = "0.12"
261proc-macro2 = { version = "0.2", features = ["nightly"] }
262```
David Tolnayed7a5082016-10-30 20:06:29 -0700263
David Tolnay35161ff2016-09-03 11:33:15 -0700264## License
265
266Licensed under either of
267
268 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
269 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
270
271at your option.
272
273### Contribution
274
275Unless you explicitly state otherwise, any contribution intentionally submitted
276for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
277be dual licensed as above, without any additional terms or conditions.