blob: b422c6c674ca49a3a4899f016868d5dee68c22e7 [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
Alex Crichtonccbb45d2017-05-23 10:58:24 -07005extern crate proc_macro2;
6
David Tolnay87d0b442016-09-04 11:52:12 -07007#[cfg(feature = "printing")]
8extern crate quote;
9
David Tolnayb5a7b142016-09-13 22:46:39 -070010#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -070011extern crate unicode_xid;
12
Alex Crichtonccbb45d2017-05-23 10:58:24 -070013#[cfg_attr(feature = "parsing", macro_use)]
David Tolnay5fe14fc2017-01-27 16:22:08 -080014extern crate synom;
David Tolnay35161ff2016-09-03 11:33:15 -070015
David Tolnay631cb8c2016-11-10 17:16:41 -080016#[cfg(feature = "aster")]
17pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019#[macro_use]
20mod macros;
21
David Tolnayb79ee962016-09-04 09:39:20 -070022mod attr;
Alex Crichton62a0a592017-05-22 13:58:53 -070023pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem, MetaItemList,
24 MetaNameValue};
David Tolnay35161ff2016-09-03 11:33:15 -070025
David Tolnay3cb23a92016-10-07 23:02:21 -070026mod constant;
Alex Crichton62a0a592017-05-22 13:58:53 -070027pub use constant::{ConstExpr, ConstCall, ConstBinary, ConstUnary, ConstCast,
28 ConstIndex, ConstParen};
David Tolnay3cb23a92016-10-07 23:02:21 -070029
David Tolnayf38cdf62016-09-23 19:07:09 -070030mod data;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070031pub use data::{Field, Variant, VariantData, Visibility, VisRestricted, VisCrate,
32 VisPublic, VisInherited};
David Tolnayf38cdf62016-09-23 19:07:09 -070033
David Tolnay631cb8c2016-11-10 17:16:41 -080034#[cfg(feature = "parsing")]
35mod escape;
36
David Tolnayf4bbbd92016-09-23 14:41:55 -070037#[cfg(feature = "full")]
38mod expr;
39#[cfg(feature = "full")]
David Tolnay05120ef2017-03-12 18:29:26 -070040pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, Local,
Alex Crichton62a0a592017-05-22 13:58:53 -070041 MacStmtStyle, Pat, RangeLimits, Stmt, ExprBox, ExprInPlace,
42 ExprArray, ExprCall, ExprMethodCall, ExprTup, ExprBinary, ExprUnary,
43 ExprCast, ExprType, ExprIf, ExprIfLet, ExprWhile, ExprWhileLet,
44 ExprForLoop, ExprLoop, ExprMatch, ExprClosure, ExprBlock,
45 ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex,
46 ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070047 ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch,
48 PatIdent, PatWild, PatStruct, PatTuple, PatTupleStruct, PatPath,
49 PatBox, PatRef, PatLit, PatRange, PatSlice};
David Tolnayf4bbbd92016-09-23 14:41:55 -070050
David Tolnayb79ee962016-09-04 09:39:20 -070051mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070052pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080053 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070054 WhereRegionPredicate, BoundLifetimes};
David Tolnaye7678922016-10-13 20:44:03 -070055#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080056pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070057
David Tolnay55337722016-09-11 12:58:56 -070058mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070059pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070060
David Tolnayf38cdf62016-09-23 19:07:09 -070061#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070062mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070063#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -070064pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ItemForeignMod,
David Tolnayc1fea502016-10-30 17:54:02 -070065 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 TraitItem, TraitItemKind, ViewPath, ItemExternCrate, ItemUse,
67 ItemStatic, ItemConst, ItemFn, ItemMod, ItemTy, ItemEnum,
68 ItemStruct, ItemUnion, ItemTrait, ItemDefaultImpl, ItemImpl,
69 PathSimple, PathGlob, PathList, ForeignItemFn, ForeignItemStatic,
70 TraitItemConst, TraitItemMethod, TraitItemType,
71 ImplItemConst, ImplItemMethod, ImplItemType, ArgSelfRef,
72 ArgSelf, ArgCaptured};
David Tolnay35161ff2016-09-03 11:33:15 -070073
David Tolnay631cb8c2016-11-10 17:16:41 -080074#[cfg(feature = "full")]
75mod krate;
76#[cfg(feature = "full")]
77pub use krate::Crate;
78
David Tolnayf4bbbd92016-09-23 14:41:55 -070079mod lit;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070080pub use lit::{Lit, LitKind};
David Tolnayf4bbbd92016-09-23 14:41:55 -070081
David Tolnayf4bbbd92016-09-23 14:41:55 -070082mod mac;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070083pub use mac::{Mac, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070084
David Tolnay0e837402016-12-22 17:25:55 -050085mod derive;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070086pub use derive::{Body, DeriveInput, BodyEnum, BodyStruct};
David Tolnayc28a4a52017-02-04 09:50:57 -080087// Deprecated. Use `DeriveInput` instead.
David Tolnay0e837402016-12-22 17:25:55 -050088#[doc(hidden)]
89pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070090
David Tolnay3cb23a92016-10-07 23:02:21 -070091mod op;
92pub use op::{BinOp, UnOp};
93
David Tolnayb79ee962016-09-04 09:39:20 -070094mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070095pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
96 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety, TySlice, TyArray,
98 TyPtr, TyRptr, TyBareFn, TyNever, TyTup, TyPath, TyTraitObject,
99 TyImplTrait, TyParen, TyInfer};
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100#[cfg(feature = "printing")]
101pub use ty::PathTokens;
102
103mod span;
104pub use span::Span;
105
106pub mod tokens;
107pub use synom::delimited;
David Tolnay35161ff2016-09-03 11:33:15 -0700108
David Tolnay55337722016-09-11 12:58:56 -0700109#[cfg(feature = "visit")]
110pub mod visit;
111
gnzlbg9ae88d82017-01-26 20:45:17 +0100112#[cfg(feature = "fold")]
113pub mod fold;
114
David Tolnay55337722016-09-11 12:58:56 -0700115#[cfg(feature = "parsing")]
116pub use parsing::*;
117
118#[cfg(feature = "parsing")]
119mod parsing {
Ted Driggs054abbb2017-05-01 12:20:52 -0700120 use std::str::FromStr;
121
David Tolnay55337722016-09-11 12:58:56 -0700122 use super::*;
Colin Kiegel108a7fe2017-03-06 13:29:57 +0100123 use {derive, generics, ident, mac, ty, attr};
David Tolnay5fe14fc2017-01-27 16:22:08 -0800124 use synom::{space, IResult};
David Tolnay55337722016-09-11 12:58:56 -0700125
David Tolnayedf2b992016-09-23 20:43:45 -0700126 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -0800127 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -0700128
Ted Driggs054abbb2017-05-01 12:20:52 -0700129 /// Parse the stringified representation of a struct or enum passed
130 /// to a `proc_macro_derive` function.
David Tolnay0e837402016-12-22 17:25:55 -0500131 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
132 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700133 }
134
David Tolnayedf2b992016-09-23 20:43:45 -0700135 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700136 pub fn parse_crate(input: &str) -> Result<Crate, String> {
137 unwrap("crate", krate::parsing::krate, input)
138 }
139
140 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700141 pub fn parse_item(input: &str) -> Result<Item, String> {
142 unwrap("item", item::parsing::item, input)
143 }
144
David Tolnayb9c8e322016-09-23 20:48:37 -0700145 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700146 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
147 unwrap("items", item::parsing::items, input)
148 }
149
150 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700151 pub fn parse_expr(input: &str) -> Result<Expr, String> {
152 unwrap("expression", expr::parsing::expr, input)
153 }
154
David Tolnay32a112e2016-09-11 17:46:15 -0700155 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700156 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700157 }
158
Ted Driggs054abbb2017-05-01 12:20:52 -0700159 /// Parse a path, such as `std::str::FromStr` or `::syn::parse_path`.
David Tolnay55337722016-09-11 12:58:56 -0700160 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700161 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700162 }
163
164 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700165 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700166 }
167
Simon Sapin095a42b2016-10-20 15:54:23 +0200168 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
169 unwrap("token trees", mac::parsing::token_trees, input)
170 }
171
David Tolnaya8228362016-12-22 15:21:54 -0500172 pub fn parse_ident(input: &str) -> Result<Ident, String> {
173 unwrap("identifier", ident::parsing::ident, input)
174 }
175
David Tolnay23d83f92017-01-25 15:41:47 -0800176 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
David Tolnay05120ef2017-03-12 18:29:26 -0700177 unwrap("type parameter bound",
178 generics::parsing::ty_param_bound,
179 input)
David Tolnay23d83f92017-01-25 15:41:47 -0800180 }
181
Ted Driggs054abbb2017-05-01 12:20:52 -0700182 /// Parse an attribute declared outside the item it annotates, such as
183 /// a struct annotation. They are written as `#[...]`.
Colin Kiegelcb724432017-03-06 12:59:18 +0100184 pub fn parse_outer_attr(input: &str) -> Result<Attribute, String> {
185 unwrap("outer attribute", attr::parsing::outer_attr, input)
186 }
187
Ted Driggs054abbb2017-05-01 12:20:52 -0700188 /// Parse an attribute declared inside the item it annotates. These are used
189 /// for crate annotations or for mod-level declarations when modules are in
190 /// their own files. They are written as `#![...]`.
Colin Kiegelcb724432017-03-06 12:59:18 +0100191 #[cfg(feature = "full")]
192 pub fn parse_inner_attr(input: &str) -> Result<Attribute, String> {
193 unwrap("inner attribute", attr::parsing::inner_attr, input)
194 }
195
Ted Driggs054abbb2017-05-01 12:20:52 -0700196 /// Deprecated: Use `parse_derive_input` instead.
David Tolnay0e837402016-12-22 17:25:55 -0500197 #[doc(hidden)]
Ted Driggs054abbb2017-05-01 12:20:52 -0700198 #[deprecated(since="0.11.0", note = "Use `parse_derive_input` instead")]
David Tolnay0e837402016-12-22 17:25:55 -0500199 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
200 parse_derive_input(input)
201 }
202
Ted Driggs054abbb2017-05-01 12:20:52 -0700203 /// Alias for `syn::parse_derive_input`.
204 impl FromStr for DeriveInput {
205 type Err = String;
206
207 fn from_str(s: &str) -> Result<Self, Self::Err> {
208 parse_derive_input(s)
209 }
210 }
211
212 /// Alias for `syn::parse_crate`.
213 #[cfg(feature = "full")]
214 impl FromStr for Crate {
215 type Err = String;
216
217 fn from_str(s: &str) -> Result<Self, Self::Err> {
218 parse_crate(s)
219 }
220 }
221
222 /// Alias for `syn::parse_item`.
223 #[cfg(feature = "full")]
224 impl FromStr for Item {
225 type Err = String;
226
227 fn from_str(s: &str) -> Result<Self, Self::Err> {
228 parse_item(s)
229 }
230 }
231
232 /// Alias for `syn::parse_expr`.
233 #[cfg(feature = "full")]
234 impl FromStr for Expr {
235 type Err = String;
236
237 fn from_str(s: &str) -> Result<Self, Self::Err> {
238 parse_expr(s)
239 }
240 }
241
242 /// Alias for `syn::parse_type`.
243 impl FromStr for Ty {
244 type Err = String;
245
246 fn from_str(s: &str) -> Result<Self, Self::Err> {
247 parse_type(s)
248 }
249 }
250
251 /// Alias for `syn::parse_path`.
252 impl FromStr for Path {
253 type Err = String;
254
255 fn from_str(s: &str) -> Result<Self, Self::Err> {
256 parse_path(s)
257 }
258 }
259
260 /// Alias for `syn::parse_where_clause`.
261 impl FromStr for WhereClause {
262 type Err = String;
263
264 fn from_str(s: &str) -> Result<Self, Self::Err> {
265 parse_where_clause(s)
266 }
267 }
268
269 /// Alias for `syn::parse_ident`.
270 impl FromStr for Ident {
271 type Err = String;
272
273 fn from_str(s: &str) -> Result<Self, Self::Err> {
274 parse_ident(s)
275 }
276 }
277
278 /// Alias for `syn::parse_ty_param_bound`.
279 impl FromStr for TyParamBound {
280 type Err = String;
281
282 fn from_str(s: &str) -> Result<Self, Self::Err> {
283 parse_ty_param_bound(s)
284 }
285 }
286
David Tolnaydaaf7742016-10-03 11:11:43 -0700287 fn unwrap<T>(name: &'static str,
288 f: fn(&str) -> IResult<&str, T>,
289 input: &str)
290 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700291 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700292 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700293 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700294 if rest.is_empty() {
295 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700296 } else if rest.len() == input.len() {
297 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700298 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700299 } else {
David Tolnayf2222f02017-01-27 17:09:20 -0800300 Err(format!("unparsed tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700301 }
David Tolnay55337722016-09-11 12:58:56 -0700302 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700303 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700304 }
David Tolnay35161ff2016-09-03 11:33:15 -0700305 }
306}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500307
308#[cfg(feature = "parsing")]
David Tolnay5fe14fc2017-01-27 16:22:08 -0800309pub mod parse {
Michael Layzell5e107ff2017-01-24 19:58:39 -0500310 //! This module contains a set of exported nom parsers which can be used to
David Tolnay5fe14fc2017-01-27 16:22:08 -0800311 //! parse custom grammars when used alongside the `synom` crate.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500312 //!
David Tolnay5fe14fc2017-01-27 16:22:08 -0800313 //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a
314 //! persistent pitfall of using `nom` to parse Rust by eliminating the
315 //! `IResult::Incomplete` variant. The `synom` crate should be used instead
316 //! of `nom` when working with the parsers in this module.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500317
David Tolnay0a8972b2017-02-27 02:10:01 -0800318 pub use synom::IResult;
319
Michael Layzell5e107ff2017-01-24 19:58:39 -0500320 #[cfg(feature = "full")]
321 pub use item::parsing::item;
322
323 #[cfg(feature = "full")]
David Tolnay1bf19132017-02-19 22:54:25 -0800324 pub use expr::parsing::{expr, pat, block, stmt};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500325
David Tolnay0ee41892017-02-19 22:52:59 -0800326 pub use lit::parsing::{lit, string, byte_string, byte, character, float, int, boolean};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500327
David Tolnay0ee41892017-02-19 22:52:59 -0800328 pub use ty::parsing::{ty, path};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500329
David Tolnay5fe14fc2017-01-27 16:22:08 -0800330 pub use mac::parsing::token_tree as tt;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500331
332 pub use ident::parsing::ident;
David Tolnay1f16b602017-02-07 20:06:55 -0500333
334 pub use generics::parsing::lifetime;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500335}