blob: 65ad6dfa4871e275a1d7af920ac5905d8d1b004a [file] [log] [blame]
David Tolnay02a8d472017-02-19 12:59:44 -08001#![cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
David Tolnayaed77b02016-09-23 20:50:31 -07002
David Tolnay87d0b442016-09-04 11:52:12 -07003#[cfg(feature = "printing")]
4extern crate quote;
5
David Tolnayb5a7b142016-09-13 22:46:39 -07006#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -07007extern crate unicode_xid;
8
9#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070010#[macro_use]
David Tolnay5fe14fc2017-01-27 16:22:08 -080011extern crate synom;
David Tolnay35161ff2016-09-03 11:33:15 -070012
David Tolnay631cb8c2016-11-10 17:16:41 -080013#[cfg(feature = "aster")]
14pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070015
David Tolnayb79ee962016-09-04 09:39:20 -070016mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070017pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070018
David Tolnay3cb23a92016-10-07 23:02:21 -070019mod constant;
20pub use constant::ConstExpr;
21
David Tolnayf38cdf62016-09-23 19:07:09 -070022mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070023pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070024
David Tolnay631cb8c2016-11-10 17:16:41 -080025#[cfg(feature = "parsing")]
26mod escape;
27
David Tolnayf4bbbd92016-09-23 14:41:55 -070028#[cfg(feature = "full")]
29mod expr;
30#[cfg(feature = "full")]
David Tolnay7b035912016-12-21 22:42:07 -050031pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue,
32 Local, MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070033
David Tolnayb79ee962016-09-04 09:39:20 -070034mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070035pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080036 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
37 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070038#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080039pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070040
David Tolnay55337722016-09-11 12:58:56 -070041mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070042pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070043
David Tolnayf38cdf62016-09-23 19:07:09 -070044#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070045mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070046#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070047pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
48 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
49 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070050
David Tolnay631cb8c2016-11-10 17:16:41 -080051#[cfg(feature = "full")]
52mod krate;
53#[cfg(feature = "full")]
54pub use krate::Crate;
55
David Tolnayf4bbbd92016-09-23 14:41:55 -070056mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070057pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnay1f16b602017-02-07 20:06:55 -050058#[cfg(feature = "parsing")]
59pub use lit::{ByteStrLit, FloatLit, IntLit, StrLit};
David Tolnayf4bbbd92016-09-23 14:41:55 -070060
David Tolnayf4bbbd92016-09-23 14:41:55 -070061mod mac;
David Tolnaydaaf7742016-10-03 11:11:43 -070062pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070063
David Tolnay0e837402016-12-22 17:25:55 -050064mod derive;
65pub use derive::{Body, DeriveInput};
David Tolnayc28a4a52017-02-04 09:50:57 -080066// Deprecated. Use `DeriveInput` instead.
David Tolnay0e837402016-12-22 17:25:55 -050067#[doc(hidden)]
68pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070069
David Tolnay3cb23a92016-10-07 23:02:21 -070070mod op;
71pub use op::{BinOp, UnOp};
72
David Tolnayb79ee962016-09-04 09:39:20 -070073mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070074pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
75 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
76 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070077
David Tolnay55337722016-09-11 12:58:56 -070078#[cfg(feature = "visit")]
79pub mod visit;
80
gnzlbg9ae88d82017-01-26 20:45:17 +010081#[cfg(feature = "fold")]
82pub mod fold;
83
David Tolnay55337722016-09-11 12:58:56 -070084#[cfg(feature = "parsing")]
85pub use parsing::*;
86
87#[cfg(feature = "parsing")]
88mod parsing {
89 use super::*;
Michael Layzell5bde96f2017-01-24 17:59:21 -050090 use {derive, generics, ident, mac, ty};
David Tolnay5fe14fc2017-01-27 16:22:08 -080091 use synom::{space, IResult};
David Tolnay55337722016-09-11 12:58:56 -070092
David Tolnayedf2b992016-09-23 20:43:45 -070093 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -080094 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070095
David Tolnay0e837402016-12-22 17:25:55 -050096 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
97 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -070098 }
99
David Tolnayedf2b992016-09-23 20:43:45 -0700100 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700101 pub fn parse_crate(input: &str) -> Result<Crate, String> {
102 unwrap("crate", krate::parsing::krate, input)
103 }
104
105 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700106 pub fn parse_item(input: &str) -> Result<Item, String> {
107 unwrap("item", item::parsing::item, input)
108 }
109
David Tolnayb9c8e322016-09-23 20:48:37 -0700110 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700111 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
112 unwrap("items", item::parsing::items, input)
113 }
114
115 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700116 pub fn parse_expr(input: &str) -> Result<Expr, String> {
117 unwrap("expression", expr::parsing::expr, input)
118 }
119
David Tolnay32a112e2016-09-11 17:46:15 -0700120 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700121 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700122 }
123
David Tolnay55337722016-09-11 12:58:56 -0700124 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700125 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700126 }
127
128 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700129 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700130 }
131
Simon Sapin095a42b2016-10-20 15:54:23 +0200132 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
133 unwrap("token trees", mac::parsing::token_trees, input)
134 }
135
David Tolnaya8228362016-12-22 15:21:54 -0500136 pub fn parse_ident(input: &str) -> Result<Ident, String> {
137 unwrap("identifier", ident::parsing::ident, input)
138 }
139
David Tolnay23d83f92017-01-25 15:41:47 -0800140 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 Tolnayc28a4a52017-02-04 09:50:57 -0800144 // Deprecated. Use `parse_derive_input` instead.
David Tolnay0e837402016-12-22 17:25:55 -0500145 #[doc(hidden)]
146 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
147 parse_derive_input(input)
148 }
149
David Tolnaydaaf7742016-10-03 11:11:43 -0700150 fn unwrap<T>(name: &'static str,
151 f: fn(&str) -> IResult<&str, T>,
152 input: &str)
153 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700154 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700155 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700156 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700157 if rest.is_empty() {
158 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700159 } else if rest.len() == input.len() {
160 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700161 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700162 } else {
David Tolnayf2222f02017-01-27 17:09:20 -0800163 Err(format!("unparsed tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700164 }
David Tolnay55337722016-09-11 12:58:56 -0700165 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700166 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700167 }
David Tolnay35161ff2016-09-03 11:33:15 -0700168 }
169}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500170
171#[cfg(feature = "parsing")]
David Tolnay5fe14fc2017-01-27 16:22:08 -0800172pub mod parse {
Michael Layzell5e107ff2017-01-24 19:58:39 -0500173 //! This module contains a set of exported nom parsers which can be used to
David Tolnay5fe14fc2017-01-27 16:22:08 -0800174 //! parse custom grammars when used alongside the `synom` crate.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500175 //!
David Tolnay5fe14fc2017-01-27 16:22:08 -0800176 //! 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 Layzell5e107ff2017-01-24 19:58:39 -0500180
David Tolnay0a8972b2017-02-27 02:10:01 -0800181 pub use synom::IResult;
182
Michael Layzell5e107ff2017-01-24 19:58:39 -0500183 #[cfg(feature = "full")]
184 pub use item::parsing::item;
185
186 #[cfg(feature = "full")]
David Tolnay1bf19132017-02-19 22:54:25 -0800187 pub use expr::parsing::{expr, pat, block, stmt};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500188
David Tolnay0ee41892017-02-19 22:52:59 -0800189 pub use lit::parsing::{lit, string, byte_string, byte, character, float, int, boolean};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500190
David Tolnay0ee41892017-02-19 22:52:59 -0800191 pub use ty::parsing::{ty, path};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500192
David Tolnay5fe14fc2017-01-27 16:22:08 -0800193 pub use mac::parsing::token_tree as tt;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500194
195 pub use ident::parsing::ident;
David Tolnay1f16b602017-02-07 20:06:55 -0500196
197 pub use generics::parsing::lifetime;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500198}