blob: 80043f46f5435738ef59de17cd193793e34d3ba6 [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 Tolnay631cb8c2016-11-10 17:16:41 -080019#[cfg(feature = "aster")]
20pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070021
David Tolnayb79ee962016-09-04 09:39:20 -070022mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070023pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
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 Tolnay631cb8c2016-11-10 17:16:41 -080031#[cfg(feature = "parsing")]
32mod escape;
33
David Tolnayf4bbbd92016-09-23 14:41:55 -070034#[cfg(feature = "full")]
35mod expr;
36#[cfg(feature = "full")]
David Tolnay7b035912016-12-21 22:42:07 -050037pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue,
38 Local, 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,
David Tolnayfa23f572017-01-23 00:19:11 -080042 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
43 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070044#[cfg(feature = "printing")]
45pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070046
David Tolnay55337722016-09-11 12:58:56 -070047mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070048pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070049
David Tolnayf38cdf62016-09-23 19:07:09 -070050#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070051mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070052#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070053pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
54 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
55 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070056
David Tolnay631cb8c2016-11-10 17:16:41 -080057#[cfg(feature = "full")]
58mod krate;
59#[cfg(feature = "full")]
60pub use krate::Crate;
61
David Tolnayf4bbbd92016-09-23 14:41:55 -070062mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070063pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070064
David Tolnay0047c712016-12-21 21:59:25 -050065#[cfg(feature = "type-macros")]
David Tolnayf4bbbd92016-09-23 14:41:55 -070066mod mac;
David Tolnay0047c712016-12-21 21:59:25 -050067#[cfg(feature = "type-macros")]
David Tolnaydaaf7742016-10-03 11:11:43 -070068pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070069
David Tolnayf38cdf62016-09-23 19:07:09 -070070mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070071pub use macro_input::{Body, MacroInput};
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 Tolnay37ffccf2016-12-22 18:24:43 -050093 use {generics, ident, macro_input, 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")]
Simon Sapin095a42b2016-10-20 15:54:23 +020097 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -070098
David Tolnayf38cdf62016-09-23 19:07:09 -070099 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
100 unwrap("macro input", macro_input::parsing::macro_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 #[cfg(feature = "full")]
136 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
137 unwrap("token trees", mac::parsing::token_trees, input)
138 }
139
David Tolnaya8228362016-12-22 15:21:54 -0500140 pub fn parse_ident(input: &str) -> Result<Ident, String> {
141 unwrap("identifier", ident::parsing::ident, 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) => {
David Tolnaydef66372016-10-24 21:51:32 -0700150 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700151 if rest.is_empty() {
152 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700153 } else if rest.len() == input.len() {
154 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700155 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700156 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700157 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700158 }
David Tolnay55337722016-09-11 12:58:56 -0700159 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700160 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700161 }
David Tolnay35161ff2016-09-03 11:33:15 -0700162 }
163}