blob: d47bfa6c67ffaa16060a9ea813448d775aced001 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnaye79ae182018-01-06 19:23:37 -08009//! Tokens representing Rust punctuation, keywords, and delimiters.
Alex Crichton954046c2017-05-30 21:49:42 -070010//!
David Tolnaye79ae182018-01-06 19:23:37 -080011//! The type names in this module can be difficult to keep straight, so we
12//! prefer to use the [`Token!`] macro instead. This is a type-macro that
13//! expands to the token type of the given token.
14//!
15//! [`Token!`]: ../macro.Token.html
16//!
17//! # Example
18//!
19//! The [`ItemStatic`] syntax tree node is defined like this.
20//!
21//! [`ItemStatic`]: ../struct.ItemStatic.html
22//!
23//! ```
24//! # #[macro_use]
25//! # extern crate syn;
26//! #
27//! # use syn::{Attribute, Visibility, Ident, Type, Expr};
28//! #
29//! pub struct ItemStatic {
30//! pub attrs: Vec<Attribute>,
31//! pub vis: Visibility,
32//! pub static_token: Token![static],
33//! pub mutability: Option<Token![mut]>,
34//! pub ident: Ident,
35//! pub colon_token: Token![:],
36//! pub ty: Box<Type>,
37//! pub eq_token: Token![=],
38//! pub expr: Box<Expr>,
39//! pub semi_token: Token![;],
40//! }
41//! #
42//! # fn main() {}
43//! ```
44//!
45//! # Parsing
46//!
47//! These tokens can be parsed using the [`Synom`] trait and the parser
48//! combinator macros [`punct!`], [`keyword!`], [`parens!`], [`braces!`], and
49//! [`brackets!`].
50//!
51//! [`Synom`]: ../synom/trait.Synom.html
52//! [`punct!`]: ../macro.punct.html
53//! [`keyword!`]: ../macro.keyword.html
54//! [`parens!`]: ../macro.parens.html
55//! [`braces!`]: ../macro.braces.html
56//! [`brackets!`]: ../macro.brackets.html
57//!
58//! ```
59//! #[macro_use]
60//! extern crate syn;
61//!
62//! use syn::synom::Synom;
63//! use syn::{Attribute, Visibility, Ident, Type, Expr};
64//! #
65//! # struct ItemStatic;
66//! # use syn::ItemStatic as SynItemStatic;
67//!
68//! // Parse the ItemStatic struct shown above.
69//! impl Synom for ItemStatic {
70//! named!(parse -> Self, do_parse!(
71//! # (ItemStatic)
72//! # ));
73//! # }
74//! #
75//! # mod example {
76//! # use super::*;
77//! # use super::SynItemStatic as ItemStatic;
78//! #
79//! # named!(parse -> ItemStatic, do_parse!(
80//! attrs: many0!(Attribute::parse_outer) >>
81//! vis: syn!(Visibility) >>
82//! static_token: keyword!(static) >>
83//! mutability: option!(keyword!(mut)) >>
84//! ident: syn!(Ident) >>
85//! colon_token: punct!(:) >>
86//! ty: syn!(Type) >>
87//! eq_token: punct!(=) >>
88//! expr: syn!(Expr) >>
89//! semi_token: punct!(;) >>
90//! (ItemStatic {
91//! attrs, vis, static_token, mutability, ident, colon_token,
92//! ty: Box::new(ty), eq_token, expr: Box::new(expr), semi_token,
93//! })
94//! ));
95//! }
96//! #
97//! # fn main() {}
98//! ```
Alex Crichton954046c2017-05-30 21:49:42 -070099
David Tolnay98942562017-12-26 21:24:35 -0500100use proc_macro2::Span;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700101
102macro_rules! tokens {
103 (
David Tolnay73c98de2017-12-31 15:56:56 -0500104 punct: {
105 $($punct:tt pub struct $punct_name:ident/$len:tt #[$punct_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700106 }
David Tolnay73c98de2017-12-31 15:56:56 -0500107 delimiter: {
108 $($delimiter:tt pub struct $delimiter_name:ident #[$delimiter_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700109 }
David Tolnay73c98de2017-12-31 15:56:56 -0500110 keyword: {
111 $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700112 }
113 ) => (
David Tolnay73c98de2017-12-31 15:56:56 -0500114 $(token_punct! { #[$punct_doc] $punct pub struct $punct_name/$len })*
115 $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })*
116 $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700117 )
118}
119
David Tolnay73c98de2017-12-31 15:56:56 -0500120macro_rules! token_punct {
David Tolnay5a20f632017-12-26 22:11:28 -0500121 (#[$doc:meta] $s:tt pub struct $name:ident/$len:tt) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700122 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700123 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500124 #[$doc]
125 pub struct $name(pub [Span; $len]);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700126
David Tolnay0bdb0552017-12-27 21:31:51 -0500127 impl $name {
128 pub fn new(span: Span) -> Self {
129 $name([span; $len])
130 }
131 }
132
Nika Layzelld73a3652017-10-24 08:57:05 -0400133 #[cfg(feature = "extra-traits")]
134 impl ::std::fmt::Debug for $name {
135 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
David Tolnay98942562017-12-26 21:24:35 -0500136 f.write_str(stringify!($name))
Nika Layzelld73a3652017-10-24 08:57:05 -0400137 }
138 }
139
David Tolnay98942562017-12-26 21:24:35 -0500140 #[cfg(feature = "extra-traits")]
141 impl ::std::cmp::Eq for $name {}
142
143 #[cfg(feature = "extra-traits")]
144 impl ::std::cmp::PartialEq for $name {
145 fn eq(&self, _other: &$name) -> bool {
146 true
147 }
148 }
149
150 #[cfg(feature = "extra-traits")]
151 impl ::std::hash::Hash for $name {
152 fn hash<H>(&self, _state: &mut H)
153 where H: ::std::hash::Hasher
154 {}
155 }
156
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700157 #[cfg(feature = "printing")]
158 impl ::quote::ToTokens for $name {
159 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
David Tolnay73c98de2017-12-31 15:56:56 -0500160 printing::punct($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700161 }
162 }
163
164 #[cfg(feature = "parsing")]
165 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800166 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500167 parsing::punct($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700168 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800169
170 fn description() -> Option<&'static str> {
171 Some(concat!("`", $s, "`"))
172 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700173 }
174 }
175}
176
David Tolnay73c98de2017-12-31 15:56:56 -0500177macro_rules! token_keyword {
David Tolnay5a20f632017-12-26 22:11:28 -0500178 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700179 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700180 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500181 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700182 pub struct $name(pub Span);
183
David Tolnay98942562017-12-26 21:24:35 -0500184 #[cfg(feature = "extra-traits")]
185 impl ::std::fmt::Debug for $name {
186 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
187 f.write_str(stringify!($name))
188 }
189 }
190
191 #[cfg(feature = "extra-traits")]
192 impl ::std::cmp::Eq for $name {}
193
194 #[cfg(feature = "extra-traits")]
195 impl ::std::cmp::PartialEq for $name {
196 fn eq(&self, _other: &$name) -> bool {
197 true
198 }
199 }
200
201 #[cfg(feature = "extra-traits")]
202 impl ::std::hash::Hash for $name {
203 fn hash<H>(&self, _state: &mut H)
204 where H: ::std::hash::Hasher
205 {}
206 }
207
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700208 #[cfg(feature = "printing")]
209 impl ::quote::ToTokens for $name {
210 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
David Tolnay73c98de2017-12-31 15:56:56 -0500211 printing::keyword($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700212 }
213 }
214
215 #[cfg(feature = "parsing")]
216 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800217 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500218 parsing::keyword($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700219 }
David Tolnay79777332018-01-07 10:04:42 -0800220
221 fn description() -> Option<&'static str> {
222 Some(concat!("`", $s, "`"))
223 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700224 }
225 }
226}
227
David Tolnay73c98de2017-12-31 15:56:56 -0500228macro_rules! token_delimiter {
David Tolnay5a20f632017-12-26 22:11:28 -0500229 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700230 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700231 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500232 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700233 pub struct $name(pub Span);
234
David Tolnay98942562017-12-26 21:24:35 -0500235 #[cfg(feature = "extra-traits")]
236 impl ::std::fmt::Debug for $name {
237 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
238 f.write_str(stringify!($name))
239 }
240 }
241
242 #[cfg(feature = "extra-traits")]
243 impl ::std::cmp::Eq for $name {}
244
245 #[cfg(feature = "extra-traits")]
246 impl ::std::cmp::PartialEq for $name {
247 fn eq(&self, _other: &$name) -> bool {
248 true
249 }
250 }
251
252 #[cfg(feature = "extra-traits")]
253 impl ::std::hash::Hash for $name {
254 fn hash<H>(&self, _state: &mut H)
255 where H: ::std::hash::Hasher
256 {}
257 }
258
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700259 impl $name {
260 #[cfg(feature = "printing")]
261 pub fn surround<F>(&self,
262 tokens: &mut ::quote::Tokens,
263 f: F)
264 where F: FnOnce(&mut ::quote::Tokens)
265 {
266 printing::delim($s, &self.0, tokens, f);
267 }
268
269 #[cfg(feature = "parsing")]
David Tolnaydfc886b2018-01-06 08:03:09 -0800270 pub fn parse<F, R>(tokens: $crate::buffer::Cursor, f: F) -> $crate::synom::PResult<($name, R)>
271 where F: FnOnce($crate::buffer::Cursor) -> $crate::synom::PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700272 {
273 parsing::delim($s, tokens, $name, f)
274 }
275 }
276 }
277}
278
279tokens! {
David Tolnay73c98de2017-12-31 15:56:56 -0500280 punct: {
David Tolnay5a20f632017-12-26 22:11:28 -0500281 "+" pub struct Add/1 /// `+`
282 "+=" pub struct AddEq/2 /// `+=`
283 "&" pub struct And/1 /// `&`
284 "&&" pub struct AndAnd/2 /// `&&`
285 "&=" pub struct AndEq/2 /// `&=`
286 "@" pub struct At/1 /// `@`
287 "!" pub struct Bang/1 /// `!`
288 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500289 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500290 ":" pub struct Colon/1 /// `:`
291 "::" pub struct Colon2/2 /// `::`
292 "," pub struct Comma/1 /// `,`
293 "/" pub struct Div/1 /// `/`
294 "/=" pub struct DivEq/2 /// `/=`
295 "." pub struct Dot/1 /// `.`
296 ".." pub struct Dot2/2 /// `..`
297 "..." pub struct Dot3/3 /// `...`
298 "..=" pub struct DotDotEq/3 /// `..=`
299 "=" pub struct Eq/1 /// `=`
300 "==" pub struct EqEq/2 /// `==`
301 ">=" pub struct Ge/2 /// `>=`
302 ">" pub struct Gt/1 /// `>`
303 "<=" pub struct Le/2 /// `<=`
304 "<" pub struct Lt/1 /// `<`
305 "*=" pub struct MulEq/2 /// `*=`
306 "!=" pub struct Ne/2 /// `!=`
307 "|" pub struct Or/1 /// `|`
308 "|=" pub struct OrEq/2 /// `|=`
309 "||" pub struct OrOr/2 /// `||`
310 "#" pub struct Pound/1 /// `#`
311 "?" pub struct Question/1 /// `?`
312 "->" pub struct RArrow/2 /// `->`
313 "<-" pub struct LArrow/2 /// `<-`
314 "%" pub struct Rem/1 /// `%`
315 "%=" pub struct RemEq/2 /// `%=`
316 "=>" pub struct Rocket/2 /// `=>`
317 ";" pub struct Semi/1 /// `;`
318 "<<" pub struct Shl/2 /// `<<`
319 "<<=" pub struct ShlEq/3 /// `<<=`
320 ">>" pub struct Shr/2 /// `>>`
321 ">>=" pub struct ShrEq/3 /// `>>=`
322 "*" pub struct Star/1 /// `*`
323 "-" pub struct Sub/1 /// `-`
324 "-=" pub struct SubEq/2 /// `-=`
325 "_" pub struct Underscore/1 /// `_`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700326 }
David Tolnay73c98de2017-12-31 15:56:56 -0500327 delimiter: {
David Tolnay5a20f632017-12-26 22:11:28 -0500328 "{" pub struct Brace /// `{...}`
329 "[" pub struct Bracket /// `[...]`
330 "(" pub struct Paren /// `(...)`
331 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700332 }
David Tolnay73c98de2017-12-31 15:56:56 -0500333 keyword: {
David Tolnay5a20f632017-12-26 22:11:28 -0500334 "as" pub struct As /// `as`
335 "auto" pub struct Auto /// `auto`
336 "box" pub struct Box /// `box`
337 "break" pub struct Break /// `break`
338 "Self" pub struct CapSelf /// `Self`
339 "catch" pub struct Catch /// `catch`
340 "const" pub struct Const /// `const`
341 "continue" pub struct Continue /// `continue`
342 "crate" pub struct Crate /// `crate`
343 "default" pub struct Default /// `default`
344 "do" pub struct Do /// `do`
345 "dyn" pub struct Dyn /// `dyn`
346 "else" pub struct Else /// `else`
347 "enum" pub struct Enum /// `enum`
348 "extern" pub struct Extern /// `extern`
349 "fn" pub struct Fn /// `fn`
350 "for" pub struct For /// `for`
351 "if" pub struct If /// `if`
352 "impl" pub struct Impl /// `impl`
353 "in" pub struct In /// `in`
354 "let" pub struct Let /// `let`
355 "loop" pub struct Loop /// `loop`
356 "macro" pub struct Macro /// `macro`
357 "match" pub struct Match /// `match`
358 "mod" pub struct Mod /// `mod`
359 "move" pub struct Move /// `move`
360 "mut" pub struct Mut /// `mut`
361 "pub" pub struct Pub /// `pub`
362 "ref" pub struct Ref /// `ref`
363 "return" pub struct Return /// `return`
364 "self" pub struct Self_ /// `self`
365 "static" pub struct Static /// `static`
366 "struct" pub struct Struct /// `struct`
367 "super" pub struct Super /// `super`
368 "trait" pub struct Trait /// `trait`
369 "type" pub struct Type /// `type`
370 "union" pub struct Union /// `union`
371 "unsafe" pub struct Unsafe /// `unsafe`
372 "use" pub struct Use /// `use`
373 "where" pub struct Where /// `where`
374 "while" pub struct While /// `while`
375 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700376 }
377}
378
David Tolnayf005f962018-01-06 21:19:41 -0800379/// A type-macro that expands to the name of the Rust type representation of a
380/// given token.
381///
382/// See the [token module] documentation for details and examples.
383///
384/// [token module]: token/index.html
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385// Unfortunate duplication due to a rustdoc bug.
386// https://github.com/rust-lang/rust/issues/45939
387#[macro_export]
388macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500389 (+) => { $crate::token::Add };
390 (+=) => { $crate::token::AddEq };
391 (&) => { $crate::token::And };
392 (&&) => { $crate::token::AndAnd };
393 (&=) => { $crate::token::AndEq };
394 (@) => { $crate::token::At };
395 (!) => { $crate::token::Bang };
396 (^) => { $crate::token::Caret };
397 (^=) => { $crate::token::CaretEq };
398 (:) => { $crate::token::Colon };
399 (::) => { $crate::token::Colon2 };
400 (,) => { $crate::token::Comma };
401 (/) => { $crate::token::Div };
402 (/=) => { $crate::token::DivEq };
403 (.) => { $crate::token::Dot };
404 (..) => { $crate::token::Dot2 };
405 (...) => { $crate::token::Dot3 };
406 (..=) => { $crate::token::DotDotEq };
407 (=) => { $crate::token::Eq };
408 (==) => { $crate::token::EqEq };
409 (>=) => { $crate::token::Ge };
410 (>) => { $crate::token::Gt };
411 (<=) => { $crate::token::Le };
412 (<) => { $crate::token::Lt };
413 (*=) => { $crate::token::MulEq };
414 (!=) => { $crate::token::Ne };
415 (|) => { $crate::token::Or };
416 (|=) => { $crate::token::OrEq };
417 (||) => { $crate::token::OrOr };
418 (#) => { $crate::token::Pound };
419 (?) => { $crate::token::Question };
420 (->) => { $crate::token::RArrow };
421 (<-) => { $crate::token::LArrow };
422 (%) => { $crate::token::Rem };
423 (%=) => { $crate::token::RemEq };
424 (=>) => { $crate::token::Rocket };
425 (;) => { $crate::token::Semi };
426 (<<) => { $crate::token::Shl };
427 (<<=) => { $crate::token::ShlEq };
428 (>>) => { $crate::token::Shr };
429 (>>=) => { $crate::token::ShrEq };
430 (*) => { $crate::token::Star };
431 (-) => { $crate::token::Sub };
432 (-=) => { $crate::token::SubEq };
433 (_) => { $crate::token::Underscore };
434 (as) => { $crate::token::As };
435 (auto) => { $crate::token::Auto };
436 (box) => { $crate::token::Box };
437 (break) => { $crate::token::Break };
438 (Self) => { $crate::token::CapSelf };
439 (catch) => { $crate::token::Catch };
440 (const) => { $crate::token::Const };
441 (continue) => { $crate::token::Continue };
442 (crate) => { $crate::token::Crate };
443 (default) => { $crate::token::Default };
444 (do) => { $crate::token::Do };
445 (dyn) => { $crate::token::Dyn };
446 (else) => { $crate::token::Else };
447 (enum) => { $crate::token::Enum };
448 (extern) => { $crate::token::Extern };
449 (fn) => { $crate::token::Fn };
450 (for) => { $crate::token::For };
451 (if) => { $crate::token::If };
452 (impl) => { $crate::token::Impl };
453 (in) => { $crate::token::In };
454 (let) => { $crate::token::Let };
455 (loop) => { $crate::token::Loop };
456 (macro) => { $crate::token::Macro };
457 (match) => { $crate::token::Match };
458 (mod) => { $crate::token::Mod };
459 (move) => { $crate::token::Move };
460 (mut) => { $crate::token::Mut };
461 (pub) => { $crate::token::Pub };
462 (ref) => { $crate::token::Ref };
463 (return) => { $crate::token::Return };
464 (self) => { $crate::token::Self_ };
465 (static) => { $crate::token::Static };
466 (struct) => { $crate::token::Struct };
467 (super) => { $crate::token::Super };
468 (trait) => { $crate::token::Trait };
469 (type) => { $crate::token::Type };
470 (union) => { $crate::token::Union };
471 (unsafe) => { $crate::token::Unsafe };
472 (use) => { $crate::token::Use };
473 (where) => { $crate::token::Where };
474 (while) => { $crate::token::While };
475 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800476}
477
David Tolnayf005f962018-01-06 21:19:41 -0800478/// Parse a single Rust punctuation token.
479///
480/// See the [token module] documentation for details and examples.
481///
482/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800483///
484/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500485#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800486#[macro_export]
487macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500488 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
489 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
490 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
491 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
492 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
493 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
494 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
495 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
496 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
497 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
498 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
499 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
500 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
501 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
502 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
503 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
504 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
505 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
506 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
507 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
508 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
509 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
510 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
511 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
512 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
513 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
514 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
515 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
516 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
517 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
518 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
519 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
520 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
521 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
522 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
523 ($i:expr, =>) => { call!($i, <$crate::token::Rocket as $crate::synom::Synom>::parse) };
524 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
525 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
526 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
527 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
528 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
529 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
530 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
531 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
532 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800533}
534
David Tolnayf005f962018-01-06 21:19:41 -0800535/// Parse a single Rust keyword token.
536///
537/// See the [token module] documentation for details and examples.
538///
539/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800540///
541/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500542#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800543#[macro_export]
544macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500545 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
546 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
547 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
548 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
549 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
550 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
551 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
552 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
553 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
554 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
555 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
556 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
557 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
558 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
559 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
560 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
561 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
562 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
563 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
564 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
565 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
566 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
567 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
568 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
569 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
570 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
571 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
572 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
573 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
574 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
575 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
576 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
577 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
578 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
579 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
580 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
581 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
582 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
583 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
584 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
585 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
586 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800587}
588
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700589#[cfg(feature = "parsing")]
590mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500591 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700592
David Tolnaydfc886b2018-01-06 08:03:09 -0800593 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500594 use parse_error;
595 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700596
597 pub trait FromSpans: Sized {
598 fn from_spans(spans: &[Span]) -> Self;
599 }
600
601 impl FromSpans for [Span; 1] {
602 fn from_spans(spans: &[Span]) -> Self {
603 [spans[0]]
604 }
605 }
606
607 impl FromSpans for [Span; 2] {
608 fn from_spans(spans: &[Span]) -> Self {
609 [spans[0], spans[1]]
610 }
611 }
612
613 impl FromSpans for [Span; 3] {
614 fn from_spans(spans: &[Span]) -> Self {
615 [spans[0], spans[1], spans[2]]
616 }
617 }
618
David Tolnay73c98de2017-12-31 15:56:56 -0500619 pub fn punct<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R>
David Tolnay51382052017-12-27 13:46:21 -0500620 where
621 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700622 {
623 let mut spans = [Span::default(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700624 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700625 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700626
Alex Crichton954046c2017-05-30 21:49:42 -0700627 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400628 match tokens.op() {
David Tolnay65729482017-12-31 16:14:50 -0500629 Some((span, op, kind, rest)) if op == ch => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400630 if i != s.len() - 1 {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700631 match kind {
632 Spacing::Joint => {}
633 _ => return parse_error(),
Michael Layzell0a1a6632017-06-02 18:07:43 -0400634 }
635 }
David Tolnay98942562017-12-26 21:24:35 -0500636 *slot = span;
Michael Layzell0a1a6632017-06-02 18:07:43 -0400637 tokens = rest;
Alex Crichton954046c2017-05-30 21:49:42 -0700638 }
David Tolnay51382052017-12-27 13:46:21 -0500639 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700640 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700641 }
David Tolnayf4aa6b42017-12-31 16:40:33 -0500642 Ok((new(T::from_spans(&spans)), tokens))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700643 }
644
David Tolnay73c98de2017-12-31 15:56:56 -0500645 pub fn keyword<'a, T>(keyword: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> {
David Tolnay65729482017-12-31 16:14:50 -0500646 if let Some((span, term, rest)) = tokens.term() {
David Tolnay73c98de2017-12-31 15:56:56 -0500647 if term.as_str() == keyword {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500648 return Ok((new(span), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400649 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700650 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400651 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700652 }
653
David Tolnay51382052017-12-27 13:46:21 -0500654 pub fn delim<'a, F, R, T>(
655 delim: &str,
656 tokens: Cursor<'a>,
657 new: fn(Span) -> T,
658 f: F,
David Tolnay8875fca2017-12-31 13:52:37 -0500659 ) -> PResult<'a, (T, R)>
David Tolnay51382052017-12-27 13:46:21 -0500660 where
661 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700662 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400663 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700664 let delim = match delim {
665 "(" => Delimiter::Parenthesis,
666 "{" => Delimiter::Brace,
667 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400668 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700669 _ => panic!("unknown delimiter: {}", delim),
670 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700671
David Tolnay65729482017-12-31 16:14:50 -0500672 if let Some((inside, span, rest)) = tokens.group(delim) {
673 match f(inside) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500674 Ok((ret, remaining)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400675 if remaining.eof() {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500676 return Ok(((new(span), ret), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400677 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700678 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400679 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700680 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700681 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400682 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700683 }
684}
685
686#[cfg(feature = "printing")]
687mod printing {
David Tolnayf2cfd722017-12-31 18:02:51 -0500688 use proc_macro2::{Delimiter, Spacing, Span, Term, TokenNode, TokenTree};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700689 use quote::Tokens;
690
David Tolnay73c98de2017-12-31 15:56:56 -0500691 pub fn punct(s: &str, spans: &[Span], tokens: &mut Tokens) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700692 assert_eq!(s.len(), spans.len());
693
694 let mut chars = s.chars();
695 let mut spans = spans.iter();
696 let ch = chars.next_back().unwrap();
697 let span = spans.next_back().unwrap();
698 for (ch, span) in chars.zip(spans) {
699 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500700 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700701 kind: TokenNode::Op(ch, Spacing::Joint),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700702 });
703 }
704
705 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500706 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700707 kind: TokenNode::Op(ch, Spacing::Alone),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700708 });
709 }
710
David Tolnay73c98de2017-12-31 15:56:56 -0500711 pub fn keyword(s: &str, span: &Span, tokens: &mut Tokens) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700712 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500713 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700714 kind: TokenNode::Term(Term::intern(s)),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700715 });
716 }
717
718 pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500719 where
720 F: FnOnce(&mut Tokens),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700721 {
David Tolnay00ab6982017-12-31 18:15:06 -0500722 let delim = match s {
723 "(" => Delimiter::Parenthesis,
724 "[" => Delimiter::Bracket,
725 "{" => Delimiter::Brace,
726 " " => Delimiter::None,
727 _ => panic!("unknown delimiter: {}", s),
728 };
729 let mut inner = Tokens::new();
730 f(&mut inner);
731 tokens.append(TokenTree {
732 span: *span,
733 kind: TokenNode::Group(delim, inner.into()),
734 });
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700735 }
736}