syn_pub_items/keyword.rs
1/// Define a type that supports parsing and printing a given identifier as if it
2/// were a keyword.
3///
4/// # Usage
5///
6/// As a convention, it is recommended that this macro be invoked within a
7/// module called `kw` or `keyword` and that the resulting parser be invoked
8/// with a `kw::` or `keyword::` prefix.
9///
10/// ```edition2018
11/// mod kw {
12/// syn::custom_keyword!(whatever);
13/// }
14/// ```
15///
16/// The generated syntax tree node supports the following operations just like
17/// any built-in keyword token.
18///
19/// - [Peeking] — `input.peek(kw::whatever)`
20///
21/// - [Parsing] — `input.parse::<kw::whatever>()?`
22///
23/// - [Printing] — `quote!( ... #whatever_token ... )`
24///
25/// - Construction from a [`Span`] — `let whatever_token = kw::whatever(sp)`
26///
27/// - Field access to its span — `let sp = whatever_token.span`
28///
29/// [Peeking]: parse/struct.ParseBuffer.html#method.peek
30/// [Parsing]: parse/struct.ParseBuffer.html#method.parse
31/// [Printing]: https://docs.rs/quote/0.6/quote/trait.ToTokens.html
32/// [`Span`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.Span.html
33///
34/// # Example
35///
36/// This example parses input that looks like `bool = true` or `str = "value"`.
37/// The key must be either the identifier `bool` or the identifier `str`. If
38/// `bool`, the value may be either `true` or `false`. If `str`, the value may
39/// be any string literal.
40///
41/// The symbols `bool` and `str` are not reserved keywords in Rust so these are
42/// not considered keywords in the `syn::token` module. Like any other
43/// identifier that is not a keyword, these can be declared as custom keywords
44/// by crates that need to use them as such.
45///
46/// ```edition2018
47/// use syn::{LitBool, LitStr, Result, Token};
48/// use syn::parse::{Parse, ParseStream};
49///
50/// mod kw {
51/// syn::custom_keyword!(bool);
52/// syn::custom_keyword!(str);
53/// }
54///
55/// enum Argument {
56/// Bool {
57/// bool_token: kw::bool,
58/// eq_token: Token![=],
59/// value: LitBool,
60/// },
61/// Str {
62/// str_token: kw::str,
63/// eq_token: Token![=],
64/// value: LitStr,
65/// },
66/// }
67///
68/// impl Parse for Argument {
69/// fn parse(input: ParseStream) -> Result<Self> {
70/// let lookahead = input.lookahead1();
71/// if lookahead.peek(kw::bool) {
72/// Ok(Argument::Bool {
73/// bool_token: input.parse::<kw::bool>()?,
74/// eq_token: input.parse()?,
75/// value: input.parse()?,
76/// })
77/// } else if lookahead.peek(kw::str) {
78/// Ok(Argument::Str {
79/// str_token: input.parse::<kw::str>()?,
80/// eq_token: input.parse()?,
81/// value: input.parse()?,
82/// })
83/// } else {
84/// Err(lookahead.error())
85/// }
86/// }
87/// }
88/// ```
89#[macro_export(local_inner_macros)]
90macro_rules! custom_keyword {
91 ($ident:ident) => {
92 #[allow(non_camel_case_types)]
93 pub struct $ident {
94 pub span: $crate::export::Span,
95 }
96
97 #[doc(hidden)]
98 #[allow(non_snake_case)]
99 pub fn $ident<__S: $crate::export::IntoSpans<[$crate::export::Span; 1]>>(
100 span: __S,
101 ) -> $ident {
102 $ident {
103 span: $crate::export::IntoSpans::into_spans(span)[0],
104 }
105 }
106
107 impl $crate::export::Default for $ident {
108 fn default() -> Self {
109 $ident {
110 span: $crate::export::Span::call_site(),
111 }
112 }
113 }
114
115 impl_parse_for_custom_keyword!($ident);
116 impl_to_tokens_for_custom_keyword!($ident);
117 impl_clone_for_custom_keyword!($ident);
118 impl_extra_traits_for_custom_keyword!($ident);
119 };
120}
121
122// Not public API.
123#[cfg(feature = "parsing")]
124#[doc(hidden)]
125#[macro_export]
126macro_rules! impl_parse_for_custom_keyword {
127 ($ident:ident) => {
128 // For peek.
129 impl $crate::token::CustomKeyword for $ident {
130 fn ident() -> &'static $crate::export::str {
131 stringify!($ident)
132 }
133
134 fn display() -> &'static $crate::export::str {
135 concat!("`", stringify!($ident), "`")
136 }
137 }
138
139 impl $crate::parse::Parse for $ident {
140 fn parse(input: $crate::parse::ParseStream) -> $crate::parse::Result<$ident> {
141 input.step(|cursor| {
142 if let $crate::export::Some((ident, rest)) = cursor.ident() {
143 if ident == stringify!($ident) {
144 return $crate::export::Ok(($ident { span: ident.span() }, rest));
145 }
146 }
147 $crate::export::Err(cursor.error(concat!(
148 "expected `",
149 stringify!($ident),
150 "`"
151 )))
152 })
153 }
154 }
155 };
156}
157
158// Not public API.
159#[cfg(not(feature = "parsing"))]
160#[doc(hidden)]
161#[macro_export]
162macro_rules! impl_parse_for_custom_keyword {
163 ($ident:ident) => {};
164}
165
166// Not public API.
167#[cfg(feature = "printing")]
168#[doc(hidden)]
169#[macro_export]
170macro_rules! impl_to_tokens_for_custom_keyword {
171 ($ident:ident) => {
172 impl $crate::export::ToTokens for $ident {
173 fn to_tokens(&self, tokens: &mut $crate::export::TokenStream2) {
174 let ident = $crate::Ident::new(stringify!($ident), self.span);
175 $crate::export::TokenStreamExt::append(tokens, ident);
176 }
177 }
178 };
179}
180
181// Not public API.
182#[cfg(not(feature = "printing"))]
183#[doc(hidden)]
184#[macro_export]
185macro_rules! impl_to_tokens_for_custom_keyword {
186 ($ident:ident) => {};
187}
188
189// Not public API.
190#[cfg(feature = "clone-impls")]
191#[doc(hidden)]
192#[macro_export]
193macro_rules! impl_clone_for_custom_keyword {
194 ($ident:ident) => {
195 impl $crate::export::Copy for $ident {}
196
197 impl $crate::export::Clone for $ident {
198 fn clone(&self) -> Self {
199 *self
200 }
201 }
202 };
203}
204
205// Not public API.
206#[cfg(not(feature = "clone-impls"))]
207#[doc(hidden)]
208#[macro_export]
209macro_rules! impl_clone_for_custom_keyword {
210 ($ident:ident) => {};
211}
212
213// Not public API.
214#[cfg(feature = "extra-traits")]
215#[doc(hidden)]
216#[macro_export]
217macro_rules! impl_extra_traits_for_custom_keyword {
218 ($ident:ident) => {
219 impl $crate::export::Debug for $ident {
220 fn fmt(&self, f: &mut $crate::export::Formatter) -> $crate::export::fmt::Result {
221 $crate::export::Formatter::write_str(f, stringify!($ident))
222 }
223 }
224
225 impl $crate::export::Eq for $ident {}
226
227 impl $crate::export::PartialEq for $ident {
228 fn eq(&self, _other: &Self) -> $crate::export::bool {
229 true
230 }
231 }
232
233 impl $crate::export::Hash for $ident {
234 fn hash<__H: $crate::export::Hasher>(&self, _state: &mut __H) {}
235 }
236 };
237}
238
239// Not public API.
240#[cfg(not(feature = "extra-traits"))]
241#[doc(hidden)]
242#[macro_export]
243macro_rules! impl_extra_traits_for_custom_keyword {
244 ($ident:ident) => {};
245}