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