blob: fd8572020a7de99d3aaaf409986888b6c10d09c8 [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 Tolnaye0aa55a2016-10-02 14:55:23 -070019#[cfg(feature = "parsing")]
David Tolnay886d8ea2016-09-13 08:34:07 -070020mod escape;
21
David Tolnayb79ee962016-09-04 09:39:20 -070022mod attr;
David Tolnaydaaf7742016-10-03 11:11:43 -070023pub use attr::{Attribute, AttrStyle, MetaItem};
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 Tolnayf4bbbd92016-09-23 14:41:55 -070031#[cfg(feature = "full")]
32mod expr;
33#[cfg(feature = "full")]
David Tolnay3cb23a92016-10-07 23:02:21 -070034pub use expr::{Arm, BindingMode, Block, BlockCheckMode, CaptureBy, Expr, FieldPat, Local,
35 MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070036
David Tolnayb79ee962016-09-04 09:39:20 -070037mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070038pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
39 WhereBoundPredicate, WhereClause, WherePredicate, WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070040#[cfg(feature = "printing")]
41pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070042
David Tolnay4a51dc72016-10-01 00:40:31 -070043#[cfg(feature = "full")]
44mod krate;
45#[cfg(feature = "full")]
46pub use krate::Crate;
47
David Tolnay55337722016-09-11 12:58:56 -070048mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070049pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070050
David Tolnayf38cdf62016-09-23 19:07:09 -070051#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070052mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070053#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070054pub use item::{Abi, Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem,
55 ForeignMod, ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig,
56 PathListItem, TraitItem, TraitItemKind, Unsafety, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070057
David Tolnayf4bbbd92016-09-23 14:41:55 -070058mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070059pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070060
61#[cfg(feature = "full")]
62mod mac;
63#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070064pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070065
David Tolnayf38cdf62016-09-23 19:07:09 -070066mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070067pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070068
David Tolnay3cb23a92016-10-07 23:02:21 -070069mod op;
70pub use op::{BinOp, UnOp};
71
David Tolnay453cfd12016-10-23 11:00:14 -070072#[cfg(feature = "expand")]
73mod registry;
74#[cfg(feature = "expand")]
75pub use registry::{CustomDerive, Expanded, Registry};
76
David Tolnaye0aa55a2016-10-02 14:55:23 -070077#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070078mod space;
79
David Tolnayb79ee962016-09-04 09:39:20 -070080mod ty;
David Tolnay3bcfb722016-10-08 11:58:36 -070081pub use ty::{AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy, Mutability,
82 ParenthesizedParameterData, Path, PathParameters, PathSegment, PolyTraitRef, QSelf,
83 Ty, TypeBinding};
David Tolnay35161ff2016-09-03 11:33:15 -070084
David Tolnay55337722016-09-11 12:58:56 -070085#[cfg(feature = "aster")]
86pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070087
David Tolnay55337722016-09-11 12:58:56 -070088#[cfg(feature = "visit")]
89pub mod visit;
90
91#[cfg(feature = "parsing")]
92pub use parsing::*;
93
94#[cfg(feature = "parsing")]
95mod parsing {
96 use super::*;
David Tolnayf5fdfa02016-10-23 15:25:17 -070097 use {generics, macro_input, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -070098 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -070099
David Tolnayedf2b992016-09-23 20:43:45 -0700100 #[cfg(feature = "full")]
Simon Sapin095a42b2016-10-20 15:54:23 +0200101 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -0700102
David Tolnayf38cdf62016-09-23 19:07:09 -0700103 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
104 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700105 }
106
David Tolnayedf2b992016-09-23 20:43:45 -0700107 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700108 pub fn parse_crate(input: &str) -> Result<Crate, String> {
109 unwrap("crate", krate::parsing::krate, input)
110 }
111
112 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700113 pub fn parse_item(input: &str) -> Result<Item, String> {
114 unwrap("item", item::parsing::item, input)
115 }
116
David Tolnayb9c8e322016-09-23 20:48:37 -0700117 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700118 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
119 unwrap("items", item::parsing::items, input)
120 }
121
122 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700123 pub fn parse_expr(input: &str) -> Result<Expr, String> {
124 unwrap("expression", expr::parsing::expr, input)
125 }
126
David Tolnay32a112e2016-09-11 17:46:15 -0700127 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700128 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700129 }
130
David Tolnay55337722016-09-11 12:58:56 -0700131 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700132 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700133 }
134
135 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700136 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700137 }
138
Simon Sapin095a42b2016-10-20 15:54:23 +0200139 #[cfg(feature = "full")]
140 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
141 unwrap("token trees", mac::parsing::token_trees, input)
142 }
143
David Tolnaydaaf7742016-10-03 11:11:43 -0700144 fn unwrap<T>(name: &'static str,
145 f: fn(&str) -> IResult<&str, T>,
146 input: &str)
147 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700148 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700149 IResult::Done(mut rest, t) => {
150 rest = match space::whitespace(rest) {
151 IResult::Done(rest, _) => rest,
152 IResult::Error => rest,
153 };
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}