blob: bf0bb97e0cd64db994dc24fdc0175e8d70d835c5 [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,
45 WhereBoundPredicate, WhereClause, WherePredicate, WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070046#[cfg(feature = "printing")]
47pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070048
David Tolnay55337722016-09-11 12:58:56 -070049mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070050pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070051
David Tolnayf38cdf62016-09-23 19:07:09 -070052#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070053mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070054#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070055pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
56 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
57 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070058
David Tolnay631cb8c2016-11-10 17:16:41 -080059#[cfg(feature = "full")]
60mod krate;
61#[cfg(feature = "full")]
62pub use krate::Crate;
63
David Tolnayf4bbbd92016-09-23 14:41:55 -070064mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070065pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070066
David Tolnay0047c712016-12-21 21:59:25 -050067#[cfg(feature = "type-macros")]
David Tolnayf4bbbd92016-09-23 14:41:55 -070068mod mac;
David Tolnay0047c712016-12-21 21:59:25 -050069#[cfg(feature = "type-macros")]
David Tolnaydaaf7742016-10-03 11:11:43 -070070pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070071
David Tolnayf38cdf62016-09-23 19:07:09 -070072mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070073pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070074
David Tolnay3cb23a92016-10-07 23:02:21 -070075mod op;
76pub use op::{BinOp, UnOp};
77
David Tolnay453cfd12016-10-23 11:00:14 -070078#[cfg(feature = "expand")]
79mod registry;
80#[cfg(feature = "expand")]
81pub use registry::{CustomDerive, Expanded, Registry};
82
David Tolnaye0aa55a2016-10-02 14:55:23 -070083#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070084mod space;
85
David Tolnayb79ee962016-09-04 09:39:20 -070086mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070087pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
88 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
89 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -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 Tolnaya8228362016-12-22 15:21:54 -0500147 pub fn parse_ident(input: &str) -> Result<Ident, String> {
148 unwrap("identifier", ident::parsing::ident, input)
149 }
150
David Tolnaydaaf7742016-10-03 11:11:43 -0700151 fn unwrap<T>(name: &'static str,
152 f: fn(&str) -> IResult<&str, T>,
153 input: &str)
154 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700155 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700156 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700157 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700158 if rest.is_empty() {
159 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700160 } else if rest.len() == input.len() {
161 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700162 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700163 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700164 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700165 }
David Tolnay55337722016-09-11 12:58:56 -0700166 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700167 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700168 }
David Tolnay35161ff2016-09-03 11:33:15 -0700169 }
170}