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