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 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 5 | #[cfg(feature = "printing")] |
| 6 | extern crate quote; |
| 7 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 8 | #[cfg(feature = "parsing")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 9 | extern crate relex; |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 10 | |
| 11 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 12 | #[macro_use] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 13 | extern crate synom; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 14 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 15 | #[cfg(feature = "aster")] |
| 16 | pub mod aster; |
David Tolnay | 886d8ea | 2016-09-13 08:34:07 -0700 | [diff] [blame] | 17 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 18 | mod attr; |
David Tolnay | b7fa2b6 | 2016-10-30 10:50:47 -0700 | [diff] [blame] | 19 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 20 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 21 | mod constant; |
| 22 | pub use constant::ConstExpr; |
| 23 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 24 | mod data; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 25 | pub use data::{Field, Variant, VariantData, Visibility}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 26 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 27 | #[cfg(feature = "full")] |
| 28 | mod expr; |
| 29 | #[cfg(feature = "full")] |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 30 | pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, Local, |
| 31 | MacStmtStyle, Pat, RangeLimits, Stmt}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 32 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 33 | mod generics; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 34 | pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound, |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 35 | WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, |
| 36 | WhereRegionPredicate}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 37 | #[cfg(feature = "printing")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 38 | pub use generics::{ImplGenerics, Turbofish, TyGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 39 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 40 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 41 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 42 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 43 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 44 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 45 | #[cfg(feature = "full")] |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 46 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod, |
| 47 | ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem, |
| 48 | TraitItem, TraitItemKind, ViewPath}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 49 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 50 | #[cfg(feature = "full")] |
| 51 | mod krate; |
| 52 | #[cfg(feature = "full")] |
| 53 | pub use krate::Crate; |
| 54 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 55 | mod lit; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 56 | pub use lit::{FloatTy, IntTy, Lit, StrStyle}; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 57 | #[cfg(feature = "parsing")] |
| 58 | pub use lit::{ByteStrLit, FloatLit, IntLit, StrLit}; |
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}; |
David Tolnay | c28a4a5 | 2017-02-04 09:50:57 -0800 | [diff] [blame] | 65 | // Deprecated. Use `DeriveInput` instead. |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 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 | |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 80 | #[cfg(feature = "fold")] |
| 81 | pub mod fold; |
| 82 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 83 | #[cfg(feature = "parsing")] |
| 84 | pub use parsing::*; |
| 85 | |
| 86 | #[cfg(feature = "parsing")] |
| 87 | mod parsing { |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 88 | use std::str::FromStr; |
| 89 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 90 | use super::*; |
Colin Kiegel | 108a7fe | 2017-03-06 13:29:57 +0100 | [diff] [blame] | 91 | use {derive, generics, ident, mac, ty, attr}; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 92 | use synom::{IResult, TokenStream}; |
| 93 | |
| 94 | use std::convert::From; |
| 95 | use std::error::Error; |
| 96 | use std::fmt; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 97 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 98 | #[cfg(feature = "full")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 99 | use {expr, item, krate}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 100 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 101 | #[derive(Debug)] |
| 102 | pub struct ParseError(String); |
| 103 | |
| 104 | impl Error for ParseError { |
| 105 | fn description(&self) -> &str { |
| 106 | &self.0 |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl fmt::Display for ParseError { |
| 111 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 112 | <String as fmt::Display>::fmt(&self.0, f) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl From<synom::LexError> for ParseError { |
| 117 | fn from(_: synom::LexError) -> ParseError { |
| 118 | ParseError("error while lexing input string".to_owned()) |
| 119 | } |
| 120 | } |
| 121 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 122 | /// Parse the stringified representation of a struct or enum passed |
| 123 | /// to a `proc_macro_derive` function. |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 124 | pub fn parse_derive_input(input: TokenStream) -> Result<DeriveInput, ParseError> { |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 125 | unwrap("derive input", derive::parsing::derive_input, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 128 | /// Parse an entire crate into an AST. This function takes a string as input |
| 129 | /// instead of a TokenStream, as we need to handle parsing the BOM and |
| 130 | /// shebang from the string. |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 131 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 132 | pub fn parse_crate(mut input: &str) -> Result<Crate, ParseError> { |
| 133 | // Strip the BOM if it is present |
| 134 | const BOM: &str = "\u{feff}"; |
| 135 | if input.starts_with(BOM) { |
| 136 | input = &input[BOM.len()..]; |
| 137 | } |
| 138 | |
| 139 | let mut shebang = None; |
| 140 | if input.starts_with("#!") && !input.starts_with("#![") { |
| 141 | if let Some(idx) = input.find('\n') { |
| 142 | shebang = Some(input[..idx].to_string()); |
| 143 | input = &input[idx..]; |
| 144 | } else { |
| 145 | shebang = Some(input.to_string()); |
| 146 | input = ""; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | let mut krate = unwrap("crate", krate::parsing::krate, |
| 151 | input.parse()?)?; |
| 152 | krate.shebang = shebang; |
| 153 | Ok(krate) |
| 154 | |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 158 | pub fn parse_item(input: TokenStream) -> Result<Item, ParseError> { |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 159 | unwrap("item", item::parsing::item, input) |
| 160 | } |
| 161 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 162 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 163 | pub fn parse_items(input: TokenStream) -> Result<Vec<Item>, ParseError> { |
David Tolnay | 453cfd1 | 2016-10-23 11:00:14 -0700 | [diff] [blame] | 164 | unwrap("items", item::parsing::items, input) |
| 165 | } |
| 166 | |
| 167 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 168 | pub fn parse_expr(input: TokenStream) -> Result<Expr, ParseError> { |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 169 | unwrap("expression", expr::parsing::expr, input) |
| 170 | } |
| 171 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 172 | pub fn parse_type(input: TokenStream) -> Result<Ty, ParseError> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 173 | unwrap("type", ty::parsing::ty, input) |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 176 | /// Parse a path, such as `std::str::FromStr` or `::syn::parse_path`. |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 177 | pub fn parse_path(input: TokenStream) -> Result<Path, ParseError> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 178 | unwrap("path", ty::parsing::path, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 181 | pub fn parse_where_clause(input: TokenStream) -> Result<WhereClause, ParseError> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 182 | unwrap("where clause", generics::parsing::where_clause, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 185 | pub fn parse_token_trees(input: TokenStream) -> Result<Vec<TokenTree>, ParseError> { |
Simon Sapin | 095a42b | 2016-10-20 15:54:23 +0200 | [diff] [blame] | 186 | unwrap("token trees", mac::parsing::token_trees, input) |
| 187 | } |
| 188 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 189 | pub fn parse_ident(input: TokenStream) -> Result<Ident, ParseError> { |
David Tolnay | a822836 | 2016-12-22 15:21:54 -0500 | [diff] [blame] | 190 | unwrap("identifier", ident::parsing::ident, input) |
| 191 | } |
| 192 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 193 | pub fn parse_ty_param_bound(input: TokenStream) -> Result<TyParamBound, ParseError> { |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 194 | unwrap("type parameter bound", |
| 195 | generics::parsing::ty_param_bound, |
| 196 | input) |
David Tolnay | 23d83f9 | 2017-01-25 15:41:47 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 199 | /// Parse an attribute declared outside the item it annotates, such as |
| 200 | /// a struct annotation. They are written as `#[...]`. |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 201 | pub fn parse_outer_attr(input: TokenStream) -> Result<Attribute, ParseError> { |
Colin Kiegel | cb72443 | 2017-03-06 12:59:18 +0100 | [diff] [blame] | 202 | unwrap("outer attribute", attr::parsing::outer_attr, input) |
| 203 | } |
| 204 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 205 | /// Parse an attribute declared inside the item it annotates. These are used |
| 206 | /// for crate annotations or for mod-level declarations when modules are in |
| 207 | /// their own files. They are written as `#![...]`. |
Colin Kiegel | cb72443 | 2017-03-06 12:59:18 +0100 | [diff] [blame] | 208 | #[cfg(feature = "full")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 209 | pub fn parse_inner_attr(input: TokenStream) -> Result<Attribute, ParseError> { |
Colin Kiegel | cb72443 | 2017-03-06 12:59:18 +0100 | [diff] [blame] | 210 | unwrap("inner attribute", attr::parsing::inner_attr, input) |
| 211 | } |
| 212 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 213 | /// Deprecated: Use `parse_derive_input` instead. |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 214 | #[doc(hidden)] |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 215 | #[deprecated(since="0.11.0", note = "Use `parse_derive_input` instead")] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 216 | pub fn parse_macro_input(input: TokenStream) -> Result<MacroInput, ParseError> { |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 217 | parse_derive_input(input) |
| 218 | } |
| 219 | |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 220 | /// Alias for `syn::parse_derive_input`. |
| 221 | impl FromStr for DeriveInput { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 222 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 223 | |
| 224 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 225 | parse_derive_input(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /// Alias for `syn::parse_crate`. |
| 230 | #[cfg(feature = "full")] |
| 231 | impl FromStr for Crate { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 232 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 233 | |
| 234 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 235 | parse_crate(s) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /// Alias for `syn::parse_item`. |
| 240 | #[cfg(feature = "full")] |
| 241 | impl FromStr for Item { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 242 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 243 | |
| 244 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 245 | parse_item(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | /// Alias for `syn::parse_expr`. |
| 250 | #[cfg(feature = "full")] |
| 251 | impl FromStr for Expr { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 252 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 253 | |
| 254 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 255 | parse_expr(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | /// Alias for `syn::parse_type`. |
| 260 | impl FromStr for Ty { |
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> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 264 | parse_type(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | /// Alias for `syn::parse_path`. |
| 269 | impl FromStr for Path { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 270 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 271 | |
| 272 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 273 | parse_path(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /// Alias for `syn::parse_where_clause`. |
| 278 | impl FromStr for WhereClause { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 279 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 280 | |
| 281 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 282 | parse_where_clause(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | /// Alias for `syn::parse_ident`. |
| 287 | impl FromStr for Ident { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 288 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 289 | |
| 290 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 291 | parse_ident(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Alias for `syn::parse_ty_param_bound`. |
| 296 | impl FromStr for TyParamBound { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 297 | type Err = ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 298 | |
| 299 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 300 | parse_ty_param_bound(s.parse()?) |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 304 | fn unwrap<T>(name: &'static str, |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 305 | f: fn(&[synom::TokenTree]) -> IResult<&[synom::TokenTree], T>, |
| 306 | input: TokenStream) |
| 307 | -> Result<T, ParseError> { |
| 308 | let input = synom::InputBuf::new(input); |
| 309 | match f(&input) { |
| 310 | IResult::Done(rest, t) => { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 311 | if rest.is_empty() { |
| 312 | Ok(t) |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 313 | } else if rest.len() == input.len() { |
| 314 | // parsed nothing |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 315 | Err(ParseError(format!("failed to parse {}", name))) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 316 | } else { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 317 | Err(ParseError(format!("unparsed tokens after {}", name))) |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 318 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 319 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame^] | 320 | IResult::Error => Err(ParseError(format!("failed to parse {}", name))), |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 321 | } |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 324 | |
| 325 | #[cfg(feature = "parsing")] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 326 | pub mod parse { |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 327 | //! 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] | 328 | //! parse custom grammars when used alongside the `synom` crate. |
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 | //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a |
| 331 | //! persistent pitfall of using `nom` to parse Rust by eliminating the |
| 332 | //! `IResult::Incomplete` variant. The `synom` crate should be used instead |
| 333 | //! of `nom` when working with the parsers in this module. |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 334 | |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 335 | pub use synom::IResult; |
| 336 | |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 337 | #[cfg(feature = "full")] |
| 338 | pub use item::parsing::item; |
| 339 | |
| 340 | #[cfg(feature = "full")] |
David Tolnay | 1bf1913 | 2017-02-19 22:54:25 -0800 | [diff] [blame] | 341 | pub use expr::parsing::{expr, pat, block, stmt}; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 342 | |
David Tolnay | 0ee4189 | 2017-02-19 22:52:59 -0800 | [diff] [blame] | 343 | 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] | 344 | |
David Tolnay | 0ee4189 | 2017-02-19 22:52:59 -0800 | [diff] [blame] | 345 | pub use ty::parsing::{ty, path}; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 346 | |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 347 | pub use mac::parsing::token_tree as tt; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 348 | |
| 349 | pub use ident::parsing::ident; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 350 | |
| 351 | pub use generics::parsing::lifetime; |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 352 | } |