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 | #[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, |
| 45 | TraitBoundModifier, TypeParam, TypeParamBound, WhereBoundPredicate, |
| 46 | WhereClause, WhereEqPredicate, WherePredicate, WhereRegionPredicate}; |
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 | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 87 | PathArguments, PathSegment, PolyTraitRef, 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 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 101 | mod gen { |
| 102 | #[cfg(feature = "visit")] |
| 103 | pub mod visit; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 104 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 105 | #[cfg(feature = "visit_mut")] |
| 106 | pub mod visit_mut; |
Nika Layzell | 2772666 | 2017-10-24 23:16:35 -0400 | [diff] [blame] | 107 | |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 108 | #[cfg(feature = "fold")] |
| 109 | pub mod fold; |
David Tolnay | f60f426 | 2017-12-28 19:17:58 -0500 | [diff] [blame] | 110 | |
| 111 | #[path = "../gen_helper.rs"] |
| 112 | mod helper; |
Nika Layzell | a6f46c4 | 2017-10-26 15:26:16 -0400 | [diff] [blame] | 113 | } |
| 114 | pub use gen::*; |
gnzlbg | 9ae88d8 | 2017-01-26 20:45:17 +0100 | [diff] [blame] | 115 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 116 | //////////////////////////////////////////////////////////////////////////////// |
| 117 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 118 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 119 | use synom::Synom; |
| 120 | #[cfg(feature = "parsing")] |
| 121 | use cursor::SynomBuffer; |
Ted Driggs | 054abbb | 2017-05-01 12:20:52 -0700 | [diff] [blame] | 122 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 123 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 124 | mod error; |
| 125 | #[cfg(feature = "parsing")] |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 126 | use error::ParseError; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 127 | |
| 128 | // Not public API. |
David Tolnay | 1b752fb | 2017-12-26 21:41:39 -0500 | [diff] [blame] | 129 | #[cfg(feature = "parsing")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 130 | #[doc(hidden)] |
| 131 | pub use error::parse_error; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 132 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 133 | /// Parse tokens of source code into the chosen syn data type. |
| 134 | /// |
| 135 | /// This is preferred over parsing a string because tokens are able to preserve |
| 136 | /// information about where in the user's code they were originally written (the |
| 137 | /// "span" of the token), possibly allowing the compiler to produce better error |
| 138 | /// messages. |
| 139 | /// |
| 140 | /// # Examples |
| 141 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 142 | /// ```rust |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 143 | /// extern crate proc_macro; |
| 144 | /// use proc_macro::TokenStream; |
| 145 | /// |
| 146 | /// extern crate syn; |
| 147 | /// |
| 148 | /// #[macro_use] |
| 149 | /// extern crate quote; |
| 150 | /// |
| 151 | /// use syn::DeriveInput; |
| 152 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 153 | /// # const IGNORE_TOKENS: &str = stringify! { |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 154 | /// #[proc_macro_derive(MyMacro)] |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 155 | /// # }; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 156 | /// pub fn my_macro(input: TokenStream) -> TokenStream { |
| 157 | /// // Parse the tokens into a syntax tree |
| 158 | /// let ast: DeriveInput = syn::parse(input).unwrap(); |
| 159 | /// |
| 160 | /// // Build the output, possibly using quasi-quotation |
| 161 | /// let expanded = quote! { |
| 162 | /// /* ... */ |
| 163 | /// }; |
| 164 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 165 | /// // Convert into a token stream and return it |
| 166 | /// expanded.into() |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 167 | /// } |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 168 | /// # |
| 169 | /// # fn main() {} |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 170 | /// ``` |
| 171 | #[cfg(feature = "parsing")] |
| 172 | pub fn parse<T>(tokens: proc_macro::TokenStream) -> Result<T, ParseError> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 173 | where |
| 174 | T: Synom, |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 175 | { |
| 176 | _parse(tokens.into()) |
| 177 | } |
| 178 | |
| 179 | #[cfg(feature = "parsing")] |
| 180 | fn _parse<T>(tokens: proc_macro2::TokenStream) -> Result<T, ParseError> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 181 | where |
| 182 | T: Synom, |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 183 | { |
| 184 | let buf = SynomBuffer::new(tokens); |
| 185 | let result = T::parse(buf.begin()); |
| 186 | let err = match result { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 187 | Ok((t, rest)) => { |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 188 | if rest.eof() { |
| 189 | return Ok(t); |
| 190 | } else if rest == buf.begin() { |
| 191 | // parsed nothing |
| 192 | ParseError::new("failed to parse anything") |
| 193 | } else { |
| 194 | ParseError::new("failed to parse all tokens") |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 195 | } |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 196 | } |
| 197 | Err(err) => err, |
| 198 | }; |
| 199 | match T::description() { |
Alex Crichton | c1b76f5 | 2017-07-06 15:04:24 -0700 | [diff] [blame] | 200 | Some(s) => Err(ParseError::new(format!("failed to parse {}: {}", s, err))), |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 201 | None => Err(err), |
| 202 | } |
| 203 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 204 | |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 205 | /// Parse a `quote::Tokens` of Rust code into the chosen syn data type. |
| 206 | /// |
| 207 | /// # Examples |
| 208 | /// |
| 209 | /// ```rust |
| 210 | /// extern crate syn; |
| 211 | /// # |
| 212 | /// # #[macro_use] |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 213 | /// # extern crate quote; |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 214 | /// # |
| 215 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 216 | /// |
| 217 | /// use syn::Expr; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 218 | /// |
| 219 | /// fn run() -> Result<()> { |
| 220 | /// let code = quote!(assert_eq!(u8::max_value(), 255)); |
David Tolnay | 3e88513 | 2017-12-26 23:15:51 -0500 | [diff] [blame] | 221 | /// let expr = syn::parse_tokens::<Expr>(code)?; |
Nika Layzell | cda7ebd | 2017-10-24 23:10:44 -0400 | [diff] [blame] | 222 | /// println!("{:#?}", expr); |
| 223 | /// Ok(()) |
| 224 | /// } |
| 225 | /// # |
| 226 | /// # fn main() { run().unwrap() } |
| 227 | /// ``` |
| 228 | #[cfg(feature = "parsing")] |
| 229 | pub fn parse_tokens<T: Synom>(tokens: quote::Tokens) -> Result<T, ParseError> { |
| 230 | _parse(tokens.into()) |
| 231 | } |
| 232 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 233 | /// Parse a string of Rust code into the chosen syn data type. |
| 234 | /// |
| 235 | /// # Examples |
| 236 | /// |
| 237 | /// ```rust |
| 238 | /// extern crate syn; |
| 239 | /// # |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 240 | /// # |
| 241 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 242 | /// |
| 243 | /// use syn::Expr; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 244 | /// |
| 245 | /// fn run() -> Result<()> { |
| 246 | /// let code = "assert_eq!(u8::max_value(), 255)"; |
| 247 | /// let expr = syn::parse_str::<Expr>(code)?; |
| 248 | /// println!("{:#?}", expr); |
| 249 | /// Ok(()) |
| 250 | /// } |
| 251 | /// # |
| 252 | /// # fn main() { run().unwrap() } |
| 253 | /// ``` |
| 254 | #[cfg(feature = "parsing")] |
| 255 | pub fn parse_str<T: Synom>(s: &str) -> Result<T, ParseError> { |
David Tolnay | ffb1f4d | 2017-12-28 00:07:59 -0500 | [diff] [blame] | 256 | match s.parse() { |
| 257 | Ok(tts) => _parse(tts), |
| 258 | Err(_) => Err(ParseError::new("error while lexing input string")), |
| 259 | } |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 260 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 261 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 262 | // FIXME the name parse_file makes it sound like you might pass in a path to a |
| 263 | // file, rather than the content. |
| 264 | /// Parse the content of a file of Rust code. |
| 265 | /// |
| 266 | /// This is different from `syn::parse_str::<File>(content)` in two ways: |
| 267 | /// |
| 268 | /// - It discards a leading byte order mark `\u{FEFF}` if the file has one. |
| 269 | /// - It preserves the shebang line of the file, such as `#!/usr/bin/env rustx`. |
| 270 | /// |
| 271 | /// If present, either of these would be an error using `from_str`. |
| 272 | /// |
| 273 | /// # Examples |
| 274 | /// |
| 275 | /// ```rust,no_run |
| 276 | /// extern crate syn; |
| 277 | /// # |
David Tolnay | 9174b97 | 2017-11-09 22:27:50 -0800 | [diff] [blame] | 278 | /// # |
| 279 | /// # type Result<T> = std::result::Result<T, Box<std::error::Error>>; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 280 | /// |
| 281 | /// use std::fs::File; |
| 282 | /// use std::io::Read; |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 283 | /// |
| 284 | /// fn run() -> Result<()> { |
| 285 | /// let mut file = File::open("path/to/code.rs")?; |
| 286 | /// let mut content = String::new(); |
| 287 | /// file.read_to_string(&mut content)?; |
| 288 | /// |
| 289 | /// let ast = syn::parse_file(&content)?; |
| 290 | /// if let Some(shebang) = ast.shebang { |
| 291 | /// println!("{}", shebang); |
| 292 | /// } |
| 293 | /// println!("{} items", ast.items.len()); |
| 294 | /// |
| 295 | /// Ok(()) |
| 296 | /// } |
| 297 | /// # |
| 298 | /// # fn main() { run().unwrap() } |
| 299 | /// ``` |
| 300 | #[cfg(all(feature = "parsing", feature = "full"))] |
| 301 | pub fn parse_file(mut content: &str) -> Result<File, ParseError> { |
| 302 | // Strip the BOM if it is present |
| 303 | const BOM: &'static str = "\u{feff}"; |
| 304 | if content.starts_with(BOM) { |
| 305 | content = &content[BOM.len()..]; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 306 | } |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 307 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 308 | let mut shebang = None; |
| 309 | if content.starts_with("#!") && !content.starts_with("#![") { |
| 310 | if let Some(idx) = content.find('\n') { |
| 311 | shebang = Some(content[..idx].to_string()); |
| 312 | content = &content[idx..]; |
| 313 | } else { |
| 314 | shebang = Some(content.to_string()); |
| 315 | content = ""; |
| 316 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 317 | } |
David Tolnay | 0a8972b | 2017-02-27 02:10:01 -0800 | [diff] [blame] | 318 | |
David Tolnay | c7a5d3d | 2017-06-04 12:11:05 -0700 | [diff] [blame] | 319 | let mut file: File = parse_str(content)?; |
| 320 | file.shebang = shebang; |
| 321 | Ok(file) |
Michael Layzell | 5e107ff | 2017-01-24 19:58:39 -0500 | [diff] [blame] | 322 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 323 | |
| 324 | #[cfg(feature = "printing")] |
| 325 | struct TokensOrDefault<'a, T: 'a>(&'a Option<T>); |
| 326 | |
| 327 | #[cfg(feature = "printing")] |
| 328 | impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 329 | where |
| 330 | T: quote::ToTokens + Default, |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 331 | { |
| 332 | fn to_tokens(&self, tokens: &mut quote::Tokens) { |
| 333 | match *self.0 { |
| 334 | Some(ref t) => t.to_tokens(tokens), |
| 335 | None => T::default().to_tokens(tokens), |
| 336 | } |
| 337 | } |
| 338 | } |