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