blob: 7e9533c31bc0a60a25fe61a942e955e28d68fc32 [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")]
Michael Layzell416724e2017-05-24 21:12:34 -04009extern crate relex;
David Tolnay10413f02016-09-30 09:12:02 -070010
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
David Tolnayb79ee962016-09-04 09:39:20 -070018mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070019pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070020
David Tolnay3cb23a92016-10-07 23:02:21 -070021mod constant;
22pub use constant::ConstExpr;
23
David Tolnayf38cdf62016-09-23 19:07:09 -070024mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070025pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070026
David Tolnayf4bbbd92016-09-23 14:41:55 -070027#[cfg(feature = "full")]
28mod expr;
29#[cfg(feature = "full")]
David Tolnay05120ef2017-03-12 18:29:26 -070030pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, Local,
31 MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070032
David Tolnayb79ee962016-09-04 09:39:20 -070033mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070034pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080035 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
36 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070037#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080038pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070039
David Tolnay55337722016-09-11 12:58:56 -070040mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070041pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070042
David Tolnayf38cdf62016-09-23 19:07:09 -070043#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070044mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070045#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070046pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
47 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
48 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070049
David Tolnay631cb8c2016-11-10 17:16:41 -080050#[cfg(feature = "full")]
51mod krate;
52#[cfg(feature = "full")]
53pub use krate::Crate;
54
David Tolnayf4bbbd92016-09-23 14:41:55 -070055mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070056pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnay1f16b602017-02-07 20:06:55 -050057#[cfg(feature = "parsing")]
58pub use lit::{ByteStrLit, FloatLit, IntLit, StrLit};
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};
David Tolnayc28a4a52017-02-04 09:50:57 -080065// Deprecated. Use `DeriveInput` instead.
David Tolnay0e837402016-12-22 17:25:55 -050066#[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
gnzlbg9ae88d82017-01-26 20:45:17 +010080#[cfg(feature = "fold")]
81pub mod fold;
82
David Tolnay55337722016-09-11 12:58:56 -070083#[cfg(feature = "parsing")]
84pub use parsing::*;
85
86#[cfg(feature = "parsing")]
87mod parsing {
Ted Driggs054abbb2017-05-01 12:20:52 -070088 use std::str::FromStr;
89
David Tolnay55337722016-09-11 12:58:56 -070090 use super::*;
Colin Kiegel108a7fe2017-03-06 13:29:57 +010091 use {derive, generics, ident, mac, ty, attr};
Michael Layzell416724e2017-05-24 21:12:34 -040092 use synom::{IResult, TokenStream};
93
94 use std::convert::From;
95 use std::error::Error;
96 use std::fmt;
David Tolnay55337722016-09-11 12:58:56 -070097
David Tolnayedf2b992016-09-23 20:43:45 -070098 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -080099 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -0700100
Michael Layzell416724e2017-05-24 21:12:34 -0400101 #[derive(Debug)]
102 pub struct ParseError(String);
103
104 impl Error for ParseError {
105 fn description(&self) -> &str {
106 &self.0
107 }
108 }
109
110 impl fmt::Display for ParseError {
111 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112 <String as fmt::Display>::fmt(&self.0, f)
113 }
114 }
115
116 impl From<synom::LexError> for ParseError {
117 fn from(_: synom::LexError) -> ParseError {
118 ParseError("error while lexing input string".to_owned())
119 }
120 }
121
Ted Driggs054abbb2017-05-01 12:20:52 -0700122 /// Parse the stringified representation of a struct or enum passed
123 /// to a `proc_macro_derive` function.
Michael Layzell416724e2017-05-24 21:12:34 -0400124 pub fn parse_derive_input(input: TokenStream) -> Result<DeriveInput, ParseError> {
David Tolnay0e837402016-12-22 17:25:55 -0500125 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700126 }
127
Michael Layzell416724e2017-05-24 21:12:34 -0400128 /// Parse an entire crate into an AST. This function takes a string as input
129 /// instead of a TokenStream, as we need to handle parsing the BOM and
130 /// shebang from the string.
David Tolnayedf2b992016-09-23 20:43:45 -0700131 #[cfg(feature = "full")]
Michael Layzell416724e2017-05-24 21:12:34 -0400132 pub fn parse_crate(mut input: &str) -> Result<Crate, ParseError> {
133 // Strip the BOM if it is present
134 const BOM: &str = "\u{feff}";
135 if input.starts_with(BOM) {
136 input = &input[BOM.len()..];
137 }
138
139 let mut shebang = None;
140 if input.starts_with("#!") && !input.starts_with("#![") {
141 if let Some(idx) = input.find('\n') {
142 shebang = Some(input[..idx].to_string());
143 input = &input[idx..];
144 } else {
145 shebang = Some(input.to_string());
146 input = "";
147 }
148 }
149
150 let mut krate = unwrap("crate", krate::parsing::krate,
151 input.parse()?)?;
152 krate.shebang = shebang;
153 Ok(krate)
154
David Tolnay4a51dc72016-10-01 00:40:31 -0700155 }
156
157 #[cfg(feature = "full")]
Michael Layzell416724e2017-05-24 21:12:34 -0400158 pub fn parse_item(input: TokenStream) -> Result<Item, ParseError> {
David Tolnayedf2b992016-09-23 20:43:45 -0700159 unwrap("item", item::parsing::item, input)
160 }
161
David Tolnayb9c8e322016-09-23 20:48:37 -0700162 #[cfg(feature = "full")]
Michael Layzell416724e2017-05-24 21:12:34 -0400163 pub fn parse_items(input: TokenStream) -> Result<Vec<Item>, ParseError> {
David Tolnay453cfd12016-10-23 11:00:14 -0700164 unwrap("items", item::parsing::items, input)
165 }
166
167 #[cfg(feature = "full")]
Michael Layzell416724e2017-05-24 21:12:34 -0400168 pub fn parse_expr(input: TokenStream) -> Result<Expr, ParseError> {
David Tolnayb9c8e322016-09-23 20:48:37 -0700169 unwrap("expression", expr::parsing::expr, input)
170 }
171
Michael Layzell416724e2017-05-24 21:12:34 -0400172 pub fn parse_type(input: TokenStream) -> Result<Ty, ParseError> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700173 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700174 }
175
Ted Driggs054abbb2017-05-01 12:20:52 -0700176 /// Parse a path, such as `std::str::FromStr` or `::syn::parse_path`.
Michael Layzell416724e2017-05-24 21:12:34 -0400177 pub fn parse_path(input: TokenStream) -> Result<Path, ParseError> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700178 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700179 }
180
Michael Layzell416724e2017-05-24 21:12:34 -0400181 pub fn parse_where_clause(input: TokenStream) -> Result<WhereClause, ParseError> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700182 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700183 }
184
Michael Layzell416724e2017-05-24 21:12:34 -0400185 pub fn parse_token_trees(input: TokenStream) -> Result<Vec<TokenTree>, ParseError> {
Simon Sapin095a42b2016-10-20 15:54:23 +0200186 unwrap("token trees", mac::parsing::token_trees, input)
187 }
188
Michael Layzell416724e2017-05-24 21:12:34 -0400189 pub fn parse_ident(input: TokenStream) -> Result<Ident, ParseError> {
David Tolnaya8228362016-12-22 15:21:54 -0500190 unwrap("identifier", ident::parsing::ident, input)
191 }
192
Michael Layzell416724e2017-05-24 21:12:34 -0400193 pub fn parse_ty_param_bound(input: TokenStream) -> Result<TyParamBound, ParseError> {
David Tolnay05120ef2017-03-12 18:29:26 -0700194 unwrap("type parameter bound",
195 generics::parsing::ty_param_bound,
196 input)
David Tolnay23d83f92017-01-25 15:41:47 -0800197 }
198
Ted Driggs054abbb2017-05-01 12:20:52 -0700199 /// Parse an attribute declared outside the item it annotates, such as
200 /// a struct annotation. They are written as `#[...]`.
Michael Layzell416724e2017-05-24 21:12:34 -0400201 pub fn parse_outer_attr(input: TokenStream) -> Result<Attribute, ParseError> {
Colin Kiegelcb724432017-03-06 12:59:18 +0100202 unwrap("outer attribute", attr::parsing::outer_attr, input)
203 }
204
Ted Driggs054abbb2017-05-01 12:20:52 -0700205 /// Parse an attribute declared inside the item it annotates. These are used
206 /// for crate annotations or for mod-level declarations when modules are in
207 /// their own files. They are written as `#![...]`.
Colin Kiegelcb724432017-03-06 12:59:18 +0100208 #[cfg(feature = "full")]
Michael Layzell416724e2017-05-24 21:12:34 -0400209 pub fn parse_inner_attr(input: TokenStream) -> Result<Attribute, ParseError> {
Colin Kiegelcb724432017-03-06 12:59:18 +0100210 unwrap("inner attribute", attr::parsing::inner_attr, input)
211 }
212
Ted Driggs054abbb2017-05-01 12:20:52 -0700213 /// Deprecated: Use `parse_derive_input` instead.
David Tolnay0e837402016-12-22 17:25:55 -0500214 #[doc(hidden)]
Ted Driggs054abbb2017-05-01 12:20:52 -0700215 #[deprecated(since="0.11.0", note = "Use `parse_derive_input` instead")]
Michael Layzell416724e2017-05-24 21:12:34 -0400216 pub fn parse_macro_input(input: TokenStream) -> Result<MacroInput, ParseError> {
David Tolnay0e837402016-12-22 17:25:55 -0500217 parse_derive_input(input)
218 }
219
Ted Driggs054abbb2017-05-01 12:20:52 -0700220 /// Alias for `syn::parse_derive_input`.
221 impl FromStr for DeriveInput {
Michael Layzell416724e2017-05-24 21:12:34 -0400222 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700223
224 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400225 parse_derive_input(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700226 }
227 }
228
229 /// Alias for `syn::parse_crate`.
230 #[cfg(feature = "full")]
231 impl FromStr for Crate {
Michael Layzell416724e2017-05-24 21:12:34 -0400232 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700233
234 fn from_str(s: &str) -> Result<Self, Self::Err> {
235 parse_crate(s)
236 }
237 }
238
239 /// Alias for `syn::parse_item`.
240 #[cfg(feature = "full")]
241 impl FromStr for Item {
Michael Layzell416724e2017-05-24 21:12:34 -0400242 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700243
244 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400245 parse_item(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700246 }
247 }
248
249 /// Alias for `syn::parse_expr`.
250 #[cfg(feature = "full")]
251 impl FromStr for Expr {
Michael Layzell416724e2017-05-24 21:12:34 -0400252 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700253
254 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400255 parse_expr(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700256 }
257 }
258
259 /// Alias for `syn::parse_type`.
260 impl FromStr for Ty {
Michael Layzell416724e2017-05-24 21:12:34 -0400261 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700262
263 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400264 parse_type(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700265 }
266 }
267
268 /// Alias for `syn::parse_path`.
269 impl FromStr for Path {
Michael Layzell416724e2017-05-24 21:12:34 -0400270 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700271
272 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400273 parse_path(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700274 }
275 }
276
277 /// Alias for `syn::parse_where_clause`.
278 impl FromStr for WhereClause {
Michael Layzell416724e2017-05-24 21:12:34 -0400279 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700280
281 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400282 parse_where_clause(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700283 }
284 }
285
286 /// Alias for `syn::parse_ident`.
287 impl FromStr for Ident {
Michael Layzell416724e2017-05-24 21:12:34 -0400288 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700289
290 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400291 parse_ident(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700292 }
293 }
294
295 /// Alias for `syn::parse_ty_param_bound`.
296 impl FromStr for TyParamBound {
Michael Layzell416724e2017-05-24 21:12:34 -0400297 type Err = ParseError;
Ted Driggs054abbb2017-05-01 12:20:52 -0700298
299 fn from_str(s: &str) -> Result<Self, Self::Err> {
Michael Layzell416724e2017-05-24 21:12:34 -0400300 parse_ty_param_bound(s.parse()?)
Ted Driggs054abbb2017-05-01 12:20:52 -0700301 }
302 }
303
David Tolnaydaaf7742016-10-03 11:11:43 -0700304 fn unwrap<T>(name: &'static str,
Michael Layzell416724e2017-05-24 21:12:34 -0400305 f: fn(&[synom::TokenTree]) -> IResult<&[synom::TokenTree], T>,
306 input: TokenStream)
307 -> Result<T, ParseError> {
308 let input = synom::InputBuf::new(input);
309 match f(&input) {
310 IResult::Done(rest, t) => {
David Tolnay55337722016-09-11 12:58:56 -0700311 if rest.is_empty() {
312 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700313 } else if rest.len() == input.len() {
314 // parsed nothing
Michael Layzell416724e2017-05-24 21:12:34 -0400315 Err(ParseError(format!("failed to parse {}", name)))
David Tolnay55337722016-09-11 12:58:56 -0700316 } else {
Michael Layzell416724e2017-05-24 21:12:34 -0400317 Err(ParseError(format!("unparsed tokens after {}", name)))
David Tolnayc94c38a2016-09-05 17:02:03 -0700318 }
David Tolnay55337722016-09-11 12:58:56 -0700319 }
Michael Layzell416724e2017-05-24 21:12:34 -0400320 IResult::Error => Err(ParseError(format!("failed to parse {}", name))),
David Tolnay35161ff2016-09-03 11:33:15 -0700321 }
David Tolnay35161ff2016-09-03 11:33:15 -0700322 }
323}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500324
325#[cfg(feature = "parsing")]
David Tolnay5fe14fc2017-01-27 16:22:08 -0800326pub mod parse {
Michael Layzell5e107ff2017-01-24 19:58:39 -0500327 //! This module contains a set of exported nom parsers which can be used to
David Tolnay5fe14fc2017-01-27 16:22:08 -0800328 //! parse custom grammars when used alongside the `synom` crate.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500329 //!
David Tolnay5fe14fc2017-01-27 16:22:08 -0800330 //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a
331 //! persistent pitfall of using `nom` to parse Rust by eliminating the
332 //! `IResult::Incomplete` variant. The `synom` crate should be used instead
333 //! of `nom` when working with the parsers in this module.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500334
David Tolnay0a8972b2017-02-27 02:10:01 -0800335 pub use synom::IResult;
336
Michael Layzell5e107ff2017-01-24 19:58:39 -0500337 #[cfg(feature = "full")]
338 pub use item::parsing::item;
339
340 #[cfg(feature = "full")]
David Tolnay1bf19132017-02-19 22:54:25 -0800341 pub use expr::parsing::{expr, pat, block, stmt};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500342
David Tolnay0ee41892017-02-19 22:52:59 -0800343 pub use lit::parsing::{lit, string, byte_string, byte, character, float, int, boolean};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500344
David Tolnay0ee41892017-02-19 22:52:59 -0800345 pub use ty::parsing::{ty, path};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500346
David Tolnay5fe14fc2017-01-27 16:22:08 -0800347 pub use mac::parsing::token_tree as tt;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500348
349 pub use ident::parsing::ident;
David Tolnay1f16b602017-02-07 20:06:55 -0500350
351 pub use generics::parsing::lifetime;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500352}