David Tolnay | ad2836d | 2017-04-20 10:11:43 -0700 | [diff] [blame] | 1 | #![doc(html_root_url = "https://dtolnay.github.io/syn")] |
| 2 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 3 | #![cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 4 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 5 | extern crate proc_macro2; |
| 6 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 7 | #[cfg(feature = "printing")] |
| 8 | extern crate quote; |
| 9 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 10 | #[cfg(feature = "parsing")] |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 11 | extern crate unicode_xid; |
| 12 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 13 | #[cfg_attr(feature = "parsing", macro_use)] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 14 | extern crate synom; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 15 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 16 | #[cfg(feature = "aster")] |
| 17 | pub mod aster; |
David Tolnay | 886d8ea | 2016-09-13 08:34:07 -0700 | [diff] [blame] | 18 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 19 | #[macro_use] |
| 20 | mod macros; |
| 21 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 22 | mod attr; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 23 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem, MetaItemList, |
| 24 | MetaNameValue}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 25 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 26 | mod constant; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 27 | pub use constant::{ConstExpr, ConstCall, ConstBinary, ConstUnary, ConstCast, |
| 28 | ConstIndex, ConstParen}; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 29 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 30 | mod data; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 31 | pub use data::{Field, Variant, VariantData, Visibility, VisRestricted, VisCrate, |
| 32 | VisPublic, VisInherited}; |
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 | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 40 | pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, Local, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 41 | MacStmtStyle, Pat, RangeLimits, Stmt, ExprBox, ExprInPlace, |
| 42 | ExprArray, ExprCall, ExprMethodCall, ExprTup, ExprBinary, ExprUnary, |
| 43 | ExprCast, ExprType, ExprIf, ExprIfLet, ExprWhile, ExprWhileLet, |
| 44 | ExprForLoop, ExprLoop, ExprMatch, ExprClosure, ExprBlock, |
| 45 | ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex, |
| 46 | ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 47 | ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch, |
| 48 | PatIdent, PatWild, PatStruct, PatTuple, PatTupleStruct, PatPath, |
| 49 | PatBox, PatRef, PatLit, PatRange, PatSlice}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 50 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 51 | mod generics; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 52 | pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound, |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 53 | WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 54 | WhereRegionPredicate, BoundLifetimes}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 55 | #[cfg(feature = "printing")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 56 | pub use generics::{ImplGenerics, Turbofish, TyGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 57 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 58 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 59 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 60 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 61 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 62 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 63 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 64 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ItemForeignMod, |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 65 | ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 66 | TraitItem, TraitItemKind, ViewPath, ItemExternCrate, ItemUse, |
| 67 | ItemStatic, ItemConst, ItemFn, ItemMod, ItemTy, ItemEnum, |
| 68 | ItemStruct, ItemUnion, ItemTrait, ItemDefaultImpl, ItemImpl, |
| 69 | PathSimple, PathGlob, PathList, ForeignItemFn, ForeignItemStatic, |
| 70 | TraitItemConst, TraitItemMethod, TraitItemType, |
| 71 | ImplItemConst, ImplItemMethod, ImplItemType, ArgSelfRef, |
| 72 | ArgSelf, ArgCaptured}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 73 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 74 | #[cfg(feature = "full")] |
| 75 | mod krate; |
| 76 | #[cfg(feature = "full")] |
| 77 | pub use krate::Crate; |
| 78 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 79 | mod lit; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 80 | pub use lit::{Lit, LitKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 81 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 82 | mod mac; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 83 | pub use mac::{Mac, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 84 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 85 | mod derive; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 86 | pub use derive::{Body, DeriveInput, BodyEnum, BodyStruct}; |
David Tolnay | c28a4a5 | 2017-02-04 09:50:57 -0800 | [diff] [blame] | 87 | // Deprecated. Use `DeriveInput` instead. |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 88 | #[doc(hidden)] |
| 89 | pub type MacroInput = DeriveInput; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 90 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 91 | mod op; |
| 92 | pub use op::{BinOp, UnOp}; |
| 93 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 94 | mod ty; |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 95 | pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy, |
| 96 | Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 97 | PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety, TySlice, TyArray, |
| 98 | TyPtr, TyRptr, TyBareFn, TyNever, TyTup, TyPath, TyTraitObject, |
| 99 | TyImplTrait, TyParen, TyInfer}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 100 | #[cfg(feature = "printing")] |
| 101 | pub use ty::PathTokens; |
| 102 | |
| 103 | mod span; |
| 104 | pub use span::Span; |
| 105 | |
| 106 | pub mod tokens; |
| 107 | pub use synom::delimited; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 108 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 109 | #[cfg(feature = "visit")] |
| 110 | pub mod visit; |
| 111 | |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 112 | #[cfg(feature = "fold")] |
| 113 | pub mod fold; |
| 114 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 115 | #[cfg(feature = "parsing")] |
| 116 | pub use parsing::*; |
| 117 | |
| 118 | #[cfg(feature = "parsing")] |
| 119 | mod parsing { |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 120 | use std::str::FromStr; |
| 121 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 122 | use super::*; |
Colin Kiegel | 108a7fe | 2017-03-06 13:29:57 +0100 | [diff] [blame] | 123 | use {derive, generics, ident, mac, ty, attr}; |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 124 | use synom::{space, IResult}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 125 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 126 | #[cfg(feature = "full")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 127 | use {expr, item, krate}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 128 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 129 | /// Parse the stringified representation of a struct or enum passed |
| 130 | /// to a `proc_macro_derive` function. |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 131 | pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> { |
| 132 | unwrap("derive input", derive::parsing::derive_input, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 135 | #[cfg(feature = "full")] |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 136 | pub fn parse_crate(input: &str) -> Result<Crate, String> { |
| 137 | unwrap("crate", krate::parsing::krate, input) |
| 138 | } |
| 139 | |
| 140 | #[cfg(feature = "full")] |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 141 | pub fn parse_item(input: &str) -> Result<Item, String> { |
| 142 | unwrap("item", item::parsing::item, input) |
| 143 | } |
| 144 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 145 | #[cfg(feature = "full")] |
David Tolnay | 453cfd1 | 2016-10-23 11:00:14 -0700 | [diff] [blame] | 146 | pub fn parse_items(input: &str) -> Result<Vec<Item>, String> { |
| 147 | unwrap("items", item::parsing::items, input) |
| 148 | } |
| 149 | |
| 150 | #[cfg(feature = "full")] |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 151 | pub fn parse_expr(input: &str) -> Result<Expr, String> { |
| 152 | unwrap("expression", expr::parsing::expr, input) |
| 153 | } |
| 154 | |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 155 | pub fn parse_type(input: &str) -> Result<Ty, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 156 | unwrap("type", ty::parsing::ty, input) |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 159 | /// Parse a path, such as `std::str::FromStr` or `::syn::parse_path`. |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 160 | pub fn parse_path(input: &str) -> Result<Path, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 161 | unwrap("path", ty::parsing::path, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 165 | unwrap("where clause", generics::parsing::where_clause, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Simon Sapin | 095a42b | 2016-10-20 15:54:23 +0200 | [diff] [blame] | 168 | pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> { |
| 169 | unwrap("token trees", mac::parsing::token_trees, input) |
| 170 | } |
| 171 | |
David Tolnay | a822836 | 2016-12-22 15:21:54 -0500 | [diff] [blame] | 172 | pub fn parse_ident(input: &str) -> Result<Ident, String> { |
| 173 | unwrap("identifier", ident::parsing::ident, input) |
| 174 | } |
| 175 | |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 176 | pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> { |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 177 | unwrap("type parameter bound", |
| 178 | generics::parsing::ty_param_bound, |
| 179 | input) |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 182 | /// Parse an attribute declared outside the item it annotates, such as |
| 183 | /// a struct annotation. They are written as `#[...]`. |
Colin Kiegel | cb72443 | 2017-03-06 12:59:18 +0100 | [diff] [blame] | 184 | pub fn parse_outer_attr(input: &str) -> Result<Attribute, String> { |
| 185 | unwrap("outer attribute", attr::parsing::outer_attr, input) |
| 186 | } |
| 187 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 188 | /// Parse an attribute declared inside the item it annotates. These are used |
| 189 | /// for crate annotations or for mod-level declarations when modules are in |
| 190 | /// their own files. They are written as `#![...]`. |
Colin Kiegel | cb72443 | 2017-03-06 12:59:18 +0100 | [diff] [blame] | 191 | #[cfg(feature = "full")] |
| 192 | pub fn parse_inner_attr(input: &str) -> Result<Attribute, String> { |
| 193 | unwrap("inner attribute", attr::parsing::inner_attr, input) |
| 194 | } |
| 195 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 196 | /// Deprecated: Use `parse_derive_input` instead. |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 197 | #[doc(hidden)] |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 198 | #[deprecated(since="0.11.0", note = "Use `parse_derive_input` instead")] |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 199 | pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> { |
| 200 | parse_derive_input(input) |
| 201 | } |
| 202 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 203 | /// Alias for `syn::parse_derive_input`. |
| 204 | impl FromStr for DeriveInput { |
| 205 | type Err = String; |
| 206 | |
| 207 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 208 | parse_derive_input(s) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// Alias for `syn::parse_crate`. |
| 213 | #[cfg(feature = "full")] |
| 214 | impl FromStr for Crate { |
| 215 | type Err = String; |
| 216 | |
| 217 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 218 | parse_crate(s) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /// Alias for `syn::parse_item`. |
| 223 | #[cfg(feature = "full")] |
| 224 | impl FromStr for Item { |
| 225 | type Err = String; |
| 226 | |
| 227 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 228 | parse_item(s) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// Alias for `syn::parse_expr`. |
| 233 | #[cfg(feature = "full")] |
| 234 | impl FromStr for Expr { |
| 235 | type Err = String; |
| 236 | |
| 237 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 238 | parse_expr(s) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /// Alias for `syn::parse_type`. |
| 243 | impl FromStr for Ty { |
| 244 | type Err = String; |
| 245 | |
| 246 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 247 | parse_type(s) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /// Alias for `syn::parse_path`. |
| 252 | impl FromStr for Path { |
| 253 | type Err = String; |
| 254 | |
| 255 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 256 | parse_path(s) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /// Alias for `syn::parse_where_clause`. |
| 261 | impl FromStr for WhereClause { |
| 262 | type Err = String; |
| 263 | |
| 264 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 265 | parse_where_clause(s) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /// Alias for `syn::parse_ident`. |
| 270 | impl FromStr for Ident { |
| 271 | type Err = String; |
| 272 | |
| 273 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 274 | parse_ident(s) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /// Alias for `syn::parse_ty_param_bound`. |
| 279 | impl FromStr for TyParamBound { |
| 280 | type Err = String; |
| 281 | |
| 282 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 283 | parse_ty_param_bound(s) |
| 284 | } |
| 285 | } |
| 286 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 287 | fn unwrap<T>(name: &'static str, |
| 288 | f: fn(&str) -> IResult<&str, T>, |
| 289 | input: &str) |
| 290 | -> Result<T, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 291 | match f(input) { |
David Tolnay | f5fdfa0 | 2016-10-23 15:25:17 -0700 | [diff] [blame] | 292 | IResult::Done(mut rest, t) => { |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 293 | rest = space::skip_whitespace(rest); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 294 | if rest.is_empty() { |
| 295 | Ok(t) |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 296 | } else if rest.len() == input.len() { |
| 297 | // parsed nothing |
David Tolnay | ba8947b | 2016-10-05 23:31:12 -0700 | [diff] [blame] | 298 | Err(format!("failed to parse {}: {:?}", name, rest)) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 299 | } else { |
David Tolnay | f2222f0 | 2017-01-27 17:09:20 -0800 | [diff] [blame] | 300 | Err(format!("unparsed tokens after {}: {:?}", name, rest)) |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 301 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 302 | } |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 303 | IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)), |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 304 | } |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 307 | |
| 308 | #[cfg(feature = "parsing")] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 309 | pub mod parse { |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 310 | //! This module contains a set of exported nom parsers which can be used to |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 311 | //! parse custom grammars when used alongside the `synom` crate. |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 312 | //! |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 313 | //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a |
| 314 | //! persistent pitfall of using `nom` to parse Rust by eliminating the |
| 315 | //! `IResult::Incomplete` variant. The `synom` crate should be used instead |
| 316 | //! of `nom` when working with the parsers in this module. |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 317 | |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 318 | pub use synom::IResult; |
| 319 | |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 320 | #[cfg(feature = "full")] |
| 321 | pub use item::parsing::item; |
| 322 | |
| 323 | #[cfg(feature = "full")] |
David Tolnay | 1bf1913 | 2017-02-19 22:54:25 -0800 | [diff] [blame] | 324 | pub use expr::parsing::{expr, pat, block, stmt}; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 325 | |
David Tolnay | 0ee4189 | 2017-02-19 22:52:59 -0800 | [diff] [blame] | 326 | pub use lit::parsing::{lit, string, byte_string, byte, character, float, int, boolean}; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 327 | |
David Tolnay | 0ee4189 | 2017-02-19 22:52:59 -0800 | [diff] [blame] | 328 | pub use ty::parsing::{ty, path}; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 329 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 330 | pub use mac::parsing::token_tree as tt; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 331 | |
| 332 | pub use ident::parsing::ident; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 333 | |
| 334 | pub use generics::parsing::lifetime; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 335 | } |