blob: 896e9990e98c27f59eba4fd82bff5b562a5268d4 [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 Tolnay9e24d4d2016-10-23 22:17:27 -07008#[cfg(feature = "pretty")]
9extern crate syntex_syntax as syntax;
10
David Tolnayb5a7b142016-09-13 22:46:39 -070011#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -070012extern crate unicode_xid;
13
14#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070015#[macro_use]
David Tolnayb5a7b142016-09-13 22:46:39 -070016mod nom;
David Tolnay6b7aaf02016-09-04 10:39:25 -070017
David Tolnaye0aa55a2016-10-02 14:55:23 -070018#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -070019#[macro_use]
David Tolnayb79ee962016-09-04 09:39:20 -070020mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -070021
David Tolnay631cb8c2016-11-10 17:16:41 -080022#[cfg(feature = "aster")]
23pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070024
David Tolnayb79ee962016-09-04 09:39:20 -070025mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070026pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070027
David Tolnay3cb23a92016-10-07 23:02:21 -070028mod constant;
29pub use constant::ConstExpr;
30
David Tolnayf38cdf62016-09-23 19:07:09 -070031mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070032pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070033
David Tolnay631cb8c2016-11-10 17:16:41 -080034#[cfg(feature = "parsing")]
35mod escape;
36
David Tolnayf4bbbd92016-09-23 14:41:55 -070037#[cfg(feature = "full")]
38mod expr;
39#[cfg(feature = "full")]
David Tolnay7b035912016-12-21 22:42:07 -050040pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue,
41 Local, MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070042
David Tolnayb79ee962016-09-04 09:39:20 -070043mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070044pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080045 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
46 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070047#[cfg(feature = "printing")]
48pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070049
David Tolnay55337722016-09-11 12:58:56 -070050mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070051pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070052
David Tolnayf38cdf62016-09-23 19:07:09 -070053#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070054mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070055#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070056pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
57 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
58 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070059
David Tolnay631cb8c2016-11-10 17:16:41 -080060#[cfg(feature = "full")]
61mod krate;
62#[cfg(feature = "full")]
63pub use krate::Crate;
64
David Tolnayf4bbbd92016-09-23 14:41:55 -070065mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070066pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070067
David Tolnay0047c712016-12-21 21:59:25 -050068#[cfg(feature = "type-macros")]
David Tolnayf4bbbd92016-09-23 14:41:55 -070069mod mac;
David Tolnay0047c712016-12-21 21:59:25 -050070#[cfg(feature = "type-macros")]
David Tolnaydaaf7742016-10-03 11:11:43 -070071pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070072
David Tolnayf38cdf62016-09-23 19:07:09 -070073mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070074pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070075
David Tolnay3cb23a92016-10-07 23:02:21 -070076mod op;
77pub use op::{BinOp, UnOp};
78
David Tolnay453cfd12016-10-23 11:00:14 -070079#[cfg(feature = "expand")]
80mod registry;
81#[cfg(feature = "expand")]
82pub use registry::{CustomDerive, Expanded, Registry};
83
David Tolnaye0aa55a2016-10-02 14:55:23 -070084#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070085mod space;
86
David Tolnayb79ee962016-09-04 09:39:20 -070087mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070088pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
89 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
90 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070091
David Tolnay55337722016-09-11 12:58:56 -070092#[cfg(feature = "visit")]
93pub mod visit;
94
95#[cfg(feature = "parsing")]
96pub use parsing::*;
97
98#[cfg(feature = "parsing")]
99mod parsing {
100 use super::*;
David Tolnay37ffccf2016-12-22 18:24:43 -0500101 use {generics, ident, macro_input, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -0700102 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -0700103
David Tolnayedf2b992016-09-23 20:43:45 -0700104 #[cfg(feature = "full")]
Simon Sapin095a42b2016-10-20 15:54:23 +0200105 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -0700106
David Tolnayf38cdf62016-09-23 19:07:09 -0700107 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
108 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700109 }
110
David Tolnayedf2b992016-09-23 20:43:45 -0700111 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700112 pub fn parse_crate(input: &str) -> Result<Crate, String> {
113 unwrap("crate", krate::parsing::krate, input)
114 }
115
116 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700117 pub fn parse_item(input: &str) -> Result<Item, String> {
118 unwrap("item", item::parsing::item, input)
119 }
120
David Tolnayb9c8e322016-09-23 20:48:37 -0700121 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700122 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
123 unwrap("items", item::parsing::items, input)
124 }
125
126 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700127 pub fn parse_expr(input: &str) -> Result<Expr, String> {
128 unwrap("expression", expr::parsing::expr, input)
129 }
130
David Tolnay32a112e2016-09-11 17:46:15 -0700131 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700132 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700133 }
134
David Tolnay55337722016-09-11 12:58:56 -0700135 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700136 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700137 }
138
139 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700140 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700141 }
142
Simon Sapin095a42b2016-10-20 15:54:23 +0200143 #[cfg(feature = "full")]
144 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
145 unwrap("token trees", mac::parsing::token_trees, input)
146 }
147
David Tolnaya8228362016-12-22 15:21:54 -0500148 pub fn parse_ident(input: &str) -> Result<Ident, String> {
149 unwrap("identifier", ident::parsing::ident, input)
150 }
151
David Tolnaydaaf7742016-10-03 11:11:43 -0700152 fn unwrap<T>(name: &'static str,
153 f: fn(&str) -> IResult<&str, T>,
154 input: &str)
155 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700156 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700157 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700158 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700159 if rest.is_empty() {
160 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700161 } else if rest.len() == input.len() {
162 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700163 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700164 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700165 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700166 }
David Tolnay55337722016-09-11 12:58:56 -0700167 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700168 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700169 }
David Tolnay35161ff2016-09-03 11:33:15 -0700170 }
171}