| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | #[cfg_attr(docsrs, doc(cfg(all(feature = "parsing", feature = "printing"))))]
|
| 81 | #[macro_export]
|
| 82 | macro_rules! parse_quote {
|
| 83 | ($($tt:tt)*) => {
|
| 84 | $crate::__private::parse_quote($crate::__private::quote::quote!($($tt)*))
|
| 85 | };
|
| 86 | }
|
| 87 |
|
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | #[cfg_attr(docsrs, doc(cfg(all(feature = "parsing", feature = "printing"))))]
|
| 113 | #[macro_export]
|
| 114 | macro_rules! parse_quote_spanned {
|
| 115 | ($span:expr=> $($tt:tt)*) => {
|
| 116 | $crate::__private::parse_quote($crate::__private::quote::quote_spanned!($span=> $($tt)*))
|
| 117 | };
|
| 118 | }
|
| 119 |
|
| 120 | |
| 121 | |
| 122 |
|
| 123 | use crate::error::Result;
|
| 124 | use crate::parse::{Parse, ParseStream, Parser};
|
| 125 | use proc_macro2::TokenStream;
|
| 126 |
|
| 127 | |
| 128 | #[doc(hidden)]
|
| 129 | #[track_caller]
|
| 130 | pub fn parse<T: ParseQuote>(token_stream: TokenStream) -> T {
|
| 131 | let parser = T::parse;
|
| 132 | match parser.parse2(token_stream) {
|
| 133 | Ok(t) => t,
|
| 134 | Err(err) => panic!("{}", err),
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | #[doc(hidden)]
|
| 139 | pub trait ParseQuote: Sized {
|
| 140 | fn parse(input: ParseStream) -> Result<Self>;
|
| 141 | }
|
| 142 |
|
| 143 | impl<T: Parse> ParseQuote for T {
|
| 144 | fn parse(input: ParseStream) -> Result<Self> {
|
| 145 | <T as Parse>::parse(input)
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | |
| 150 | |
| 151 |
|
| 152 | use crate::punctuated::Punctuated;
|
| 153 | #[cfg(any(feature = "full", feature = "derive"))]
|
| 154 | use crate::{attr, Attribute, Field, FieldMutability, Ident, Type, Visibility};
|
| 155 | #[cfg(feature = "full")]
|
| 156 | use crate::{Arm, Block, Pat, Stmt};
|
| 157 |
|
| 158 | #[cfg(any(feature = "full", feature = "derive"))]
|
| 159 | impl ParseQuote for Attribute {
|
| 160 | fn parse(input: ParseStream) -> Result<Self> {
|
| 161 | if input.peek(Token![#]) && input.peek2(Token![!]) {
|
| 162 | attr::parsing::single_parse_inner(input)
|
| 163 | } else {
|
| 164 | attr::parsing::single_parse_outer(input)
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | #[cfg(any(feature = "full", feature = "derive"))]
|
| 170 | impl ParseQuote for Vec<Attribute> {
|
| 171 | fn parse(input: ParseStream) -> Result<Self> {
|
| 172 | let mut attrs = Vec::new();
|
| 173 | while !input.is_empty() {
|
| 174 | attrs.push(ParseQuote::parse(input)?);
|
| 175 | }
|
| 176 | Ok(attrs)
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | #[cfg(any(feature = "full", feature = "derive"))]
|
| 181 | impl ParseQuote for Field {
|
| 182 | fn parse(input: ParseStream) -> Result<Self> {
|
| 183 | let attrs = input.call(Attribute::parse_outer)?;
|
| 184 | let vis: Visibility = input.parse()?;
|
| 185 |
|
| 186 | let ident: Option<Ident>;
|
| 187 | let colon_token: Option<Token![:]>;
|
| 188 | let is_named = input.peek(Ident) && input.peek2(Token![:]) && !input.peek2(Token![::]);
|
| 189 | if is_named {
|
| 190 | ident = Some(input.parse()?);
|
| 191 | colon_token = Some(input.parse()?);
|
| 192 | } else {
|
| 193 | ident = None;
|
| 194 | colon_token = None;
|
| 195 | }
|
| 196 |
|
| 197 | let ty: Type = input.parse()?;
|
| 198 |
|
| 199 | Ok(Field {
|
| 200 | attrs,
|
| 201 | vis,
|
| 202 | mutability: FieldMutability::None,
|
| 203 | ident,
|
| 204 | colon_token,
|
| 205 | ty,
|
| 206 | })
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | #[cfg(feature = "full")]
|
| 211 | impl ParseQuote for Pat {
|
| 212 | fn parse(input: ParseStream) -> Result<Self> {
|
| 213 | Pat::parse_multi_with_leading_vert(input)
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | #[cfg(feature = "full")]
|
| 218 | impl ParseQuote for Box<Pat> {
|
| 219 | fn parse(input: ParseStream) -> Result<Self> {
|
| 220 | <Pat as ParseQuote>::parse(input).map(Box::new)
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | impl<T: Parse, P: Parse> ParseQuote for Punctuated<T, P> {
|
| 225 | fn parse(input: ParseStream) -> Result<Self> {
|
| 226 | Self::parse_terminated(input)
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | #[cfg(feature = "full")]
|
| 231 | impl ParseQuote for Vec<Stmt> {
|
| 232 | fn parse(input: ParseStream) -> Result<Self> {
|
| 233 | Block::parse_within(input)
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | #[cfg(feature = "full")]
|
| 238 | impl ParseQuote for Vec<Arm> {
|
| 239 | fn parse(input: ParseStream) -> Result<Self> {
|
| 240 | Arm::parse_multiple(input)
|
| 241 | }
|
| 242 | }
|