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