blob: 5419fe7b0e10751a7bff17e179bf748a1935ba25 [file] [log] [blame]
Alex Crichton954046c2017-05-30 21:49:42 -07001//! Discrete tokens that can be parsed out by synom.
2//!
3//! This module contains a number of useful tokens like `+=` and `/` along with
4//! keywords like `crate` and such. These structures are used to track the spans
5//! of these tokens and all implment the `ToTokens` and `Synom` traits when the
6//! corresponding feature is activated.
7
David Tolnay98942562017-12-26 21:24:35 -05008use proc_macro2::Span;
Alex Crichton7b9e02f2017-05-30 15:54:33 -07009
10macro_rules! tokens {
11 (
12 ops: {
David Tolnay5a20f632017-12-26 22:11:28 -050013 $($op:tt pub struct $op_name:ident/$len:tt #[$op_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070014 }
15 delim: {
David Tolnay5a20f632017-12-26 22:11:28 -050016 $($delim:tt pub struct $delim_name:ident #[$delim_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070017 }
18 syms: {
David Tolnay5a20f632017-12-26 22:11:28 -050019 $($sym:tt pub struct $sym_name:ident #[$sym_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070020 }
21 ) => (
David Tolnay5a20f632017-12-26 22:11:28 -050022 $(op! { #[$op_doc] $op pub struct $op_name/$len })*
23 $(delim! { #[$delim_doc] $delim pub struct $delim_name })*
24 $(sym! { #[$sym_doc] $sym pub struct $sym_name })*
Alex Crichton7b9e02f2017-05-30 15:54:33 -070025 )
26}
27
28macro_rules! op {
David Tolnay5a20f632017-12-26 22:11:28 -050029 (#[$doc:meta] $s:tt pub struct $name:ident/$len:tt) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070030 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070031 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -050032 #[$doc]
33 pub struct $name(pub [Span; $len]);
Alex Crichton7b9e02f2017-05-30 15:54:33 -070034
David Tolnay0bdb0552017-12-27 21:31:51 -050035 impl $name {
36 pub fn new(span: Span) -> Self {
37 $name([span; $len])
38 }
39 }
40
Nika Layzelld73a3652017-10-24 08:57:05 -040041 #[cfg(feature = "extra-traits")]
42 impl ::std::fmt::Debug for $name {
43 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
David Tolnay98942562017-12-26 21:24:35 -050044 f.write_str(stringify!($name))
Nika Layzelld73a3652017-10-24 08:57:05 -040045 }
46 }
47
David Tolnay98942562017-12-26 21:24:35 -050048 #[cfg(feature = "extra-traits")]
49 impl ::std::cmp::Eq for $name {}
50
51 #[cfg(feature = "extra-traits")]
52 impl ::std::cmp::PartialEq for $name {
53 fn eq(&self, _other: &$name) -> bool {
54 true
55 }
56 }
57
58 #[cfg(feature = "extra-traits")]
59 impl ::std::hash::Hash for $name {
60 fn hash<H>(&self, _state: &mut H)
61 where H: ::std::hash::Hasher
62 {}
63 }
64
Alex Crichton7b9e02f2017-05-30 15:54:33 -070065 #[cfg(feature = "printing")]
66 impl ::quote::ToTokens for $name {
67 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
68 printing::op($s, &self.0, tokens);
69 }
70 }
71
72 #[cfg(feature = "parsing")]
73 impl ::Synom for $name {
David Tolnay203557a2017-12-27 23:59:33 -050074 fn parse(tokens: $crate::synom::Cursor) -> $crate::synom::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070075 parsing::op($s, tokens, $name)
76 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -080077
78 fn description() -> Option<&'static str> {
79 Some(concat!("`", $s, "`"))
80 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -070081 }
82 }
83}
84
85macro_rules! sym {
David Tolnay5a20f632017-12-26 22:11:28 -050086 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070087 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070088 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -050089 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070090 pub struct $name(pub Span);
91
David Tolnay98942562017-12-26 21:24:35 -050092 #[cfg(feature = "extra-traits")]
93 impl ::std::fmt::Debug for $name {
94 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
95 f.write_str(stringify!($name))
96 }
97 }
98
99 #[cfg(feature = "extra-traits")]
100 impl ::std::cmp::Eq for $name {}
101
102 #[cfg(feature = "extra-traits")]
103 impl ::std::cmp::PartialEq for $name {
104 fn eq(&self, _other: &$name) -> bool {
105 true
106 }
107 }
108
109 #[cfg(feature = "extra-traits")]
110 impl ::std::hash::Hash for $name {
111 fn hash<H>(&self, _state: &mut H)
112 where H: ::std::hash::Hasher
113 {}
114 }
115
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700116 #[cfg(feature = "printing")]
117 impl ::quote::ToTokens for $name {
118 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
119 printing::sym($s, &self.0, tokens);
120 }
121 }
122
123 #[cfg(feature = "parsing")]
124 impl ::Synom for $name {
David Tolnay203557a2017-12-27 23:59:33 -0500125 fn parse(tokens: $crate::synom::Cursor) -> $crate::synom::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700126 parsing::sym($s, tokens, $name)
127 }
128 }
129 }
130}
131
132macro_rules! delim {
David Tolnay5a20f632017-12-26 22:11:28 -0500133 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700134 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700135 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500136 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700137 pub struct $name(pub Span);
138
David Tolnay98942562017-12-26 21:24:35 -0500139 #[cfg(feature = "extra-traits")]
140 impl ::std::fmt::Debug for $name {
141 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
142 f.write_str(stringify!($name))
143 }
144 }
145
146 #[cfg(feature = "extra-traits")]
147 impl ::std::cmp::Eq for $name {}
148
149 #[cfg(feature = "extra-traits")]
150 impl ::std::cmp::PartialEq for $name {
151 fn eq(&self, _other: &$name) -> bool {
152 true
153 }
154 }
155
156 #[cfg(feature = "extra-traits")]
157 impl ::std::hash::Hash for $name {
158 fn hash<H>(&self, _state: &mut H)
159 where H: ::std::hash::Hasher
160 {}
161 }
162
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700163 impl $name {
164 #[cfg(feature = "printing")]
165 pub fn surround<F>(&self,
166 tokens: &mut ::quote::Tokens,
167 f: F)
168 where F: FnOnce(&mut ::quote::Tokens)
169 {
170 printing::delim($s, &self.0, tokens, f);
171 }
172
173 #[cfg(feature = "parsing")]
David Tolnay203557a2017-12-27 23:59:33 -0500174 pub fn parse<F, R>(tokens: $crate::synom::Cursor, f: F) -> $crate::synom::PResult<(R, $name)>
175 where F: FnOnce($crate::synom::Cursor) -> $crate::synom::PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700176 {
177 parsing::delim($s, tokens, $name, f)
178 }
179 }
180 }
181}
182
183tokens! {
184 ops: {
David Tolnay5a20f632017-12-26 22:11:28 -0500185 "+" pub struct Add/1 /// `+`
186 "+=" pub struct AddEq/2 /// `+=`
187 "&" pub struct And/1 /// `&`
188 "&&" pub struct AndAnd/2 /// `&&`
189 "&=" pub struct AndEq/2 /// `&=`
190 "@" pub struct At/1 /// `@`
191 "!" pub struct Bang/1 /// `!`
192 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500193 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500194 ":" pub struct Colon/1 /// `:`
195 "::" pub struct Colon2/2 /// `::`
196 "," pub struct Comma/1 /// `,`
197 "/" pub struct Div/1 /// `/`
198 "/=" pub struct DivEq/2 /// `/=`
199 "." pub struct Dot/1 /// `.`
200 ".." pub struct Dot2/2 /// `..`
201 "..." pub struct Dot3/3 /// `...`
202 "..=" pub struct DotDotEq/3 /// `..=`
203 "=" pub struct Eq/1 /// `=`
204 "==" pub struct EqEq/2 /// `==`
205 ">=" pub struct Ge/2 /// `>=`
206 ">" pub struct Gt/1 /// `>`
207 "<=" pub struct Le/2 /// `<=`
208 "<" pub struct Lt/1 /// `<`
209 "*=" pub struct MulEq/2 /// `*=`
210 "!=" pub struct Ne/2 /// `!=`
211 "|" pub struct Or/1 /// `|`
212 "|=" pub struct OrEq/2 /// `|=`
213 "||" pub struct OrOr/2 /// `||`
214 "#" pub struct Pound/1 /// `#`
215 "?" pub struct Question/1 /// `?`
216 "->" pub struct RArrow/2 /// `->`
217 "<-" pub struct LArrow/2 /// `<-`
218 "%" pub struct Rem/1 /// `%`
219 "%=" pub struct RemEq/2 /// `%=`
220 "=>" pub struct Rocket/2 /// `=>`
221 ";" pub struct Semi/1 /// `;`
222 "<<" pub struct Shl/2 /// `<<`
223 "<<=" pub struct ShlEq/3 /// `<<=`
224 ">>" pub struct Shr/2 /// `>>`
225 ">>=" pub struct ShrEq/3 /// `>>=`
226 "*" pub struct Star/1 /// `*`
227 "-" pub struct Sub/1 /// `-`
228 "-=" pub struct SubEq/2 /// `-=`
229 "_" pub struct Underscore/1 /// `_`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700230 }
231 delim: {
David Tolnay5a20f632017-12-26 22:11:28 -0500232 "{" pub struct Brace /// `{...}`
233 "[" pub struct Bracket /// `[...]`
234 "(" pub struct Paren /// `(...)`
235 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700236 }
237 syms: {
David Tolnay5a20f632017-12-26 22:11:28 -0500238 "as" pub struct As /// `as`
239 "auto" pub struct Auto /// `auto`
240 "box" pub struct Box /// `box`
241 "break" pub struct Break /// `break`
242 "Self" pub struct CapSelf /// `Self`
243 "catch" pub struct Catch /// `catch`
244 "const" pub struct Const /// `const`
245 "continue" pub struct Continue /// `continue`
246 "crate" pub struct Crate /// `crate`
247 "default" pub struct Default /// `default`
248 "do" pub struct Do /// `do`
249 "dyn" pub struct Dyn /// `dyn`
250 "else" pub struct Else /// `else`
251 "enum" pub struct Enum /// `enum`
252 "extern" pub struct Extern /// `extern`
253 "fn" pub struct Fn /// `fn`
254 "for" pub struct For /// `for`
255 "if" pub struct If /// `if`
256 "impl" pub struct Impl /// `impl`
257 "in" pub struct In /// `in`
258 "let" pub struct Let /// `let`
259 "loop" pub struct Loop /// `loop`
260 "macro" pub struct Macro /// `macro`
261 "match" pub struct Match /// `match`
262 "mod" pub struct Mod /// `mod`
263 "move" pub struct Move /// `move`
264 "mut" pub struct Mut /// `mut`
265 "pub" pub struct Pub /// `pub`
266 "ref" pub struct Ref /// `ref`
267 "return" pub struct Return /// `return`
268 "self" pub struct Self_ /// `self`
269 "static" pub struct Static /// `static`
270 "struct" pub struct Struct /// `struct`
271 "super" pub struct Super /// `super`
272 "trait" pub struct Trait /// `trait`
273 "type" pub struct Type /// `type`
274 "union" pub struct Union /// `union`
275 "unsafe" pub struct Unsafe /// `unsafe`
276 "use" pub struct Use /// `use`
277 "where" pub struct Where /// `where`
278 "while" pub struct While /// `while`
279 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700280 }
281}
282
David Tolnayf8db7ba2017-11-11 22:52:16 -0800283// Unfortunate duplication due to a rustdoc bug.
284// https://github.com/rust-lang/rust/issues/45939
285#[macro_export]
286macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500287 (+) => { $crate::token::Add };
288 (+=) => { $crate::token::AddEq };
289 (&) => { $crate::token::And };
290 (&&) => { $crate::token::AndAnd };
291 (&=) => { $crate::token::AndEq };
292 (@) => { $crate::token::At };
293 (!) => { $crate::token::Bang };
294 (^) => { $crate::token::Caret };
295 (^=) => { $crate::token::CaretEq };
296 (:) => { $crate::token::Colon };
297 (::) => { $crate::token::Colon2 };
298 (,) => { $crate::token::Comma };
299 (/) => { $crate::token::Div };
300 (/=) => { $crate::token::DivEq };
301 (.) => { $crate::token::Dot };
302 (..) => { $crate::token::Dot2 };
303 (...) => { $crate::token::Dot3 };
304 (..=) => { $crate::token::DotDotEq };
305 (=) => { $crate::token::Eq };
306 (==) => { $crate::token::EqEq };
307 (>=) => { $crate::token::Ge };
308 (>) => { $crate::token::Gt };
309 (<=) => { $crate::token::Le };
310 (<) => { $crate::token::Lt };
311 (*=) => { $crate::token::MulEq };
312 (!=) => { $crate::token::Ne };
313 (|) => { $crate::token::Or };
314 (|=) => { $crate::token::OrEq };
315 (||) => { $crate::token::OrOr };
316 (#) => { $crate::token::Pound };
317 (?) => { $crate::token::Question };
318 (->) => { $crate::token::RArrow };
319 (<-) => { $crate::token::LArrow };
320 (%) => { $crate::token::Rem };
321 (%=) => { $crate::token::RemEq };
322 (=>) => { $crate::token::Rocket };
323 (;) => { $crate::token::Semi };
324 (<<) => { $crate::token::Shl };
325 (<<=) => { $crate::token::ShlEq };
326 (>>) => { $crate::token::Shr };
327 (>>=) => { $crate::token::ShrEq };
328 (*) => { $crate::token::Star };
329 (-) => { $crate::token::Sub };
330 (-=) => { $crate::token::SubEq };
331 (_) => { $crate::token::Underscore };
332 (as) => { $crate::token::As };
333 (auto) => { $crate::token::Auto };
334 (box) => { $crate::token::Box };
335 (break) => { $crate::token::Break };
336 (Self) => { $crate::token::CapSelf };
337 (catch) => { $crate::token::Catch };
338 (const) => { $crate::token::Const };
339 (continue) => { $crate::token::Continue };
340 (crate) => { $crate::token::Crate };
341 (default) => { $crate::token::Default };
342 (do) => { $crate::token::Do };
343 (dyn) => { $crate::token::Dyn };
344 (else) => { $crate::token::Else };
345 (enum) => { $crate::token::Enum };
346 (extern) => { $crate::token::Extern };
347 (fn) => { $crate::token::Fn };
348 (for) => { $crate::token::For };
349 (if) => { $crate::token::If };
350 (impl) => { $crate::token::Impl };
351 (in) => { $crate::token::In };
352 (let) => { $crate::token::Let };
353 (loop) => { $crate::token::Loop };
354 (macro) => { $crate::token::Macro };
355 (match) => { $crate::token::Match };
356 (mod) => { $crate::token::Mod };
357 (move) => { $crate::token::Move };
358 (mut) => { $crate::token::Mut };
359 (pub) => { $crate::token::Pub };
360 (ref) => { $crate::token::Ref };
361 (return) => { $crate::token::Return };
362 (self) => { $crate::token::Self_ };
363 (static) => { $crate::token::Static };
364 (struct) => { $crate::token::Struct };
365 (super) => { $crate::token::Super };
366 (trait) => { $crate::token::Trait };
367 (type) => { $crate::token::Type };
368 (union) => { $crate::token::Union };
369 (unsafe) => { $crate::token::Unsafe };
370 (use) => { $crate::token::Use };
371 (where) => { $crate::token::Where };
372 (while) => { $crate::token::While };
373 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800374}
375
David Tolnay0fbe3282017-12-26 21:46:16 -0500376#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800377#[macro_export]
378macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500379 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
380 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
381 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
382 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
383 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
384 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
385 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
386 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
387 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
388 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
389 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
390 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
391 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
392 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
393 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
394 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
395 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
396 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
397 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
398 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
399 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
400 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
401 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
402 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
403 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
404 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
405 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
406 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
407 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
408 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
409 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
410 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
411 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
412 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
413 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
414 ($i:expr, =>) => { call!($i, <$crate::token::Rocket as $crate::synom::Synom>::parse) };
415 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
416 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
417 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
418 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
419 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
420 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
421 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
422 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
423 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800424}
425
David Tolnay0fbe3282017-12-26 21:46:16 -0500426#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800427#[macro_export]
428macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500429 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
430 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
431 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
432 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
433 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
434 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
435 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
436 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
437 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
438 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
439 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
440 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
441 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
442 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
443 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
444 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
445 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
446 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
447 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
448 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
449 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
450 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
451 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
452 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
453 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
454 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
455 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
456 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
457 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
458 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
459 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
460 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
461 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
462 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
463 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
464 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
465 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
466 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
467 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
468 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
469 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
470 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800471}
472
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700473#[cfg(feature = "parsing")]
474mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500475 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700476
David Tolnayc5ab8c62017-12-26 16:43:39 -0500477 use cursor::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500478 use parse_error;
479 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700480
481 pub trait FromSpans: Sized {
482 fn from_spans(spans: &[Span]) -> Self;
483 }
484
485 impl FromSpans for [Span; 1] {
486 fn from_spans(spans: &[Span]) -> Self {
487 [spans[0]]
488 }
489 }
490
491 impl FromSpans for [Span; 2] {
492 fn from_spans(spans: &[Span]) -> Self {
493 [spans[0], spans[1]]
494 }
495 }
496
497 impl FromSpans for [Span; 3] {
498 fn from_spans(spans: &[Span]) -> Self {
499 [spans[0], spans[1], spans[2]]
500 }
501 }
502
David Tolnay51382052017-12-27 13:46:21 -0500503 pub fn op<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R>
504 where
505 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700506 {
507 let mut spans = [Span::default(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700508 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700509 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700510
Alex Crichton954046c2017-05-30 21:49:42 -0700511 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400512 match tokens.op() {
513 Some((rest, span, c, kind)) if c == ch => {
514 if i != s.len() - 1 {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700515 match kind {
516 Spacing::Joint => {}
517 _ => return parse_error(),
Michael Layzell0a1a6632017-06-02 18:07:43 -0400518 }
519 }
David Tolnay98942562017-12-26 21:24:35 -0500520 *slot = span;
Michael Layzell0a1a6632017-06-02 18:07:43 -0400521 tokens = rest;
Alex Crichton954046c2017-05-30 21:49:42 -0700522 }
David Tolnay51382052017-12-27 13:46:21 -0500523 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700524 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700525 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400526 Ok((tokens, new(T::from_spans(&spans))))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700527 }
528
David Tolnay51382052017-12-27 13:46:21 -0500529 pub fn sym<'a, T>(sym: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400530 if let Some((rest, span, s)) = tokens.word() {
531 if s.as_str() == sym {
David Tolnay98942562017-12-26 21:24:35 -0500532 return Ok((rest, new(span)));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400533 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700534 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400535 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700536 }
537
David Tolnay51382052017-12-27 13:46:21 -0500538 pub fn delim<'a, F, R, T>(
539 delim: &str,
540 tokens: Cursor<'a>,
541 new: fn(Span) -> T,
542 f: F,
543 ) -> PResult<'a, (R, T)>
544 where
545 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700546 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400547 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700548 let delim = match delim {
549 "(" => Delimiter::Parenthesis,
550 "{" => Delimiter::Brace,
551 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400552 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700553 _ => panic!("unknown delimiter: {}", delim),
554 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700555
David Tolnayc10676a2017-12-27 23:42:36 -0500556 if let Some(seqinfo) = tokens.group(delim) {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400557 match f(seqinfo.inside) {
558 Ok((remaining, ret)) => {
559 if remaining.eof() {
David Tolnay98942562017-12-26 21:24:35 -0500560 return Ok((seqinfo.outside, (ret, new(seqinfo.span))));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400561 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700562 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400563 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700564 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700565 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400566 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700567 }
568}
569
570#[cfg(feature = "printing")]
571mod printing {
David Tolnay51382052017-12-27 13:46:21 -0500572 use proc_macro2::{Spacing, Span, Term, TokenNode, TokenTree};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700573 use quote::Tokens;
574
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700575 pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) {
576 assert_eq!(s.len(), spans.len());
577
578 let mut chars = s.chars();
579 let mut spans = spans.iter();
580 let ch = chars.next_back().unwrap();
581 let span = spans.next_back().unwrap();
582 for (ch, span) in chars.zip(spans) {
583 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500584 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700585 kind: TokenNode::Op(ch, Spacing::Joint),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700586 });
587 }
588
589 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500590 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700591 kind: TokenNode::Op(ch, Spacing::Alone),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700592 });
593 }
594
595 pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) {
596 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500597 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700598 kind: TokenNode::Term(Term::intern(s)),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700599 });
600 }
601
602 pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500603 where
604 F: FnOnce(&mut Tokens),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700605 {
David Tolnay98942562017-12-26 21:24:35 -0500606 tokens.append_delimited(s, *span, f)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700607 }
608}