blob: a59e8b66c6ef15c3dcd71d729613437a73d1f43f [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 Tolnaye0aa55a2016-10-02 14:55:23 -070022#[cfg(feature = "parsing")]
David Tolnay886d8ea2016-09-13 08:34:07 -070023mod escape;
24
David Tolnayb79ee962016-09-04 09:39:20 -070025mod attr;
David Tolnaydaaf7742016-10-03 11:11:43 -070026pub use attr::{Attribute, AttrStyle, MetaItem};
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 Tolnayf4bbbd92016-09-23 14:41:55 -070034#[cfg(feature = "full")]
35mod expr;
36#[cfg(feature = "full")]
David Tolnay3cb23a92016-10-07 23:02:21 -070037pub use expr::{Arm, BindingMode, Block, BlockCheckMode, CaptureBy, Expr, FieldPat, Local,
38 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,
42 WhereBoundPredicate, WhereClause, WherePredicate, WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070043#[cfg(feature = "printing")]
44pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070045
David Tolnay4a51dc72016-10-01 00:40:31 -070046#[cfg(feature = "full")]
47mod krate;
48#[cfg(feature = "full")]
49pub use krate::Crate;
50
David Tolnay55337722016-09-11 12:58:56 -070051mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070052pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070053
David Tolnayf38cdf62016-09-23 19:07:09 -070054#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070055mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070056#[cfg(feature = "full")]
David Tolnayb8d8ef52016-10-29 14:30:08 -070057pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem,
David Tolnaydaaf7742016-10-03 11:11:43 -070058 ForeignMod, ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig,
David Tolnayb8d8ef52016-10-29 14:30:08 -070059 PathListItem, TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070060
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
64#[cfg(feature = "full")]
65mod mac;
66#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070067pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070068
David Tolnayf38cdf62016-09-23 19:07:09 -070069mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070070pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070071
David Tolnay3cb23a92016-10-07 23:02:21 -070072mod op;
73pub use op::{BinOp, UnOp};
74
David Tolnay453cfd12016-10-23 11:00:14 -070075#[cfg(feature = "expand")]
76mod registry;
77#[cfg(feature = "expand")]
78pub use registry::{CustomDerive, Expanded, Registry};
79
David Tolnaye0aa55a2016-10-02 14:55:23 -070080#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070081mod space;
82
David Tolnayb79ee962016-09-04 09:39:20 -070083mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070084pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
85 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
86 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070087
David Tolnay55337722016-09-11 12:58:56 -070088#[cfg(feature = "aster")]
89pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070090
David Tolnay55337722016-09-11 12:58:56 -070091#[cfg(feature = "visit")]
92pub mod visit;
93
94#[cfg(feature = "parsing")]
95pub use parsing::*;
96
97#[cfg(feature = "parsing")]
98mod parsing {
99 use super::*;
David Tolnayf5fdfa02016-10-23 15:25:17 -0700100 use {generics, macro_input, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -0700101 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -0700102
David Tolnayedf2b992016-09-23 20:43:45 -0700103 #[cfg(feature = "full")]
Simon Sapin095a42b2016-10-20 15:54:23 +0200104 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -0700105
David Tolnayf38cdf62016-09-23 19:07:09 -0700106 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
107 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700108 }
109
David Tolnayedf2b992016-09-23 20:43:45 -0700110 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700111 pub fn parse_crate(input: &str) -> Result<Crate, String> {
112 unwrap("crate", krate::parsing::krate, input)
113 }
114
115 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700116 pub fn parse_item(input: &str) -> Result<Item, String> {
117 unwrap("item", item::parsing::item, input)
118 }
119
David Tolnayb9c8e322016-09-23 20:48:37 -0700120 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700121 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
122 unwrap("items", item::parsing::items, input)
123 }
124
125 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700126 pub fn parse_expr(input: &str) -> Result<Expr, String> {
127 unwrap("expression", expr::parsing::expr, input)
128 }
129
David Tolnay32a112e2016-09-11 17:46:15 -0700130 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700131 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700132 }
133
David Tolnay55337722016-09-11 12:58:56 -0700134 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700135 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700136 }
137
138 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700139 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700140 }
141
Simon Sapin095a42b2016-10-20 15:54:23 +0200142 #[cfg(feature = "full")]
143 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
144 unwrap("token trees", mac::parsing::token_trees, input)
145 }
146
David Tolnaydaaf7742016-10-03 11:11:43 -0700147 fn unwrap<T>(name: &'static str,
148 f: fn(&str) -> IResult<&str, T>,
149 input: &str)
150 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700151 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700152 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700153 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700154 if rest.is_empty() {
155 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700156 } else if rest.len() == input.len() {
157 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700158 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700159 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700160 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700161 }
David Tolnay55337722016-09-11 12:58:56 -0700162 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700163 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700164 }
David Tolnay35161ff2016-09-03 11:33:15 -0700165 }
166}