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