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 | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 3 | #![cfg_attr(feature = "cargo-clippy", allow( |
| 4 | const_static_lifetime, |
| 5 | doc_markdown, |
| 6 | large_enum_variant, |
| 7 | redundant_closure, |
| 8 | ))] |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 9 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 10 | extern crate proc_macro; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 11 | extern crate proc_macro2; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 12 | extern crate unicode_xid; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 13 | |
Nika Layzell | 7f5fc68 | 2017-10-25 00:47:10 -0400 | [diff] [blame] | 14 | #[cfg(any(feature = "printing", feature = "parsing"))] |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 15 | extern crate quote; |
| 16 | |
David Tolnay | 1b752fb | 2017-12-26 21:41:39 -0500 | [diff] [blame] | 17 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 18 | #[macro_use] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 19 | #[doc(hidden)] |
| 20 | pub mod parsers; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 21 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 22 | #[macro_use] |
| 23 | mod macros; |
| 24 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 25 | #[cfg(feature = "parsing")] |
| 26 | #[doc(hidden)] |
| 27 | #[macro_use] |
| 28 | pub mod helper; |
| 29 | |
| 30 | #[macro_use] |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame^] | 31 | pub mod token; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 32 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 33 | mod attr; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | pub use attr::{Attribute, AttrStyle, MetaItem, NestedMetaItem, MetaItemList, |
| 35 | MetaNameValue}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 36 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 37 | mod data; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 38 | pub use data::{Field, Variant, VariantData, Visibility, VisRestricted, VisCrate, |
| 39 | VisPublic, VisInherited}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 40 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 41 | mod expr; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 42 | pub use expr::{Expr, ExprKind, ExprBox, ExprInPlace, ExprArray, ExprCall, |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 43 | ExprMethodCall, ExprTuple, ExprBinary, ExprUnary, ExprCast, |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 44 | ExprType, ExprIf, ExprIfLet, ExprWhile, ExprWhileLet, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 45 | ExprForLoop, ExprLoop, ExprMatch, ExprClosure, ExprBlock, |
| 46 | ExprAssign, ExprAssignOp, ExprField, ExprTupField, ExprIndex, |
| 47 | ExprRange, ExprPath, ExprAddrOf, ExprBreak, ExprContinue, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 48 | ExprRet, ExprStruct, ExprRepeat, ExprParen, ExprTry, ExprCatch, |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 49 | ExprGroup, ExprYield, ExprUnsafe}; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 50 | |
| 51 | #[cfg(feature = "full")] |
| 52 | pub use expr::{Arm, BindingMode, Block, CaptureBy, FieldPat, FieldValue, Local, |
| 53 | MacStmtStyle, Pat, RangeLimits, Stmt, PatIdent, PatWild, |
| 54 | PatStruct, PatTuple, PatTupleStruct, PatPath, PatBox, PatRef, |
| 55 | PatLit, PatRange, PatSlice, InPlaceKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 56 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 57 | mod generics; |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 58 | pub use generics::{Generics, GenericParam, LifetimeDef, TraitBoundModifier, TypeParam, TypeParamBound, |
David Tolnay | fa23f57 | 2017-01-23 00:19:11 -0800 | [diff] [blame] | 59 | WhereBoundPredicate, WhereClause, WhereEqPredicate, WherePredicate, |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 60 | WhereRegionPredicate, BoundLifetimes, ConstParam}; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 61 | #[cfg(feature = "printing")] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 62 | pub use generics::{ImplGenerics, Turbofish, TypeGenerics}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 63 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 64 | mod ident; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 65 | pub use ident::Ident; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 66 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 67 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 68 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 69 | #[cfg(feature = "full")] |
David Tolnay | 8894f60 | 2017-11-11 12:11:04 -0800 | [diff] [blame] | 70 | pub use item::{Constness, Defaultness, FnArg, FnDecl, ForeignItem, ItemForeignMod, |
David Tolnay | 5f332a9 | 2017-12-26 00:42:45 -0500 | [diff] [blame] | 71 | ImplItem, ImplPolarity, Item, MethodSig, |
| 72 | TraitItem, ItemExternCrate, ItemUse, |
David Tolnay | 500d832 | 2017-12-18 00:32:51 -0800 | [diff] [blame] | 73 | ItemStatic, ItemConst, ItemFn, ItemMacro, ItemMacro2, ItemMod, ItemType, ItemEnum, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 74 | ItemStruct, ItemUnion, ItemTrait, ItemDefaultImpl, ItemImpl, |
David Tolnay | 5f332a9 | 2017-12-26 00:42:45 -0500 | [diff] [blame] | 75 | UsePath, UseGlob, UseList, ForeignItemFn, ForeignItemStatic, ForeignItemType, |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 76 | TraitItemConst, TraitItemMacro, TraitItemMethod, TraitItemType, |
David Tolnay | 857628c | 2017-11-11 12:25:31 -0800 | [diff] [blame] | 77 | ImplItemConst, ImplItemMacro, ImplItemMethod, ImplItemType, ArgSelfRef, |
David Tolnay | 5f332a9 | 2017-12-26 00:42:45 -0500 | [diff] [blame] | 78 | ArgSelf, ArgCaptured, UseTree}; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 79 | |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 80 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 81 | mod file; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 82 | #[cfg(feature = "full")] |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 83 | pub use file::File; |
David Tolnay | 631cb8c | 2016-11-10 17:16:41 -0800 | [diff] [blame] | 84 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 85 | mod lifetime; |
| 86 | pub use lifetime::Lifetime; |
| 87 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 88 | mod lit; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 89 | pub use lit::{Lit, LitKind}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 90 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 91 | mod mac; |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 92 | pub use mac::{Macro, TokenTree}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 93 | |
David Tolnay | 0e83740 | 2016-12-22 17:25:55 -0500 | [diff] [blame] | 94 | mod derive; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 95 | pub use derive::{Body, DeriveInput, BodyEnum, BodyStruct}; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 96 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 97 | mod op; |
| 98 | pub use op::{BinOp, UnOp}; |
| 99 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 100 | mod ty; |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 101 | pub use ty::{Abi, AbiKind, AngleBracketedGenericArguments, BareFnArg, |
| 102 | BareFnArgName, BareFnType, ReturnType, MutType, Mutability, |
| 103 | ParenthesizedGenericArguments, Path, PathArguments, PathSegment, |
| 104 | PolyTraitRef, QSelf, Type, TypeBinding, Unsafety, TypeSlice, |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 105 | TypeArray, TypePtr, TypeReference, TypeBareFn, TypeNever, TypeTuple, |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 106 | TypePath, TypeTraitObject, TypeImplTrait, TypeParen, TypeInfer, |
| 107 | TypeGroup, GenericArgument}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 108 | #[cfg(feature = "printing")] |
| 109 | pub use ty::PathTokens; |
| 110 | |
David Tolnay | 1b752fb | 2017-12-26 21:41:39 -0500 | [diff] [blame] | 111 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 112 | mod cursor; |
David Tolnay | 1b752fb | 2017-12-26 21:41:39 -0500 | [diff] [blame] | 113 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 114 | pub mod synom; |
| 115 | pub mod delimited; |
| 116 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 117 | mod gen { |
| 118 | #[cfg(feature = "visit")] |
| 119 | pub mod visit; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 120 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 121 | #[cfg(feature = "visit_mut")] |
| 122 | pub mod visit_mut; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 123 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 124 | #[cfg(feature = "fold")] |
| 125 | pub mod fold; |
| 126 | } |
| 127 | pub use gen::*; |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 128 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 129 | //////////////////////////////////////////////////////////////////////////////// |
| 130 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 131 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 132 | use synom::Synom; |
| 133 | #[cfg(feature = "parsing")] |
| 134 | use cursor::SynomBuffer; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 135 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 136 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 137 | mod error; |
| 138 | #[cfg(feature = "parsing")] |
| 139 | pub use error::{PResult, ParseError}; |
| 140 | |
| 141 | // Not public API. |
David Tolnay | 1b752fb | 2017-12-26 21:41:39 -0500 | [diff] [blame] | 142 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 143 | #[doc(hidden)] |
| 144 | pub use error::parse_error; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 145 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 146 | /// Parse tokens of source code into the chosen syn data type. |
| 147 | /// |
| 148 | /// This is preferred over parsing a string because tokens are able to preserve |
| 149 | /// information about where in the user's code they were originally written (the |
| 150 | /// "span" of the token), possibly allowing the compiler to produce better error |
| 151 | /// messages. |
| 152 | /// |
| 153 | /// # Examples |
| 154 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 155 | /// ```rust |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 156 | /// extern crate proc_macro; |
| 157 | /// use proc_macro::TokenStream; |
| 158 | /// |
| 159 | /// extern crate syn; |
| 160 | /// |
| 161 | /// #[macro_use] |
| 162 | /// extern crate quote; |
| 163 | /// |
| 164 | /// use syn::DeriveInput; |
| 165 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 166 | /// # const IGNORE_TOKENS: &str = stringify! { |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 167 | /// #[proc_macro_derive(MyMacro)] |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 168 | /// # }; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 169 | /// pub fn my_macro(input: TokenStream) -> TokenStream { |
| 170 | /// // Parse the tokens into a syntax tree |
| 171 | /// let ast: DeriveInput = syn::parse(input).unwrap(); |
| 172 | /// |
| 173 | /// // Build the output, possibly using quasi-quotation |
| 174 | /// let expanded = quote! { |
| 175 | /// /* ... */ |
| 176 | /// }; |
| 177 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 178 | /// // Convert into a token stream and return it |
| 179 | /// expanded.into() |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 180 | /// } |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 181 | /// # |
| 182 | /// # fn main() {} |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 183 | /// ``` |
| 184 | #[cfg(feature = "parsing")] |
| 185 | pub fn parse<T>(tokens: proc_macro::TokenStream) -> Result<T, ParseError> |
| 186 | where T: Synom, |
| 187 | { |
| 188 | _parse(tokens.into()) |
| 189 | } |
| 190 | |
| 191 | #[cfg(feature = "parsing")] |
| 192 | fn _parse<T>(tokens: proc_macro2::TokenStream) -> Result<T, ParseError> |
| 193 | where T: Synom, |
| 194 | { |
| 195 | let buf = SynomBuffer::new(tokens); |
| 196 | let result = T::parse(buf.begin()); |
| 197 | let err = match result { |
| 198 | Ok((rest, t)) => { |
| 199 | if rest.eof() { |
| 200 | return Ok(t); |
| 201 | } else if rest == buf.begin() { |
| 202 | // parsed nothing |
| 203 | ParseError::new("failed to parse anything") |
| 204 | } else { |
| 205 | ParseError::new("failed to parse all tokens") |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 206 | } |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 207 | } |
| 208 | Err(err) => err, |
| 209 | }; |
| 210 | match T::description() { |
Alex Crichton | c1b76f5 | 2017-07-06 15:04:24 -0700 | [diff] [blame] | 211 | Some(s) => Err(ParseError::new(format!("failed to parse {}: {}", s, err))), |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 212 | None => Err(err), |
| 213 | } |
| 214 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 215 | |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 216 | /// Parse a `quote::Tokens` of Rust code into the chosen syn data type. |
| 217 | /// |
| 218 | /// # Examples |
| 219 | /// |
| 220 | /// ```rust |
| 221 | /// extern crate syn; |
| 222 | /// # |
| 223 | /// # #[macro_use] |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 224 | /// # extern crate quote; |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 225 | /// # |
| 226 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 227 | /// |
| 228 | /// use syn::Expr; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 229 | /// |
| 230 | /// fn run() -> Result<()> { |
| 231 | /// let code = quote!(assert_eq!(u8::max_value(), 255)); |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame^] | 232 | /// let expr = syn::parse_token::<Expr>(code)?; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 233 | /// println!("{:#?}", expr); |
| 234 | /// Ok(()) |
| 235 | /// } |
| 236 | /// # |
| 237 | /// # fn main() { run().unwrap() } |
| 238 | /// ``` |
| 239 | #[cfg(feature = "parsing")] |
| 240 | pub fn parse_tokens<T: Synom>(tokens: quote::Tokens) -> Result<T, ParseError> { |
| 241 | _parse(tokens.into()) |
| 242 | } |
| 243 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 244 | /// Parse a string of Rust code into the chosen syn data type. |
| 245 | /// |
| 246 | /// # Examples |
| 247 | /// |
| 248 | /// ```rust |
| 249 | /// extern crate syn; |
| 250 | /// # |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 251 | /// # |
| 252 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 253 | /// |
| 254 | /// use syn::Expr; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 255 | /// |
| 256 | /// fn run() -> Result<()> { |
| 257 | /// let code = "assert_eq!(u8::max_value(), 255)"; |
| 258 | /// let expr = syn::parse_str::<Expr>(code)?; |
| 259 | /// println!("{:#?}", expr); |
| 260 | /// Ok(()) |
| 261 | /// } |
| 262 | /// # |
| 263 | /// # fn main() { run().unwrap() } |
| 264 | /// ``` |
| 265 | #[cfg(feature = "parsing")] |
| 266 | pub fn parse_str<T: Synom>(s: &str) -> Result<T, ParseError> { |
| 267 | _parse(s.parse()?) |
| 268 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 269 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 270 | // FIXME the name parse_file makes it sound like you might pass in a path to a |
| 271 | // file, rather than the content. |
| 272 | /// Parse the content of a file of Rust code. |
| 273 | /// |
| 274 | /// This is different from `syn::parse_str::<File>(content)` in two ways: |
| 275 | /// |
| 276 | /// - It discards a leading byte order mark `\u{FEFF}` if the file has one. |
| 277 | /// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`. |
| 278 | /// |
| 279 | /// If present, either of these would be an error using `from_str`. |
| 280 | /// |
| 281 | /// # Examples |
| 282 | /// |
| 283 | /// ```rust,no_run |
| 284 | /// extern crate syn; |
| 285 | /// # |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 286 | /// # |
| 287 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 288 | /// |
| 289 | /// use std::fs::File; |
| 290 | /// use std::io::Read; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 291 | /// |
| 292 | /// fn run() -> Result<()> { |
| 293 | /// let mut file = File::open("path/to/code.rs")?; |
| 294 | /// let mut content = String::new(); |
| 295 | /// file.read_to_string(&mut content)?; |
| 296 | /// |
| 297 | /// let ast = syn::parse_file(&content)?; |
| 298 | /// if let Some(shebang) = ast.shebang { |
| 299 | /// println!("{}", shebang); |
| 300 | /// } |
| 301 | /// println!("{} items", ast.items.len()); |
| 302 | /// |
| 303 | /// Ok(()) |
| 304 | /// } |
| 305 | /// # |
| 306 | /// # fn main() { run().unwrap() } |
| 307 | /// ``` |
| 308 | #[cfg(all(feature = "parsing", feature = "full"))] |
| 309 | pub fn parse_file(mut content: &str) -> Result<File, ParseError> { |
| 310 | // Strip the BOM if it is present |
| 311 | const BOM: &'static str = "\u{feff}"; |
| 312 | if content.starts_with(BOM) { |
| 313 | content = &content[BOM.len()..]; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 314 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 315 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 316 | let mut shebang = None; |
| 317 | if content.starts_with("#!") && !content.starts_with("#![") { |
| 318 | if let Some(idx) = content.find('\n') { |
| 319 | shebang = Some(content[..idx].to_string()); |
| 320 | content = &content[idx..]; |
| 321 | } else { |
| 322 | shebang = Some(content.to_string()); |
| 323 | content = ""; |
| 324 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 325 | } |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 326 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 327 | let mut file: File = parse_str(content)?; |
| 328 | file.shebang = shebang; |
| 329 | Ok(file) |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 330 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 331 | |
| 332 | #[cfg(feature = "printing")] |
| 333 | struct TokensOrDefault<'a, T: 'a>(&'a Option<T>); |
| 334 | |
| 335 | #[cfg(feature = "printing")] |
| 336 | impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T> |
| 337 | where T: quote::ToTokens + Default, |
| 338 | { |
| 339 | fn to_tokens(&self, tokens: &mut quote::Tokens) { |
| 340 | match *self.0 { |
| 341 | Some(ref t) => t.to_tokens(tokens), |
| 342 | None => T::default().to_tokens(tokens), |
| 343 | } |
| 344 | } |
| 345 | } |