David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // 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 Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 9 | //! Syn is a parsing library for parsing a stream of Rust tokens into a syntax |
| 10 | //! tree of Rust source code. |
| 11 | //! |
David Tolnay | 6b889eb | 2018-09-01 18:12:17 -0700 | [diff] [blame] | 12 | //! Currently this library is geared toward use in Rust procedural macros, but |
| 13 | //! contains some APIs that may be useful more generally. |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 14 | //! |
| 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 Tolnay | 6b889eb | 2018-09-01 18:12:17 -0700 | [diff] [blame] | 26 | //! - **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 Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 31 | //! |
| 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 Tolnay | 6b889eb | 2018-09-01 18:12:17 -0700 | [diff] [blame] | 47 | //! [parser functions]: parse/index.html |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 48 | //! |
| 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 Tolnay | b28acf3 | 2018-09-06 09:01:40 -0700 | [diff] [blame] | 68 | //! syn = "0.15" |
David Tolnay | 87003d0 | 2018-05-20 19:45:13 -0700 | [diff] [blame] | 69 | //! quote = "0.6" |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 70 | //! |
| 71 | //! [lib] |
| 72 | //! proc-macro = true |
| 73 | //! ``` |
| 74 | //! |
| 75 | //! ```rust |
David Tolnay | a1c9807 | 2018-09-06 08:58:10 -0700 | [diff] [blame] | 76 | //! #[macro_use] |
| 77 | //! extern crate quote; |
| 78 | //! #[macro_use] |
| 79 | //! extern crate syn; |
| 80 | //! |
| 81 | //! extern crate proc_macro; |
| 82 | //! |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 83 | //! use proc_macro::TokenStream; |
David Tolnay | a1c9807 | 2018-09-06 08:58:10 -0700 | [diff] [blame] | 84 | //! use syn::DeriveInput; |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 85 | //! |
| 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 Tolnay | 6b889eb | 2018-09-01 18:12:17 -0700 | [diff] [blame] | 91 | //! let input = parse_macro_input!(input as DeriveInput); |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 92 | //! |
| 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 Tolnay | 35b498e | 2018-09-01 20:10:40 -0700 | [diff] [blame] | 99 | //! TokenStream::from(expanded) |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 100 | //! } |
| 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 Tolnay | efff2ff | 2018-01-07 11:49:52 -0800 | [diff] [blame] | 179 | //! | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HeapSize` is not implemented for `Thread` |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 180 | //! ``` |
| 181 | //! |
David Tolnay | 6b889eb | 2018-09-01 18:12:17 -0700 | [diff] [blame] | 182 | //! ## Parsing a custom syntax |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 183 | //! |
| 184 | //! The [`lazy-static`] example directory shows the implementation of a |
| 185 | //! `functionlike!(...)` procedural macro in which the input tokens are parsed |
David Tolnay | 6b889eb | 2018-09-01 18:12:17 -0700 | [diff] [blame] | 186 | //! using Syn's parsing API. |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 187 | //! |
| 188 | //! [`lazy-static`]: https://github.com/dtolnay/syn/tree/master/examples/lazy-static |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 189 | //! |
| 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 Tolnay | 324db2d | 2018-01-07 11:51:09 -0800 | [diff] [blame] | 220 | //! [`cargo expand`]: https://github.com/dtolnay/cargo-expand |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 221 | //! |
| 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 Tolnay | 34981cf | 2018-01-06 16:22:35 -0800 | [diff] [blame] | 247 | //! - **`visit-mut`** — Trait for traversing and mutating in place a syntax |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 248 | //! 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. |
hcpl | 4b72a38 | 2018-04-04 14:50:24 +0300 | [diff] [blame] | 254 | //! - **`proc-macro`** *(enabled by default)* — Runtime dependency on the |
| 255 | //! dynamic library libproc_macro from rustc toolchain. |
David Tolnay | 5e84e97 | 2018-01-05 17:51:06 -0800 | [diff] [blame] | 256 | |
David Tolnay | 4cf7db8 | 2018-01-07 15:22:01 -0800 | [diff] [blame] | 257 | // Syn types in rustdoc of other crates get linked to here. |
David Tolnay | 0587ab2 | 2018-10-13 19:56:16 -0700 | [diff] [blame^] | 258 | #![doc(html_root_url = "https://docs.rs/syn/0.15.11")] |
David Tolnay | 46b17c0 | 2018-09-22 13:27:40 -0700 | [diff] [blame] | 259 | #![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))] |
David Tolnay | 34071ba | 2018-05-20 20:00:41 -0700 | [diff] [blame] | 260 | #![cfg_attr(feature = "cargo-clippy", deny(clippy, clippy_pedantic))] |
David Tolnay | 34071ba | 2018-05-20 20:00:41 -0700 | [diff] [blame] | 261 | // Ignored clippy lints. |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 262 | #![cfg_attr( |
| 263 | feature = "cargo-clippy", |
| 264 | allow( |
David Tolnay | 4831ac6 | 2018-08-30 21:04:16 -0700 | [diff] [blame] | 265 | block_in_if_condition_stmt, |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 266 | const_static_lifetime, |
David Tolnay | 24b079d | 2018-08-27 08:28:10 -0700 | [diff] [blame] | 267 | cyclomatic_complexity, |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 268 | doc_markdown, |
David Tolnay | 24b079d | 2018-08-27 08:28:10 -0700 | [diff] [blame] | 269 | eval_order_dependence, |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 270 | large_enum_variant, |
| 271 | match_bool, |
David Tolnay | 6fb8746 | 2018-09-01 16:51:49 -0700 | [diff] [blame] | 272 | never_loop, |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 273 | redundant_closure, |
| 274 | needless_pass_by_value, |
| 275 | redundant_field_names, |
| 276 | trivially_copy_pass_by_ref |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 277 | ) |
| 278 | )] |
David Tolnay | 34071ba | 2018-05-20 20:00:41 -0700 | [diff] [blame] | 279 | // Ignored clippy_pedantic lints. |
| 280 | #![cfg_attr( |
| 281 | feature = "cargo-clippy", |
| 282 | allow( |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 283 | cast_possible_truncation, |
| 284 | cast_possible_wrap, |
David Tolnay | b161775 | 2018-08-24 21:16:33 -0400 | [diff] [blame] | 285 | empty_enum, |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 286 | if_not_else, |
| 287 | indexing_slicing, |
| 288 | items_after_statements, |
David Tolnay | 151f92f | 2018-08-14 22:44:53 -0700 | [diff] [blame] | 289 | shadow_unrelated, |
David Tolnay | 0cec3e6 | 2018-07-21 09:08:30 -0700 | [diff] [blame] | 290 | similar_names, |
| 291 | single_match_else, |
| 292 | stutter, |
| 293 | unseparated_literal_suffix, |
| 294 | use_self, |
| 295 | used_underscore_binding |
David Tolnay | 34071ba | 2018-05-20 20:00:41 -0700 | [diff] [blame] | 296 | ) |
| 297 | )] |
David Tolnay | b503da6 | 2018-10-12 21:29:26 -0700 | [diff] [blame] | 298 | // False positive: https://github.com/rust-lang-nursery/rust-clippy/issues/3274 |
| 299 | #![cfg_attr(feature = "cargo-clippy", allow(map_clone))] |
David Tolnay | ad2836d | 2017-04-20 10:11:43 -0700 | [diff] [blame] | 300 | |
David Tolnay | 278f9e3 | 2018-08-14 22:41:11 -0700 | [diff] [blame] | 301 | #[cfg(all( |
| 302 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 303 | feature = "proc-macro" |
| 304 | ))] |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 305 | extern crate proc_macro; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 306 | extern crate proc_macro2; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 307 | extern crate unicode_xid; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 308 | |
David Tolnay | 1cf8091 | 2017-12-31 18:35:12 -0500 | [diff] [blame] | 309 | #[cfg(feature = "printing")] |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 310 | extern crate quote; |
| 311 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 312 | #[macro_use] |
| 313 | mod macros; |
| 314 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 315 | // Not public API. |
David Tolnay | 852bff7 | 2018-08-27 08:24:02 -0700 | [diff] [blame] | 316 | #[cfg(feature = "parsing")] |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 317 | #[doc(hidden)] |
David Tolnay | 852bff7 | 2018-08-27 08:24:02 -0700 | [diff] [blame] | 318 | #[macro_use] |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 319 | pub mod group; |
David Tolnay | 852bff7 | 2018-08-27 08:24:02 -0700 | [diff] [blame] | 320 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 321 | #[macro_use] |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 322 | pub mod token; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 323 | |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 324 | mod ident; |
| 325 | pub use ident::Ident; |
David Tolnay | e303b7c | 2018-05-20 16:46:35 -0700 | [diff] [blame] | 326 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 327 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 328 | mod attr; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 329 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 161f2de | 2018-10-13 14:38:20 -0700 | [diff] [blame] | 330 | pub use attr::{AttrStyle, Attribute, AttributeArgs, Meta, MetaList, MetaNameValue, NestedMeta}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 331 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 332 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 333 | mod data; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 334 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 335 | pub use data::{ |
| 336 | Field, Fields, FieldsNamed, FieldsUnnamed, Variant, VisCrate, VisPublic, VisRestricted, |
| 337 | Visibility, |
| 338 | }; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 339 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 340 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 341 | mod expr; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 342 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 343 | pub use expr::{ |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 344 | Expr, ExprArray, ExprAssign, ExprAssignOp, ExprAsync, ExprBinary, ExprBlock, ExprBox, |
| 345 | ExprBreak, ExprCall, ExprCast, ExprClosure, ExprContinue, ExprField, ExprForLoop, ExprGroup, |
David Tolnay | 9c11912 | 2018-09-01 18:47:02 -0700 | [diff] [blame] | 346 | ExprIf, ExprInPlace, ExprIndex, ExprLet, ExprLit, ExprLoop, ExprMacro, ExprMatch, |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 347 | ExprMethodCall, ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, |
| 348 | ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprVerbatim, |
David Tolnay | 9c11912 | 2018-09-01 18:47:02 -0700 | [diff] [blame] | 349 | ExprWhile, ExprYield, Index, Member, |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 350 | }; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 351 | |
| 352 | #[cfg(feature = "full")] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 353 | pub use expr::{ |
| 354 | Arm, Block, FieldPat, FieldValue, GenericMethodArgument, Label, Local, MethodTurbofish, Pat, |
| 355 | PatBox, PatIdent, PatLit, PatMacro, PatPath, PatRange, PatRef, PatSlice, PatStruct, PatTuple, |
| 356 | PatTupleStruct, PatVerbatim, PatWild, RangeLimits, Stmt, |
| 357 | }; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 358 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 359 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 360 | mod generics; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 361 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 362 | pub use generics::{ |
| 363 | BoundLifetimes, ConstParam, GenericParam, Generics, LifetimeDef, PredicateEq, |
| 364 | PredicateLifetime, PredicateType, TraitBound, TraitBoundModifier, TypeParam, TypeParamBound, |
| 365 | WhereClause, WherePredicate, |
| 366 | }; |
David Tolnay | 278f9e3 | 2018-08-14 22:41:11 -0700 | [diff] [blame] | 367 | #[cfg(all( |
| 368 | any(feature = "full", feature = "derive"), |
| 369 | feature = "printing" |
| 370 | ))] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 371 | pub use generics::{ImplGenerics, Turbofish, TypeGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 372 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 373 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 374 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 375 | #[cfg(feature = "full")] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 376 | pub use item::{ |
David Tolnay | 435c178 | 2018-08-24 16:15:44 -0400 | [diff] [blame] | 377 | ArgCaptured, ArgSelf, ArgSelfRef, FnArg, FnDecl, ForeignItem, ForeignItemFn, ForeignItemMacro, |
| 378 | ForeignItemStatic, ForeignItemType, ForeignItemVerbatim, ImplItem, ImplItemConst, |
David Tolnay | bb82ef0 | 2018-08-24 20:15:45 -0400 | [diff] [blame] | 379 | ImplItemExistential, ImplItemMacro, ImplItemMethod, ImplItemType, ImplItemVerbatim, Item, |
| 380 | ItemConst, ItemEnum, ItemExistential, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, |
David Tolnay | c6b04dd | 2018-08-30 23:22:51 -0700 | [diff] [blame] | 381 | ItemMacro, ItemMacro2, ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, |
| 382 | ItemUnion, ItemUse, ItemVerbatim, MethodSig, TraitItem, TraitItemConst, TraitItemMacro, |
| 383 | TraitItemMethod, TraitItemType, TraitItemVerbatim, UseGlob, UseGroup, UseName, UsePath, |
| 384 | UseRename, UseTree, |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 385 | }; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 386 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 387 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 388 | mod file; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 389 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 390 | pub use file::File; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 391 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 392 | mod lifetime; |
| 393 | pub use lifetime::Lifetime; |
| 394 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 395 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 396 | mod lit; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 397 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 398 | pub use lit::{ |
| 399 | FloatSuffix, IntSuffix, Lit, LitBool, LitByte, LitByteStr, LitChar, LitFloat, LitInt, LitStr, |
| 400 | LitVerbatim, StrStyle, |
| 401 | }; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 402 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 403 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 404 | mod mac; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 405 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 406 | pub use mac::{Macro, MacroDelimiter}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 407 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 408 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 409 | mod derive; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 410 | #[cfg(feature = "derive")] |
David Tolnay | e3d41b7 | 2017-12-31 15:24:00 -0500 | [diff] [blame] | 411 | pub use derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 412 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 413 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 414 | mod op; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 415 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 416 | pub use op::{BinOp, UnOp}; |
| 417 | |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 418 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 419 | mod ty; |
David Tolnay | 3cfd1d3 | 2018-01-03 00:22:08 -0800 | [diff] [blame] | 420 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 421 | pub use ty::{ |
| 422 | Abi, BareFnArg, BareFnArgName, ReturnType, Type, TypeArray, TypeBareFn, TypeGroup, |
| 423 | TypeImplTrait, TypeInfer, TypeMacro, TypeNever, TypeParen, TypePath, TypePtr, TypeReference, |
| 424 | TypeSlice, TypeTraitObject, TypeTuple, TypeVerbatim, |
| 425 | }; |
David Tolnay | 056de30 | 2018-01-05 14:29:05 -0800 | [diff] [blame] | 426 | |
| 427 | #[cfg(any(feature = "full", feature = "derive"))] |
| 428 | mod path; |
| 429 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 430 | pub use path::{ |
David Tolnay | 9d0882a | 2018-09-01 19:49:14 -0700 | [diff] [blame] | 431 | AngleBracketedGenericArguments, Binding, Constraint, GenericArgument, |
| 432 | ParenthesizedGenericArguments, Path, PathArguments, PathSegment, QSelf, |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 433 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 434 | |
David Tolnay | 1b752fb | 2017-12-26 21:41:39 -0500 | [diff] [blame] | 435 | #[cfg(feature = "parsing")] |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 436 | pub mod buffer; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 437 | #[cfg(feature = "parsing")] |
| 438 | pub mod ext; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 439 | pub mod punctuated; |
David Tolnay | 3779bb7 | 2018-08-26 18:46:07 -0700 | [diff] [blame] | 440 | #[cfg(all( |
| 441 | any(feature = "full", feature = "derive"), |
| 442 | feature = "extra-traits" |
| 443 | ))] |
David Tolnay | e082403 | 2017-12-27 15:25:56 -0500 | [diff] [blame] | 444 | mod tt; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 445 | |
David Tolnay | e83ef5a | 2018-01-11 15:18:36 -0800 | [diff] [blame] | 446 | // Not public API except the `parse_quote!` macro. |
David Tolnay | 491680a | 2018-01-23 00:34:40 -0800 | [diff] [blame] | 447 | #[cfg(feature = "parsing")] |
David Tolnay | e83ef5a | 2018-01-11 15:18:36 -0800 | [diff] [blame] | 448 | #[doc(hidden)] |
| 449 | pub mod parse_quote; |
| 450 | |
David Tolnay | f98865f | 2018-10-13 14:37:07 -0700 | [diff] [blame] | 451 | // Not public API except the `parse_macro_input!` macro. |
| 452 | #[cfg(all( |
| 453 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 454 | feature = "parsing", |
| 455 | feature = "proc-macro" |
| 456 | ))] |
| 457 | #[doc(hidden)] |
| 458 | pub mod parse_macro_input; |
| 459 | |
David Tolnay | 4d942b4 | 2018-01-02 22:14:04 -0800 | [diff] [blame] | 460 | #[cfg(all(feature = "parsing", feature = "printing"))] |
David Tolnay | f790b61 | 2017-12-31 18:46:57 -0500 | [diff] [blame] | 461 | pub mod spanned; |
| 462 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 463 | mod gen { |
David Tolnay | ded2d68 | 2018-01-06 18:53:53 -0800 | [diff] [blame] | 464 | /// Syntax tree traversal to walk a shared borrow of a syntax tree. |
| 465 | /// |
| 466 | /// Each method of the [`Visit`] trait is a hook that can be overridden to |
| 467 | /// customize the behavior when visiting the corresponding type of node. By |
| 468 | /// default, every method recursively visits the substructure of the input |
| 469 | /// by invoking the right visitor method of each of its fields. |
| 470 | /// |
| 471 | /// [`Visit`]: trait.Visit.html |
| 472 | /// |
| 473 | /// ```rust |
| 474 | /// # use syn::{Attribute, BinOp, Expr, ExprBinary}; |
| 475 | /// # |
| 476 | /// pub trait Visit<'ast> { |
| 477 | /// /* ... */ |
| 478 | /// |
| 479 | /// fn visit_expr_binary(&mut self, node: &'ast ExprBinary) { |
| 480 | /// for attr in &node.attrs { |
| 481 | /// self.visit_attribute(attr); |
| 482 | /// } |
| 483 | /// self.visit_expr(&*node.left); |
| 484 | /// self.visit_bin_op(&node.op); |
| 485 | /// self.visit_expr(&*node.right); |
| 486 | /// } |
| 487 | /// |
| 488 | /// /* ... */ |
| 489 | /// # fn visit_attribute(&mut self, node: &'ast Attribute); |
| 490 | /// # fn visit_expr(&mut self, node: &'ast Expr); |
| 491 | /// # fn visit_bin_op(&mut self, node: &'ast BinOp); |
| 492 | /// } |
| 493 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 494 | /// |
| 495 | /// *This module is available if Syn is built with the `"visit"` feature.* |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 496 | #[cfg(feature = "visit")] |
| 497 | pub mod visit; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 498 | |
David Tolnay | ded2d68 | 2018-01-06 18:53:53 -0800 | [diff] [blame] | 499 | /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in |
| 500 | /// place. |
| 501 | /// |
| 502 | /// Each method of the [`VisitMut`] trait is a hook that can be overridden |
| 503 | /// to customize the behavior when mutating the corresponding type of node. |
| 504 | /// By default, every method recursively visits the substructure of the |
| 505 | /// input by invoking the right visitor method of each of its fields. |
| 506 | /// |
| 507 | /// [`VisitMut`]: trait.VisitMut.html |
| 508 | /// |
| 509 | /// ```rust |
| 510 | /// # use syn::{Attribute, BinOp, Expr, ExprBinary}; |
| 511 | /// # |
| 512 | /// pub trait VisitMut { |
| 513 | /// /* ... */ |
| 514 | /// |
| 515 | /// fn visit_expr_binary_mut(&mut self, node: &mut ExprBinary) { |
| 516 | /// for attr in &mut node.attrs { |
| 517 | /// self.visit_attribute_mut(attr); |
| 518 | /// } |
| 519 | /// self.visit_expr_mut(&mut *node.left); |
| 520 | /// self.visit_bin_op_mut(&mut node.op); |
| 521 | /// self.visit_expr_mut(&mut *node.right); |
| 522 | /// } |
| 523 | /// |
| 524 | /// /* ... */ |
| 525 | /// # fn visit_attribute_mut(&mut self, node: &mut Attribute); |
| 526 | /// # fn visit_expr_mut(&mut self, node: &mut Expr); |
| 527 | /// # fn visit_bin_op_mut(&mut self, node: &mut BinOp); |
| 528 | /// } |
| 529 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 530 | /// |
| 531 | /// *This module is available if Syn is built with the `"visit-mut"` |
| 532 | /// feature.* |
David Tolnay | 9df02c4 | 2018-01-06 13:52:48 -0800 | [diff] [blame] | 533 | #[cfg(feature = "visit-mut")] |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 534 | pub mod visit_mut; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 535 | |
David Tolnay | ded2d68 | 2018-01-06 18:53:53 -0800 | [diff] [blame] | 536 | /// Syntax tree traversal to transform the nodes of an owned syntax tree. |
| 537 | /// |
| 538 | /// Each method of the [`Fold`] trait is a hook that can be overridden to |
| 539 | /// customize the behavior when transforming the corresponding type of node. |
| 540 | /// By default, every method recursively visits the substructure of the |
| 541 | /// input by invoking the right visitor method of each of its fields. |
| 542 | /// |
| 543 | /// [`Fold`]: trait.Fold.html |
| 544 | /// |
| 545 | /// ```rust |
| 546 | /// # use syn::{Attribute, BinOp, Expr, ExprBinary}; |
| 547 | /// # |
| 548 | /// pub trait Fold { |
| 549 | /// /* ... */ |
| 550 | /// |
| 551 | /// fn fold_expr_binary(&mut self, node: ExprBinary) -> ExprBinary { |
| 552 | /// ExprBinary { |
| 553 | /// attrs: node.attrs |
| 554 | /// .into_iter() |
| 555 | /// .map(|attr| self.fold_attribute(attr)) |
| 556 | /// .collect(), |
| 557 | /// left: Box::new(self.fold_expr(*node.left)), |
| 558 | /// op: self.fold_bin_op(node.op), |
| 559 | /// right: Box::new(self.fold_expr(*node.right)), |
| 560 | /// } |
| 561 | /// } |
| 562 | /// |
| 563 | /// /* ... */ |
| 564 | /// # fn fold_attribute(&mut self, node: Attribute) -> Attribute; |
| 565 | /// # fn fold_expr(&mut self, node: Expr) -> Expr; |
| 566 | /// # fn fold_bin_op(&mut self, node: BinOp) -> BinOp; |
| 567 | /// } |
| 568 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 569 | /// |
| 570 | /// *This module is available if Syn is built with the `"fold"` feature.* |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 571 | #[cfg(feature = "fold")] |
| 572 | pub mod fold; |
David Tolnay | f60f426 | 2017-12-28 19:17:58 -0500 | [diff] [blame] | 573 | |
David Tolnay | 0a0d78c | 2018-01-05 15:24:01 -0800 | [diff] [blame] | 574 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | f60f426 | 2017-12-28 19:17:58 -0500 | [diff] [blame] | 575 | #[path = "../gen_helper.rs"] |
| 576 | mod helper; |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 577 | } |
| 578 | pub use gen::*; |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 579 | |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 580 | // Not public API. |
| 581 | #[doc(hidden)] |
| 582 | pub mod export; |
| 583 | |
David Tolnay | 7fb11e7 | 2018-09-06 01:02:27 -0700 | [diff] [blame] | 584 | mod keyword; |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 585 | |
| 586 | #[cfg(feature = "parsing")] |
David Tolnay | 7fb11e7 | 2018-09-06 01:02:27 -0700 | [diff] [blame] | 587 | mod lookahead; |
Louis Kureuil Person | c0beaf3 | 2018-09-05 00:12:43 +0200 | [diff] [blame] | 588 | |
| 589 | #[cfg(feature = "parsing")] |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 590 | pub mod parse; |
| 591 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 592 | mod span; |
| 593 | |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 594 | #[cfg(all( |
| 595 | any(feature = "full", feature = "derive"), |
| 596 | feature = "printing" |
| 597 | ))] |
| 598 | mod print; |
| 599 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 600 | //////////////////////////////////////////////////////////////////////////////// |
| 601 | |
David Tolnay | aa77a85 | 2018-08-31 11:15:10 -0700 | [diff] [blame] | 602 | #[cfg(any(feature = "parsing", feature = "full", feature = "derive"))] |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 603 | #[allow(non_camel_case_types)] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 604 | struct private; |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 605 | |
| 606 | //////////////////////////////////////////////////////////////////////////////// |
| 607 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 608 | #[cfg(feature = "parsing")] |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 609 | mod error; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 610 | #[cfg(feature = "parsing")] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 611 | use error::Error; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 612 | |
David Tolnay | ccab0be | 2018-01-06 22:24:47 -0800 | [diff] [blame] | 613 | /// Parse tokens of source code into the chosen syntax tree node. |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 614 | /// |
| 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 Tolnay | ccab0be | 2018-01-06 22:24:47 -0800 | [diff] [blame] | 620 | /// 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 | /// |
hcpl | 4b72a38 | 2018-04-04 14:50:24 +0300 | [diff] [blame] | 626 | /// *This function is available if Syn is built with both the `"parsing"` and |
| 627 | /// `"proc-macro"` features.* |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 628 | /// |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 629 | /// # Examples |
| 630 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 631 | /// ```rust |
David Tolnay | a1c9807 | 2018-09-06 08:58:10 -0700 | [diff] [blame] | 632 | /// #[macro_use] |
| 633 | /// extern crate quote; |
| 634 | /// |
| 635 | /// extern crate proc_macro; |
| 636 | /// extern crate syn; |
| 637 | /// |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 638 | /// use proc_macro::TokenStream; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 639 | /// use syn::DeriveInput; |
| 640 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 641 | /// # const IGNORE_TOKENS: &str = stringify! { |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 642 | /// #[proc_macro_derive(MyMacro)] |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 643 | /// # }; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 644 | /// pub fn my_macro(input: TokenStream) -> TokenStream { |
| 645 | /// // Parse the tokens into a syntax tree |
| 646 | /// let ast: DeriveInput = syn::parse(input).unwrap(); |
| 647 | /// |
| 648 | /// // Build the output, possibly using quasi-quotation |
| 649 | /// let expanded = quote! { |
| 650 | /// /* ... */ |
| 651 | /// }; |
| 652 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 653 | /// // Convert into a token stream and return it |
| 654 | /// expanded.into() |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 655 | /// } |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 656 | /// # |
| 657 | /// # fn main() {} |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 658 | /// ``` |
David Tolnay | 278f9e3 | 2018-08-14 22:41:11 -0700 | [diff] [blame] | 659 | #[cfg(all( |
| 660 | not(all(target_arch = "wasm32", target_os = "unknown")), |
| 661 | feature = "parsing", |
| 662 | feature = "proc-macro" |
| 663 | ))] |
David Tolnay | e82a2b1 | 2018-08-30 16:31:10 -0700 | [diff] [blame] | 664 | pub fn parse<T: parse::Parse>(tokens: proc_macro::TokenStream) -> Result<T, Error> { |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 665 | parse::Parser::parse(T::parse, tokens) |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 666 | } |
| 667 | |
David Tolnay | ccab0be | 2018-01-06 22:24:47 -0800 | [diff] [blame] | 668 | /// Parse a proc-macro2 token stream into the chosen syntax tree node. |
| 669 | /// |
| 670 | /// This function parses a `proc_macro2::TokenStream` which is commonly useful |
| 671 | /// when the input comes from a node of the Syn syntax tree, for example the tts |
| 672 | /// of a [`Macro`] node. When in a procedural macro parsing the |
| 673 | /// `proc_macro::TokenStream` provided by the compiler, use [`syn::parse`] |
| 674 | /// instead. |
| 675 | /// |
| 676 | /// [`Macro`]: struct.Macro.html |
| 677 | /// [`syn::parse`]: fn.parse.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 678 | /// |
| 679 | /// *This function is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 680 | #[cfg(feature = "parsing")] |
David Tolnay | e82a2b1 | 2018-08-30 16:31:10 -0700 | [diff] [blame] | 681 | pub fn parse2<T: parse::Parse>(tokens: proc_macro2::TokenStream) -> Result<T, Error> { |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 682 | parse::Parser::parse2(T::parse, tokens) |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 683 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 684 | |
David Tolnay | ccab0be | 2018-01-06 22:24:47 -0800 | [diff] [blame] | 685 | /// Parse a string of Rust code into the chosen syntax tree node. |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 686 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 687 | /// *This function is available if Syn is built with the `"parsing"` feature.* |
| 688 | /// |
David Tolnay | 3f5b06f | 2018-01-11 21:02:00 -0800 | [diff] [blame] | 689 | /// # Hygiene |
| 690 | /// |
| 691 | /// Every span in the resulting syntax tree will be set to resolve at the macro |
| 692 | /// call site. |
| 693 | /// |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 694 | /// # Examples |
| 695 | /// |
| 696 | /// ```rust |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 697 | /// # extern crate syn; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 698 | /// # |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 699 | /// use syn::Expr; |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 700 | /// use syn::parse::Result; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 701 | /// |
| 702 | /// fn run() -> Result<()> { |
| 703 | /// let code = "assert_eq!(u8::max_value(), 255)"; |
| 704 | /// let expr = syn::parse_str::<Expr>(code)?; |
| 705 | /// println!("{:#?}", expr); |
| 706 | /// Ok(()) |
| 707 | /// } |
| 708 | /// # |
| 709 | /// # fn main() { run().unwrap() } |
| 710 | /// ``` |
| 711 | #[cfg(feature = "parsing")] |
David Tolnay | e82a2b1 | 2018-08-30 16:31:10 -0700 | [diff] [blame] | 712 | pub fn parse_str<T: parse::Parse>(s: &str) -> Result<T, Error> { |
David Tolnay | 80a914f | 2018-08-30 23:49:53 -0700 | [diff] [blame] | 713 | parse::Parser::parse_str(T::parse, s) |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 714 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 715 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 716 | // FIXME the name parse_file makes it sound like you might pass in a path to a |
| 717 | // file, rather than the content. |
| 718 | /// Parse the content of a file of Rust code. |
| 719 | /// |
| 720 | /// This is different from `syn::parse_str::<File>(content)` in two ways: |
| 721 | /// |
| 722 | /// - It discards a leading byte order mark `\u{FEFF}` if the file has one. |
| 723 | /// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`. |
| 724 | /// |
| 725 | /// If present, either of these would be an error using `from_str`. |
| 726 | /// |
Christopher Serr | 9727ef2 | 2018-02-03 16:42:12 +0100 | [diff] [blame] | 727 | /// *This function is available if Syn is built with the `"parsing"` and `"full"` features.* |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 728 | /// |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 729 | /// # Examples |
| 730 | /// |
| 731 | /// ```rust,no_run |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 732 | /// # extern crate syn; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 733 | /// # |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 734 | /// use std::error::Error; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 735 | /// use std::fs::File; |
| 736 | /// use std::io::Read; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 737 | /// |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 738 | /// fn run() -> Result<(), Box<Error>> { |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 739 | /// let mut file = File::open("path/to/code.rs")?; |
| 740 | /// let mut content = String::new(); |
| 741 | /// file.read_to_string(&mut content)?; |
| 742 | /// |
| 743 | /// let ast = syn::parse_file(&content)?; |
| 744 | /// if let Some(shebang) = ast.shebang { |
| 745 | /// println!("{}", shebang); |
| 746 | /// } |
| 747 | /// println!("{} items", ast.items.len()); |
| 748 | /// |
| 749 | /// Ok(()) |
| 750 | /// } |
| 751 | /// # |
| 752 | /// # fn main() { run().unwrap() } |
| 753 | /// ``` |
| 754 | #[cfg(all(feature = "parsing", feature = "full"))] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 755 | pub fn parse_file(mut content: &str) -> Result<File, Error> { |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 756 | // Strip the BOM if it is present |
| 757 | const BOM: &'static str = "\u{feff}"; |
| 758 | if content.starts_with(BOM) { |
| 759 | content = &content[BOM.len()..]; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 760 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 761 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 762 | let mut shebang = None; |
| 763 | if content.starts_with("#!") && !content.starts_with("#![") { |
| 764 | if let Some(idx) = content.find('\n') { |
| 765 | shebang = Some(content[..idx].to_string()); |
| 766 | content = &content[idx..]; |
| 767 | } else { |
| 768 | shebang = Some(content.to_string()); |
| 769 | content = ""; |
| 770 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 771 | } |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 772 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 773 | let mut file: File = parse_str(content)?; |
| 774 | file.shebang = shebang; |
| 775 | Ok(file) |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 776 | } |