David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 1 | #![cfg_attr(feature = "clippy", feature(plugin))] |
| 2 | #![cfg_attr(feature = "clippy", plugin(clippy))] |
| 3 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 4 | #[cfg(feature = "printing")] |
David Tolnay | 453cfd1 | 2016-10-23 11:00:14 -0700 | [diff] [blame] | 5 | #[macro_use] |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 6 | extern crate quote; |
| 7 | |
David Tolnay | 9e24d4d | 2016-10-23 22:17:27 -0700 | [diff] [blame] | 8 | #[cfg(feature = "pretty")] |
| 9 | extern crate syntex_syntax as syntax; |
| 10 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 11 | #[cfg(feature = "parsing")] |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 12 | extern crate unicode_xid; |
| 13 | |
| 14 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 15 | #[macro_use] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 16 | mod nom; |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 17 | |
David Tolnay | e0aa55a | 2016-10-02 14:55:23 -0700 | [diff] [blame] | 18 | #[cfg(feature = "parsing")] |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 19 | #[macro_use] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 20 | mod helper; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 21 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 22 | #[cfg(feature = "aster")] |
| 23 | pub mod aster; |
David Tolnay | 886d8ea | 2016-09-13 08:34:07 -0700 | [diff] [blame] | 24 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 25 | mod attr; |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 26 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 27 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 28 | mod constant; |
| 29 | pub use constant::ConstExpr; |
| 30 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 31 | mod data; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 32 | pub use data::{Field, Variant, VariantData, Visibility}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 33 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 34 | #[cfg(feature = "parsing")] |
| 35 | mod escape; |
| 36 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 37 | #[cfg(feature = "full")] |
| 38 | mod expr; |
| 39 | #[cfg(feature = "full")] |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 40 | pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, |
| 41 | Local, MacStmtStyle, Pat, RangeLimits, Stmt}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 42 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 43 | mod generics; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 44 | pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound, |
| 45 | WhereBoundPredicate, WhereClause, WherePredicate, WhereRegionPredicate}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 46 | #[cfg(feature = "printing")] |
| 47 | pub use generics::{ImplGenerics, TyGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 48 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 49 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 50 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 51 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 52 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 53 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 54 | #[cfg(feature = "full")] |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 55 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod, |
| 56 | ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem, |
| 57 | TraitItem, TraitItemKind, ViewPath}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 58 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 59 | #[cfg(feature = "full")] |
| 60 | mod krate; |
| 61 | #[cfg(feature = "full")] |
| 62 | pub use krate::Crate; |
| 63 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 64 | mod lit; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 65 | pub use lit::{FloatTy, IntTy, Lit, StrStyle}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 66 | |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 67 | #[cfg(feature = "type-macros")] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 68 | mod mac; |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 69 | #[cfg(feature = "type-macros")] |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 70 | pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 71 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame^] | 72 | mod derive; |
| 73 | pub use derive::{Body, DeriveInput}; |
| 74 | // Deprecated. |
| 75 | #[doc(hidden)] |
| 76 | pub type MacroInput = DeriveInput; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 77 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 78 | mod op; |
| 79 | pub use op::{BinOp, UnOp}; |
| 80 | |
David Tolnay | 453cfd1 | 2016-10-23 11:00:14 -0700 | [diff] [blame] | 81 | #[cfg(feature = "expand")] |
| 82 | mod registry; |
| 83 | #[cfg(feature = "expand")] |
| 84 | pub use registry::{CustomDerive, Expanded, Registry}; |
| 85 | |
David Tolnay | e0aa55a | 2016-10-02 14:55:23 -0700 | [diff] [blame] | 86 | #[cfg(feature = "parsing")] |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 87 | mod space; |
| 88 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 89 | mod ty; |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 90 | pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy, |
| 91 | Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment, |
| 92 | PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 93 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 94 | #[cfg(feature = "visit")] |
| 95 | pub mod visit; |
| 96 | |
| 97 | #[cfg(feature = "parsing")] |
| 98 | pub use parsing::*; |
| 99 | |
| 100 | #[cfg(feature = "parsing")] |
| 101 | mod parsing { |
| 102 | use super::*; |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame^] | 103 | use {derive, generics, space, ty}; |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 104 | use nom::IResult; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 105 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 106 | #[cfg(feature = "full")] |
Simon Sapin | 095a42b | 2016-10-20 15:54:23 +0200 | [diff] [blame] | 107 | use {expr, item, krate, mac}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 108 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame^] | 109 | pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> { |
| 110 | unwrap("derive input", derive::parsing::derive_input, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 111 | } |
| 112 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 113 | #[cfg(feature = "full")] |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 114 | pub fn parse_crate(input: &str) -> Result<Crate, String> { |
| 115 | unwrap("crate", krate::parsing::krate, input) |
| 116 | } |
| 117 | |
| 118 | #[cfg(feature = "full")] |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 119 | pub fn parse_item(input: &str) -> Result<Item, String> { |
| 120 | unwrap("item", item::parsing::item, input) |
| 121 | } |
| 122 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 123 | #[cfg(feature = "full")] |
David Tolnay | 453cfd1 | 2016-10-23 11:00:14 -0700 | [diff] [blame] | 124 | pub fn parse_items(input: &str) -> Result<Vec<Item>, String> { |
| 125 | unwrap("items", item::parsing::items, input) |
| 126 | } |
| 127 | |
| 128 | #[cfg(feature = "full")] |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 129 | pub fn parse_expr(input: &str) -> Result<Expr, String> { |
| 130 | unwrap("expression", expr::parsing::expr, input) |
| 131 | } |
| 132 | |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 133 | pub fn parse_type(input: &str) -> Result<Ty, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 134 | unwrap("type", ty::parsing::ty, input) |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 135 | } |
| 136 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 137 | pub fn parse_path(input: &str) -> Result<Path, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 138 | unwrap("path", ty::parsing::path, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 142 | unwrap("where clause", generics::parsing::where_clause, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Simon Sapin | 095a42b | 2016-10-20 15:54:23 +0200 | [diff] [blame] | 145 | #[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 Tolnay | a822836 | 2016-12-22 15:21:54 -0500 | [diff] [blame] | 150 | pub fn parse_ident(input: &str) -> Result<Ident, String> { |
| 151 | unwrap("identifier", ident::parsing::ident, input) |
| 152 | } |
| 153 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame^] | 154 | // Deprecated. |
| 155 | #[doc(hidden)] |
| 156 | pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> { |
| 157 | parse_derive_input(input) |
| 158 | } |
| 159 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 160 | fn unwrap<T>(name: &'static str, |
| 161 | f: fn(&str) -> IResult<&str, T>, |
| 162 | input: &str) |
| 163 | -> Result<T, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 164 | match f(input) { |
David Tolnay | f5fdfa0 | 2016-10-23 15:25:17 -0700 | [diff] [blame] | 165 | IResult::Done(mut rest, t) => { |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 166 | rest = space::skip_whitespace(rest); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 167 | if rest.is_empty() { |
| 168 | Ok(t) |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 169 | } else if rest.len() == input.len() { |
| 170 | // parsed nothing |
David Tolnay | ba8947b | 2016-10-05 23:31:12 -0700 | [diff] [blame] | 171 | Err(format!("failed to parse {}: {:?}", name, rest)) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 172 | } else { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 173 | Err(format!("failed to parse tokens after {}: {:?}", name, rest)) |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 174 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 175 | } |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 176 | IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)), |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 177 | } |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 178 | } |
| 179 | } |