blob: 85d7685839f7b6a13e11df192ff9444ca461b55b [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]
David Tolnay5fe14fc2017-01-27 16:22:08 -080012extern crate synom;
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 Tolnay1f16b602017-02-07 20:06:55 -050059#[cfg(feature = "parsing")]
60pub use lit::{ByteStrLit, FloatLit, IntLit, StrLit};
David Tolnayf4bbbd92016-09-23 14:41:55 -070061
David Tolnayf4bbbd92016-09-23 14:41:55 -070062mod mac;
David Tolnaydaaf7742016-10-03 11:11:43 -070063pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070064
David Tolnay0e837402016-12-22 17:25:55 -050065mod derive;
66pub use derive::{Body, DeriveInput};
67// Deprecated.
68#[doc(hidden)]
69pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070070
David Tolnay3cb23a92016-10-07 23:02:21 -070071mod op;
72pub use op::{BinOp, UnOp};
73
David Tolnayb79ee962016-09-04 09:39:20 -070074mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070075pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
76 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
77 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070078
David Tolnay55337722016-09-11 12:58:56 -070079#[cfg(feature = "visit")]
80pub mod visit;
81
gnzlbg9ae88d82017-01-26 20:45:17 +010082#[cfg(feature = "fold")]
83pub mod fold;
84
David Tolnay55337722016-09-11 12:58:56 -070085#[cfg(feature = "parsing")]
86pub use parsing::*;
87
88#[cfg(feature = "parsing")]
89mod parsing {
90 use super::*;
Michael Layzell5bde96f2017-01-24 17:59:21 -050091 use {derive, generics, ident, mac, ty};
David Tolnay5fe14fc2017-01-27 16:22:08 -080092 use synom::{space, IResult};
David Tolnay55337722016-09-11 12:58:56 -070093
David Tolnayedf2b992016-09-23 20:43:45 -070094 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -080095 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070096
David Tolnay0e837402016-12-22 17:25:55 -050097 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
98 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -070099 }
100
David Tolnayedf2b992016-09-23 20:43:45 -0700101 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700102 pub fn parse_crate(input: &str) -> Result<Crate, String> {
103 unwrap("crate", krate::parsing::krate, input)
104 }
105
106 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700107 pub fn parse_item(input: &str) -> Result<Item, String> {
108 unwrap("item", item::parsing::item, input)
109 }
110
David Tolnayb9c8e322016-09-23 20:48:37 -0700111 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700112 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
113 unwrap("items", item::parsing::items, input)
114 }
115
116 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700117 pub fn parse_expr(input: &str) -> Result<Expr, String> {
118 unwrap("expression", expr::parsing::expr, input)
119 }
120
David Tolnay32a112e2016-09-11 17:46:15 -0700121 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700122 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700123 }
124
David Tolnay55337722016-09-11 12:58:56 -0700125 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700126 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700127 }
128
129 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700130 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700131 }
132
Simon Sapin095a42b2016-10-20 15:54:23 +0200133 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
134 unwrap("token trees", mac::parsing::token_trees, input)
135 }
136
David Tolnaya8228362016-12-22 15:21:54 -0500137 pub fn parse_ident(input: &str) -> Result<Ident, String> {
138 unwrap("identifier", ident::parsing::ident, input)
139 }
140
David Tolnay23d83f92017-01-25 15:41:47 -0800141 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
142 unwrap("type parameter bound", generics::parsing::ty_param_bound, input)
143 }
144
David Tolnay0e837402016-12-22 17:25:55 -0500145 // Deprecated.
146 #[doc(hidden)]
147 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
148 parse_derive_input(input)
149 }
150
David Tolnaydaaf7742016-10-03 11:11:43 -0700151 fn unwrap<T>(name: &'static str,
152 f: fn(&str) -> IResult<&str, T>,
153 input: &str)
154 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700155 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700156 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700157 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700158 if rest.is_empty() {
159 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700160 } else if rest.len() == input.len() {
161 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700162 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700163 } else {
David Tolnayf2222f02017-01-27 17:09:20 -0800164 Err(format!("unparsed tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700165 }
David Tolnay55337722016-09-11 12:58:56 -0700166 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700167 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700168 }
David Tolnay35161ff2016-09-03 11:33:15 -0700169 }
170}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500171
172#[cfg(feature = "parsing")]
David Tolnay5fe14fc2017-01-27 16:22:08 -0800173pub mod parse {
Michael Layzell5e107ff2017-01-24 19:58:39 -0500174 //! This module contains a set of exported nom parsers which can be used to
David Tolnay5fe14fc2017-01-27 16:22:08 -0800175 //! parse custom grammars when used alongside the `synom` crate.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500176 //!
David Tolnay5fe14fc2017-01-27 16:22:08 -0800177 //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a
178 //! persistent pitfall of using `nom` to parse Rust by eliminating the
179 //! `IResult::Incomplete` variant. The `synom` crate should be used instead
180 //! of `nom` when working with the parsers in this module.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500181
182 #[cfg(feature = "full")]
183 pub use item::parsing::item;
184
185 #[cfg(feature = "full")]
Michael Layzell5e107ff2017-01-24 19:58:39 -0500186 pub use expr::parsing::expr;
187
188 pub use lit::parsing::lit;
189
David Tolnay5fe14fc2017-01-27 16:22:08 -0800190 pub use lit::parsing::string;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500191
David Tolnay5fe14fc2017-01-27 16:22:08 -0800192 pub use lit::parsing::byte_string;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500193
David Tolnay5fe14fc2017-01-27 16:22:08 -0800194 pub use lit::parsing::byte;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500195
David Tolnay5fe14fc2017-01-27 16:22:08 -0800196 pub use lit::parsing::character;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500197
David Tolnay5fe14fc2017-01-27 16:22:08 -0800198 pub use lit::parsing::float;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500199
David Tolnay5fe14fc2017-01-27 16:22:08 -0800200 pub use lit::parsing::int;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500201
David Tolnay5fe14fc2017-01-27 16:22:08 -0800202 pub use lit::parsing::boolean;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500203
204 pub use ty::parsing::ty;
205
206 pub use ty::parsing::path;
207
David Tolnay5fe14fc2017-01-27 16:22:08 -0800208 pub use mac::parsing::token_tree as tt;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500209
210 pub use ident::parsing::ident;
David Tolnay1f16b602017-02-07 20:06:55 -0500211
212 pub use generics::parsing::lifetime;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500213}