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")] |
| 5 | extern crate quote; |
| 6 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 7 | #[cfg(feature = "parsing")] |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 8 | extern crate unicode_xid; |
| 9 | |
| 10 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 11 | #[macro_use] |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 12 | extern crate syn_nom as nom; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 13 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 14 | #[cfg(feature = "aster")] |
| 15 | pub mod aster; |
David Tolnay | 886d8ea | 2016-09-13 08:34:07 -0700 | [diff] [blame] | 16 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 17 | mod attr; |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 18 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 19 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 20 | mod constant; |
| 21 | pub use constant::ConstExpr; |
| 22 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 23 | mod data; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 24 | pub use data::{Field, Variant, VariantData, Visibility}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 25 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 26 | #[cfg(feature = "parsing")] |
| 27 | mod escape; |
| 28 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 29 | #[cfg(feature = "full")] |
| 30 | mod expr; |
| 31 | #[cfg(feature = "full")] |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 32 | pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, |
| 33 | Local, MacStmtStyle, Pat, RangeLimits, Stmt}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 34 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 35 | mod generics; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 36 | pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound, |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 37 | WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, |
| 38 | WhereRegionPredicate}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 39 | #[cfg(feature = "printing")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 40 | pub use generics::{ImplGenerics, Turbofish, TyGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 41 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 42 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 43 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 44 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 45 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 46 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 47 | #[cfg(feature = "full")] |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 48 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod, |
| 49 | ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem, |
| 50 | TraitItem, TraitItemKind, ViewPath}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 51 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 52 | #[cfg(feature = "full")] |
| 53 | mod krate; |
| 54 | #[cfg(feature = "full")] |
| 55 | pub use krate::Crate; |
| 56 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 57 | mod lit; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 58 | pub use lit::{FloatTy, IntTy, Lit, StrStyle}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 59 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 60 | mod mac; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 61 | pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 62 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 63 | mod derive; |
| 64 | pub use derive::{Body, DeriveInput}; |
| 65 | // Deprecated. |
| 66 | #[doc(hidden)] |
| 67 | pub type MacroInput = DeriveInput; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 68 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 69 | mod op; |
| 70 | pub use op::{BinOp, UnOp}; |
| 71 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 72 | mod ty; |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 73 | pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy, |
| 74 | Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment, |
| 75 | PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 76 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 77 | #[cfg(feature = "visit")] |
| 78 | pub mod visit; |
| 79 | |
| 80 | #[cfg(feature = "parsing")] |
| 81 | pub use parsing::*; |
| 82 | |
| 83 | #[cfg(feature = "parsing")] |
| 84 | mod parsing { |
| 85 | use super::*; |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 86 | use {derive, generics, ident, mac, ty}; |
| 87 | use nom::{space, IResult}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 88 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 89 | #[cfg(feature = "full")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 90 | use {expr, item, krate}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 91 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 92 | pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> { |
| 93 | unwrap("derive input", derive::parsing::derive_input, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 94 | } |
| 95 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 96 | #[cfg(feature = "full")] |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 97 | pub fn parse_crate(input: &str) -> Result<Crate, String> { |
| 98 | unwrap("crate", krate::parsing::krate, input) |
| 99 | } |
| 100 | |
| 101 | #[cfg(feature = "full")] |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 102 | pub fn parse_item(input: &str) -> Result<Item, String> { |
| 103 | unwrap("item", item::parsing::item, input) |
| 104 | } |
| 105 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 106 | #[cfg(feature = "full")] |
David Tolnay | 453cfd1 | 2016-10-23 11:00:14 -0700 | [diff] [blame] | 107 | pub fn parse_items(input: &str) -> Result<Vec<Item>, String> { |
| 108 | unwrap("items", item::parsing::items, input) |
| 109 | } |
| 110 | |
| 111 | #[cfg(feature = "full")] |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 112 | pub fn parse_expr(input: &str) -> Result<Expr, String> { |
| 113 | unwrap("expression", expr::parsing::expr, input) |
| 114 | } |
| 115 | |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 116 | pub fn parse_type(input: &str) -> Result<Ty, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 117 | unwrap("type", ty::parsing::ty, input) |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 118 | } |
| 119 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 120 | pub fn parse_path(input: &str) -> Result<Path, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 121 | unwrap("path", ty::parsing::path, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 125 | unwrap("where clause", generics::parsing::where_clause, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Simon Sapin | 095a42b | 2016-10-20 15:54:23 +0200 | [diff] [blame] | 128 | pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> { |
| 129 | unwrap("token trees", mac::parsing::token_trees, input) |
| 130 | } |
| 131 | |
David Tolnay | a822836 | 2016-12-22 15:21:54 -0500 | [diff] [blame] | 132 | pub fn parse_ident(input: &str) -> Result<Ident, String> { |
| 133 | unwrap("identifier", ident::parsing::ident, input) |
| 134 | } |
| 135 | |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 136 | pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> { |
| 137 | unwrap("type parameter bound", generics::parsing::ty_param_bound, input) |
| 138 | } |
| 139 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 140 | // Deprecated. |
| 141 | #[doc(hidden)] |
| 142 | pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> { |
| 143 | parse_derive_input(input) |
| 144 | } |
| 145 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 146 | fn unwrap<T>(name: &'static str, |
| 147 | f: fn(&str) -> IResult<&str, T>, |
| 148 | input: &str) |
| 149 | -> Result<T, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 150 | match f(input) { |
David Tolnay | f5fdfa0 | 2016-10-23 15:25:17 -0700 | [diff] [blame] | 151 | IResult::Done(mut rest, t) => { |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 152 | rest = space::skip_whitespace(rest); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 153 | if rest.is_empty() { |
| 154 | Ok(t) |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 155 | } else if rest.len() == input.len() { |
| 156 | // parsed nothing |
David Tolnay | ba8947b | 2016-10-05 23:31:12 -0700 | [diff] [blame] | 157 | Err(format!("failed to parse {}: {:?}", name, rest)) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 158 | } else { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 159 | Err(format!("failed to parse tokens after {}: {:?}", name, rest)) |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 160 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 161 | } |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 162 | IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)), |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 163 | } |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame^] | 166 | |
| 167 | #[cfg(feature = "parsing")] |
| 168 | pub mod parser { |
| 169 | //! This module contains a set of exported nom parsers which can be used to |
| 170 | //! build nom parsers for custom grammars when used alongside the `syn_nom` |
| 171 | //! crate. |
| 172 | //! |
| 173 | //! `syn` uses its own custom fork of `nom`, `syn_nom`, which is smaller, and |
| 174 | //! thus improves build speeds. This should be used instead of `nom` when |
| 175 | //! building parsers using these parsers. |
| 176 | |
| 177 | #[cfg(feature = "full")] |
| 178 | pub use krate::parsing::krate; |
| 179 | |
| 180 | #[cfg(feature = "full")] |
| 181 | pub use item::parsing::item; |
| 182 | |
| 183 | #[cfg(feature = "full")] |
| 184 | pub use item::parsing::items; |
| 185 | |
| 186 | #[cfg(feature = "full")] |
| 187 | pub use expr::parsing::expr; |
| 188 | |
| 189 | pub use lit::parsing::lit; |
| 190 | |
| 191 | pub use lit::parsing::string as str_lit; |
| 192 | |
| 193 | pub use lit::parsing::byte_string as byte_str_lit; |
| 194 | |
| 195 | pub use lit::parsing::byte as byte_lit; |
| 196 | |
| 197 | pub use lit::parsing::character as char_lit; |
| 198 | |
| 199 | pub use lit::parsing::float as float_lit; |
| 200 | |
| 201 | pub use lit::parsing::int as int_lit; |
| 202 | |
| 203 | pub use lit::parsing::boolean as bool_lit; |
| 204 | |
| 205 | pub use ty::parsing::ty; |
| 206 | |
| 207 | pub use ty::parsing::path; |
| 208 | |
| 209 | pub use generics::parsing::where_clause; |
| 210 | |
| 211 | #[cfg(feature = "full")] |
| 212 | pub use mac::parsing::token_trees; |
| 213 | |
| 214 | pub use ident::parsing::ident; |
| 215 | } |