blob: f79ab8a32c5dccdcd2fe9d60fbe0e4d472187127 [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")]
David Tolnay453cfd12016-10-23 11:00:14 -07005#[macro_use]
David Tolnay87d0b442016-09-04 11:52:12 -07006extern 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 Tolnayb5a7b142016-09-13 22:46:39 -070013mod nom;
David Tolnay6b7aaf02016-09-04 10:39:25 -070014
David Tolnaye0aa55a2016-10-02 14:55:23 -070015#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -070016#[macro_use]
David Tolnayb79ee962016-09-04 09:39:20 -070017mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -070018
David Tolnay631cb8c2016-11-10 17:16:41 -080019#[cfg(feature = "aster")]
20pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070021
David Tolnayb79ee962016-09-04 09:39:20 -070022mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070023pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070024
David Tolnay3cb23a92016-10-07 23:02:21 -070025mod constant;
26pub use constant::ConstExpr;
27
David Tolnayf38cdf62016-09-23 19:07:09 -070028mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070029pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070030
David Tolnay631cb8c2016-11-10 17:16:41 -080031#[cfg(feature = "parsing")]
32mod escape;
33
David Tolnayf4bbbd92016-09-23 14:41:55 -070034#[cfg(feature = "full")]
35mod expr;
36#[cfg(feature = "full")]
David Tolnay7b035912016-12-21 22:42:07 -050037pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue,
38 Local, MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070039
David Tolnayb79ee962016-09-04 09:39:20 -070040mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070041pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080042 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
43 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070044#[cfg(feature = "printing")]
45pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070046
David Tolnay55337722016-09-11 12:58:56 -070047mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070048pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070049
David Tolnayf38cdf62016-09-23 19:07:09 -070050#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070051mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070052#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070053pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
54 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
55 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070056
David Tolnay631cb8c2016-11-10 17:16:41 -080057#[cfg(feature = "full")]
58mod krate;
59#[cfg(feature = "full")]
60pub use krate::Crate;
61
David Tolnayf4bbbd92016-09-23 14:41:55 -070062mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070063pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070064
David Tolnay0047c712016-12-21 21:59:25 -050065#[cfg(feature = "type-macros")]
David Tolnayf4bbbd92016-09-23 14:41:55 -070066mod mac;
David Tolnay0047c712016-12-21 21:59:25 -050067#[cfg(feature = "type-macros")]
David Tolnaydaaf7742016-10-03 11:11:43 -070068pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070069
David Tolnay0e837402016-12-22 17:25:55 -050070mod derive;
71pub use derive::{Body, DeriveInput};
72// Deprecated.
73#[doc(hidden)]
74pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070075
David Tolnay3cb23a92016-10-07 23:02:21 -070076mod op;
77pub use op::{BinOp, UnOp};
78
David Tolnaye0aa55a2016-10-02 14:55:23 -070079#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070080mod space;
81
David Tolnayb79ee962016-09-04 09:39:20 -070082mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070083pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
84 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
85 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070086
David Tolnay55337722016-09-11 12:58:56 -070087#[cfg(feature = "visit")]
88pub mod visit;
89
90#[cfg(feature = "parsing")]
91pub use parsing::*;
92
93#[cfg(feature = "parsing")]
94mod parsing {
95 use super::*;
David Tolnay720e17f2017-01-23 00:51:29 -080096 use {derive, generics, ident, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -070097 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -070098
David Tolnayedf2b992016-09-23 20:43:45 -070099 #[cfg(feature = "full")]
Simon Sapin095a42b2016-10-20 15:54:23 +0200100 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -0700101
David Tolnay0e837402016-12-22 17:25:55 -0500102 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
103 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700104 }
105
David Tolnayedf2b992016-09-23 20:43:45 -0700106 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700107 pub fn parse_crate(input: &str) -> Result<Crate, String> {
108 unwrap("crate", krate::parsing::krate, input)
109 }
110
111 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700112 pub fn parse_item(input: &str) -> Result<Item, String> {
113 unwrap("item", item::parsing::item, input)
114 }
115
David Tolnayb9c8e322016-09-23 20:48:37 -0700116 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700117 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
118 unwrap("items", item::parsing::items, input)
119 }
120
121 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700122 pub fn parse_expr(input: &str) -> Result<Expr, String> {
123 unwrap("expression", expr::parsing::expr, input)
124 }
125
David Tolnay32a112e2016-09-11 17:46:15 -0700126 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700127 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700128 }
129
David Tolnay55337722016-09-11 12:58:56 -0700130 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700131 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700132 }
133
134 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700135 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700136 }
137
Simon Sapin095a42b2016-10-20 15:54:23 +0200138 #[cfg(feature = "full")]
139 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
140 unwrap("token trees", mac::parsing::token_trees, input)
141 }
142
David Tolnaya8228362016-12-22 15:21:54 -0500143 pub fn parse_ident(input: &str) -> Result<Ident, String> {
144 unwrap("identifier", ident::parsing::ident, input)
145 }
146
David Tolnay0e837402016-12-22 17:25:55 -0500147 // Deprecated.
148 #[doc(hidden)]
149 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
150 parse_derive_input(input)
151 }
152
David Tolnaydaaf7742016-10-03 11:11:43 -0700153 fn unwrap<T>(name: &'static str,
154 f: fn(&str) -> IResult<&str, T>,
155 input: &str)
156 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700157 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700158 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700159 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700160 if rest.is_empty() {
161 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700162 } else if rest.len() == input.len() {
163 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700164 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700165 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700166 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700167 }
David Tolnay55337722016-09-11 12:58:56 -0700168 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700169 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700170 }
David Tolnay35161ff2016-09-03 11:33:15 -0700171 }
172}