blob: a9f939ae245295a3146932d7d49cd852cd704b7d [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 Tolnayb5a7b142016-09-13 22:46:39 -070012mod nom;
David Tolnay6b7aaf02016-09-04 10:39:25 -070013
David Tolnaye0aa55a2016-10-02 14:55:23 -070014#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -070015#[macro_use]
David Tolnayb79ee962016-09-04 09:39:20 -070016mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -070017
David Tolnay631cb8c2016-11-10 17:16:41 -080018#[cfg(feature = "aster")]
19pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070020
David Tolnayb79ee962016-09-04 09:39:20 -070021mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070022pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070023
David Tolnay3cb23a92016-10-07 23:02:21 -070024mod constant;
25pub use constant::ConstExpr;
26
David Tolnayf38cdf62016-09-23 19:07:09 -070027mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070028pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070029
David Tolnay631cb8c2016-11-10 17:16:41 -080030#[cfg(feature = "parsing")]
31mod escape;
32
David Tolnayf4bbbd92016-09-23 14:41:55 -070033#[cfg(feature = "full")]
34mod expr;
35#[cfg(feature = "full")]
David Tolnay7b035912016-12-21 22:42:07 -050036pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue,
37 Local, MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070038
David Tolnayb79ee962016-09-04 09:39:20 -070039mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070040pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080041 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
42 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070043#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080044pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070045
David Tolnay55337722016-09-11 12:58:56 -070046mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070047pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070048
David Tolnayf38cdf62016-09-23 19:07:09 -070049#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070050mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070051#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070052pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
53 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
54 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070055
David Tolnay631cb8c2016-11-10 17:16:41 -080056#[cfg(feature = "full")]
57mod krate;
58#[cfg(feature = "full")]
59pub use krate::Crate;
60
David Tolnayf4bbbd92016-09-23 14:41:55 -070061mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070062pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070063
David Tolnayf4bbbd92016-09-23 14:41:55 -070064mod mac;
David Tolnaydaaf7742016-10-03 11:11:43 -070065pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070066
David Tolnay0e837402016-12-22 17:25:55 -050067mod derive;
68pub use derive::{Body, DeriveInput};
69// Deprecated.
70#[doc(hidden)]
71pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070072
David Tolnay3cb23a92016-10-07 23:02:21 -070073mod op;
74pub use op::{BinOp, UnOp};
75
David Tolnaye0aa55a2016-10-02 14:55:23 -070076#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070077mod space;
78
David Tolnayb79ee962016-09-04 09:39:20 -070079mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070080pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
81 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
82 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070083
David Tolnay55337722016-09-11 12:58:56 -070084#[cfg(feature = "visit")]
85pub mod visit;
86
gnzlbg9ae88d82017-01-26 20:45:17 +010087#[cfg(feature = "fold")]
88pub mod fold;
89
David Tolnay55337722016-09-11 12:58:56 -070090#[cfg(feature = "parsing")]
91pub use parsing::*;
92
93#[cfg(feature = "parsing")]
94mod parsing {
95 use super::*;
David Tolnayc879a502017-01-25 15:51:32 -080096 use {derive, generics, ident, mac, 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")]
David Tolnayc879a502017-01-25 15:51:32 -0800100 use {expr, item, krate};
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 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
139 unwrap("token trees", mac::parsing::token_trees, input)
140 }
141
David Tolnaya8228362016-12-22 15:21:54 -0500142 pub fn parse_ident(input: &str) -> Result<Ident, String> {
143 unwrap("identifier", ident::parsing::ident, input)
144 }
145
David Tolnay23d83f92017-01-25 15:41:47 -0800146 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
147 unwrap("type parameter bound", generics::parsing::ty_param_bound, input)
148 }
149
David Tolnay0e837402016-12-22 17:25:55 -0500150 // Deprecated.
151 #[doc(hidden)]
152 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
153 parse_derive_input(input)
154 }
155
David Tolnaydaaf7742016-10-03 11:11:43 -0700156 fn unwrap<T>(name: &'static str,
157 f: fn(&str) -> IResult<&str, T>,
158 input: &str)
159 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700160 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700161 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700162 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700163 if rest.is_empty() {
164 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700165 } else if rest.len() == input.len() {
166 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700167 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700168 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700169 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700170 }
David Tolnay55337722016-09-11 12:58:56 -0700171 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700172 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700173 }
David Tolnay35161ff2016-09-03 11:33:15 -0700174 }
175}