blob: 9e9ffd0a9460cd01c53ba5b8808c423b938de1d7 [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 Tolnayf38cdf62016-09-23 19:07:09 -070024mod data;
David Tolnaydaaf7742016-10-03 11:11:43 -070025pub use data::{Discriminant, Field, Variant, VariantData, Visibility};
David Tolnayf38cdf62016-09-23 19:07:09 -070026
David Tolnayf4bbbd92016-09-23 14:41:55 -070027#[cfg(feature = "full")]
28mod expr;
29#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070030pub use expr::{Arm, BinOp, BindingMode, Block, BlockCheckMode, CaptureBy, Expr, FieldPat, Local,
31 MacStmtStyle, Pat, RangeLimits, Stmt, UnOp};
David Tolnayf4bbbd92016-09-23 14:41:55 -070032
David Tolnayb79ee962016-09-04 09:39:20 -070033mod generics;
David Tolnaydaaf7742016-10-03 11:11:43 -070034pub use generics::{Generics, Lifetime, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound,
35 WhereBoundPredicate, WhereClause, WherePredicate, WhereRegionPredicate};
David Tolnay35161ff2016-09-03 11:33:15 -070036
David Tolnay4a51dc72016-10-01 00:40:31 -070037#[cfg(feature = "full")]
38mod krate;
39#[cfg(feature = "full")]
40pub use krate::Crate;
41
David Tolnay55337722016-09-11 12:58:56 -070042mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070043pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070044
David Tolnayf38cdf62016-09-23 19:07:09 -070045#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070046mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070047#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070048pub use item::{Abi, Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem,
49 ForeignMod, ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig,
50 PathListItem, TraitItem, TraitItemKind, Unsafety, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070051
David Tolnayf4bbbd92016-09-23 14:41:55 -070052mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070053pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070054
55#[cfg(feature = "full")]
56mod mac;
57#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070058pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070059
David Tolnayf38cdf62016-09-23 19:07:09 -070060mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070061pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070062
David Tolnaye0aa55a2016-10-02 14:55:23 -070063#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070064mod space;
65
David Tolnayb79ee962016-09-04 09:39:20 -070066mod ty;
David Tolnay429168f2016-10-05 23:41:04 -070067pub use ty::{AngleBracketedParameterData, ArrayLen, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
68 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
69 PolyTraitRef, QSelf, Ty, TypeBinding};
David Tolnay35161ff2016-09-03 11:33:15 -070070
David Tolnay55337722016-09-11 12:58:56 -070071#[cfg(feature = "aster")]
72pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070073
David Tolnay55337722016-09-11 12:58:56 -070074#[cfg(feature = "visit")]
75pub mod visit;
76
77#[cfg(feature = "parsing")]
78pub use parsing::*;
79
80#[cfg(feature = "parsing")]
81mod parsing {
82 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -070083 use {generics, macro_input, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -070084 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -070085
David Tolnayedf2b992016-09-23 20:43:45 -070086 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -070087 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070088
David Tolnayf38cdf62016-09-23 19:07:09 -070089 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
90 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -070091 }
92
David Tolnayedf2b992016-09-23 20:43:45 -070093 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -070094 pub fn parse_crate(input: &str) -> Result<Crate, String> {
95 unwrap("crate", krate::parsing::krate, input)
96 }
97
98 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -070099 pub fn parse_item(input: &str) -> Result<Item, String> {
100 unwrap("item", item::parsing::item, input)
101 }
102
David Tolnayb9c8e322016-09-23 20:48:37 -0700103 #[cfg(feature = "full")]
104 pub fn parse_expr(input: &str) -> Result<Expr, String> {
105 unwrap("expression", expr::parsing::expr, input)
106 }
107
David Tolnay32a112e2016-09-11 17:46:15 -0700108 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700109 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700110 }
111
David Tolnay55337722016-09-11 12:58:56 -0700112 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700113 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700114 }
115
116 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700117 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700118 }
119
David Tolnaydaaf7742016-10-03 11:11:43 -0700120 fn unwrap<T>(name: &'static str,
121 f: fn(&str) -> IResult<&str, T>,
122 input: &str)
123 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700124 match f(input) {
David Tolnay14cbdeb2016-10-01 12:13:59 -0700125 IResult::Done(rest, t) => {
David Tolnay55337722016-09-11 12:58:56 -0700126 if rest.is_empty() {
127 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700128 } else if rest.len() == input.len() {
129 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700130 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700131 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700132 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700133 }
David Tolnay55337722016-09-11 12:58:56 -0700134 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700135 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700136 }
David Tolnay35161ff2016-09-03 11:33:15 -0700137 }
138}