blob: f30683f61a7382917b27f90dc7bb1f5229ebee94 [file] [log] [blame]
David Tolnayad2836d2017-04-20 10:11:43 -07001#![doc(html_root_url = "https://dtolnay.github.io/syn")]
2
David Tolnay02a8d472017-02-19 12:59:44 -08003#![cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
David Tolnayaed77b02016-09-23 20:50:31 -07004
David Tolnay87d0b442016-09-04 11:52:12 -07005#[cfg(feature = "printing")]
6extern crate quote;
7
David Tolnayb5a7b142016-09-13 22:46:39 -07008#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -07009extern crate unicode_xid;
10
11#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070012#[macro_use]
David Tolnay5fe14fc2017-01-27 16:22:08 -080013extern crate synom;
David Tolnay35161ff2016-09-03 11:33:15 -070014
David Tolnay631cb8c2016-11-10 17:16:41 -080015#[cfg(feature = "aster")]
16pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070017
Alex Crichton62a0a592017-05-22 13:58:53 -070018#[macro_use]
19mod macros;
20
David Tolnayb79ee962016-09-04 09:39:20 -070021mod attr;
Alex Crichton62a0a592017-05-22 13:58:53 -070022pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem, MetaItemList,
23 MetaNameValue};
David Tolnay35161ff2016-09-03 11:33:15 -070024
David Tolnay3cb23a92016-10-07 23:02:21 -070025mod constant;
Alex Crichton62a0a592017-05-22 13:58:53 -070026pub use constant::{ConstExpr, ConstCall, ConstBinary, ConstUnary, ConstCast,
27 ConstIndex, ConstParen};
David Tolnay3cb23a92016-10-07 23:02:21 -070028
David Tolnayf38cdf62016-09-23 19:07:09 -070029mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070030pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070031
David Tolnay631cb8c2016-11-10 17:16:41 -080032#[cfg(feature = "parsing")]
33mod escape;
34
David Tolnayf4bbbd92016-09-23 14:41:55 -070035#[cfg(feature = "full")]
36mod expr;
37#[cfg(feature = "full")]
David Tolnay05120ef2017-03-12 18:29:26 -070038pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, Local,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 MacStmtStyle, Pat, RangeLimits, Stmt, ExprBox, ExprInPlace,
40 ExprArray, ExprCall, ExprMethodCall, ExprTup, ExprBinary, ExprUnary,
41 ExprCast, ExprType, ExprIf, ExprIfLet, ExprWhile, ExprWhileLet,
42 ExprForLoop, ExprLoop, ExprMatch, ExprClosure, ExprBlock,
43 ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex,
44 ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue,
45 ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch};
David Tolnayf4bbbd92016-09-23 14:41:55 -070046
David Tolnayb79ee962016-09-04 09:39:20 -070047mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070048pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080049 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
50 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070051#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080052pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070053
David Tolnay55337722016-09-11 12:58:56 -070054mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070055pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070056
David Tolnayf38cdf62016-09-23 19:07:09 -070057#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070058mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070059#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -070060pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ItemForeignMod,
David Tolnayc1fea502016-10-30 17:54:02 -070061 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
Alex Crichton62a0a592017-05-22 13:58:53 -070062 TraitItem, TraitItemKind, ViewPath, ItemExternCrate, ItemUse,
63 ItemStatic, ItemConst, ItemFn, ItemMod, ItemTy, ItemEnum,
64 ItemStruct, ItemUnion, ItemTrait, ItemDefaultImpl, ItemImpl,
65 PathSimple, PathGlob, PathList, ForeignItemFn, ForeignItemStatic,
66 TraitItemConst, TraitItemMethod, TraitItemType,
67 ImplItemConst, ImplItemMethod, ImplItemType, ArgSelfRef,
68 ArgSelf, ArgCaptured};
David Tolnay35161ff2016-09-03 11:33:15 -070069
David Tolnay631cb8c2016-11-10 17:16:41 -080070#[cfg(feature = "full")]
71mod krate;
72#[cfg(feature = "full")]
73pub use krate::Crate;
74
David Tolnayf4bbbd92016-09-23 14:41:55 -070075mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070076pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnay1f16b602017-02-07 20:06:55 -050077#[cfg(feature = "parsing")]
78pub use lit::{ByteStrLit, FloatLit, IntLit, StrLit};
David Tolnayf4bbbd92016-09-23 14:41:55 -070079
David Tolnayf4bbbd92016-09-23 14:41:55 -070080mod mac;
David Tolnaydaaf7742016-10-03 11:11:43 -070081pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070082
David Tolnay0e837402016-12-22 17:25:55 -050083mod derive;
84pub use derive::{Body, DeriveInput};
David Tolnayc28a4a52017-02-04 09:50:57 -080085// Deprecated. Use `DeriveInput` instead.
David Tolnay0e837402016-12-22 17:25:55 -050086#[doc(hidden)]
87pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070088
David Tolnay3cb23a92016-10-07 23:02:21 -070089mod op;
90pub use op::{BinOp, UnOp};
91
David Tolnayb79ee962016-09-04 09:39:20 -070092mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070093pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
94 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
Alex Crichton62a0a592017-05-22 13:58:53 -070095 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety, TySlice, TyArray,
96 TyPtr, TyRptr, TyBareFn, TyNever, TyTup, TyPath, TyTraitObject,
97 TyImplTrait, TyParen, TyInfer};
David Tolnay35161ff2016-09-03 11:33:15 -070098
David Tolnay55337722016-09-11 12:58:56 -070099#[cfg(feature = "visit")]
100pub mod visit;
101
gnzlbg9ae88d82017-01-26 20:45:17 +0100102#[cfg(feature = "fold")]
103pub mod fold;
104
David Tolnay55337722016-09-11 12:58:56 -0700105#[cfg(feature = "parsing")]
106pub use parsing::*;
107
108#[cfg(feature = "parsing")]
109mod parsing {
Ted Driggs054abbb2017-05-01 12:20:52 -0700110 use std::str::FromStr;
111
David Tolnay55337722016-09-11 12:58:56 -0700112 use super::*;
Colin Kiegel108a7fe2017-03-06 13:29:57 +0100113 use {derive, generics, ident, mac, ty, attr};
David Tolnay5fe14fc2017-01-27 16:22:08 -0800114 use synom::{space, IResult};
David Tolnay55337722016-09-11 12:58:56 -0700115
David Tolnayedf2b992016-09-23 20:43:45 -0700116 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -0800117 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -0700118
Ted Driggs054abbb2017-05-01 12:20:52 -0700119 /// Parse the stringified representation of a struct or enum passed
120 /// to a `proc_macro_derive` function.
David Tolnay0e837402016-12-22 17:25:55 -0500121 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
122 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700123 }
124
David Tolnayedf2b992016-09-23 20:43:45 -0700125 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700126 pub fn parse_crate(input: &str) -> Result<Crate, String> {
127 unwrap("crate", krate::parsing::krate, input)
128 }
129
130 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700131 pub fn parse_item(input: &str) -> Result<Item, String> {
132 unwrap("item", item::parsing::item, input)
133 }
134
David Tolnayb9c8e322016-09-23 20:48:37 -0700135 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700136 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
137 unwrap("items", item::parsing::items, input)
138 }
139
140 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700141 pub fn parse_expr(input: &str) -> Result<Expr, String> {
142 unwrap("expression", expr::parsing::expr, input)
143 }
144
David Tolnay32a112e2016-09-11 17:46:15 -0700145 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700146 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700147 }
148
Ted Driggs054abbb2017-05-01 12:20:52 -0700149 /// Parse a path, such as `std::str::FromStr` or `::syn::parse_path`.
David Tolnay55337722016-09-11 12:58:56 -0700150 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700151 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700152 }
153
154 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700155 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700156 }
157
Simon Sapin095a42b2016-10-20 15:54:23 +0200158 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
159 unwrap("token trees", mac::parsing::token_trees, input)
160 }
161
David Tolnaya8228362016-12-22 15:21:54 -0500162 pub fn parse_ident(input: &str) -> Result<Ident, String> {
163 unwrap("identifier", ident::parsing::ident, input)
164 }
165
David Tolnay23d83f92017-01-25 15:41:47 -0800166 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
David Tolnay05120ef2017-03-12 18:29:26 -0700167 unwrap("type parameter bound",
168 generics::parsing::ty_param_bound,
169 input)
David Tolnay23d83f92017-01-25 15:41:47 -0800170 }
171
Ted Driggs054abbb2017-05-01 12:20:52 -0700172 /// Parse an attribute declared outside the item it annotates, such as
173 /// a struct annotation. They are written as `#[...]`.
Colin Kiegelcb724432017-03-06 12:59:18 +0100174 pub fn parse_outer_attr(input: &str) -> Result<Attribute, String> {
175 unwrap("outer attribute", attr::parsing::outer_attr, input)
176 }
177
Ted Driggs054abbb2017-05-01 12:20:52 -0700178 /// Parse an attribute declared inside the item it annotates. These are used
179 /// for crate annotations or for mod-level declarations when modules are in
180 /// their own files. They are written as `#![...]`.
Colin Kiegelcb724432017-03-06 12:59:18 +0100181 #[cfg(feature = "full")]
182 pub fn parse_inner_attr(input: &str) -> Result<Attribute, String> {
183 unwrap("inner attribute", attr::parsing::inner_attr, input)
184 }
185
Ted Driggs054abbb2017-05-01 12:20:52 -0700186 /// Deprecated: Use `parse_derive_input` instead.
David Tolnay0e837402016-12-22 17:25:55 -0500187 #[doc(hidden)]
Ted Driggs054abbb2017-05-01 12:20:52 -0700188 #[deprecated(since="0.11.0", note = "Use `parse_derive_input` instead")]
David Tolnay0e837402016-12-22 17:25:55 -0500189 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
190 parse_derive_input(input)
191 }
192
Ted Driggs054abbb2017-05-01 12:20:52 -0700193 /// Alias for `syn::parse_derive_input`.
194 impl FromStr for DeriveInput {
195 type Err = String;
196
197 fn from_str(s: &str) -> Result<Self, Self::Err> {
198 parse_derive_input(s)
199 }
200 }
201
202 /// Alias for `syn::parse_crate`.
203 #[cfg(feature = "full")]
204 impl FromStr for Crate {
205 type Err = String;
206
207 fn from_str(s: &str) -> Result<Self, Self::Err> {
208 parse_crate(s)
209 }
210 }
211
212 /// Alias for `syn::parse_item`.
213 #[cfg(feature = "full")]
214 impl FromStr for Item {
215 type Err = String;
216
217 fn from_str(s: &str) -> Result<Self, Self::Err> {
218 parse_item(s)
219 }
220 }
221
222 /// Alias for `syn::parse_expr`.
223 #[cfg(feature = "full")]
224 impl FromStr for Expr {
225 type Err = String;
226
227 fn from_str(s: &str) -> Result<Self, Self::Err> {
228 parse_expr(s)
229 }
230 }
231
232 /// Alias for `syn::parse_type`.
233 impl FromStr for Ty {
234 type Err = String;
235
236 fn from_str(s: &str) -> Result<Self, Self::Err> {
237 parse_type(s)
238 }
239 }
240
241 /// Alias for `syn::parse_path`.
242 impl FromStr for Path {
243 type Err = String;
244
245 fn from_str(s: &str) -> Result<Self, Self::Err> {
246 parse_path(s)
247 }
248 }
249
250 /// Alias for `syn::parse_where_clause`.
251 impl FromStr for WhereClause {
252 type Err = String;
253
254 fn from_str(s: &str) -> Result<Self, Self::Err> {
255 parse_where_clause(s)
256 }
257 }
258
259 /// Alias for `syn::parse_ident`.
260 impl FromStr for Ident {
261 type Err = String;
262
263 fn from_str(s: &str) -> Result<Self, Self::Err> {
264 parse_ident(s)
265 }
266 }
267
268 /// Alias for `syn::parse_ty_param_bound`.
269 impl FromStr for TyParamBound {
270 type Err = String;
271
272 fn from_str(s: &str) -> Result<Self, Self::Err> {
273 parse_ty_param_bound(s)
274 }
275 }
276
David Tolnaydaaf7742016-10-03 11:11:43 -0700277 fn unwrap<T>(name: &'static str,
278 f: fn(&str) -> IResult<&str, T>,
279 input: &str)
280 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700281 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700282 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700283 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700284 if rest.is_empty() {
285 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700286 } else if rest.len() == input.len() {
287 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700288 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700289 } else {
David Tolnayf2222f02017-01-27 17:09:20 -0800290 Err(format!("unparsed tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700291 }
David Tolnay55337722016-09-11 12:58:56 -0700292 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700293 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700294 }
David Tolnay35161ff2016-09-03 11:33:15 -0700295 }
296}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500297
298#[cfg(feature = "parsing")]
David Tolnay5fe14fc2017-01-27 16:22:08 -0800299pub mod parse {
Michael Layzell5e107ff2017-01-24 19:58:39 -0500300 //! This module contains a set of exported nom parsers which can be used to
David Tolnay5fe14fc2017-01-27 16:22:08 -0800301 //! parse custom grammars when used alongside the `synom` crate.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500302 //!
David Tolnay5fe14fc2017-01-27 16:22:08 -0800303 //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a
304 //! persistent pitfall of using `nom` to parse Rust by eliminating the
305 //! `IResult::Incomplete` variant. The `synom` crate should be used instead
306 //! of `nom` when working with the parsers in this module.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500307
David Tolnay0a8972b2017-02-27 02:10:01 -0800308 pub use synom::IResult;
309
Michael Layzell5e107ff2017-01-24 19:58:39 -0500310 #[cfg(feature = "full")]
311 pub use item::parsing::item;
312
313 #[cfg(feature = "full")]
David Tolnay1bf19132017-02-19 22:54:25 -0800314 pub use expr::parsing::{expr, pat, block, stmt};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500315
David Tolnay0ee41892017-02-19 22:52:59 -0800316 pub use lit::parsing::{lit, string, byte_string, byte, character, float, int, boolean};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500317
David Tolnay0ee41892017-02-19 22:52:59 -0800318 pub use ty::parsing::{ty, path};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500319
David Tolnay5fe14fc2017-01-27 16:22:08 -0800320 pub use mac::parsing::token_tree as tt;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500321
322 pub use ident::parsing::ident;
David Tolnay1f16b602017-02-07 20:06:55 -0500323
324 pub use generics::parsing::lifetime;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500325}