blob: f3b13a1d71981762d184b4a9d666d661a99b1843 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnay5e84e972018-01-05 17:51:06 -08009//! Syn is a parsing library for parsing a stream of Rust tokens into a syntax
10//! tree of Rust source code.
11//!
12//! Currently this library is geared toward the [custom derive] use case but
13//! contains some APIs that may be useful for Rust procedural macros more
14//! generally.
15//!
16//! [custom derive]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
17//!
18//! - **Data structures** — Syn provides a complete syntax tree that can
19//! represent any valid Rust source code. The syntax tree is rooted at
20//! [`syn::File`] which represents a full source file, but there are other
21//! entry points that may be useful to procedural macros including
22//! [`syn::Item`], [`syn::Expr`] and [`syn::Type`].
23//!
24//! - **Custom derives** — Of particular interest to custom derives is
25//! [`syn::DeriveInput`] which is any of the three legal input items to a
26//! derive macro. An example below shows using this type in a library that can
27//! derive implementations of a trait of your own.
28//!
29//! - **Parser combinators** — Parsing in Syn is built on a suite of public
30//! parser combinator macros that you can use for parsing any token-based
31//! syntax you dream up within a `functionlike!(...)` procedural macro. Every
32//! syntax tree node defined by Syn is individually parsable and may be used
33//! as a building block for custom syntaxes, or you may do it all yourself
34//! working from the most primitive tokens.
35//!
36//! - **Location information** — Every token parsed by Syn is associated with a
37//! `Span` that tracks line and column information back to the source of that
38//! token. These spans allow a procedural macro to display detailed error
39//! messages pointing to all the right places in the user's code. There is an
40//! example of this below.
41//!
42//! - **Feature flags** — Functionality is aggressively feature gated so your
43//! procedural macros enable only what they need, and do not pay in compile
44//! time for all the rest.
45//!
46//! [`syn::File`]: struct.File.html
47//! [`syn::Item`]: enum.Item.html
48//! [`syn::Expr`]: enum.Expr.html
49//! [`syn::Type`]: enum.Type.html
50//! [`syn::DeriveInput`]: struct.DeriveInput.html
51//!
52//! *Version requirement: Syn supports any compiler version back to Rust's very
53//! first support for procedural macros in Rust 1.15.0. Some features especially
54//! around error reporting are only available in newer compilers or on the
55//! nightly channel.*
56//!
57//! ## Example of a custom derive
58//!
59//! The canonical custom derive using Syn looks like this. We write an ordinary
60//! Rust function tagged with a `proc_macro_derive` attribute and the name of
61//! the trait we are deriving. Any time that derive appears in the user's code,
62//! the Rust compiler passes their data structure as tokens into our macro. We
63//! get to execute arbitrary Rust code to figure out what to do with those
64//! tokens, then hand some tokens back to the compiler to compile into the
65//! user's crate.
66//!
67//! [`TokenStream`]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html
68//!
69//! ```toml
70//! [dependencies]
David Tolnay87003d02018-05-20 19:45:13 -070071//! syn = "0.14"
72//! quote = "0.6"
David Tolnay5e84e972018-01-05 17:51:06 -080073//!
74//! [lib]
75//! proc-macro = true
76//! ```
77//!
78//! ```rust
79//! extern crate proc_macro;
80//! extern crate syn;
81//!
82//! #[macro_use]
83//! extern crate quote;
84//!
85//! use proc_macro::TokenStream;
86//! use syn::DeriveInput;
87//!
88//! # const IGNORE_TOKENS: &str = stringify! {
89//! #[proc_macro_derive(MyMacro)]
90//! # };
91//! pub fn my_macro(input: TokenStream) -> TokenStream {
92//! // Parse the input tokens into a syntax tree
93//! let input: DeriveInput = syn::parse(input).unwrap();
94//!
95//! // Build the output, possibly using quasi-quotation
96//! let expanded = quote! {
97//! // ...
98//! };
99//!
100//! // Hand the output tokens back to the compiler
101//! expanded.into()
102//! }
103//! #
104//! # fn main() {}
105//! ```
106//!
107//! The [`heapsize`] example directory shows a complete working Macros 1.1
108//! implementation of a custom derive. It works on any Rust compiler \>=1.15.0.
109//! The example derives a `HeapSize` trait which computes an estimate of the
110//! amount of heap memory owned by a value.
111//!
112//! [`heapsize`]: https://github.com/dtolnay/syn/tree/master/examples/heapsize
113//!
114//! ```rust
115//! pub trait HeapSize {
116//! /// Total number of bytes of heap memory owned by `self`.
117//! fn heap_size_of_children(&self) -> usize;
118//! }
119//! ```
120//!
121//! The custom derive allows users to write `#[derive(HeapSize)]` on data
122//! structures in their program.
123//!
124//! ```rust
125//! # const IGNORE_TOKENS: &str = stringify! {
126//! #[derive(HeapSize)]
127//! # };
128//! struct Demo<'a, T: ?Sized> {
129//! a: Box<T>,
130//! b: u8,
131//! c: &'a str,
132//! d: String,
133//! }
134//! ```
135//!
136//! ## Spans and error reporting
137//!
138//! The [`heapsize2`] example directory is an extension of the `heapsize`
139//! example that demonstrates some of the hygiene and error reporting properties
140//! of Macros 2.0. This example currently requires a nightly Rust compiler
141//! \>=1.24.0-nightly but we are working to stabilize all of the APIs involved.
142//!
143//! [`heapsize2`]: https://github.com/dtolnay/syn/tree/master/examples/heapsize2
144//!
145//! The token-based procedural macro API provides great control over where the
146//! compiler's error messages are displayed in user code. Consider the error the
147//! user sees if one of their field types does not implement `HeapSize`.
148//!
149//! ```rust
150//! # const IGNORE_TOKENS: &str = stringify! {
151//! #[derive(HeapSize)]
152//! # };
153//! struct Broken {
154//! ok: String,
155//! bad: std::thread::Thread,
156//! }
157//! ```
158//!
159//! In the Macros 1.1 string-based procedural macro world, the resulting error
160//! would point unhelpfully to the invocation of the derive macro and not to the
161//! actual problematic field.
162//!
163//! ```text
164//! error[E0599]: no method named `heap_size_of_children` found for type `std::thread::Thread` in the current scope
165//! --> src/main.rs:4:10
166//! |
167//! 4 | #[derive(HeapSize)]
168//! | ^^^^^^^^
169//! ```
170//!
171//! By tracking span information all the way through the expansion of a
172//! procedural macro as shown in the `heapsize2` example, token-based macros in
173//! Syn are able to trigger errors that directly pinpoint the source of the
174//! problem.
175//!
176//! ```text
177//! error[E0277]: the trait bound `std::thread::Thread: HeapSize` is not satisfied
178//! --> src/main.rs:7:5
179//! |
180//! 7 | bad: std::thread::Thread,
David Tolnayefff2ff2018-01-07 11:49:52 -0800181//! | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HeapSize` is not implemented for `Thread`
David Tolnay5e84e972018-01-05 17:51:06 -0800182//! ```
183//!
184//! ## Parsing a custom syntax using combinators
185//!
186//! The [`lazy-static`] example directory shows the implementation of a
187//! `functionlike!(...)` procedural macro in which the input tokens are parsed
188//! using [`nom`]-style parser combinators.
189//!
190//! [`lazy-static`]: https://github.com/dtolnay/syn/tree/master/examples/lazy-static
191//! [`nom`]: https://github.com/Geal/nom
192//!
193//! The example reimplements the popular `lazy_static` crate from crates.io as a
194//! procedural macro.
195//!
196//! ```
197//! # macro_rules! lazy_static {
198//! # ($($tt:tt)*) => {}
199//! # }
200//! #
201//! lazy_static! {
202//! static ref USERNAME: Regex = Regex::new("^[a-z0-9_-]{3,16}$").unwrap();
203//! }
204//! ```
205//!
206//! The implementation shows how to trigger custom warnings and error messages
207//! on the macro input.
208//!
209//! ```text
210//! warning: come on, pick a more creative name
211//! --> src/main.rs:10:16
212//! |
213//! 10 | static ref FOO: String = "lazy_static".to_owned();
214//! | ^^^
215//! ```
216//!
217//! ## Debugging
218//!
219//! When developing a procedural macro it can be helpful to look at what the
220//! generated code looks like. Use `cargo rustc -- -Zunstable-options
221//! --pretty=expanded` or the [`cargo expand`] subcommand.
222//!
David Tolnay324db2d2018-01-07 11:51:09 -0800223//! [`cargo expand`]: https://github.com/dtolnay/cargo-expand
David Tolnay5e84e972018-01-05 17:51:06 -0800224//!
225//! To show the expanded code for some crate that uses your procedural macro,
226//! run `cargo expand` from that crate. To show the expanded code for one of
227//! your own test cases, run `cargo expand --test the_test_case` where the last
228//! argument is the name of the test file without the `.rs` extension.
229//!
230//! This write-up by Brandon W Maister discusses debugging in more detail:
231//! [Debugging Rust's new Custom Derive system][debugging].
232//!
233//! [debugging]: https://quodlibetor.github.io/posts/debugging-rusts-new-custom-derive-system/
234//!
235//! ## Optional features
236//!
237//! Syn puts a lot of functionality behind optional features in order to
238//! optimize compile time for the most common use cases. The following features
239//! are available.
240//!
241//! - **`derive`** *(enabled by default)* — Data structures for representing the
242//! possible input to a custom derive, including structs and enums and types.
243//! - **`full`** — Data structures for representing the syntax tree of all valid
244//! Rust source code, including items and expressions.
245//! - **`parsing`** *(enabled by default)* — Ability to parse input tokens into
246//! a syntax tree node of a chosen type.
247//! - **`printing`** *(enabled by default)* — Ability to print a syntax tree
248//! node as tokens of Rust source code.
249//! - **`visit`** — Trait for traversing a syntax tree.
David Tolnay34981cf2018-01-06 16:22:35 -0800250//! - **`visit-mut`** — Trait for traversing and mutating in place a syntax
David Tolnay5e84e972018-01-05 17:51:06 -0800251//! tree.
252//! - **`fold`** — Trait for transforming an owned syntax tree.
253//! - **`clone-impls`** *(enabled by default)* — Clone impls for all syntax tree
254//! types.
255//! - **`extra-traits`** — Debug, Eq, PartialEq, Hash impls for all syntax tree
256//! types.
hcpl4b72a382018-04-04 14:50:24 +0300257//! - **`proc-macro`** *(enabled by default)* — Runtime dependency on the
258//! dynamic library libproc_macro from rustc toolchain.
David Tolnay5e84e972018-01-05 17:51:06 -0800259
David Tolnay4cf7db82018-01-07 15:22:01 -0800260// Syn types in rustdoc of other crates get linked to here.
David Tolnaye6ec9e02018-08-21 21:32:03 -0400261#![doc(html_root_url = "https://docs.rs/syn/0.14.9")]
David Tolnay34071ba2018-05-20 20:00:41 -0700262#![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
David Tolnay34071ba2018-05-20 20:00:41 -0700263// Ignored clippy lints.
David Tolnay94d2b792018-04-29 12:26:10 -0700264#![cfg_attr(
265 feature = "cargo-clippy",
266 allow(
David Tolnay0cec3e62018-07-21 09:08:30 -0700267 const_static_lifetime,
David Tolnay24b079d2018-08-27 08:28:10 -0700268 cyclomatic_complexity,
David Tolnay0cec3e62018-07-21 09:08:30 -0700269 doc_markdown,
David Tolnay24b079d2018-08-27 08:28:10 -0700270 eval_order_dependence,
David Tolnay0cec3e62018-07-21 09:08:30 -0700271 large_enum_variant,
272 match_bool,
273 redundant_closure,
274 needless_pass_by_value,
275 redundant_field_names,
276 trivially_copy_pass_by_ref
David Tolnay94d2b792018-04-29 12:26:10 -0700277 )
278)]
David Tolnay34071ba2018-05-20 20:00:41 -0700279// Ignored clippy_pedantic lints.
280#![cfg_attr(
281 feature = "cargo-clippy",
282 allow(
David Tolnay0cec3e62018-07-21 09:08:30 -0700283 cast_possible_truncation,
284 cast_possible_wrap,
David Tolnayb1617752018-08-24 21:16:33 -0400285 empty_enum,
David Tolnay0cec3e62018-07-21 09:08:30 -0700286 if_not_else,
287 indexing_slicing,
288 items_after_statements,
David Tolnay151f92f2018-08-14 22:44:53 -0700289 shadow_unrelated,
David Tolnay0cec3e62018-07-21 09:08:30 -0700290 similar_names,
291 single_match_else,
292 stutter,
293 unseparated_literal_suffix,
294 use_self,
295 used_underscore_binding
David Tolnay34071ba2018-05-20 20:00:41 -0700296 )
297)]
David Tolnayad2836d2017-04-20 10:11:43 -0700298
David Tolnay278f9e32018-08-14 22:41:11 -0700299#[cfg(all(
300 not(all(target_arch = "wasm32", target_os = "unknown")),
301 feature = "proc-macro"
302))]
David Tolnay51382052017-12-27 13:46:21 -0500303extern crate proc_macro;
David Tolnay94d2b792018-04-29 12:26:10 -0700304extern crate proc_macro2;
David Tolnay570695e2017-06-03 16:15:13 -0700305extern crate unicode_xid;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700306
David Tolnay1cf80912017-12-31 18:35:12 -0500307#[cfg(feature = "printing")]
David Tolnay87d0b442016-09-04 11:52:12 -0700308extern crate quote;
309
David Tolnay1b752fb2017-12-26 21:41:39 -0500310#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800311#[macro_use]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500312#[doc(hidden)]
313pub mod parsers;
David Tolnay35161ff2016-09-03 11:33:15 -0700314
Alex Crichton62a0a592017-05-22 13:58:53 -0700315#[macro_use]
316mod macros;
317
David Tolnay852bff72018-08-27 08:24:02 -0700318#[cfg(feature = "parsing")]
319#[macro_use]
320mod group;
321
David Tolnayc5ab8c62017-12-26 16:43:39 -0500322#[macro_use]
David Tolnay32954ef2017-12-26 22:43:16 -0500323pub mod token;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500324
David Tolnay4fb71232018-08-25 23:14:50 -0400325mod ident;
326pub use ident::Ident;
David Tolnaye303b7c2018-05-20 16:46:35 -0700327
David Tolnay3cfd1d32018-01-03 00:22:08 -0800328#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb79ee962016-09-04 09:39:20 -0700329mod attr;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800330#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayaaadd782018-01-06 22:58:13 -0800331pub use attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue, NestedMeta};
David Tolnay35161ff2016-09-03 11:33:15 -0700332
David Tolnay3cfd1d32018-01-03 00:22:08 -0800333#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf38cdf62016-09-23 19:07:09 -0700334mod data;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800335#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700336pub use data::{
337 Field, Fields, FieldsNamed, FieldsUnnamed, Variant, VisCrate, VisPublic, VisRestricted,
338 Visibility,
339};
David Tolnayf38cdf62016-09-23 19:07:09 -0700340
David Tolnay3cfd1d32018-01-03 00:22:08 -0800341#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf4bbbd92016-09-23 14:41:55 -0700342mod expr;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800343#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700344pub use expr::{
David Tolnay02a9c6f2018-08-24 18:58:45 -0400345 Expr, ExprArray, ExprAssign, ExprAssignOp, ExprAsync, ExprBinary, ExprBlock, ExprBox,
346 ExprBreak, ExprCall, ExprCast, ExprClosure, ExprContinue, ExprField, ExprForLoop, ExprGroup,
347 ExprIf, ExprIfLet, ExprInPlace, ExprIndex, ExprLit, ExprLoop, ExprMacro, ExprMatch,
348 ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn,
349 ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprVerbatim,
350 ExprWhile, ExprWhileLet, ExprYield, Index, Member,
David Tolnayb57c8492018-05-05 00:32:04 -0700351};
Michael Layzell734adb42017-06-07 16:58:31 -0400352
353#[cfg(feature = "full")]
David Tolnayb57c8492018-05-05 00:32:04 -0700354pub use expr::{
355 Arm, Block, FieldPat, FieldValue, GenericMethodArgument, Label, Local, MethodTurbofish, Pat,
356 PatBox, PatIdent, PatLit, PatMacro, PatPath, PatRange, PatRef, PatSlice, PatStruct, PatTuple,
357 PatTupleStruct, PatVerbatim, PatWild, RangeLimits, Stmt,
358};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700359
David Tolnay3cfd1d32018-01-03 00:22:08 -0800360#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb79ee962016-09-04 09:39:20 -0700361mod generics;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800362#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700363pub use generics::{
364 BoundLifetimes, ConstParam, GenericParam, Generics, LifetimeDef, PredicateEq,
365 PredicateLifetime, PredicateType, TraitBound, TraitBoundModifier, TypeParam, TypeParamBound,
366 WhereClause, WherePredicate,
367};
David Tolnay278f9e32018-08-14 22:41:11 -0700368#[cfg(all(
369 any(feature = "full", feature = "derive"),
370 feature = "printing"
371))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800372pub use generics::{ImplGenerics, Turbofish, TypeGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -0700373
David Tolnayf38cdf62016-09-23 19:07:09 -0700374#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -0700375mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -0700376#[cfg(feature = "full")]
David Tolnayb57c8492018-05-05 00:32:04 -0700377pub use item::{
David Tolnay435c1782018-08-24 16:15:44 -0400378 ArgCaptured, ArgSelf, ArgSelfRef, FnArg, FnDecl, ForeignItem, ForeignItemFn, ForeignItemMacro,
379 ForeignItemStatic, ForeignItemType, ForeignItemVerbatim, ImplItem, ImplItemConst,
David Tolnaybb82ef02018-08-24 20:15:45 -0400380 ImplItemExistential, ImplItemMacro, ImplItemMethod, ImplItemType, ImplItemVerbatim, Item,
381 ItemConst, ItemEnum, ItemExistential, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl,
382 ItemMacro, ItemMacro2, ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemType, ItemUnion,
David Tolnay3779bb72018-08-26 18:46:07 -0700383 ItemUse, ItemVerbatim, MethodSig, TraitItem, TraitItemConst, TraitItemMacro, TraitItemMethod,
384 TraitItemType, TraitItemVerbatim, UseGlob, UseGroup, UseName, UsePath, UseRename, UseTree,
David Tolnayb57c8492018-05-05 00:32:04 -0700385};
David Tolnay35161ff2016-09-03 11:33:15 -0700386
David Tolnay631cb8c2016-11-10 17:16:41 -0800387#[cfg(feature = "full")]
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700388mod file;
David Tolnay631cb8c2016-11-10 17:16:41 -0800389#[cfg(feature = "full")]
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700390pub use file::File;
David Tolnay631cb8c2016-11-10 17:16:41 -0800391
David Tolnay3cfd1d32018-01-03 00:22:08 -0800392#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay63e3dee2017-06-03 20:13:17 -0700393mod lifetime;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800394#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay63e3dee2017-06-03 20:13:17 -0700395pub use lifetime::Lifetime;
396
David Tolnay3cfd1d32018-01-03 00:22:08 -0800397#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf4bbbd92016-09-23 14:41:55 -0700398mod lit;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800399#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700400pub use lit::{
401 FloatSuffix, IntSuffix, Lit, LitBool, LitByte, LitByteStr, LitChar, LitFloat, LitInt, LitStr,
402 LitVerbatim, StrStyle,
403};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700404
David Tolnay3cfd1d32018-01-03 00:22:08 -0800405#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf4bbbd92016-09-23 14:41:55 -0700406mod mac;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800407#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayab919512017-12-30 23:31:51 -0500408pub use mac::{Macro, MacroDelimiter};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700409
David Tolnay3cfd1d32018-01-03 00:22:08 -0800410#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay0e837402016-12-22 17:25:55 -0500411mod derive;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800412#[cfg(feature = "derive")]
David Tolnaye3d41b72017-12-31 15:24:00 -0500413pub use derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput};
David Tolnayf38cdf62016-09-23 19:07:09 -0700414
David Tolnay3cfd1d32018-01-03 00:22:08 -0800415#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay3cb23a92016-10-07 23:02:21 -0700416mod op;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800417#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay3cb23a92016-10-07 23:02:21 -0700418pub use op::{BinOp, UnOp};
419
David Tolnay3cfd1d32018-01-03 00:22:08 -0800420#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb79ee962016-09-04 09:39:20 -0700421mod ty;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800422#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700423pub use ty::{
424 Abi, BareFnArg, BareFnArgName, ReturnType, Type, TypeArray, TypeBareFn, TypeGroup,
425 TypeImplTrait, TypeInfer, TypeMacro, TypeNever, TypeParen, TypePath, TypePtr, TypeReference,
426 TypeSlice, TypeTraitObject, TypeTuple, TypeVerbatim,
427};
David Tolnay056de302018-01-05 14:29:05 -0800428
429#[cfg(any(feature = "full", feature = "derive"))]
430mod path;
David Tolnay278f9e32018-08-14 22:41:11 -0700431#[cfg(all(
432 any(feature = "full", feature = "derive"),
433 feature = "printing"
434))]
David Tolnay94d2b792018-04-29 12:26:10 -0700435pub use path::PathTokens;
David Tolnay056de302018-01-05 14:29:05 -0800436#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700437pub use path::{
438 AngleBracketedGenericArguments, Binding, GenericArgument, ParenthesizedGenericArguments, Path,
439 PathArguments, PathSegment, QSelf,
440};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700441
David Tolnay1b752fb2017-12-26 21:41:39 -0500442#[cfg(feature = "parsing")]
David Tolnaydfc886b2018-01-06 08:03:09 -0800443pub mod buffer;
David Tolnay94d2b792018-04-29 12:26:10 -0700444pub mod punctuated;
David Tolnay1b752fb2017-12-26 21:41:39 -0500445#[cfg(feature = "parsing")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500446pub mod synom;
David Tolnay3779bb72018-08-26 18:46:07 -0700447#[cfg(all(
448 any(feature = "full", feature = "derive"),
449 feature = "extra-traits"
450))]
David Tolnaye0824032017-12-27 15:25:56 -0500451mod tt;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500452
David Tolnaye83ef5a2018-01-11 15:18:36 -0800453// Not public API except the `parse_quote!` macro.
David Tolnay491680a2018-01-23 00:34:40 -0800454#[cfg(feature = "parsing")]
David Tolnaye83ef5a2018-01-11 15:18:36 -0800455#[doc(hidden)]
456pub mod parse_quote;
457
David Tolnay4d942b42018-01-02 22:14:04 -0800458#[cfg(all(feature = "parsing", feature = "printing"))]
David Tolnayf790b612017-12-31 18:46:57 -0500459pub mod spanned;
460
David Tolnaydc3d6242018-08-01 00:30:47 -0700461#[cfg(all(feature = "parsing", feature = "full"))]
David Tolnay9be32582018-07-31 22:37:26 -0700462mod verbatim;
463
Nika Layzella6f46c42017-10-26 15:26:16 -0400464mod gen {
David Tolnayded2d682018-01-06 18:53:53 -0800465 /// Syntax tree traversal to walk a shared borrow of a syntax tree.
466 ///
467 /// Each method of the [`Visit`] trait is a hook that can be overridden to
468 /// customize the behavior when visiting the corresponding type of node. By
469 /// default, every method recursively visits the substructure of the input
470 /// by invoking the right visitor method of each of its fields.
471 ///
472 /// [`Visit`]: trait.Visit.html
473 ///
474 /// ```rust
475 /// # use syn::{Attribute, BinOp, Expr, ExprBinary};
476 /// #
477 /// pub trait Visit<'ast> {
478 /// /* ... */
479 ///
480 /// fn visit_expr_binary(&mut self, node: &'ast ExprBinary) {
481 /// for attr in &node.attrs {
482 /// self.visit_attribute(attr);
483 /// }
484 /// self.visit_expr(&*node.left);
485 /// self.visit_bin_op(&node.op);
486 /// self.visit_expr(&*node.right);
487 /// }
488 ///
489 /// /* ... */
490 /// # fn visit_attribute(&mut self, node: &'ast Attribute);
491 /// # fn visit_expr(&mut self, node: &'ast Expr);
492 /// # fn visit_bin_op(&mut self, node: &'ast BinOp);
493 /// }
494 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800495 ///
496 /// *This module is available if Syn is built with the `"visit"` feature.*
Nika Layzella6f46c42017-10-26 15:26:16 -0400497 #[cfg(feature = "visit")]
498 pub mod visit;
David Tolnay55337722016-09-11 12:58:56 -0700499
David Tolnayded2d682018-01-06 18:53:53 -0800500 /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in
501 /// place.
502 ///
503 /// Each method of the [`VisitMut`] trait is a hook that can be overridden
504 /// to customize the behavior when mutating the corresponding type of node.
505 /// By default, every method recursively visits the substructure of the
506 /// input by invoking the right visitor method of each of its fields.
507 ///
508 /// [`VisitMut`]: trait.VisitMut.html
509 ///
510 /// ```rust
511 /// # use syn::{Attribute, BinOp, Expr, ExprBinary};
512 /// #
513 /// pub trait VisitMut {
514 /// /* ... */
515 ///
516 /// fn visit_expr_binary_mut(&mut self, node: &mut ExprBinary) {
517 /// for attr in &mut node.attrs {
518 /// self.visit_attribute_mut(attr);
519 /// }
520 /// self.visit_expr_mut(&mut *node.left);
521 /// self.visit_bin_op_mut(&mut node.op);
522 /// self.visit_expr_mut(&mut *node.right);
523 /// }
524 ///
525 /// /* ... */
526 /// # fn visit_attribute_mut(&mut self, node: &mut Attribute);
527 /// # fn visit_expr_mut(&mut self, node: &mut Expr);
528 /// # fn visit_bin_op_mut(&mut self, node: &mut BinOp);
529 /// }
530 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800531 ///
532 /// *This module is available if Syn is built with the `"visit-mut"`
533 /// feature.*
David Tolnay9df02c42018-01-06 13:52:48 -0800534 #[cfg(feature = "visit-mut")]
Nika Layzella6f46c42017-10-26 15:26:16 -0400535 pub mod visit_mut;
Nika Layzell27726662017-10-24 23:16:35 -0400536
David Tolnayded2d682018-01-06 18:53:53 -0800537 /// Syntax tree traversal to transform the nodes of an owned syntax tree.
538 ///
539 /// Each method of the [`Fold`] trait is a hook that can be overridden to
540 /// customize the behavior when transforming the corresponding type of node.
541 /// By default, every method recursively visits the substructure of the
542 /// input by invoking the right visitor method of each of its fields.
543 ///
544 /// [`Fold`]: trait.Fold.html
545 ///
546 /// ```rust
547 /// # use syn::{Attribute, BinOp, Expr, ExprBinary};
548 /// #
549 /// pub trait Fold {
550 /// /* ... */
551 ///
552 /// fn fold_expr_binary(&mut self, node: ExprBinary) -> ExprBinary {
553 /// ExprBinary {
554 /// attrs: node.attrs
555 /// .into_iter()
556 /// .map(|attr| self.fold_attribute(attr))
557 /// .collect(),
558 /// left: Box::new(self.fold_expr(*node.left)),
559 /// op: self.fold_bin_op(node.op),
560 /// right: Box::new(self.fold_expr(*node.right)),
561 /// }
562 /// }
563 ///
564 /// /* ... */
565 /// # fn fold_attribute(&mut self, node: Attribute) -> Attribute;
566 /// # fn fold_expr(&mut self, node: Expr) -> Expr;
567 /// # fn fold_bin_op(&mut self, node: BinOp) -> BinOp;
568 /// }
569 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800570 ///
571 /// *This module is available if Syn is built with the `"fold"` feature.*
Nika Layzella6f46c42017-10-26 15:26:16 -0400572 #[cfg(feature = "fold")]
573 pub mod fold;
David Tolnayf60f4262017-12-28 19:17:58 -0500574
David Tolnay0a0d78c2018-01-05 15:24:01 -0800575 #[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf60f4262017-12-28 19:17:58 -0500576 #[path = "../gen_helper.rs"]
577 mod helper;
Nika Layzella6f46c42017-10-26 15:26:16 -0400578}
579pub use gen::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100580
David Tolnay456c9822018-08-25 08:09:46 -0400581// Not public API.
582#[doc(hidden)]
583pub mod export;
584
David Tolnay776f8e02018-08-24 22:32:10 -0400585#[cfg(feature = "parsing")]
David Tolnay544a90f2018-08-24 20:34:03 -0400586pub mod next;
David Tolnay6d67c742018-08-24 20:42:39 -0400587
David Tolnay7c6f21b2018-08-25 08:28:55 -0400588#[cfg(feature = "parsing")]
David Tolnayb6254182018-08-25 08:44:54 -0400589mod lookahead;
590
591#[cfg(feature = "parsing")]
592pub mod parse;
593
David Tolnay776f8e02018-08-24 22:32:10 -0400594mod span;
595
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700596////////////////////////////////////////////////////////////////////////////////
597
David Tolnay55337722016-09-11 12:58:56 -0700598#[cfg(feature = "parsing")]
David Tolnayad4b2472018-08-25 08:25:24 -0400599use proc_macro2::Span;
David Tolnay8c39ac52018-08-25 08:46:21 -0400600#[cfg(feature = "parsing")]
601use synom::{Parser, Synom};
Ted Driggs054abbb2017-05-01 12:20:52 -0700602
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700603#[cfg(feature = "parsing")]
David Tolnayb6254182018-08-25 08:44:54 -0400604mod error;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500605#[cfg(feature = "parsing")]
David Tolnayad4b2472018-08-25 08:25:24 -0400606use error::Error;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500607
608// Not public API.
David Tolnay1b752fb2017-12-26 21:41:39 -0500609#[cfg(feature = "parsing")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500610#[doc(hidden)]
611pub use error::parse_error;
Michael Layzell416724e2017-05-24 21:12:34 -0400612
David Tolnayccab0be2018-01-06 22:24:47 -0800613/// Parse tokens of source code into the chosen syntax tree node.
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700614///
615/// This is preferred over parsing a string because tokens are able to preserve
616/// information about where in the user's code they were originally written (the
617/// "span" of the token), possibly allowing the compiler to produce better error
618/// messages.
619///
David Tolnayccab0be2018-01-06 22:24:47 -0800620/// This function parses a `proc_macro::TokenStream` which is the type used for
621/// interop with the compiler in a procedural macro. To parse a
622/// `proc_macro2::TokenStream`, use [`syn::parse2`] instead.
623///
624/// [`syn::parse2`]: fn.parse2.html
625///
hcpl4b72a382018-04-04 14:50:24 +0300626/// *This function is available if Syn is built with both the `"parsing"` and
627/// `"proc-macro"` features.*
David Tolnay461d98e2018-01-07 11:07:19 -0800628///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700629/// # Examples
630///
David Tolnaybcf26022017-12-25 22:10:52 -0500631/// ```rust
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700632/// extern crate proc_macro;
633/// use proc_macro::TokenStream;
634///
635/// extern crate syn;
636///
637/// #[macro_use]
638/// extern crate quote;
639///
640/// use syn::DeriveInput;
641///
David Tolnaybcf26022017-12-25 22:10:52 -0500642/// # const IGNORE_TOKENS: &str = stringify! {
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700643/// #[proc_macro_derive(MyMacro)]
David Tolnaybcf26022017-12-25 22:10:52 -0500644/// # };
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700645/// pub fn my_macro(input: TokenStream) -> TokenStream {
646/// // Parse the tokens into a syntax tree
647/// let ast: DeriveInput = syn::parse(input).unwrap();
648///
649/// // Build the output, possibly using quasi-quotation
650/// let expanded = quote! {
651/// /* ... */
652/// };
653///
David Tolnaybcf26022017-12-25 22:10:52 -0500654/// // Convert into a token stream and return it
655/// expanded.into()
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700656/// }
David Tolnaybcf26022017-12-25 22:10:52 -0500657/// #
658/// # fn main() {}
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700659/// ```
David Tolnay278f9e32018-08-14 22:41:11 -0700660#[cfg(all(
661 not(all(target_arch = "wasm32", target_os = "unknown")),
662 feature = "parsing",
663 feature = "proc-macro"
664))]
David Tolnayad4b2472018-08-25 08:25:24 -0400665pub fn parse<T>(tokens: proc_macro::TokenStream) -> Result<T, Error>
David Tolnay51382052017-12-27 13:46:21 -0500666where
667 T: Synom,
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700668{
David Tolnay04c5dca2018-01-05 20:12:21 -0800669 parse2(tokens.into())
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700670}
671
David Tolnayccab0be2018-01-06 22:24:47 -0800672/// Parse a proc-macro2 token stream into the chosen syntax tree node.
673///
674/// This function parses a `proc_macro2::TokenStream` which is commonly useful
675/// when the input comes from a node of the Syn syntax tree, for example the tts
676/// of a [`Macro`] node. When in a procedural macro parsing the
677/// `proc_macro::TokenStream` provided by the compiler, use [`syn::parse`]
678/// instead.
679///
680/// [`Macro`]: struct.Macro.html
681/// [`syn::parse`]: fn.parse.html
David Tolnay461d98e2018-01-07 11:07:19 -0800682///
683/// *This function is available if Syn is built with the `"parsing"` feature.*
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700684#[cfg(feature = "parsing")]
David Tolnayad4b2472018-08-25 08:25:24 -0400685pub fn parse2<T>(tokens: proc_macro2::TokenStream) -> Result<T, Error>
David Tolnay51382052017-12-27 13:46:21 -0500686where
687 T: Synom,
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700688{
David Tolnayca7cd972018-01-11 14:23:06 -0800689 let parser = T::parse;
David Tolnay94d2b792018-04-29 12:26:10 -0700690 parser.parse2(tokens).map_err(|err| match T::description() {
David Tolnayad4b2472018-08-25 08:25:24 -0400691 Some(s) => Error::new(Span::call_site(), format!("failed to parse {}: {}", s, err)),
David Tolnay94d2b792018-04-29 12:26:10 -0700692 None => err,
David Tolnayca7cd972018-01-11 14:23:06 -0800693 })
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700694}
Alex Crichton954046c2017-05-30 21:49:42 -0700695
David Tolnayccab0be2018-01-06 22:24:47 -0800696/// Parse a string of Rust code into the chosen syntax tree node.
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700697///
David Tolnay461d98e2018-01-07 11:07:19 -0800698/// *This function is available if Syn is built with the `"parsing"` feature.*
699///
David Tolnay3f5b06f2018-01-11 21:02:00 -0800700/// # Hygiene
701///
702/// Every span in the resulting syntax tree will be set to resolve at the macro
703/// call site.
704///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700705/// # Examples
706///
707/// ```rust
708/// extern crate syn;
709/// #
David Tolnay9174b972017-11-09 22:27:50 -0800710/// #
711/// # type Result<T> = std::result::Result<T, Box<std::error::Error>>;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700712///
713/// use syn::Expr;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700714///
715/// fn run() -> Result<()> {
716/// let code = "assert_eq!(u8::max_value(), 255)";
717/// let expr = syn::parse_str::<Expr>(code)?;
718/// println!("{:#?}", expr);
719/// Ok(())
720/// }
721/// #
722/// # fn main() { run().unwrap() }
723/// ```
724#[cfg(feature = "parsing")]
David Tolnayad4b2472018-08-25 08:25:24 -0400725pub fn parse_str<T: Synom>(s: &str) -> Result<T, Error> {
David Tolnayffb1f4d2017-12-28 00:07:59 -0500726 match s.parse() {
David Tolnay04c5dca2018-01-05 20:12:21 -0800727 Ok(tts) => parse2(tts),
David Tolnay8c39ac52018-08-25 08:46:21 -0400728 Err(_) => Err(Error::new(
729 Span::call_site(),
730 "error while lexing input string",
731 )),
David Tolnayffb1f4d2017-12-28 00:07:59 -0500732 }
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700733}
Alex Crichton954046c2017-05-30 21:49:42 -0700734
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700735// FIXME the name parse_file makes it sound like you might pass in a path to a
736// file, rather than the content.
737/// Parse the content of a file of Rust code.
738///
739/// This is different from `syn::parse_str::<File>(content)` in two ways:
740///
741/// - It discards a leading byte order mark `\u{FEFF}` if the file has one.
742/// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`.
743///
744/// If present, either of these would be an error using `from_str`.
745///
Christopher Serr9727ef22018-02-03 16:42:12 +0100746/// *This function is available if Syn is built with the `"parsing"` and `"full"` features.*
David Tolnay461d98e2018-01-07 11:07:19 -0800747///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700748/// # Examples
749///
750/// ```rust,no_run
751/// extern crate syn;
752/// #
David Tolnay9174b972017-11-09 22:27:50 -0800753/// #
754/// # type Result<T> = std::result::Result<T, Box<std::error::Error>>;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700755///
756/// use std::fs::File;
757/// use std::io::Read;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700758///
759/// fn run() -> Result<()> {
760/// let mut file = File::open("path/to/code.rs")?;
761/// let mut content = String::new();
762/// file.read_to_string(&mut content)?;
763///
764/// let ast = syn::parse_file(&content)?;
765/// if let Some(shebang) = ast.shebang {
766/// println!("{}", shebang);
767/// }
768/// println!("{} items", ast.items.len());
769///
770/// Ok(())
771/// }
772/// #
773/// # fn main() { run().unwrap() }
774/// ```
775#[cfg(all(feature = "parsing", feature = "full"))]
David Tolnayad4b2472018-08-25 08:25:24 -0400776pub fn parse_file(mut content: &str) -> Result<File, Error> {
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700777 // Strip the BOM if it is present
778 const BOM: &'static str = "\u{feff}";
779 if content.starts_with(BOM) {
780 content = &content[BOM.len()..];
David Tolnay35161ff2016-09-03 11:33:15 -0700781 }
Michael Layzell5e107ff2017-01-24 19:58:39 -0500782
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700783 let mut shebang = None;
784 if content.starts_with("#!") && !content.starts_with("#![") {
785 if let Some(idx) = content.find('\n') {
786 shebang = Some(content[..idx].to_string());
787 content = &content[idx..];
788 } else {
789 shebang = Some(content.to_string());
790 content = "";
791 }
Alex Crichton954046c2017-05-30 21:49:42 -0700792 }
David Tolnay0a8972b2017-02-27 02:10:01 -0800793
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700794 let mut file: File = parse_str(content)?;
795 file.shebang = shebang;
796 Ok(file)
Michael Layzell5e107ff2017-01-24 19:58:39 -0500797}
Alex Crichton259ee532017-07-14 06:51:02 -0700798
David Tolnay278f9e32018-08-14 22:41:11 -0700799#[cfg(all(
800 any(feature = "full", feature = "derive"),
801 feature = "printing"
802))]
Alex Crichton259ee532017-07-14 06:51:02 -0700803struct TokensOrDefault<'a, T: 'a>(&'a Option<T>);
804
David Tolnay278f9e32018-08-14 22:41:11 -0700805#[cfg(all(
806 any(feature = "full", feature = "derive"),
807 feature = "printing"
808))]
Alex Crichton259ee532017-07-14 06:51:02 -0700809impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T>
David Tolnay51382052017-12-27 13:46:21 -0500810where
811 T: quote::ToTokens + Default,
Alex Crichton259ee532017-07-14 06:51:02 -0700812{
Alex Crichtona74a1c82018-05-16 10:20:44 -0700813 fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
Alex Crichton259ee532017-07-14 06:51:02 -0700814 match *self.0 {
815 Some(ref t) => t.to_tokens(tokens),
816 None => T::default().to_tokens(tokens),
817 }
818 }
819}