blob: 85c3593529537d9e5fb928662ceeef02deed0eb2 [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,
122 SequenceRepetition,
123 Token,
124 TokenTree,
125};
126
David Tolnayf38cdf62016-09-23 19:07:09 -0700127mod macro_input;
128pub use macro_input::{
129 Body,
130 MacroInput,
131};
132
David Tolnaye0aa55a2016-10-02 14:55:23 -0700133#[cfg(feature = "parsing")]
David Tolnay14cbdeb2016-10-01 12:13:59 -0700134mod space;
135
David Tolnayb79ee962016-09-04 09:39:20 -0700136mod ty;
137pub use ty::{
138 AngleBracketedParameterData,
David Tolnay62f374c2016-10-02 13:37:00 -0700139 BareFnArg,
David Tolnayb79ee962016-09-04 09:39:20 -0700140 BareFnTy,
David Tolnayb79ee962016-09-04 09:39:20 -0700141 FunctionRetTy,
142 MutTy,
143 Mutability,
144 ParenthesizedParameterData,
145 Path,
146 PathParameters,
147 PathSegment,
148 PolyTraitRef,
149 QSelf,
150 Ty,
151 TypeBinding,
152};
David Tolnay35161ff2016-09-03 11:33:15 -0700153
David Tolnay55337722016-09-11 12:58:56 -0700154#[cfg(feature = "aster")]
155pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -0700156
David Tolnay55337722016-09-11 12:58:56 -0700157#[cfg(feature = "visit")]
158pub mod visit;
159
160#[cfg(feature = "parsing")]
161pub use parsing::*;
162
163#[cfg(feature = "parsing")]
164mod parsing {
165 use super::*;
David Tolnayf38cdf62016-09-23 19:07:09 -0700166 use {generics, macro_input, ty};
David Tolnay14cbdeb2016-10-01 12:13:59 -0700167 use nom::IResult;
David Tolnay55337722016-09-11 12:58:56 -0700168
David Tolnayedf2b992016-09-23 20:43:45 -0700169 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700170 use {expr, item, krate};
David Tolnayedf2b992016-09-23 20:43:45 -0700171
David Tolnayf38cdf62016-09-23 19:07:09 -0700172 pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> {
173 unwrap("macro input", macro_input::parsing::macro_input, input)
David Tolnay55337722016-09-11 12:58:56 -0700174 }
175
David Tolnayedf2b992016-09-23 20:43:45 -0700176 #[cfg(feature = "full")]
David Tolnay4a51dc72016-10-01 00:40:31 -0700177 pub fn parse_crate(input: &str) -> Result<Crate, String> {
178 unwrap("crate", krate::parsing::krate, input)
179 }
180
181 #[cfg(feature = "full")]
David Tolnayedf2b992016-09-23 20:43:45 -0700182 pub fn parse_item(input: &str) -> Result<Item, String> {
183 unwrap("item", item::parsing::item, input)
184 }
185
David Tolnayb9c8e322016-09-23 20:48:37 -0700186 #[cfg(feature = "full")]
187 pub fn parse_expr(input: &str) -> Result<Expr, String> {
188 unwrap("expression", expr::parsing::expr, input)
189 }
190
David Tolnay32a112e2016-09-11 17:46:15 -0700191 pub fn parse_type(input: &str) -> Result<Ty, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700192 unwrap("type", ty::parsing::ty, input)
David Tolnay32a112e2016-09-11 17:46:15 -0700193 }
194
David Tolnay55337722016-09-11 12:58:56 -0700195 pub fn parse_path(input: &str) -> Result<Path, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700196 unwrap("path", ty::parsing::path, input)
David Tolnay55337722016-09-11 12:58:56 -0700197 }
198
199 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700200 unwrap("where clause", generics::parsing::where_clause, input)
David Tolnay55337722016-09-11 12:58:56 -0700201 }
202
David Tolnay14cbdeb2016-10-01 12:13:59 -0700203 fn unwrap<T>(name: &'static str, f: fn(&str) -> IResult<&str, T>, input: &str) -> Result<T, String> {
David Tolnayb5a7b142016-09-13 22:46:39 -0700204 match f(input) {
David Tolnay14cbdeb2016-10-01 12:13:59 -0700205 IResult::Done(rest, t) => {
David Tolnay55337722016-09-11 12:58:56 -0700206 if rest.is_empty() {
207 Ok(t)
208 } else {
209 Err(format!("remaining tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700210 }
David Tolnay55337722016-09-11 12:58:56 -0700211 }
David Tolnay14cbdeb2016-10-01 12:13:59 -0700212 IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
David Tolnay35161ff2016-09-03 11:33:15 -0700213 }
David Tolnay35161ff2016-09-03 11:33:15 -0700214 }
215}