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