blob: 76ca608728ab8dc98b5ccc49a7a60d36b4209871 [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 Tolnay35161ff2016-09-03 11:33:15 -070039
David Tolnay4a51dc72016-10-01 00:40:31 -070040#[cfg(feature = "full")]
41mod krate;
42#[cfg(feature = "full")]
43pub use krate::Crate;
44
David Tolnay55337722016-09-11 12:58:56 -070045mod ident;
David Tolnaydaaf7742016-10-03 11:11:43 -070046pub use ident::Ident;
David Tolnay55337722016-09-11 12:58:56 -070047
David Tolnayf38cdf62016-09-23 19:07:09 -070048#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070049mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070050#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070051pub use item::{Abi, Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem,
52 ForeignMod, ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig,
53 PathListItem, TraitItem, TraitItemKind, Unsafety, ViewPath};
David Tolnay35161ff2016-09-03 11:33:15 -070054
David Tolnayf4bbbd92016-09-23 14:41:55 -070055mod lit;
David Tolnaydaaf7742016-10-03 11:11:43 -070056pub use lit::{FloatTy, IntTy, Lit, StrStyle};
David Tolnayf4bbbd92016-09-23 14:41:55 -070057
58#[cfg(feature = "full")]
59mod mac;
60#[cfg(feature = "full")]
David Tolnaydaaf7742016-10-03 11:11:43 -070061pub use mac::{BinOpToken, DelimToken, Delimited, Mac, Token, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -070062
David Tolnayf38cdf62016-09-23 19:07:09 -070063mod macro_input;
David Tolnaydaaf7742016-10-03 11:11:43 -070064pub use macro_input::{Body, MacroInput};
David Tolnayf38cdf62016-09-23 19:07:09 -070065
David Tolnay3cb23a92016-10-07 23:02:21 -070066mod op;
67pub use op::{BinOp, UnOp};
68
David Tolnaye0aa55a2016-10-02 14:55:23 -070069#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -070070mod space;
71
David Tolnayb79ee962016-09-04 09:39:20 -070072mod ty;
David Tolnay3cb23a92016-10-07 23:02:21 -070073pub use ty::{AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy,
David Tolnay429168f2016-10-05 23:41:04 -070074 Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment,
75 PolyTraitRef, QSelf, Ty, TypeBinding};
David Tolnay35161ff2016-09-03 11:33:15 -070076
David Tolnay55337722016-09-11 12:58:56 -070077#[cfg(feature = "aster")]
78pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070079
David Tolnay55337722016-09-11 12:58:56 -070080#[cfg(feature = "visit")]
81pub mod visit;
82
83#[cfg(feature = "parsing")]
84pub use parsing::*;
85
86#[cfg(feature = "parsing")]
87mod parsing {
88 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -070089 use {generics, macro_input, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -070090 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -070091
David Tolnayedf2b992016-09-23 20:43:45 -070092 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -070093 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -070094
David Tolnayf38cdf62016-09-23 19:07:09 -070095 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
96 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -070097 }
98
David Tolnayedf2b992016-09-23 20:43:45 -070099 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700100 pub fn parse_crate(input: &str) -> Result<Crate, String> {
101 unwrap("crate", krate::parsing::krate, input)
102 }
103
104 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700105 pub fn parse_item(input: &str) -> Result<Item, String> {
106 unwrap("item", item::parsing::item, input)
107 }
108
David Tolnayb9c8e322016-09-23 20:48:37 -0700109 #[cfg(feature = "full")]
110 pub fn parse_expr(input: &str) -> Result<Expr, String> {
111 unwrap("expression", expr::parsing::expr, input)
112 }
113
David Tolnay32a112e2016-09-11 17:46:15 -0700114 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700115 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700116 }
117
David Tolnay55337722016-09-11 12:58:56 -0700118 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700119 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700120 }
121
122 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700123 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700124 }
125
David Tolnaydaaf7742016-10-03 11:11:43 -0700126 fn unwrap<T>(name: &'static str,
127 f: fn(&str) -> IResult<&str, T>,
128 input: &str)
129 -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700130 match f(input) {
David Tolnay14cbdeb2016-10-01 12:13:59 -0700131 IResult::Done(rest, t) => {
David Tolnay55337722016-09-11 12:58:56 -0700132 if rest.is_empty() {
133 Ok(t)
David Tolnay2e737362016-10-05 23:44:15 -0700134 } else if rest.len() == input.len() {
135 // parsed nothing
David Tolnayba8947b2016-10-05 23:31:12 -0700136 Err(format!("failed to parse {}: {:?}", name, rest))
David Tolnay55337722016-09-11 12:58:56 -0700137 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700138 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700139 }
David Tolnay55337722016-09-11 12:58:56 -0700140 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700141 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700142 }
David Tolnay35161ff2016-09-03 11:33:15 -0700143 }
144}