David Tolnay | ad2836d | 2017-04-20 10:11:43 -0700 | [diff] [blame] | 1 | #![doc(html_root_url = "https://dtolnay.github.io/syn")] |
| 2 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 3 | #![cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))] |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 4 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 5 | extern crate proc_macro; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 6 | extern crate proc_macro2; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 7 | extern crate unicode_xid; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 8 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 9 | #[cfg(feature = "printing")] |
| 10 | extern crate quote; |
| 11 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 12 | #[cfg_attr(feature = "parsing", macro_use)] |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 13 | extern crate synom; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 14 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 15 | #[macro_use] |
| 16 | mod macros; |
| 17 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 18 | mod attr; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 19 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem, MetaItemList, |
| 20 | MetaNameValue}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 21 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 22 | mod data; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 23 | pub use data::{Field, Variant, VariantData, Visibility, VisRestricted, VisCrate, |
| 24 | VisPublic, VisInherited}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 25 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 26 | mod expr; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 27 | pub use expr::{Expr, ExprKind, ExprBox, ExprInPlace, ExprArray, ExprCall, |
| 28 | ExprMethodCall, ExprTup, ExprBinary, ExprUnary, ExprCast, |
| 29 | ExprType, ExprIf, ExprIfLet, ExprWhile, ExprWhileLet, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | ExprForLoop, ExprLoop, ExprMatch, ExprClosure, ExprBlock, |
| 31 | ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex, |
| 32 | ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 33 | ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch, |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 34 | ExprGroup}; |
| 35 | |
| 36 | #[cfg(feature = "full")] |
| 37 | pub use expr::{Arm, BindingMode, Block, CaptureBy, FieldPat, FieldValue, Local, |
| 38 | MacStmtStyle, Pat, RangeLimits, Stmt, PatIdent, PatWild, |
| 39 | PatStruct, PatTuple, PatTupleStruct, PatPath, PatBox, PatRef, |
| 40 | PatLit, PatRange, PatSlice, InPlaceKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 41 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 42 | mod generics; |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 43 | pub use generics::{Generics, LifetimeDef, TraitBoundModifier, TyParam, TyParamBound, |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 44 | WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 45 | WhereRegionPredicate, BoundLifetimes}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 46 | #[cfg(feature = "printing")] |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 47 | pub use generics::{ImplGenerics, Turbofish, TyGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 48 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 49 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 50 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 51 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 52 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 53 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 54 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 55 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItemKind, ForeignItem, ItemForeignMod, |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 56 | ImplItem, ImplItemKind, ImplPolarity, Item, ItemKind, MethodSig, PathListItem, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 57 | TraitItem, TraitItemKind, ViewPath, ItemExternCrate, ItemUse, |
| 58 | ItemStatic, ItemConst, ItemFn, ItemMod, ItemTy, ItemEnum, |
| 59 | ItemStruct, ItemUnion, ItemTrait, ItemDefaultImpl, ItemImpl, |
| 60 | PathSimple, PathGlob, PathList, ForeignItemFn, ForeignItemStatic, |
| 61 | TraitItemConst, TraitItemMethod, TraitItemType, |
| 62 | ImplItemConst, ImplItemMethod, ImplItemType, ArgSelfRef, |
| 63 | ArgSelf, ArgCaptured}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 64 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 65 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 66 | mod file; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 67 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 68 | pub use file::File; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 69 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 70 | mod lifetime; |
| 71 | pub use lifetime::Lifetime; |
| 72 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 73 | mod lit; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 74 | pub use lit::{Lit, LitKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 75 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 76 | mod mac; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 77 | pub use mac::{Mac, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 78 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 79 | mod derive; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 80 | pub use derive::{Body, DeriveInput, BodyEnum, BodyStruct}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 81 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 82 | mod op; |
| 83 | pub use op::{BinOp, UnOp}; |
| 84 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 85 | mod ty; |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 86 | pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetTy, MutTy, |
| 87 | Mutability, ParenthesizedParameterData, Path, PathParameters, PathSegment, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 88 | PolyTraitRef, QSelf, Ty, TypeBinding, Unsafety, TySlice, TyArray, |
| 89 | TyPtr, TyRptr, TyBareFn, TyNever, TyTup, TyPath, TyTraitObject, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 90 | TyImplTrait, TyParen, TyInfer, TyGroup}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 91 | #[cfg(feature = "printing")] |
| 92 | pub use ty::PathTokens; |
| 93 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 94 | pub use synom::span::Span; |
| 95 | pub use synom::tokens; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 96 | pub use synom::delimited; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 97 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 98 | #[cfg(feature = "visit")] |
| 99 | pub mod visit; |
| 100 | |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 101 | #[cfg(feature = "fold")] |
| 102 | pub mod fold; |
| 103 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 104 | //////////////////////////////////////////////////////////////////////////////// |
| 105 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 106 | #[cfg(feature = "parsing")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 107 | pub use synom::ParseError; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 108 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 109 | #[cfg(feature = "parsing")] |
| 110 | use synom::{Synom, SynomBuffer}; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 111 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 112 | /// Parse tokens of source code into the chosen syn data type. |
| 113 | /// |
| 114 | /// This is preferred over parsing a string because tokens are able to preserve |
| 115 | /// information about where in the user's code they were originally written (the |
| 116 | /// "span" of the token), possibly allowing the compiler to produce better error |
| 117 | /// messages. |
| 118 | /// |
| 119 | /// # Examples |
| 120 | /// |
| 121 | /// ```rust,ignore |
| 122 | /// extern crate proc_macro; |
| 123 | /// use proc_macro::TokenStream; |
| 124 | /// |
| 125 | /// extern crate syn; |
| 126 | /// |
| 127 | /// #[macro_use] |
| 128 | /// extern crate quote; |
| 129 | /// |
| 130 | /// use syn::DeriveInput; |
| 131 | /// |
| 132 | /// #[proc_macro_derive(MyMacro)] |
| 133 | /// pub fn my_macro(input: TokenStream) -> TokenStream { |
| 134 | /// // Parse the tokens into a syntax tree |
| 135 | /// let ast: DeriveInput = syn::parse(input).unwrap(); |
| 136 | /// |
| 137 | /// // Build the output, possibly using quasi-quotation |
| 138 | /// let expanded = quote! { |
| 139 | /// /* ... */ |
| 140 | /// }; |
| 141 | /// |
| 142 | /// // Parse back to a token stream and return it |
| 143 | /// expanded.parse().unwrap() |
| 144 | /// } |
| 145 | /// ``` |
| 146 | #[cfg(feature = "parsing")] |
| 147 | pub fn parse<T>(tokens: proc_macro::TokenStream) -> Result<T, ParseError> |
| 148 | where T: Synom, |
| 149 | { |
| 150 | _parse(tokens.into()) |
| 151 | } |
| 152 | |
| 153 | #[cfg(feature = "parsing")] |
| 154 | fn _parse<T>(tokens: proc_macro2::TokenStream) -> Result<T, ParseError> |
| 155 | where T: Synom, |
| 156 | { |
| 157 | let buf = SynomBuffer::new(tokens); |
| 158 | let result = T::parse(buf.begin()); |
| 159 | let err = match result { |
| 160 | Ok((rest, t)) => { |
| 161 | if rest.eof() { |
| 162 | return Ok(t); |
| 163 | } else if rest == buf.begin() { |
| 164 | // parsed nothing |
| 165 | ParseError::new("failed to parse anything") |
| 166 | } else { |
| 167 | ParseError::new("failed to parse all tokens") |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 168 | } |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 169 | } |
| 170 | Err(err) => err, |
| 171 | }; |
| 172 | match T::description() { |
| 173 | Some(s) => Err(ParseError::new(format!("parsing {}: {}", s, err))), |
| 174 | None => Err(err), |
| 175 | } |
| 176 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 177 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 178 | /// Parse a string of Rust code into the chosen syn data type. |
| 179 | /// |
| 180 | /// # Examples |
| 181 | /// |
| 182 | /// ```rust |
| 183 | /// extern crate syn; |
| 184 | /// # |
| 185 | /// # #[macro_use] |
| 186 | /// # extern crate error_chain; |
| 187 | /// |
| 188 | /// use syn::Expr; |
| 189 | /// # |
| 190 | /// # error_chain! { |
| 191 | /// # foreign_links { |
| 192 | /// # Syn(syn::ParseError); |
| 193 | /// # } |
| 194 | /// # } |
| 195 | /// |
| 196 | /// fn run() -> Result<()> { |
| 197 | /// let code = "assert_eq!(u8::max_value(), 255)"; |
| 198 | /// let expr = syn::parse_str::<Expr>(code)?; |
| 199 | /// println!("{:#?}", expr); |
| 200 | /// Ok(()) |
| 201 | /// } |
| 202 | /// # |
| 203 | /// # fn main() { run().unwrap() } |
| 204 | /// ``` |
| 205 | #[cfg(feature = "parsing")] |
| 206 | pub fn parse_str<T: Synom>(s: &str) -> Result<T, ParseError> { |
| 207 | _parse(s.parse()?) |
| 208 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 209 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 210 | // FIXME the name parse_file makes it sound like you might pass in a path to a |
| 211 | // file, rather than the content. |
| 212 | /// Parse the content of a file of Rust code. |
| 213 | /// |
| 214 | /// This is different from `syn::parse_str::<File>(content)` in two ways: |
| 215 | /// |
| 216 | /// - It discards a leading byte order mark `\u{FEFF}` if the file has one. |
| 217 | /// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`. |
| 218 | /// |
| 219 | /// If present, either of these would be an error using `from_str`. |
| 220 | /// |
| 221 | /// # Examples |
| 222 | /// |
| 223 | /// ```rust,no_run |
| 224 | /// extern crate syn; |
| 225 | /// # |
| 226 | /// # #[macro_use] |
| 227 | /// # extern crate error_chain; |
| 228 | /// |
| 229 | /// use std::fs::File; |
| 230 | /// use std::io::Read; |
| 231 | /// # |
| 232 | /// # error_chain! { |
| 233 | /// # foreign_links { |
| 234 | /// # Io(std::io::Error); |
| 235 | /// # Syn(syn::ParseError); |
| 236 | /// # } |
| 237 | /// # } |
| 238 | /// |
| 239 | /// fn run() -> Result<()> { |
| 240 | /// let mut file = File::open("path/to/code.rs")?; |
| 241 | /// let mut content = String::new(); |
| 242 | /// file.read_to_string(&mut content)?; |
| 243 | /// |
| 244 | /// let ast = syn::parse_file(&content)?; |
| 245 | /// if let Some(shebang) = ast.shebang { |
| 246 | /// println!("{}", shebang); |
| 247 | /// } |
| 248 | /// println!("{} items", ast.items.len()); |
| 249 | /// |
| 250 | /// Ok(()) |
| 251 | /// } |
| 252 | /// # |
| 253 | /// # fn main() { run().unwrap() } |
| 254 | /// ``` |
| 255 | #[cfg(all(feature = "parsing", feature = "full"))] |
| 256 | pub fn parse_file(mut content: &str) -> Result<File, ParseError> { |
| 257 | // Strip the BOM if it is present |
| 258 | const BOM: &'static str = "\u{feff}"; |
| 259 | if content.starts_with(BOM) { |
| 260 | content = &content[BOM.len()..]; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 261 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 262 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 263 | let mut shebang = None; |
| 264 | if content.starts_with("#!") && !content.starts_with("#![") { |
| 265 | if let Some(idx) = content.find('\n') { |
| 266 | shebang = Some(content[..idx].to_string()); |
| 267 | content = &content[idx..]; |
| 268 | } else { |
| 269 | shebang = Some(content.to_string()); |
| 270 | content = ""; |
| 271 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 272 | } |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 273 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame^] | 274 | let mut file: File = parse_str(content)?; |
| 275 | file.shebang = shebang; |
| 276 | Ok(file) |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 277 | } |