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