blob: 6bbd1154f866a98d7ed7b9b21bf053bb6eebca33 [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 Tolnay0e837402016-12-22 17:25:55 -050072mod derive;
73pub use derive::{Body, DeriveInput};
74// Deprecated.
75#[doc(hidden)]
76pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070077
David Tolnay3cb23a92016-10-07 23:02:21 -070078mod op;
79pub use op::{BinOp, UnOp};
80
David Tolnay453cfd12016-10-23 11:00:14 -070081#[cfg(feature = "expand")]
82mod registry;
83#[cfg(feature = "expand")]
84pub use registry::{CustomDerive, Expanded, Registry};
85
David Tolnaye0aa55a2016-10-02 14:55:23 -070086#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070087mod space;
88
David Tolnayb79ee962016-09-04 09:39:20 -070089mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070090pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
91 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
92 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070093
David Tolnay55337722016-09-11 12:58:56 -070094#[cfg(feature = "visit")]
95pub mod visit;
96
97#[cfg(feature = "parsing")]
98pub use parsing::*;
99
100#[cfg(feature = "parsing")]
101mod parsing {
102 use super::*;
David Tolnay0e837402016-12-22 17:25:55 -0500103 use {derive, generics, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -0700104 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -0700105
David Tolnayedf2b992016-09-23 20:43:45 -0700106 #[cfg(feature = "full")]
Simon Sapin095a42b2016-10-20 15:54:23 +0200107 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -0700108
David Tolnay0e837402016-12-22 17:25:55 -0500109 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
110 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700111 }
112
David Tolnayedf2b992016-09-23 20:43:45 -0700113 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700114 pub fn parse_crate(input: &str) -> Result<Crate, String> {
115 unwrap("crate", krate::parsing::krate, input)
116 }
117
118 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700119 pub fn parse_item(input: &str) -> Result<Item, String> {
120 unwrap("item", item::parsing::item, input)
121 }
122
David Tolnayb9c8e322016-09-23 20:48:37 -0700123 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700124 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
125 unwrap("items", item::parsing::items, input)
126 }
127
128 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700129 pub fn parse_expr(input: &str) -> Result<Expr, String> {
130 unwrap("expression", expr::parsing::expr, input)
131 }
132
David Tolnay32a112e2016-09-11 17:46:15 -0700133 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700134 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700135 }
136
David Tolnay55337722016-09-11 12:58:56 -0700137 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700138 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700139 }
140
141 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700142 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700143 }
144
Simon Sapin095a42b2016-10-20 15:54:23 +0200145 #[cfg(feature = "full")]
146 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
147 unwrap("token trees", mac::parsing::token_trees, input)
148 }
149
David Tolnaya8228362016-12-22 15:21:54 -0500150 pub fn parse_ident(input: &str) -> Result<Ident, String> {
151 unwrap("identifier", ident::parsing::ident, input)
152 }
153
David Tolnay0e837402016-12-22 17:25:55 -0500154 // Deprecated.
155 #[doc(hidden)]
156 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
157 parse_derive_input(input)
158 }
159
David Tolnaydaaf7742016-10-03 11:11:43 -0700160 fn unwrap<T>(name: &'static str,
161 f: fn(&str) -> IResult<&str, T>,
162 input: &str)
163 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700164 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700165 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700166 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700167 if rest.is_empty() {
168 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700169 } else if rest.len() == input.len() {
170 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700171 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700172 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700173 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700174 }
David Tolnay55337722016-09-11 12:58:56 -0700175 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700176 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700177 }
David Tolnay35161ff2016-09-03 11:33:15 -0700178 }
179}