blob: fe80c6b874cb459c9cea43919c04edc33aaf5827 [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
87#[cfg(feature = "parsing")]
88pub use parsing::*;
89
90#[cfg(feature = "parsing")]
91mod parsing {
92 use super::*;
David Tolnayc879a502017-01-25 15:51:32 -080093 use {derive, generics, ident, mac, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -070094 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -070095
David Tolnayedf2b992016-09-23 20:43:45 -070096 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -080097 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070098
David Tolnay0e837402016-12-22 17:25:55 -050099 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
100 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700101 }
102
David Tolnayedf2b992016-09-23 20:43:45 -0700103 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700104 pub fn parse_crate(input: &str) -> Result<Crate, String> {
105 unwrap("crate", krate::parsing::krate, input)
106 }
107
108 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700109 pub fn parse_item(input: &str) -> Result<Item, String> {
110 unwrap("item", item::parsing::item, input)
111 }
112
David Tolnayb9c8e322016-09-23 20:48:37 -0700113 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700114 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
115 unwrap("items", item::parsing::items, input)
116 }
117
118 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700119 pub fn parse_expr(input: &str) -> Result<Expr, String> {
120 unwrap("expression", expr::parsing::expr, input)
121 }
122
David Tolnay32a112e2016-09-11 17:46:15 -0700123 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700124 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700125 }
126
David Tolnay55337722016-09-11 12:58:56 -0700127 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700128 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700129 }
130
131 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700132 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700133 }
134
Simon Sapin095a42b2016-10-20 15:54:23 +0200135 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
136 unwrap("token trees", mac::parsing::token_trees, input)
137 }
138
David Tolnaya8228362016-12-22 15:21:54 -0500139 pub fn parse_ident(input: &str) -> Result<Ident, String> {
140 unwrap("identifier", ident::parsing::ident, input)
141 }
142
David Tolnay23d83f92017-01-25 15:41:47 -0800143 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
144 unwrap("type parameter bound", generics::parsing::ty_param_bound, 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}