blob: a8a7c06fc98e3bfa0c0ef3a0a4e2f425ac5b3669 [file] [log] [blame]
David Tolnayad2836d2017-04-20 10:11:43 -07001#![doc(html_root_url = "https://dtolnay.github.io/syn")]
2
David Tolnay02a8d472017-02-19 12:59:44 -08003#![cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
David Tolnayaed77b02016-09-23 20:50:31 -07004
David Tolnay87d0b442016-09-04 11:52:12 -07005#[cfg(feature = "printing")]
6extern crate quote;
7
David Tolnayb5a7b142016-09-13 22:46:39 -07008#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -07009extern crate unicode_xid;
10
11#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070012#[macro_use]
David Tolnay5fe14fc2017-01-27 16:22:08 -080013extern crate synom;
David Tolnay35161ff2016-09-03 11:33:15 -070014
David Tolnay631cb8c2016-11-10 17:16:41 -080015#[cfg(feature = "aster")]
16pub mod aster;
David Tolnay886d8ea2016-09-13 08:34:07 -070017
David Tolnayb79ee962016-09-04 09:39:20 -070018mod attr;
David Tolnayb7fa2b62016-10-30 10:50:47 -070019pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070020
David Tolnay3cb23a92016-10-07 23:02:21 -070021mod constant;
22pub use constant::ConstExpr;
23
David Tolnayf38cdf62016-09-23 19:07:09 -070024mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070025pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070026
David Tolnay631cb8c2016-11-10 17:16:41 -080027#[cfg(feature = "parsing")]
28mod escape;
29
David Tolnayf4bbbd92016-09-23 14:41:55 -070030#[cfg(feature = "full")]
31mod expr;
32#[cfg(feature = "full")]
David Tolnay05120ef2017-03-12 18:29:26 -070033pub use expr::{Arm, BindingMode, Block, CaptureBy, Expr, ExprKind, FieldPat, FieldValue, Local,
34 MacStmtStyle, Pat, RangeLimits, Stmt};
David Tolnayf4bbbd92016-09-23 14:41:55 -070035
David Tolnayb79ee962016-09-04 09:39:20 -070036mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070037pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
David Tolnayfa23f572017-01-23 00:19:11 -080038 WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate,
39 WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070040#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080041pub use generics::{ImplGenerics, Turbofish, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070042
David Tolnay55337722016-09-11 12:58:56 -070043mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070044pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070045
David Tolnayf38cdf62016-09-23 19:07:09 -070046#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070047mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070048#[cfg(feature = "full")]
David Tolnayc1fea502016-10-30 17:54:02 -070049pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ForeignMod,
50 ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem,
51 TraitItem, TraitItemKind, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070052
David Tolnay631cb8c2016-11-10 17:16:41 -080053#[cfg(feature = "full")]
54mod krate;
55#[cfg(feature = "full")]
56pub use krate::Crate;
57
David Tolnayf4bbbd92016-09-23 14:41:55 -070058mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070059pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnay1f16b602017-02-07 20:06:55 -050060#[cfg(feature = "parsing")]
61pub use lit::{ByteStrLit, FloatLit, IntLit, StrLit};
David Tolnayf4bbbd92016-09-23 14:41:55 -070062
David Tolnayf4bbbd92016-09-23 14:41:55 -070063mod mac;
David Tolnaydaaf7742016-10-03 11:11:43 -070064pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070065
David Tolnay0e837402016-12-22 17:25:55 -050066mod derive;
67pub use derive::{Body, DeriveInput};
David Tolnayc28a4a52017-02-04 09:50:57 -080068// Deprecated. Use `DeriveInput` instead.
David Tolnay0e837402016-12-22 17:25:55 -050069#[doc(hidden)]
70pub type MacroInput = DeriveInput;
David Tolnayf38cdf62016-09-23 19:07:09 -070071
David Tolnay3cb23a92016-10-07 23:02:21 -070072mod op;
73pub use op::{BinOp, UnOp};
74
David Tolnayb79ee962016-09-04 09:39:20 -070075mod ty;
David Tolnayb8d8ef52016-10-29 14:30:08 -070076pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
77 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
78 PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety};
David Tolnay35161ff2016-09-03 11:33:15 -070079
David Tolnay55337722016-09-11 12:58:56 -070080#[cfg(feature = "visit")]
81pub mod visit;
82
gnzlbg9ae88d82017-01-26 20:45:17 +010083#[cfg(feature = "fold")]
84pub mod fold;
85
David Tolnay55337722016-09-11 12:58:56 -070086#[cfg(feature = "parsing")]
87pub use parsing::*;
88
89#[cfg(feature = "parsing")]
90mod parsing {
91 use super::*;
Colin Kiegel108a7fe2017-03-06 13:29:57 +010092 use {derive, generics, ident, mac, ty, attr};
David Tolnay5fe14fc2017-01-27 16:22:08 -080093 use synom::{space, IResult};
David Tolnay55337722016-09-11 12:58:56 -070094
David Tolnayedf2b992016-09-23 20:43:45 -070095 #[cfg(feature = "full")]
David Tolnayc879a502017-01-25 15:51:32 -080096 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070097
David Tolnay0e837402016-12-22 17:25:55 -050098 pub fn parse_derive_input(input: &str) -> Result<DeriveInput, String> {
99 unwrap("derive input", derive::parsing::derive_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700100 }
101
David Tolnayedf2b992016-09-23 20:43:45 -0700102 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700103 pub fn parse_crate(input: &str) -> Result<Crate, String> {
104 unwrap("crate", krate::parsing::krate, input)
105 }
106
107 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700108 pub fn parse_item(input: &str) -> Result<Item, String> {
109 unwrap("item", item::parsing::item, input)
110 }
111
David Tolnayb9c8e322016-09-23 20:48:37 -0700112 #[cfg(feature = "full")]
David Tolnay453cfd12016-10-23 11:00:14 -0700113 pub fn parse_items(input: &str) -> Result<Vec<Item>, String> {
114 unwrap("items", item::parsing::items, input)
115 }
116
117 #[cfg(feature = "full")]
David Tolnayb9c8e322016-09-23 20:48:37 -0700118 pub fn parse_expr(input: &str) -> Result<Expr, String> {
119 unwrap("expression", expr::parsing::expr, input)
120 }
121
David Tolnay32a112e2016-09-11 17:46:15 -0700122 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700123 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700124 }
125
David Tolnay55337722016-09-11 12:58:56 -0700126 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700127 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700128 }
129
130 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700131 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700132 }
133
Simon Sapin095a42b2016-10-20 15:54:23 +0200134 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
135 unwrap("token trees", mac::parsing::token_trees, input)
136 }
137
David Tolnaya8228362016-12-22 15:21:54 -0500138 pub fn parse_ident(input: &str) -> Result<Ident, String> {
139 unwrap("identifier", ident::parsing::ident, input)
140 }
141
David Tolnay23d83f92017-01-25 15:41:47 -0800142 pub fn parse_ty_param_bound(input: &str) -> Result<TyParamBound, String> {
David Tolnay05120ef2017-03-12 18:29:26 -0700143 unwrap("type parameter bound",
144 generics::parsing::ty_param_bound,
145 input)
David Tolnay23d83f92017-01-25 15:41:47 -0800146 }
147
Colin Kiegelcb724432017-03-06 12:59:18 +0100148 pub fn parse_outer_attr(input: &str) -> Result<Attribute, String> {
149 unwrap("outer attribute", attr::parsing::outer_attr, input)
150 }
151
152 #[cfg(feature = "full")]
153 pub fn parse_inner_attr(input: &str) -> Result<Attribute, String> {
154 unwrap("inner attribute", attr::parsing::inner_attr, input)
155 }
156
David Tolnayc28a4a52017-02-04 09:50:57 -0800157 // Deprecated. Use `parse_derive_input` instead.
David Tolnay0e837402016-12-22 17:25:55 -0500158 #[doc(hidden)]
159 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
160 parse_derive_input(input)
161 }
162
David Tolnaydaaf7742016-10-03 11:11:43 -0700163 fn unwrap<T>(name: &'static str,
164 f: fn(&str) -> IResult<&str, T>,
165 input: &str)
166 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700167 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700168 IResult::Done(mut rest, t) => {
David Tolnaydef66372016-10-24 21:51:32 -0700169 rest = space::skip_whitespace(rest);
David Tolnay55337722016-09-11 12:58:56 -0700170 if rest.is_empty() {
171 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700172 } else if rest.len() == input.len() {
173 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700174 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700175 } else {
David Tolnayf2222f02017-01-27 17:09:20 -0800176 Err(format!("unparsed tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700177 }
David Tolnay55337722016-09-11 12:58:56 -0700178 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700179 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700180 }
David Tolnay35161ff2016-09-03 11:33:15 -0700181 }
182}
Michael Layzell5e107ff2017-01-24 19:58:39 -0500183
184#[cfg(feature = "parsing")]
David Tolnay5fe14fc2017-01-27 16:22:08 -0800185pub mod parse {
Michael Layzell5e107ff2017-01-24 19:58:39 -0500186 //! This module contains a set of exported nom parsers which can be used to
David Tolnay5fe14fc2017-01-27 16:22:08 -0800187 //! parse custom grammars when used alongside the `synom` crate.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500188 //!
David Tolnay5fe14fc2017-01-27 16:22:08 -0800189 //! Internally, `syn` uses a fork of `nom` called `synom` which resolves a
190 //! persistent pitfall of using `nom` to parse Rust by eliminating the
191 //! `IResult::Incomplete` variant. The `synom` crate should be used instead
192 //! of `nom` when working with the parsers in this module.
Michael Layzell5e107ff2017-01-24 19:58:39 -0500193
David Tolnay0a8972b2017-02-27 02:10:01 -0800194 pub use synom::IResult;
195
Michael Layzell5e107ff2017-01-24 19:58:39 -0500196 #[cfg(feature = "full")]
197 pub use item::parsing::item;
198
199 #[cfg(feature = "full")]
David Tolnay1bf19132017-02-19 22:54:25 -0800200 pub use expr::parsing::{expr, pat, block, stmt};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500201
David Tolnay0ee41892017-02-19 22:52:59 -0800202 pub use lit::parsing::{lit, string, byte_string, byte, character, float, int, boolean};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500203
David Tolnay0ee41892017-02-19 22:52:59 -0800204 pub use ty::parsing::{ty, path};
Michael Layzell5e107ff2017-01-24 19:58:39 -0500205
David Tolnay5fe14fc2017-01-27 16:22:08 -0800206 pub use mac::parsing::token_tree as tt;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500207
208 pub use ident::parsing::ident;
David Tolnay1f16b602017-02-07 20:06:55 -0500209
210 pub use generics::parsing::lifetime;
Michael Layzell5e107ff2017-01-24 19:58:39 -0500211}