blob: 603e094a0aa687743e5d03461510634a7370e4f4 [file] [log] [blame]
David Tolnayaed77b02016-09-23 20:50:31 -07001#![cfg_attr(feature = "clippy", feature(plugin))]
2#![cfg_attr(feature = "clippy", plugin(clippy))]
3
David Tolnay87d0b442016-09-04 11:52:12 -07004#[cfg(feature = "printing")]
5extern crate quote;
6
David Tolnayb5a7b142016-09-13 22:46:39 -07007#[cfg(feature = "parsing")]
David Tolnay10413f02016-09-30 09:12:02 -07008extern crate unicode_xid;
9
10#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -070011#[macro_use]
David Tolnayb5a7b142016-09-13 22:46:39 -070012mod nom;
David Tolnay6b7aaf02016-09-04 10:39:25 -070013
David Tolnaye0aa55a2016-10-02 14:55:23 -070014#[cfg(feature = "parsing")]
David Tolnay6b7aaf02016-09-04 10:39:25 -070015#[macro_use]
David Tolnayb79ee962016-09-04 09:39:20 -070016mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -070017
David Tolnaye0aa55a2016-10-02 14:55:23 -070018#[cfg(feature = "parsing")]
David Tolnay886d8ea2016-09-13 08:34:07 -070019mod escape;
20
David Tolnayb79ee962016-09-04 09:39:20 -070021mod attr;
David Tolnaydaaf7742016-10-03 11:11:43 -070022pub use attr::{Attribute, AttrStyle, MetaItem};
David Tolnay35161ff2016-09-03 11:33:15 -070023
David Tolnay3cb23a92016-10-07 23:02:21 -070024mod constant;
25pub use constant::ConstExpr;
26
David Tolnayf38cdf62016-09-23 19:07:09 -070027mod data;
David Tolnay3cb23a92016-10-07 23:02:21 -070028pub use data::{Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070029
David Tolnayf4bbbd92016-09-23 14:41:55 -070030#[cfg(feature = "full")]
31mod expr;
32#[cfg(feature = "full")]
David Tolnay3cb23a92016-10-07 23:02:21 -070033pub use expr::{Arm, BindingMode, Block, BlockCheckMode, CaptureBy, Expr, FieldPat, 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,
38 WhereBoundPredicate, WhereClause, WherePredicate, WhereRegionPredicate};
David Tolnaye7678922016-10-13 20:44:03 -070039#[cfg(feature = "printing")]
40pub use generics::{ImplGenerics, TyGenerics};
David Tolnay35161ff2016-09-03 11:33:15 -070041
David Tolnay4a51dc72016-10-01 00:40:31 -070042#[cfg(feature = "full")]
43mod krate;
44#[cfg(feature = "full")]
45pub use krate::Crate;
46
David Tolnay55337722016-09-11 12:58:56 -070047mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070048pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070049
David Tolnayf38cdf62016-09-23 19:07:09 -070050#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070051mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070052#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070053pub use item::{Abi, Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem,
54 ForeignMod, ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig,
55 PathListItem, TraitItem, TraitItemKind, Unsafety, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070056
David Tolnayf4bbbd92016-09-23 14:41:55 -070057mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070058pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070059
60#[cfg(feature = "full")]
61mod mac;
62#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070063pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070064
David Tolnayf38cdf62016-09-23 19:07:09 -070065mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070066pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070067
David Tolnay3cb23a92016-10-07 23:02:21 -070068mod op;
69pub use op::{BinOp, UnOp};
70
David Tolnaye0aa55a2016-10-02 14:55:23 -070071#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070072mod space;
73
David Tolnayb79ee962016-09-04 09:39:20 -070074mod ty;
David Tolnay3bcfb722016-10-08 11:58:36 -070075pub use ty::{AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy, Mutability,
76 ParenthesizedParameterData, Path, PathParameters, PathSegment, PolyTraitRef, QSelf,
77 Ty, TypeBinding};
David Tolnay35161ff2016-09-03 11:33:15 -070078
David Tolnay55337722016-09-11 12:58:56 -070079#[cfg(feature = "aster")]
80pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070081
David Tolnay55337722016-09-11 12:58:56 -070082#[cfg(feature = "visit")]
83pub mod visit;
84
85#[cfg(feature = "parsing")]
86pub use parsing::*;
87
88#[cfg(feature = "parsing")]
89mod parsing {
90 use super::*;
David Tolnayf5fdfa02016-10-23 15:25:17 -070091 use {generics, macro_input, space, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -070092 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -070093
David Tolnayedf2b992016-09-23 20:43:45 -070094 #[cfg(feature = "full")]
Simon Sapin095a42b2016-10-20 15:54:23 +020095 use {expr, item, krate, mac};
David Tolnayedf2b992016-09-23 20:43:45 -070096
David Tolnayf38cdf62016-09-23 19:07:09 -070097 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
98 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -070099 }
100
David Tolnayedf2b992016-09-23 20:43:45 -0700101 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700102 pub fn parse_crate(input: &str) -> Result<Crate, String> {
103 unwrap("crate", krate::parsing::krate, input)
104 }
105
106 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700107 pub fn parse_item(input: &str) -> Result<Item, String> {
108 unwrap("item", item::parsing::item, input)
109 }
110
David Tolnayb9c8e322016-09-23 20:48:37 -0700111 #[cfg(feature = "full")]
112 pub fn parse_expr(input: &str) -> Result<Expr, String> {
113 unwrap("expression", expr::parsing::expr, input)
114 }
115
David Tolnay32a112e2016-09-11 17:46:15 -0700116 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700117 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700118 }
119
David Tolnay55337722016-09-11 12:58:56 -0700120 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700121 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700122 }
123
124 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700125 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700126 }
127
Simon Sapin095a42b2016-10-20 15:54:23 +0200128 #[cfg(feature = "full")]
129 pub fn parse_token_trees(input: &str) -> Result<Vec<TokenTree>, String> {
130 unwrap("token trees", mac::parsing::token_trees, input)
131 }
132
David Tolnaydaaf7742016-10-03 11:11:43 -0700133 fn unwrap<T>(name: &'static str,
134 f: fn(&str) -> IResult<&str, T>,
135 input: &str)
136 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700137 match f(input) {
David Tolnayf5fdfa02016-10-23 15:25:17 -0700138 IResult::Done(mut rest, t) => {
139 rest = match space::whitespace(rest) {
140 IResult::Done(rest, _) => rest,
141 IResult::Error => rest,
142 };
David Tolnay55337722016-09-11 12:58:56 -0700143 if rest.is_empty() {
144 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700145 } else if rest.len() == input.len() {
146 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700147 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700148 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700149 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700150 }
David Tolnay55337722016-09-11 12:58:56 -0700151 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700152 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700153 }
David Tolnay35161ff2016-09-03 11:33:15 -0700154 }
155}