blob: 7173b6f86a51de6d951ddc387aecf05fa3f88381 [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;
22pub use attr::{
23 Attribute,
David Tolnay4a51dc72016-10-01 00:40:31 -070024 AttrStyle,
David Tolnayb79ee962016-09-04 09:39:20 -070025 MetaItem,
26};
David Tolnay35161ff2016-09-03 11:33:15 -070027
David Tolnayf38cdf62016-09-23 19:07:09 -070028mod data;
29pub use data::{
30 Discriminant,
31 Field,
32 Variant,
33 VariantData,
34 Visibility,
35};
36
David Tolnayf4bbbd92016-09-23 14:41:55 -070037#[cfg(feature = "full")]
38mod expr;
39#[cfg(feature = "full")]
40pub use expr::{
41 Arm,
42 BinOp,
43 BindingMode,
44 Block,
45 BlockCheckMode,
46 CaptureBy,
47 Expr,
48 FieldPat,
49 Local,
50 MacStmtStyle,
51 Pat,
52 RangeLimits,
53 Stmt,
54 UnOp,
55};
56
David Tolnayb79ee962016-09-04 09:39:20 -070057mod generics;
58pub use generics::{
59 Generics,
60 Lifetime,
61 LifetimeDef,
David Tolnay55337722016-09-11 12:58:56 -070062 TraitBoundModifier,
David Tolnayb79ee962016-09-04 09:39:20 -070063 TyParam,
64 TyParamBound,
65 WhereBoundPredicate,
David Tolnay55337722016-09-11 12:58:56 -070066 WhereClause,
David Tolnayb79ee962016-09-04 09:39:20 -070067 WherePredicate,
68 WhereRegionPredicate,
69};
David Tolnay35161ff2016-09-03 11:33:15 -070070
David Tolnay4a51dc72016-10-01 00:40:31 -070071#[cfg(feature = "full")]
72mod krate;
73#[cfg(feature = "full")]
74pub use krate::Crate;
75
David Tolnay55337722016-09-11 12:58:56 -070076mod ident;
77pub use ident::{
78 Ident,
79};
80
David Tolnayf38cdf62016-09-23 19:07:09 -070081#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070082mod item;
David Tolnayf38cdf62016-09-23 19:07:09 -070083#[cfg(feature = "full")]
David Tolnayb79ee962016-09-04 09:39:20 -070084pub use item::{
David Tolnayf38cdf62016-09-23 19:07:09 -070085 Abi,
86 Constness,
87 Defaultness,
David Tolnay62f374c2016-10-02 13:37:00 -070088 FnArg,
89 FnDecl,
David Tolnayf38cdf62016-09-23 19:07:09 -070090 ForeignItemKind,
91 ForeignItem,
92 ForeignMod,
93 ImplItem,
94 ImplItemKind,
95 ImplPolarity,
David Tolnayb79ee962016-09-04 09:39:20 -070096 Item,
David Tolnayf38cdf62016-09-23 19:07:09 -070097 ItemKind,
98 MethodSig,
99 PathListItem,
100 TraitItem,
101 TraitItemKind,
102 Unsafety,
103 ViewPath,
David Tolnayb79ee962016-09-04 09:39:20 -0700104};
David Tolnay35161ff2016-09-03 11:33:15 -0700105
David Tolnayf4bbbd92016-09-23 14:41:55 -0700106mod lit;
107pub use lit::{
108 FloatTy,
109 IntTy,
110 Lit,
111 StrStyle,
112};
113
114#[cfg(feature = "full")]
115mod mac;
116#[cfg(feature = "full")]
117pub use mac::{
118 BinOpToken,
119 DelimToken,
120 Delimited,
121 Mac,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700122 Token,
123 TokenTree,
124};
125
David Tolnayf38cdf62016-09-23 19:07:09 -0700126mod macro_input;
127pub use macro_input::{
128 Body,
129 MacroInput,
130};
131
David Tolnaye0aa55a2016-10-02 14:55:23 -0700132#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -0700133mod space;
134
David Tolnayb79ee962016-09-04 09:39:20 -0700135mod ty;
136pub use ty::{
137 AngleBracketedParameterData,
David Tolnay62f374c2016-10-02 13:37:00 -0700138 BareFnArg,
David Tolnayb79ee962016-09-04 09:39:20 -0700139 BareFnTy,
David Tolnayb79ee962016-09-04 09:39:20 -0700140 FunctionRetTy,
141 MutTy,
142 Mutability,
143 ParenthesizedParameterData,
144 Path,
145 PathParameters,
146 PathSegment,
147 PolyTraitRef,
148 QSelf,
149 Ty,
150 TypeBinding,
151};
David Tolnay35161ff2016-09-03 11:33:15 -0700152
David Tolnay55337722016-09-11 12:58:56 -0700153#[cfg(feature = "aster")]
154pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -0700155
David Tolnay55337722016-09-11 12:58:56 -0700156#[cfg(feature = "visit")]
157pub mod visit;
158
159#[cfg(feature = "parsing")]
160pub use parsing::*;
161
162#[cfg(feature = "parsing")]
163mod parsing {
164 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -0700165 use {generics, macro_input, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -0700166 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -0700167
David Tolnayedf2b992016-09-23 20:43:45 -0700168 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700169 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -0700170
David Tolnayf38cdf62016-09-23 19:07:09 -0700171 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
172 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700173 }
174
David Tolnayedf2b992016-09-23 20:43:45 -0700175 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700176 pub fn parse_crate(input: &str) -> Result<Crate, String> {
177 unwrap("crate", krate::parsing::krate, input)
178 }
179
180 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700181 pub fn parse_item(input: &str) -> Result<Item, String> {
182 unwrap("item", item::parsing::item, input)
183 }
184
David Tolnayb9c8e322016-09-23 20:48:37 -0700185 #[cfg(feature = "full")]
186 pub fn parse_expr(input: &str) -> Result<Expr, String> {
187 unwrap("expression", expr::parsing::expr, input)
188 }
189
David Tolnay32a112e2016-09-11 17:46:15 -0700190 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700191 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700192 }
193
David Tolnay55337722016-09-11 12:58:56 -0700194 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700195 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700196 }
197
198 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700199 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700200 }
201
David Tolnay14cbdeb2016-10-01 12:13:59 -0700202 fn unwrap<T>(name: &'static str, f: fn(&str) -> IResult<&str, T>, input: &str) -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700203 match f(input) {
David Tolnay14cbdeb2016-10-01 12:13:59 -0700204 IResult::Done(rest, t) => {
David Tolnay55337722016-09-11 12:58:56 -0700205 if rest.is_empty() {
206 Ok(t)
207 } else {
David Tolnay055a7042016-10-02 19:23:54 -0700208 Err(format!("failed to parse tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700209 }
David Tolnay55337722016-09-11 12:58:56 -0700210 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700211 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700212 }
David Tolnay35161ff2016-09-03 11:33:15 -0700213 }
214}