blob: c52a546b50d5d24383ef84adf07a673e5497176b [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
Alex Crichton954046c2017-05-30 21:49:42 -07009//! Discrete tokens that can be parsed out by synom.
10//!
11//! This module contains a number of useful tokens like `+=` and `/` along with
12//! keywords like `crate` and such. These structures are used to track the spans
13//! of these tokens and all implment the `ToTokens` and `Synom` traits when the
14//! corresponding feature is activated.
15
David Tolnay98942562017-12-26 21:24:35 -050016use proc_macro2::Span;
Alex Crichton7b9e02f2017-05-30 15:54:33 -070017
18macro_rules! tokens {
19 (
David Tolnay73c98de2017-12-31 15:56:56 -050020 punct: {
21 $($punct:tt pub struct $punct_name:ident/$len:tt #[$punct_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070022 }
David Tolnay73c98de2017-12-31 15:56:56 -050023 delimiter: {
24 $($delimiter:tt pub struct $delimiter_name:ident #[$delimiter_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070025 }
David Tolnay73c98de2017-12-31 15:56:56 -050026 keyword: {
27 $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070028 }
29 ) => (
David Tolnay73c98de2017-12-31 15:56:56 -050030 $(token_punct! { #[$punct_doc] $punct pub struct $punct_name/$len })*
31 $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })*
32 $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070033 )
34}
35
David Tolnay73c98de2017-12-31 15:56:56 -050036macro_rules! token_punct {
David Tolnay5a20f632017-12-26 22:11:28 -050037 (#[$doc:meta] $s:tt pub struct $name:ident/$len:tt) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070038 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070039 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -050040 #[$doc]
41 pub struct $name(pub [Span; $len]);
Alex Crichton7b9e02f2017-05-30 15:54:33 -070042
David Tolnay0bdb0552017-12-27 21:31:51 -050043 impl $name {
44 pub fn new(span: Span) -> Self {
45 $name([span; $len])
46 }
47 }
48
Nika Layzelld73a3652017-10-24 08:57:05 -040049 #[cfg(feature = "extra-traits")]
50 impl ::std::fmt::Debug for $name {
51 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
David Tolnay98942562017-12-26 21:24:35 -050052 f.write_str(stringify!($name))
Nika Layzelld73a3652017-10-24 08:57:05 -040053 }
54 }
55
David Tolnay98942562017-12-26 21:24:35 -050056 #[cfg(feature = "extra-traits")]
57 impl ::std::cmp::Eq for $name {}
58
59 #[cfg(feature = "extra-traits")]
60 impl ::std::cmp::PartialEq for $name {
61 fn eq(&self, _other: &$name) -> bool {
62 true
63 }
64 }
65
66 #[cfg(feature = "extra-traits")]
67 impl ::std::hash::Hash for $name {
68 fn hash<H>(&self, _state: &mut H)
69 where H: ::std::hash::Hasher
70 {}
71 }
72
Alex Crichton7b9e02f2017-05-30 15:54:33 -070073 #[cfg(feature = "printing")]
74 impl ::quote::ToTokens for $name {
75 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
David Tolnay73c98de2017-12-31 15:56:56 -050076 printing::punct($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -070077 }
78 }
79
80 #[cfg(feature = "parsing")]
81 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -080082 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -050083 parsing::punct($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -070084 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -080085
86 fn description() -> Option<&'static str> {
87 Some(concat!("`", $s, "`"))
88 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -070089 }
90 }
91}
92
David Tolnay73c98de2017-12-31 15:56:56 -050093macro_rules! token_keyword {
David Tolnay5a20f632017-12-26 22:11:28 -050094 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070095 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070096 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -050097 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070098 pub struct $name(pub Span);
99
David Tolnay98942562017-12-26 21:24:35 -0500100 #[cfg(feature = "extra-traits")]
101 impl ::std::fmt::Debug for $name {
102 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
103 f.write_str(stringify!($name))
104 }
105 }
106
107 #[cfg(feature = "extra-traits")]
108 impl ::std::cmp::Eq for $name {}
109
110 #[cfg(feature = "extra-traits")]
111 impl ::std::cmp::PartialEq for $name {
112 fn eq(&self, _other: &$name) -> bool {
113 true
114 }
115 }
116
117 #[cfg(feature = "extra-traits")]
118 impl ::std::hash::Hash for $name {
119 fn hash<H>(&self, _state: &mut H)
120 where H: ::std::hash::Hasher
121 {}
122 }
123
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700124 #[cfg(feature = "printing")]
125 impl ::quote::ToTokens for $name {
126 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
David Tolnay73c98de2017-12-31 15:56:56 -0500127 printing::keyword($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700128 }
129 }
130
131 #[cfg(feature = "parsing")]
132 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800133 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500134 parsing::keyword($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700135 }
136 }
137 }
138}
139
David Tolnay73c98de2017-12-31 15:56:56 -0500140macro_rules! token_delimiter {
David Tolnay5a20f632017-12-26 22:11:28 -0500141 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700142 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700143 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500144 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700145 pub struct $name(pub Span);
146
David Tolnay98942562017-12-26 21:24:35 -0500147 #[cfg(feature = "extra-traits")]
148 impl ::std::fmt::Debug for $name {
149 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
150 f.write_str(stringify!($name))
151 }
152 }
153
154 #[cfg(feature = "extra-traits")]
155 impl ::std::cmp::Eq for $name {}
156
157 #[cfg(feature = "extra-traits")]
158 impl ::std::cmp::PartialEq for $name {
159 fn eq(&self, _other: &$name) -> bool {
160 true
161 }
162 }
163
164 #[cfg(feature = "extra-traits")]
165 impl ::std::hash::Hash for $name {
166 fn hash<H>(&self, _state: &mut H)
167 where H: ::std::hash::Hasher
168 {}
169 }
170
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700171 impl $name {
172 #[cfg(feature = "printing")]
173 pub fn surround<F>(&self,
174 tokens: &mut ::quote::Tokens,
175 f: F)
176 where F: FnOnce(&mut ::quote::Tokens)
177 {
178 printing::delim($s, &self.0, tokens, f);
179 }
180
181 #[cfg(feature = "parsing")]
David Tolnaydfc886b2018-01-06 08:03:09 -0800182 pub fn parse<F, R>(tokens: $crate::buffer::Cursor, f: F) -> $crate::synom::PResult<($name, R)>
183 where F: FnOnce($crate::buffer::Cursor) -> $crate::synom::PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700184 {
185 parsing::delim($s, tokens, $name, f)
186 }
187 }
188 }
189}
190
191tokens! {
David Tolnay73c98de2017-12-31 15:56:56 -0500192 punct: {
David Tolnay5a20f632017-12-26 22:11:28 -0500193 "+" pub struct Add/1 /// `+`
194 "+=" pub struct AddEq/2 /// `+=`
195 "&" pub struct And/1 /// `&`
196 "&&" pub struct AndAnd/2 /// `&&`
197 "&=" pub struct AndEq/2 /// `&=`
198 "@" pub struct At/1 /// `@`
199 "!" pub struct Bang/1 /// `!`
200 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500201 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500202 ":" pub struct Colon/1 /// `:`
203 "::" pub struct Colon2/2 /// `::`
204 "," pub struct Comma/1 /// `,`
205 "/" pub struct Div/1 /// `/`
206 "/=" pub struct DivEq/2 /// `/=`
207 "." pub struct Dot/1 /// `.`
208 ".." pub struct Dot2/2 /// `..`
209 "..." pub struct Dot3/3 /// `...`
210 "..=" pub struct DotDotEq/3 /// `..=`
211 "=" pub struct Eq/1 /// `=`
212 "==" pub struct EqEq/2 /// `==`
213 ">=" pub struct Ge/2 /// `>=`
214 ">" pub struct Gt/1 /// `>`
215 "<=" pub struct Le/2 /// `<=`
216 "<" pub struct Lt/1 /// `<`
217 "*=" pub struct MulEq/2 /// `*=`
218 "!=" pub struct Ne/2 /// `!=`
219 "|" pub struct Or/1 /// `|`
220 "|=" pub struct OrEq/2 /// `|=`
221 "||" pub struct OrOr/2 /// `||`
222 "#" pub struct Pound/1 /// `#`
223 "?" pub struct Question/1 /// `?`
224 "->" pub struct RArrow/2 /// `->`
225 "<-" pub struct LArrow/2 /// `<-`
226 "%" pub struct Rem/1 /// `%`
227 "%=" pub struct RemEq/2 /// `%=`
228 "=>" pub struct Rocket/2 /// `=>`
229 ";" pub struct Semi/1 /// `;`
230 "<<" pub struct Shl/2 /// `<<`
231 "<<=" pub struct ShlEq/3 /// `<<=`
232 ">>" pub struct Shr/2 /// `>>`
233 ">>=" pub struct ShrEq/3 /// `>>=`
234 "*" pub struct Star/1 /// `*`
235 "-" pub struct Sub/1 /// `-`
236 "-=" pub struct SubEq/2 /// `-=`
237 "_" pub struct Underscore/1 /// `_`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700238 }
David Tolnay73c98de2017-12-31 15:56:56 -0500239 delimiter: {
David Tolnay5a20f632017-12-26 22:11:28 -0500240 "{" pub struct Brace /// `{...}`
241 "[" pub struct Bracket /// `[...]`
242 "(" pub struct Paren /// `(...)`
243 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700244 }
David Tolnay73c98de2017-12-31 15:56:56 -0500245 keyword: {
David Tolnay5a20f632017-12-26 22:11:28 -0500246 "as" pub struct As /// `as`
247 "auto" pub struct Auto /// `auto`
248 "box" pub struct Box /// `box`
249 "break" pub struct Break /// `break`
250 "Self" pub struct CapSelf /// `Self`
251 "catch" pub struct Catch /// `catch`
252 "const" pub struct Const /// `const`
253 "continue" pub struct Continue /// `continue`
254 "crate" pub struct Crate /// `crate`
255 "default" pub struct Default /// `default`
256 "do" pub struct Do /// `do`
257 "dyn" pub struct Dyn /// `dyn`
258 "else" pub struct Else /// `else`
259 "enum" pub struct Enum /// `enum`
260 "extern" pub struct Extern /// `extern`
261 "fn" pub struct Fn /// `fn`
262 "for" pub struct For /// `for`
263 "if" pub struct If /// `if`
264 "impl" pub struct Impl /// `impl`
265 "in" pub struct In /// `in`
266 "let" pub struct Let /// `let`
267 "loop" pub struct Loop /// `loop`
268 "macro" pub struct Macro /// `macro`
269 "match" pub struct Match /// `match`
270 "mod" pub struct Mod /// `mod`
271 "move" pub struct Move /// `move`
272 "mut" pub struct Mut /// `mut`
273 "pub" pub struct Pub /// `pub`
274 "ref" pub struct Ref /// `ref`
275 "return" pub struct Return /// `return`
276 "self" pub struct Self_ /// `self`
277 "static" pub struct Static /// `static`
278 "struct" pub struct Struct /// `struct`
279 "super" pub struct Super /// `super`
280 "trait" pub struct Trait /// `trait`
281 "type" pub struct Type /// `type`
282 "union" pub struct Union /// `union`
283 "unsafe" pub struct Unsafe /// `unsafe`
284 "use" pub struct Use /// `use`
285 "where" pub struct Where /// `where`
286 "while" pub struct While /// `while`
287 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700288 }
289}
290
David Tolnayf8db7ba2017-11-11 22:52:16 -0800291// Unfortunate duplication due to a rustdoc bug.
292// https://github.com/rust-lang/rust/issues/45939
293#[macro_export]
294macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500295 (+) => { $crate::token::Add };
296 (+=) => { $crate::token::AddEq };
297 (&) => { $crate::token::And };
298 (&&) => { $crate::token::AndAnd };
299 (&=) => { $crate::token::AndEq };
300 (@) => { $crate::token::At };
301 (!) => { $crate::token::Bang };
302 (^) => { $crate::token::Caret };
303 (^=) => { $crate::token::CaretEq };
304 (:) => { $crate::token::Colon };
305 (::) => { $crate::token::Colon2 };
306 (,) => { $crate::token::Comma };
307 (/) => { $crate::token::Div };
308 (/=) => { $crate::token::DivEq };
309 (.) => { $crate::token::Dot };
310 (..) => { $crate::token::Dot2 };
311 (...) => { $crate::token::Dot3 };
312 (..=) => { $crate::token::DotDotEq };
313 (=) => { $crate::token::Eq };
314 (==) => { $crate::token::EqEq };
315 (>=) => { $crate::token::Ge };
316 (>) => { $crate::token::Gt };
317 (<=) => { $crate::token::Le };
318 (<) => { $crate::token::Lt };
319 (*=) => { $crate::token::MulEq };
320 (!=) => { $crate::token::Ne };
321 (|) => { $crate::token::Or };
322 (|=) => { $crate::token::OrEq };
323 (||) => { $crate::token::OrOr };
324 (#) => { $crate::token::Pound };
325 (?) => { $crate::token::Question };
326 (->) => { $crate::token::RArrow };
327 (<-) => { $crate::token::LArrow };
328 (%) => { $crate::token::Rem };
329 (%=) => { $crate::token::RemEq };
330 (=>) => { $crate::token::Rocket };
331 (;) => { $crate::token::Semi };
332 (<<) => { $crate::token::Shl };
333 (<<=) => { $crate::token::ShlEq };
334 (>>) => { $crate::token::Shr };
335 (>>=) => { $crate::token::ShrEq };
336 (*) => { $crate::token::Star };
337 (-) => { $crate::token::Sub };
338 (-=) => { $crate::token::SubEq };
339 (_) => { $crate::token::Underscore };
340 (as) => { $crate::token::As };
341 (auto) => { $crate::token::Auto };
342 (box) => { $crate::token::Box };
343 (break) => { $crate::token::Break };
344 (Self) => { $crate::token::CapSelf };
345 (catch) => { $crate::token::Catch };
346 (const) => { $crate::token::Const };
347 (continue) => { $crate::token::Continue };
348 (crate) => { $crate::token::Crate };
349 (default) => { $crate::token::Default };
350 (do) => { $crate::token::Do };
351 (dyn) => { $crate::token::Dyn };
352 (else) => { $crate::token::Else };
353 (enum) => { $crate::token::Enum };
354 (extern) => { $crate::token::Extern };
355 (fn) => { $crate::token::Fn };
356 (for) => { $crate::token::For };
357 (if) => { $crate::token::If };
358 (impl) => { $crate::token::Impl };
359 (in) => { $crate::token::In };
360 (let) => { $crate::token::Let };
361 (loop) => { $crate::token::Loop };
362 (macro) => { $crate::token::Macro };
363 (match) => { $crate::token::Match };
364 (mod) => { $crate::token::Mod };
365 (move) => { $crate::token::Move };
366 (mut) => { $crate::token::Mut };
367 (pub) => { $crate::token::Pub };
368 (ref) => { $crate::token::Ref };
369 (return) => { $crate::token::Return };
370 (self) => { $crate::token::Self_ };
371 (static) => { $crate::token::Static };
372 (struct) => { $crate::token::Struct };
373 (super) => { $crate::token::Super };
374 (trait) => { $crate::token::Trait };
375 (type) => { $crate::token::Type };
376 (union) => { $crate::token::Union };
377 (unsafe) => { $crate::token::Unsafe };
378 (use) => { $crate::token::Use };
379 (where) => { $crate::token::Where };
380 (while) => { $crate::token::While };
381 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800382}
383
David Tolnay0fbe3282017-12-26 21:46:16 -0500384#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800385#[macro_export]
386macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500387 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
388 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
389 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
390 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
391 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
392 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
393 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
394 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
395 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
396 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
397 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
398 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
399 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
400 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
401 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
402 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
403 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
404 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
405 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
406 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
407 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
408 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
409 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
410 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
411 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
412 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
413 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
414 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
415 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
416 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
417 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
418 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
419 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
420 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
421 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
422 ($i:expr, =>) => { call!($i, <$crate::token::Rocket as $crate::synom::Synom>::parse) };
423 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
424 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
425 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
426 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
427 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
428 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
429 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
430 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
431 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432}
433
David Tolnay0fbe3282017-12-26 21:46:16 -0500434#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435#[macro_export]
436macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500437 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
438 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
439 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
440 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
441 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
442 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
443 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
444 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
445 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
446 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
447 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
448 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
449 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
450 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
451 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
452 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
453 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
454 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
455 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
456 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
457 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
458 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
459 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
460 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
461 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
462 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
463 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
464 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
465 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
466 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
467 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
468 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
469 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
470 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
471 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
472 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
473 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
474 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
475 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
476 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
477 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
478 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800479}
480
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700481#[cfg(feature = "parsing")]
482mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500483 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700484
David Tolnaydfc886b2018-01-06 08:03:09 -0800485 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500486 use parse_error;
487 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700488
489 pub trait FromSpans: Sized {
490 fn from_spans(spans: &[Span]) -> Self;
491 }
492
493 impl FromSpans for [Span; 1] {
494 fn from_spans(spans: &[Span]) -> Self {
495 [spans[0]]
496 }
497 }
498
499 impl FromSpans for [Span; 2] {
500 fn from_spans(spans: &[Span]) -> Self {
501 [spans[0], spans[1]]
502 }
503 }
504
505 impl FromSpans for [Span; 3] {
506 fn from_spans(spans: &[Span]) -> Self {
507 [spans[0], spans[1], spans[2]]
508 }
509 }
510
David Tolnay73c98de2017-12-31 15:56:56 -0500511 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 -0500512 where
513 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700514 {
515 let mut spans = [Span::default(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700516 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700517 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700518
Alex Crichton954046c2017-05-30 21:49:42 -0700519 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400520 match tokens.op() {
David Tolnay65729482017-12-31 16:14:50 -0500521 Some((span, op, kind, rest)) if op == ch => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400522 if i != s.len() - 1 {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700523 match kind {
524 Spacing::Joint => {}
525 _ => return parse_error(),
Michael Layzell0a1a6632017-06-02 18:07:43 -0400526 }
527 }
David Tolnay98942562017-12-26 21:24:35 -0500528 *slot = span;
Michael Layzell0a1a6632017-06-02 18:07:43 -0400529 tokens = rest;
Alex Crichton954046c2017-05-30 21:49:42 -0700530 }
David Tolnay51382052017-12-27 13:46:21 -0500531 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700532 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700533 }
David Tolnayf4aa6b42017-12-31 16:40:33 -0500534 Ok((new(T::from_spans(&spans)), tokens))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700535 }
536
David Tolnay73c98de2017-12-31 15:56:56 -0500537 pub fn keyword<'a, T>(keyword: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> {
David Tolnay65729482017-12-31 16:14:50 -0500538 if let Some((span, term, rest)) = tokens.term() {
David Tolnay73c98de2017-12-31 15:56:56 -0500539 if term.as_str() == keyword {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500540 return Ok((new(span), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400541 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700542 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400543 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700544 }
545
David Tolnay51382052017-12-27 13:46:21 -0500546 pub fn delim<'a, F, R, T>(
547 delim: &str,
548 tokens: Cursor<'a>,
549 new: fn(Span) -> T,
550 f: F,
David Tolnay8875fca2017-12-31 13:52:37 -0500551 ) -> PResult<'a, (T, R)>
David Tolnay51382052017-12-27 13:46:21 -0500552 where
553 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700554 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400555 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700556 let delim = match delim {
557 "(" => Delimiter::Parenthesis,
558 "{" => Delimiter::Brace,
559 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400560 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700561 _ => panic!("unknown delimiter: {}", delim),
562 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700563
David Tolnay65729482017-12-31 16:14:50 -0500564 if let Some((inside, span, rest)) = tokens.group(delim) {
565 match f(inside) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500566 Ok((ret, remaining)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400567 if remaining.eof() {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500568 return Ok(((new(span), ret), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400569 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700570 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400571 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700572 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700573 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400574 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700575 }
576}
577
578#[cfg(feature = "printing")]
579mod printing {
David Tolnayf2cfd722017-12-31 18:02:51 -0500580 use proc_macro2::{Delimiter, Spacing, Span, Term, TokenNode, TokenTree};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700581 use quote::Tokens;
582
David Tolnay73c98de2017-12-31 15:56:56 -0500583 pub fn punct(s: &str, spans: &[Span], tokens: &mut Tokens) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700584 assert_eq!(s.len(), spans.len());
585
586 let mut chars = s.chars();
587 let mut spans = spans.iter();
588 let ch = chars.next_back().unwrap();
589 let span = spans.next_back().unwrap();
590 for (ch, span) in chars.zip(spans) {
591 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500592 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700593 kind: TokenNode::Op(ch, Spacing::Joint),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700594 });
595 }
596
597 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500598 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700599 kind: TokenNode::Op(ch, Spacing::Alone),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700600 });
601 }
602
David Tolnay73c98de2017-12-31 15:56:56 -0500603 pub fn keyword(s: &str, span: &Span, tokens: &mut Tokens) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700604 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500605 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700606 kind: TokenNode::Term(Term::intern(s)),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700607 });
608 }
609
610 pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500611 where
612 F: FnOnce(&mut Tokens),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700613 {
David Tolnay00ab6982017-12-31 18:15:06 -0500614 let delim = match s {
615 "(" => Delimiter::Parenthesis,
616 "[" => Delimiter::Bracket,
617 "{" => Delimiter::Brace,
618 " " => Delimiter::None,
619 _ => panic!("unknown delimiter: {}", s),
620 };
621 let mut inner = Tokens::new();
622 f(&mut inner);
623 tokens.append(TokenTree {
624 span: *span,
625 kind: TokenNode::Group(delim, inner.into()),
626 });
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700627 }
628}