blob: 00d5e1dba0fe4f15b38fec48dc21d7779a66cb74 [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//!
David Tolnay6b889eb2018-09-01 18:12:17 -070012//! Currently this library is geared toward use in Rust procedural macros, but
13//! contains some APIs that may be useful more generally.
David Tolnay5e84e972018-01-05 17:51:06 -080014//!
15//! - **Data structures** — Syn provides a complete syntax tree that can
16//! represent any valid Rust source code. The syntax tree is rooted at
17//! [`syn::File`] which represents a full source file, but there are other
18//! entry points that may be useful to procedural macros including
19//! [`syn::Item`], [`syn::Expr`] and [`syn::Type`].
20//!
21//! - **Custom derives** — Of particular interest to custom derives is
22//! [`syn::DeriveInput`] which is any of the three legal input items to a
23//! derive macro. An example below shows using this type in a library that can
24//! derive implementations of a trait of your own.
25//!
David Tolnay6b889eb2018-09-01 18:12:17 -070026//! - **Parsing** — Parsing in Syn is built around [parser functions] with the
27//! signature `fn(ParseStream) -> Result<T>`. Every syntax tree node defined
28//! by Syn is individually parsable and may be used as a building block for
29//! custom syntaxes, or you may dream up your own brand new syntax without
30//! involving any of our syntax tree types.
David Tolnay5e84e972018-01-05 17:51:06 -080031//!
32//! - **Location information** — Every token parsed by Syn is associated with a
33//! `Span` that tracks line and column information back to the source of that
34//! token. These spans allow a procedural macro to display detailed error
35//! messages pointing to all the right places in the user's code. There is an
36//! example of this below.
37//!
38//! - **Feature flags** — Functionality is aggressively feature gated so your
39//! procedural macros enable only what they need, and do not pay in compile
40//! time for all the rest.
41//!
42//! [`syn::File`]: struct.File.html
43//! [`syn::Item`]: enum.Item.html
44//! [`syn::Expr`]: enum.Expr.html
45//! [`syn::Type`]: enum.Type.html
46//! [`syn::DeriveInput`]: struct.DeriveInput.html
David Tolnay6b889eb2018-09-01 18:12:17 -070047//! [parser functions]: parse/index.html
David Tolnay5e84e972018-01-05 17:51:06 -080048//!
49//! *Version requirement: Syn supports any compiler version back to Rust's very
50//! first support for procedural macros in Rust 1.15.0. Some features especially
51//! around error reporting are only available in newer compilers or on the
52//! nightly channel.*
53//!
54//! ## Example of a custom derive
55//!
56//! The canonical custom derive using Syn looks like this. We write an ordinary
57//! Rust function tagged with a `proc_macro_derive` attribute and the name of
58//! the trait we are deriving. Any time that derive appears in the user's code,
59//! the Rust compiler passes their data structure as tokens into our macro. We
60//! get to execute arbitrary Rust code to figure out what to do with those
61//! tokens, then hand some tokens back to the compiler to compile into the
62//! user's crate.
63//!
64//! [`TokenStream`]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html
65//!
66//! ```toml
67//! [dependencies]
David Tolnayb28acf32018-09-06 09:01:40 -070068//! syn = "0.15"
David Tolnay87003d02018-05-20 19:45:13 -070069//! quote = "0.6"
David Tolnay5e84e972018-01-05 17:51:06 -080070//!
71//! [lib]
72//! proc-macro = true
73//! ```
74//!
75//! ```rust
David Tolnaya1c98072018-09-06 08:58:10 -070076//! #[macro_use]
77//! extern crate quote;
78//! #[macro_use]
79//! extern crate syn;
80//!
81//! extern crate proc_macro;
82//!
David Tolnay5e84e972018-01-05 17:51:06 -080083//! use proc_macro::TokenStream;
David Tolnaya1c98072018-09-06 08:58:10 -070084//! use syn::DeriveInput;
David Tolnay5e84e972018-01-05 17:51:06 -080085//!
86//! # const IGNORE_TOKENS: &str = stringify! {
87//! #[proc_macro_derive(MyMacro)]
88//! # };
89//! pub fn my_macro(input: TokenStream) -> TokenStream {
90//! // Parse the input tokens into a syntax tree
David Tolnay6b889eb2018-09-01 18:12:17 -070091//! let input = parse_macro_input!(input as DeriveInput);
David Tolnay5e84e972018-01-05 17:51:06 -080092//!
93//! // Build the output, possibly using quasi-quotation
94//! let expanded = quote! {
95//! // ...
96//! };
97//!
98//! // Hand the output tokens back to the compiler
David Tolnay35b498e2018-09-01 20:10:40 -070099//! TokenStream::from(expanded)
David Tolnay5e84e972018-01-05 17:51:06 -0800100//! }
101//! #
102//! # fn main() {}
103//! ```
104//!
105//! The [`heapsize`] example directory shows a complete working Macros 1.1
106//! implementation of a custom derive. It works on any Rust compiler \>=1.15.0.
107//! The example derives a `HeapSize` trait which computes an estimate of the
108//! amount of heap memory owned by a value.
109//!
110//! [`heapsize`]: https://github.com/dtolnay/syn/tree/master/examples/heapsize
111//!
112//! ```rust
113//! pub trait HeapSize {
114//! /// Total number of bytes of heap memory owned by `self`.
115//! fn heap_size_of_children(&self) -> usize;
116//! }
117//! ```
118//!
119//! The custom derive allows users to write `#[derive(HeapSize)]` on data
120//! structures in their program.
121//!
122//! ```rust
123//! # const IGNORE_TOKENS: &str = stringify! {
124//! #[derive(HeapSize)]
125//! # };
126//! struct Demo<'a, T: ?Sized> {
127//! a: Box<T>,
128//! b: u8,
129//! c: &'a str,
130//! d: String,
131//! }
132//! ```
133//!
134//! ## Spans and error reporting
135//!
136//! The [`heapsize2`] example directory is an extension of the `heapsize`
137//! example that demonstrates some of the hygiene and error reporting properties
138//! of Macros 2.0. This example currently requires a nightly Rust compiler
139//! \>=1.24.0-nightly but we are working to stabilize all of the APIs involved.
140//!
141//! [`heapsize2`]: https://github.com/dtolnay/syn/tree/master/examples/heapsize2
142//!
143//! The token-based procedural macro API provides great control over where the
144//! compiler's error messages are displayed in user code. Consider the error the
145//! user sees if one of their field types does not implement `HeapSize`.
146//!
147//! ```rust
148//! # const IGNORE_TOKENS: &str = stringify! {
149//! #[derive(HeapSize)]
150//! # };
151//! struct Broken {
152//! ok: String,
153//! bad: std::thread::Thread,
154//! }
155//! ```
156//!
157//! In the Macros 1.1 string-based procedural macro world, the resulting error
158//! would point unhelpfully to the invocation of the derive macro and not to the
159//! actual problematic field.
160//!
161//! ```text
162//! error[E0599]: no method named `heap_size_of_children` found for type `std::thread::Thread` in the current scope
163//! --> src/main.rs:4:10
164//! |
165//! 4 | #[derive(HeapSize)]
166//! | ^^^^^^^^
167//! ```
168//!
169//! By tracking span information all the way through the expansion of a
170//! procedural macro as shown in the `heapsize2` example, token-based macros in
171//! Syn are able to trigger errors that directly pinpoint the source of the
172//! problem.
173//!
174//! ```text
175//! error[E0277]: the trait bound `std::thread::Thread: HeapSize` is not satisfied
176//! --> src/main.rs:7:5
177//! |
178//! 7 | bad: std::thread::Thread,
David Tolnayefff2ff2018-01-07 11:49:52 -0800179//! | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HeapSize` is not implemented for `Thread`
David Tolnay5e84e972018-01-05 17:51:06 -0800180//! ```
181//!
David Tolnay6b889eb2018-09-01 18:12:17 -0700182//! ## Parsing a custom syntax
David Tolnay5e84e972018-01-05 17:51:06 -0800183//!
184//! The [`lazy-static`] example directory shows the implementation of a
185//! `functionlike!(...)` procedural macro in which the input tokens are parsed
David Tolnay6b889eb2018-09-01 18:12:17 -0700186//! using Syn's parsing API.
David Tolnay5e84e972018-01-05 17:51:06 -0800187//!
188//! [`lazy-static`]: https://github.com/dtolnay/syn/tree/master/examples/lazy-static
David Tolnay5e84e972018-01-05 17:51:06 -0800189//!
190//! The example reimplements the popular `lazy_static` crate from crates.io as a
191//! procedural macro.
192//!
193//! ```
194//! # macro_rules! lazy_static {
195//! # ($($tt:tt)*) => {}
196//! # }
197//! #
198//! lazy_static! {
199//! static ref USERNAME: Regex = Regex::new("^[a-z0-9_-]{3,16}$").unwrap();
200//! }
201//! ```
202//!
203//! The implementation shows how to trigger custom warnings and error messages
204//! on the macro input.
205//!
206//! ```text
207//! warning: come on, pick a more creative name
208//! --> src/main.rs:10:16
209//! |
210//! 10 | static ref FOO: String = "lazy_static".to_owned();
211//! | ^^^
212//! ```
213//!
214//! ## Debugging
215//!
216//! When developing a procedural macro it can be helpful to look at what the
217//! generated code looks like. Use `cargo rustc -- -Zunstable-options
218//! --pretty=expanded` or the [`cargo expand`] subcommand.
219//!
David Tolnay324db2d2018-01-07 11:51:09 -0800220//! [`cargo expand`]: https://github.com/dtolnay/cargo-expand
David Tolnay5e84e972018-01-05 17:51:06 -0800221//!
222//! To show the expanded code for some crate that uses your procedural macro,
223//! run `cargo expand` from that crate. To show the expanded code for one of
224//! your own test cases, run `cargo expand --test the_test_case` where the last
225//! argument is the name of the test file without the `.rs` extension.
226//!
227//! This write-up by Brandon W Maister discusses debugging in more detail:
228//! [Debugging Rust's new Custom Derive system][debugging].
229//!
230//! [debugging]: https://quodlibetor.github.io/posts/debugging-rusts-new-custom-derive-system/
231//!
232//! ## Optional features
233//!
234//! Syn puts a lot of functionality behind optional features in order to
235//! optimize compile time for the most common use cases. The following features
236//! are available.
237//!
238//! - **`derive`** *(enabled by default)* — Data structures for representing the
239//! possible input to a custom derive, including structs and enums and types.
240//! - **`full`** — Data structures for representing the syntax tree of all valid
241//! Rust source code, including items and expressions.
242//! - **`parsing`** *(enabled by default)* — Ability to parse input tokens into
243//! a syntax tree node of a chosen type.
244//! - **`printing`** *(enabled by default)* — Ability to print a syntax tree
245//! node as tokens of Rust source code.
246//! - **`visit`** — Trait for traversing a syntax tree.
David Tolnay34981cf2018-01-06 16:22:35 -0800247//! - **`visit-mut`** — Trait for traversing and mutating in place a syntax
David Tolnay5e84e972018-01-05 17:51:06 -0800248//! tree.
249//! - **`fold`** — Trait for transforming an owned syntax tree.
250//! - **`clone-impls`** *(enabled by default)* — Clone impls for all syntax tree
251//! types.
252//! - **`extra-traits`** — Debug, Eq, PartialEq, Hash impls for all syntax tree
253//! types.
hcpl4b72a382018-04-04 14:50:24 +0300254//! - **`proc-macro`** *(enabled by default)* — Runtime dependency on the
255//! dynamic library libproc_macro from rustc toolchain.
David Tolnay5e84e972018-01-05 17:51:06 -0800256
David Tolnay4cf7db82018-01-07 15:22:01 -0800257// Syn types in rustdoc of other crates get linked to here.
David Tolnay51bd7a02018-10-06 20:48:54 -0700258#![doc(html_root_url = "https://docs.rs/syn/0.15.9")]
David Tolnay46b17c02018-09-22 13:27:40 -0700259#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
David Tolnay34071ba2018-05-20 20:00:41 -0700260#![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))]
David Tolnay34071ba2018-05-20 20:00:41 -0700261// Ignored clippy lints.
David Tolnay94d2b792018-04-29 12:26:10 -0700262#![cfg_attr(
263 feature = "cargo-clippy",
264 allow(
David Tolnay4831ac62018-08-30 21:04:16 -0700265 block_in_if_condition_stmt,
David Tolnay0cec3e62018-07-21 09:08:30 -0700266 const_static_lifetime,
David Tolnay24b079d2018-08-27 08:28:10 -0700267 cyclomatic_complexity,
David Tolnay0cec3e62018-07-21 09:08:30 -0700268 doc_markdown,
David Tolnay24b079d2018-08-27 08:28:10 -0700269 eval_order_dependence,
David Tolnay0cec3e62018-07-21 09:08:30 -0700270 large_enum_variant,
271 match_bool,
David Tolnay6fb87462018-09-01 16:51:49 -0700272 never_loop,
David Tolnay0cec3e62018-07-21 09:08:30 -0700273 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
Alex Crichton62a0a592017-05-22 13:58:53 -0700310#[macro_use]
311mod macros;
312
David Tolnay734079e2018-09-01 02:03:37 -0700313// Not public API.
David Tolnay852bff72018-08-27 08:24:02 -0700314#[cfg(feature = "parsing")]
David Tolnay734079e2018-09-01 02:03:37 -0700315#[doc(hidden)]
David Tolnay852bff72018-08-27 08:24:02 -0700316#[macro_use]
David Tolnay734079e2018-09-01 02:03:37 -0700317pub mod group;
David Tolnay852bff72018-08-27 08:24:02 -0700318
David Tolnayc5ab8c62017-12-26 16:43:39 -0500319#[macro_use]
David Tolnay32954ef2017-12-26 22:43:16 -0500320pub mod token;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500321
David Tolnay4fb71232018-08-25 23:14:50 -0400322mod ident;
323pub use ident::Ident;
David Tolnaye303b7c2018-05-20 16:46:35 -0700324
David Tolnay3cfd1d32018-01-03 00:22:08 -0800325#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb79ee962016-09-04 09:39:20 -0700326mod attr;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800327#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayaaadd782018-01-06 22:58:13 -0800328pub use attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue, NestedMeta};
David Tolnay35161ff2016-09-03 11:33:15 -0700329
David Tolnay3cfd1d32018-01-03 00:22:08 -0800330#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf38cdf62016-09-23 19:07:09 -0700331mod data;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800332#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700333pub use data::{
334 Field, Fields, FieldsNamed, FieldsUnnamed, Variant, VisCrate, VisPublic, VisRestricted,
335 Visibility,
336};
David Tolnayf38cdf62016-09-23 19:07:09 -0700337
David Tolnay3cfd1d32018-01-03 00:22:08 -0800338#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf4bbbd92016-09-23 14:41:55 -0700339mod expr;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800340#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700341pub use expr::{
David Tolnay02a9c6f2018-08-24 18:58:45 -0400342 Expr, ExprArray, ExprAssign, ExprAssignOp, ExprAsync, ExprBinary, ExprBlock, ExprBox,
343 ExprBreak, ExprCall, ExprCast, ExprClosure, ExprContinue, ExprField, ExprForLoop, ExprGroup,
David Tolnay9c119122018-09-01 18:47:02 -0700344 ExprIf, ExprInPlace, ExprIndex, ExprLet, ExprLit, ExprLoop, ExprMacro, ExprMatch,
David Tolnay02a9c6f2018-08-24 18:58:45 -0400345 ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn,
346 ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprVerbatim,
David Tolnay9c119122018-09-01 18:47:02 -0700347 ExprWhile, ExprYield, Index, Member,
David Tolnayb57c8492018-05-05 00:32:04 -0700348};
Michael Layzell734adb42017-06-07 16:58:31 -0400349
350#[cfg(feature = "full")]
David Tolnayb57c8492018-05-05 00:32:04 -0700351pub use expr::{
352 Arm, Block, FieldPat, FieldValue, GenericMethodArgument, Label, Local, MethodTurbofish, Pat,
353 PatBox, PatIdent, PatLit, PatMacro, PatPath, PatRange, PatRef, PatSlice, PatStruct, PatTuple,
354 PatTupleStruct, PatVerbatim, PatWild, RangeLimits, Stmt,
355};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700356
David Tolnay3cfd1d32018-01-03 00:22:08 -0800357#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb79ee962016-09-04 09:39:20 -0700358mod generics;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800359#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700360pub use generics::{
361 BoundLifetimes, ConstParam, GenericParam, Generics, LifetimeDef, PredicateEq,
362 PredicateLifetime, PredicateType, TraitBound, TraitBoundModifier, TypeParam, TypeParamBound,
363 WhereClause, WherePredicate,
364};
David Tolnay278f9e32018-08-14 22:41:11 -0700365#[cfg(all(
366 any(feature = "full", feature = "derive"),
367 feature = "printing"
368))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800369pub use generics::{ImplGenerics, Turbofish, TypeGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -0700370
David Tolnayf38cdf62016-09-23 19:07:09 -0700371#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -0700372mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -0700373#[cfg(feature = "full")]
David Tolnayb57c8492018-05-05 00:32:04 -0700374pub use item::{
David Tolnay435c1782018-08-24 16:15:44 -0400375 ArgCaptured, ArgSelf, ArgSelfRef, FnArg, FnDecl, ForeignItem, ForeignItemFn, ForeignItemMacro,
376 ForeignItemStatic, ForeignItemType, ForeignItemVerbatim, ImplItem, ImplItemConst,
David Tolnaybb82ef02018-08-24 20:15:45 -0400377 ImplItemExistential, ImplItemMacro, ImplItemMethod, ImplItemType, ImplItemVerbatim, Item,
378 ItemConst, ItemEnum, ItemExistential, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl,
David Tolnayc6b04dd2018-08-30 23:22:51 -0700379 ItemMacro, ItemMacro2, ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType,
380 ItemUnion, ItemUse, ItemVerbatim, MethodSig, TraitItem, TraitItemConst, TraitItemMacro,
381 TraitItemMethod, TraitItemType, TraitItemVerbatim, UseGlob, UseGroup, UseName, UsePath,
382 UseRename, UseTree,
David Tolnayb57c8492018-05-05 00:32:04 -0700383};
David Tolnay35161ff2016-09-03 11:33:15 -0700384
David Tolnay631cb8c2016-11-10 17:16:41 -0800385#[cfg(feature = "full")]
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700386mod file;
David Tolnay631cb8c2016-11-10 17:16:41 -0800387#[cfg(feature = "full")]
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700388pub use file::File;
David Tolnay631cb8c2016-11-10 17:16:41 -0800389
David Tolnay63e3dee2017-06-03 20:13:17 -0700390mod lifetime;
391pub use lifetime::Lifetime;
392
David Tolnay3cfd1d32018-01-03 00:22:08 -0800393#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf4bbbd92016-09-23 14:41:55 -0700394mod lit;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800395#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700396pub use lit::{
397 FloatSuffix, IntSuffix, Lit, LitBool, LitByte, LitByteStr, LitChar, LitFloat, LitInt, LitStr,
398 LitVerbatim, StrStyle,
399};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700400
David Tolnay3cfd1d32018-01-03 00:22:08 -0800401#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf4bbbd92016-09-23 14:41:55 -0700402mod mac;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800403#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayab919512017-12-30 23:31:51 -0500404pub use mac::{Macro, MacroDelimiter};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700405
David Tolnay3cfd1d32018-01-03 00:22:08 -0800406#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay0e837402016-12-22 17:25:55 -0500407mod derive;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800408#[cfg(feature = "derive")]
David Tolnaye3d41b72017-12-31 15:24:00 -0500409pub use derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput};
David Tolnayf38cdf62016-09-23 19:07:09 -0700410
David Tolnay3cfd1d32018-01-03 00:22:08 -0800411#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay3cb23a92016-10-07 23:02:21 -0700412mod op;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800413#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay3cb23a92016-10-07 23:02:21 -0700414pub use op::{BinOp, UnOp};
415
David Tolnay3cfd1d32018-01-03 00:22:08 -0800416#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb79ee962016-09-04 09:39:20 -0700417mod ty;
David Tolnay3cfd1d32018-01-03 00:22:08 -0800418#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700419pub use ty::{
420 Abi, BareFnArg, BareFnArgName, ReturnType, Type, TypeArray, TypeBareFn, TypeGroup,
421 TypeImplTrait, TypeInfer, TypeMacro, TypeNever, TypeParen, TypePath, TypePtr, TypeReference,
422 TypeSlice, TypeTraitObject, TypeTuple, TypeVerbatim,
423};
David Tolnay056de302018-01-05 14:29:05 -0800424
425#[cfg(any(feature = "full", feature = "derive"))]
426mod path;
427#[cfg(any(feature = "full", feature = "derive"))]
David Tolnayb57c8492018-05-05 00:32:04 -0700428pub use path::{
David Tolnay9d0882a2018-09-01 19:49:14 -0700429 AngleBracketedGenericArguments, Binding, Constraint, GenericArgument,
430 ParenthesizedGenericArguments, Path, PathArguments, PathSegment, QSelf,
David Tolnayb57c8492018-05-05 00:32:04 -0700431};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700432
David Tolnay1b752fb2017-12-26 21:41:39 -0500433#[cfg(feature = "parsing")]
David Tolnaydfc886b2018-01-06 08:03:09 -0800434pub mod buffer;
David Tolnay94d304f2018-08-30 23:43:53 -0700435#[cfg(feature = "parsing")]
436pub mod ext;
David Tolnay94d2b792018-04-29 12:26:10 -0700437pub mod punctuated;
David Tolnay3779bb72018-08-26 18:46:07 -0700438#[cfg(all(
439 any(feature = "full", feature = "derive"),
440 feature = "extra-traits"
441))]
David Tolnaye0824032017-12-27 15:25:56 -0500442mod tt;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500443
David Tolnaye83ef5a2018-01-11 15:18:36 -0800444// Not public API except the `parse_quote!` macro.
David Tolnay491680a2018-01-23 00:34:40 -0800445#[cfg(feature = "parsing")]
David Tolnaye83ef5a2018-01-11 15:18:36 -0800446#[doc(hidden)]
447pub mod parse_quote;
448
David Tolnayf98865f2018-10-13 14:37:07 -0700449// Not public API except the `parse_macro_input!` macro.
450#[cfg(all(
451 not(all(target_arch = "wasm32", target_os = "unknown")),
452 feature = "parsing",
453 feature = "proc-macro"
454))]
455#[doc(hidden)]
456pub mod parse_macro_input;
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
Nika Layzella6f46c42017-10-26 15:26:16 -0400461mod gen {
David Tolnayded2d682018-01-06 18:53:53 -0800462 /// Syntax tree traversal to walk a shared borrow of a syntax tree.
463 ///
464 /// Each method of the [`Visit`] trait is a hook that can be overridden to
465 /// customize the behavior when visiting the corresponding type of node. By
466 /// default, every method recursively visits the substructure of the input
467 /// by invoking the right visitor method of each of its fields.
468 ///
469 /// [`Visit`]: trait.Visit.html
470 ///
471 /// ```rust
472 /// # use syn::{Attribute, BinOp, Expr, ExprBinary};
473 /// #
474 /// pub trait Visit<'ast> {
475 /// /* ... */
476 ///
477 /// fn visit_expr_binary(&mut self, node: &'ast ExprBinary) {
478 /// for attr in &node.attrs {
479 /// self.visit_attribute(attr);
480 /// }
481 /// self.visit_expr(&*node.left);
482 /// self.visit_bin_op(&node.op);
483 /// self.visit_expr(&*node.right);
484 /// }
485 ///
486 /// /* ... */
487 /// # fn visit_attribute(&mut self, node: &'ast Attribute);
488 /// # fn visit_expr(&mut self, node: &'ast Expr);
489 /// # fn visit_bin_op(&mut self, node: &'ast BinOp);
490 /// }
491 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800492 ///
493 /// *This module is available if Syn is built with the `"visit"` feature.*
Nika Layzella6f46c42017-10-26 15:26:16 -0400494 #[cfg(feature = "visit")]
495 pub mod visit;
David Tolnay55337722016-09-11 12:58:56 -0700496
David Tolnayded2d682018-01-06 18:53:53 -0800497 /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in
498 /// place.
499 ///
500 /// Each method of the [`VisitMut`] trait is a hook that can be overridden
501 /// to customize the behavior when mutating the corresponding type of node.
502 /// By default, every method recursively visits the substructure of the
503 /// input by invoking the right visitor method of each of its fields.
504 ///
505 /// [`VisitMut`]: trait.VisitMut.html
506 ///
507 /// ```rust
508 /// # use syn::{Attribute, BinOp, Expr, ExprBinary};
509 /// #
510 /// pub trait VisitMut {
511 /// /* ... */
512 ///
513 /// fn visit_expr_binary_mut(&mut self, node: &mut ExprBinary) {
514 /// for attr in &mut node.attrs {
515 /// self.visit_attribute_mut(attr);
516 /// }
517 /// self.visit_expr_mut(&mut *node.left);
518 /// self.visit_bin_op_mut(&mut node.op);
519 /// self.visit_expr_mut(&mut *node.right);
520 /// }
521 ///
522 /// /* ... */
523 /// # fn visit_attribute_mut(&mut self, node: &mut Attribute);
524 /// # fn visit_expr_mut(&mut self, node: &mut Expr);
525 /// # fn visit_bin_op_mut(&mut self, node: &mut BinOp);
526 /// }
527 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800528 ///
529 /// *This module is available if Syn is built with the `"visit-mut"`
530 /// feature.*
David Tolnay9df02c42018-01-06 13:52:48 -0800531 #[cfg(feature = "visit-mut")]
Nika Layzella6f46c42017-10-26 15:26:16 -0400532 pub mod visit_mut;
Nika Layzell27726662017-10-24 23:16:35 -0400533
David Tolnayded2d682018-01-06 18:53:53 -0800534 /// Syntax tree traversal to transform the nodes of an owned syntax tree.
535 ///
536 /// Each method of the [`Fold`] trait is a hook that can be overridden to
537 /// customize the behavior when transforming the corresponding type of node.
538 /// By default, every method recursively visits the substructure of the
539 /// input by invoking the right visitor method of each of its fields.
540 ///
541 /// [`Fold`]: trait.Fold.html
542 ///
543 /// ```rust
544 /// # use syn::{Attribute, BinOp, Expr, ExprBinary};
545 /// #
546 /// pub trait Fold {
547 /// /* ... */
548 ///
549 /// fn fold_expr_binary(&mut self, node: ExprBinary) -> ExprBinary {
550 /// ExprBinary {
551 /// attrs: node.attrs
552 /// .into_iter()
553 /// .map(|attr| self.fold_attribute(attr))
554 /// .collect(),
555 /// left: Box::new(self.fold_expr(*node.left)),
556 /// op: self.fold_bin_op(node.op),
557 /// right: Box::new(self.fold_expr(*node.right)),
558 /// }
559 /// }
560 ///
561 /// /* ... */
562 /// # fn fold_attribute(&mut self, node: Attribute) -> Attribute;
563 /// # fn fold_expr(&mut self, node: Expr) -> Expr;
564 /// # fn fold_bin_op(&mut self, node: BinOp) -> BinOp;
565 /// }
566 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800567 ///
568 /// *This module is available if Syn is built with the `"fold"` feature.*
Nika Layzella6f46c42017-10-26 15:26:16 -0400569 #[cfg(feature = "fold")]
570 pub mod fold;
David Tolnayf60f4262017-12-28 19:17:58 -0500571
David Tolnay0a0d78c2018-01-05 15:24:01 -0800572 #[cfg(any(feature = "full", feature = "derive"))]
David Tolnayf60f4262017-12-28 19:17:58 -0500573 #[path = "../gen_helper.rs"]
574 mod helper;
Nika Layzella6f46c42017-10-26 15:26:16 -0400575}
576pub use gen::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100577
David Tolnay456c9822018-08-25 08:09:46 -0400578// Not public API.
579#[doc(hidden)]
580pub mod export;
581
David Tolnay7fb11e72018-09-06 01:02:27 -0700582mod keyword;
David Tolnayb6254182018-08-25 08:44:54 -0400583
584#[cfg(feature = "parsing")]
David Tolnay7fb11e72018-09-06 01:02:27 -0700585mod lookahead;
Louis Kureuil Personc0beaf32018-09-05 00:12:43 +0200586
587#[cfg(feature = "parsing")]
David Tolnayb6254182018-08-25 08:44:54 -0400588pub mod parse;
589
David Tolnay776f8e02018-08-24 22:32:10 -0400590mod span;
591
David Tolnay64023912018-08-31 09:51:12 -0700592#[cfg(all(
593 any(feature = "full", feature = "derive"),
594 feature = "printing"
595))]
596mod print;
597
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700598////////////////////////////////////////////////////////////////////////////////
599
David Tolnayaa77a852018-08-31 11:15:10 -0700600#[cfg(any(feature = "parsing", feature = "full", feature = "derive"))]
David Tolnay94f06632018-08-31 10:17:17 -0700601#[allow(non_camel_case_types)]
David Tolnay10951d52018-08-31 10:27:39 -0700602struct private;
David Tolnay94f06632018-08-31 10:17:17 -0700603
604////////////////////////////////////////////////////////////////////////////////
605
David Tolnay55337722016-09-11 12:58:56 -0700606#[cfg(feature = "parsing")]
David Tolnayb6254182018-08-25 08:44:54 -0400607mod error;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500608#[cfg(feature = "parsing")]
David Tolnayad4b2472018-08-25 08:25:24 -0400609use error::Error;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500610
David Tolnayccab0be2018-01-06 22:24:47 -0800611/// Parse tokens of source code into the chosen syntax tree node.
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700612///
613/// This is preferred over parsing a string because tokens are able to preserve
614/// information about where in the user's code they were originally written (the
615/// "span" of the token), possibly allowing the compiler to produce better error
616/// messages.
617///
David Tolnayccab0be2018-01-06 22:24:47 -0800618/// This function parses a `proc_macro::TokenStream` which is the type used for
619/// interop with the compiler in a procedural macro. To parse a
620/// `proc_macro2::TokenStream`, use [`syn::parse2`] instead.
621///
622/// [`syn::parse2`]: fn.parse2.html
623///
hcpl4b72a382018-04-04 14:50:24 +0300624/// *This function is available if Syn is built with both the `"parsing"` and
625/// `"proc-macro"` features.*
David Tolnay461d98e2018-01-07 11:07:19 -0800626///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700627/// # Examples
628///
David Tolnaybcf26022017-12-25 22:10:52 -0500629/// ```rust
David Tolnaya1c98072018-09-06 08:58:10 -0700630/// #[macro_use]
631/// extern crate quote;
632///
633/// extern crate proc_macro;
634/// extern crate syn;
635///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700636/// use proc_macro::TokenStream;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700637/// use syn::DeriveInput;
638///
David Tolnaybcf26022017-12-25 22:10:52 -0500639/// # const IGNORE_TOKENS: &str = stringify! {
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700640/// #[proc_macro_derive(MyMacro)]
David Tolnaybcf26022017-12-25 22:10:52 -0500641/// # };
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700642/// pub fn my_macro(input: TokenStream) -> TokenStream {
643/// // Parse the tokens into a syntax tree
644/// let ast: DeriveInput = syn::parse(input).unwrap();
645///
646/// // Build the output, possibly using quasi-quotation
647/// let expanded = quote! {
648/// /* ... */
649/// };
650///
David Tolnaybcf26022017-12-25 22:10:52 -0500651/// // Convert into a token stream and return it
652/// expanded.into()
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700653/// }
David Tolnaybcf26022017-12-25 22:10:52 -0500654/// #
655/// # fn main() {}
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700656/// ```
David Tolnay278f9e32018-08-14 22:41:11 -0700657#[cfg(all(
658 not(all(target_arch = "wasm32", target_os = "unknown")),
659 feature = "parsing",
660 feature = "proc-macro"
661))]
David Tolnaye82a2b12018-08-30 16:31:10 -0700662pub fn parse<T: parse::Parse>(tokens: proc_macro::TokenStream) -> Result<T, Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700663 parse::Parser::parse(T::parse, tokens)
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700664}
665
David Tolnayccab0be2018-01-06 22:24:47 -0800666/// Parse a proc-macro2 token stream into the chosen syntax tree node.
667///
668/// This function parses a `proc_macro2::TokenStream` which is commonly useful
669/// when the input comes from a node of the Syn syntax tree, for example the tts
670/// of a [`Macro`] node. When in a procedural macro parsing the
671/// `proc_macro::TokenStream` provided by the compiler, use [`syn::parse`]
672/// instead.
673///
674/// [`Macro`]: struct.Macro.html
675/// [`syn::parse`]: fn.parse.html
David Tolnay461d98e2018-01-07 11:07:19 -0800676///
677/// *This function is available if Syn is built with the `"parsing"` feature.*
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700678#[cfg(feature = "parsing")]
David Tolnaye82a2b12018-08-30 16:31:10 -0700679pub fn parse2<T: parse::Parse>(tokens: proc_macro2::TokenStream) -> Result<T, Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700680 parse::Parser::parse2(T::parse, tokens)
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700681}
Alex Crichton954046c2017-05-30 21:49:42 -0700682
David Tolnayccab0be2018-01-06 22:24:47 -0800683/// Parse a string of Rust code into the chosen syntax tree node.
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700684///
David Tolnay461d98e2018-01-07 11:07:19 -0800685/// *This function is available if Syn is built with the `"parsing"` feature.*
686///
David Tolnay3f5b06f2018-01-11 21:02:00 -0800687/// # Hygiene
688///
689/// Every span in the resulting syntax tree will be set to resolve at the macro
690/// call site.
691///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700692/// # Examples
693///
694/// ```rust
David Tolnay9b00f652018-09-01 10:31:02 -0700695/// # extern crate syn;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700696/// #
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700697/// use syn::Expr;
David Tolnay9b00f652018-09-01 10:31:02 -0700698/// use syn::parse::Result;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700699///
700/// fn run() -> Result<()> {
701/// let code = "assert_eq!(u8::max_value(), 255)";
702/// let expr = syn::parse_str::<Expr>(code)?;
703/// println!("{:#?}", expr);
704/// Ok(())
705/// }
706/// #
707/// # fn main() { run().unwrap() }
708/// ```
709#[cfg(feature = "parsing")]
David Tolnaye82a2b12018-08-30 16:31:10 -0700710pub fn parse_str<T: parse::Parse>(s: &str) -> Result<T, Error> {
David Tolnay80a914f2018-08-30 23:49:53 -0700711 parse::Parser::parse_str(T::parse, s)
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700712}
Alex Crichton954046c2017-05-30 21:49:42 -0700713
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700714// FIXME the name parse_file makes it sound like you might pass in a path to a
715// file, rather than the content.
716/// Parse the content of a file of Rust code.
717///
718/// This is different from `syn::parse_str::<File>(content)` in two ways:
719///
720/// - It discards a leading byte order mark `\u{FEFF}` if the file has one.
721/// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`.
722///
723/// If present, either of these would be an error using `from_str`.
724///
Christopher Serr9727ef22018-02-03 16:42:12 +0100725/// *This function is available if Syn is built with the `"parsing"` and `"full"` features.*
David Tolnay461d98e2018-01-07 11:07:19 -0800726///
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700727/// # Examples
728///
729/// ```rust,no_run
David Tolnay9b00f652018-09-01 10:31:02 -0700730/// # extern crate syn;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700731/// #
David Tolnay9b00f652018-09-01 10:31:02 -0700732/// use std::error::Error;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700733/// use std::fs::File;
734/// use std::io::Read;
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700735///
David Tolnay9b00f652018-09-01 10:31:02 -0700736/// fn run() -> Result<(), Box<Error>> {
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700737/// let mut file = File::open("path/to/code.rs")?;
738/// let mut content = String::new();
739/// file.read_to_string(&mut content)?;
740///
741/// let ast = syn::parse_file(&content)?;
742/// if let Some(shebang) = ast.shebang {
743/// println!("{}", shebang);
744/// }
745/// println!("{} items", ast.items.len());
746///
747/// Ok(())
748/// }
749/// #
750/// # fn main() { run().unwrap() }
751/// ```
752#[cfg(all(feature = "parsing", feature = "full"))]
David Tolnayad4b2472018-08-25 08:25:24 -0400753pub fn parse_file(mut content: &str) -> Result<File, Error> {
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700754 // Strip the BOM if it is present
755 const BOM: &'static str = "\u{feff}";
756 if content.starts_with(BOM) {
757 content = &content[BOM.len()..];
David Tolnay35161ff2016-09-03 11:33:15 -0700758 }
Michael Layzell5e107ff2017-01-24 19:58:39 -0500759
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700760 let mut shebang = None;
761 if content.starts_with("#!") && !content.starts_with("#![") {
762 if let Some(idx) = content.find('\n') {
763 shebang = Some(content[..idx].to_string());
764 content = &content[idx..];
765 } else {
766 shebang = Some(content.to_string());
767 content = "";
768 }
Alex Crichton954046c2017-05-30 21:49:42 -0700769 }
David Tolnay0a8972b2017-02-27 02:10:01 -0800770
David Tolnayc7a5d3d2017-06-04 12:11:05 -0700771 let mut file: File = parse_str(content)?;
772 file.shebang = shebang;
773 Ok(file)
Michael Layzell5e107ff2017-01-24 19:58:39 -0500774}