blob: e18e7e668732ecd773ebcf6b0abbc7f5f543be4e [file] [log] [blame]
David Tolnayaed77b02016-09-23 20:50:31 -07001#![cfg_attr(feature = "clippy", feature(plugin))]
2#![cfg_attr(feature = "clippy", plugin(clippy))]
3
David Tolnay87d0b442016-09-04 11:52:12 -07004#[cfg(feature = "printing")]
5extern crate quote;
6
David Tolnayb5a7b142016-09-13 22:46:39 -07007#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -07008extern crate unicode_xid;
9
10#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070011#[macro_use]
Michael Layzell5bde96f2017-01-24 17:59:21 -050012extern crate syn_nom as nom;
David Tolnay35161ff2016-09-03 11:33:15 -070013
David Tolnay631cb8c2016-11-10 17:16:41 -080014#[cfg(feature = "aster")]
15pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070016
David Tolnayb79ee962016-09-04 09:39:20 -070017mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070018pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070019
David Tolnay3cb23a92016-10-07 23:02:21 -070020mod constant;
21pub use constant::ConstExpr;
22
David Tolnayf38cdf62016-09-23 19:07:09 -070023mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070024pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070025
David Tolnay631cb8c2016-11-10 17:16:41 -080026#[cfg(feature = "parsing")]
27mod escape;
28
David Tolnayf4bbbd92016-09-23 14:41:55 -070029#[cfg(feature = "full")]
30mod expr;
31#[cfg(feature = "full")]
David Tolnay7b035912016-12-21 22:42:07 -050032pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue,
33 Local, MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070034
David Tolnayb79ee962016-09-04 09:39:20 -070035mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070036pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080037 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
38 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070039#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080040pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070041
David Tolnay55337722016-09-11 12:58:56 -070042mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070043pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070044
David Tolnayf38cdf62016-09-23 19:07:09 -070045#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070046mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070047#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070048pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
49 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
50 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070051
David Tolnay631cb8c2016-11-10 17:16:41 -080052#[cfg(feature = "full")]
53mod krate;
54#[cfg(feature = "full")]
55pub use krate::Crate;
56
David Tolnayf4bbbd92016-09-23 14:41:55 -070057mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070058pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070059
David Tolnayf4bbbd92016-09-23 14:41:55 -070060mod mac;
David Tolnaydaaf7742016-10-03 11:11:43 -070061pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070062
David Tolnay0e837402016-12-22 17:25:55 -050063mod derive;
64pub use derive::{Body, DeriveInput};
65// Deprecated.
66#[doc(hidden)]
67pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070068
David Tolnay3cb23a92016-10-07 23:02:21 -070069mod op;
70pub use op::{BinOp, UnOp};
71
David Tolnayb79ee962016-09-04 09:39:20 -070072mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070073pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
74 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
75 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070076
David Tolnay55337722016-09-11 12:58:56 -070077#[cfg(feature = "visit")]
78pub mod visit;
79
80#[cfg(feature = "parsing")]
81pub use parsing::*;
82
83#[cfg(feature = "parsing")]
84mod parsing {
85 use super::*;
Michael Layzell5bde96f2017-01-24 17:59:21 -050086 use {derive, generics, ident, mac, ty};
87 use nom::{space, IResult};
David Tolnay55337722016-09-11 12:58:56 -070088
David Tolnayedf2b992016-09-23 20:43:45 -070089 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -080090 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070091
David Tolnay0e837402016-12-22 17:25:55 -050092 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
93 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -070094 }
95
David Tolnayedf2b992016-09-23 20:43:45 -070096 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -070097 pub fn parse_crate(input: &str) -> Result<Crate, String> {
98 unwrap("crate", krate::parsing::krate, input)
99 }
100
101 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700102 pub fn parse_item(input: &str) -> Result<Item, String> {
103 unwrap("item", item::parsing::item, input)
104 }
105
David Tolnayb9c8e322016-09-23 20:48:37 -0700106 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700107 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
108 unwrap("items", item::parsing::items, input)
109 }
110
111 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700112 pub fn parse_expr(input: &str) -> Result<Expr, String> {
113 unwrap("expression", expr::parsing::expr, input)
114 }
115
David Tolnay32a112e2016-09-11 17:46:15 -0700116 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700117 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700118 }
119
David Tolnay55337722016-09-11 12:58:56 -0700120 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700121 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700122 }
123
124 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700125 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700126 }
127
Simon Sapin095a42b2016-10-20 15:54:23 +0200128 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
129 unwrap("token trees", mac::parsing::token_trees, input)
130 }
131
David Tolnaya8228362016-12-22 15:21:54 -0500132 pub fn parse_ident(input: &str) -> Result<Ident, String> {
133 unwrap("identifier", ident::parsing::ident, input)
134 }
135
David Tolnay23d83f92017-01-25 15:41:47 -0800136 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
137 unwrap("type parameter bound", generics::parsing::ty_param_bound, input)
138 }
139
David Tolnay0e837402016-12-22 17:25:55 -0500140 // Deprecated.
141 #[doc(hidden)]
142 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
143 parse_derive_input(input)
144 }
145
David Tolnaydaaf7742016-10-03 11:11:43 -0700146 fn unwrap<T>(name: &'static str,
147 f: fn(&str) -> IResult<&str, T>,
148 input: &str)
149 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700150 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700151 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700152 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700153 if rest.is_empty() {
154 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700155 } else if rest.len() == input.len() {
156 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700157 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700158 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700159 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700160 }
David Tolnay55337722016-09-11 12:58:56 -0700161 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700162 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700163 }
David Tolnay35161ff2016-09-03 11:33:15 -0700164 }
165}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500166
167#[cfg(feature = "parsing")]
168pub mod parser {
169 //! This module contains a set of exported nom parsers which can be used to
170 //! build nom parsers for custom grammars when used alongside the `syn_nom`
171 //! crate.
172 //!
173 //! `syn` uses its own custom fork of `nom`, `syn_nom`, which is smaller, and
174 //! thus improves build speeds. This should be used instead of `nom` when
175 //! building parsers using these parsers.
176
177 #[cfg(feature = "full")]
178 pub use krate::parsing::krate;
179
180 #[cfg(feature = "full")]
181 pub use item::parsing::item;
182
183 #[cfg(feature = "full")]
184 pub use item::parsing::items;
185
186 #[cfg(feature = "full")]
187 pub use expr::parsing::expr;
188
189 pub use lit::parsing::lit;
190
191 pub use lit::parsing::string as str_lit;
192
193 pub use lit::parsing::byte_string as byte_str_lit;
194
195 pub use lit::parsing::byte as byte_lit;
196
197 pub use lit::parsing::character as char_lit;
198
199 pub use lit::parsing::float as float_lit;
200
201 pub use lit::parsing::int as int_lit;
202
203 pub use lit::parsing::boolean as bool_lit;
204
205 pub use ty::parsing::ty;
206
207 pub use ty::parsing::path;
208
209 pub use generics::parsing::where_clause;
210
211 #[cfg(feature = "full")]
212 pub use mac::parsing::token_trees;
213
214 pub use ident::parsing::ident;
215}