blob: 8c2f2d08fb45e139d0245b10629da63d387d4418 [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 Tolnayc5ab8c62017-12-26 16:43:39 -050074 fn parse(tokens: $crate::synom::Cursor) -> $crate::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070075 parsing::op($s, tokens, $name)
76 }
77 }
78 }
79}
80
81macro_rules! sym {
David Tolnay5a20f632017-12-26 22:11:28 -050082 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070083 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070084 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -050085 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070086 pub struct $name(pub Span);
87
David Tolnay98942562017-12-26 21:24:35 -050088 #[cfg(feature = "extra-traits")]
89 impl ::std::fmt::Debug for $name {
90 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
91 f.write_str(stringify!($name))
92 }
93 }
94
95 #[cfg(feature = "extra-traits")]
96 impl ::std::cmp::Eq for $name {}
97
98 #[cfg(feature = "extra-traits")]
99 impl ::std::cmp::PartialEq for $name {
100 fn eq(&self, _other: &$name) -> bool {
101 true
102 }
103 }
104
105 #[cfg(feature = "extra-traits")]
106 impl ::std::hash::Hash for $name {
107 fn hash<H>(&self, _state: &mut H)
108 where H: ::std::hash::Hasher
109 {}
110 }
111
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700112 #[cfg(feature = "printing")]
113 impl ::quote::ToTokens for $name {
114 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
115 printing::sym($s, &self.0, tokens);
116 }
117 }
118
119 #[cfg(feature = "parsing")]
120 impl ::Synom for $name {
David Tolnayc5ab8c62017-12-26 16:43:39 -0500121 fn parse(tokens: $crate::synom::Cursor) -> $crate::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700122 parsing::sym($s, tokens, $name)
123 }
124 }
125 }
126}
127
128macro_rules! delim {
David Tolnay5a20f632017-12-26 22:11:28 -0500129 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700130 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700131 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500132 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700133 pub struct $name(pub Span);
134
David Tolnay98942562017-12-26 21:24:35 -0500135 #[cfg(feature = "extra-traits")]
136 impl ::std::fmt::Debug for $name {
137 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
138 f.write_str(stringify!($name))
139 }
140 }
141
142 #[cfg(feature = "extra-traits")]
143 impl ::std::cmp::Eq for $name {}
144
145 #[cfg(feature = "extra-traits")]
146 impl ::std::cmp::PartialEq for $name {
147 fn eq(&self, _other: &$name) -> bool {
148 true
149 }
150 }
151
152 #[cfg(feature = "extra-traits")]
153 impl ::std::hash::Hash for $name {
154 fn hash<H>(&self, _state: &mut H)
155 where H: ::std::hash::Hasher
156 {}
157 }
158
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700159 impl $name {
160 #[cfg(feature = "printing")]
161 pub fn surround<F>(&self,
162 tokens: &mut ::quote::Tokens,
163 f: F)
164 where F: FnOnce(&mut ::quote::Tokens)
165 {
166 printing::delim($s, &self.0, tokens, f);
167 }
168
169 #[cfg(feature = "parsing")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500170 pub fn parse<F, R>(tokens: $crate::synom::Cursor, f: F) -> $crate::PResult<(R, $name)>
171 where F: FnOnce($crate::synom::Cursor) -> $crate::PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700172 {
173 parsing::delim($s, tokens, $name, f)
174 }
175 }
176 }
177}
178
179tokens! {
180 ops: {
David Tolnay5a20f632017-12-26 22:11:28 -0500181 "+" pub struct Add/1 /// `+`
182 "+=" pub struct AddEq/2 /// `+=`
183 "&" pub struct And/1 /// `&`
184 "&&" pub struct AndAnd/2 /// `&&`
185 "&=" pub struct AndEq/2 /// `&=`
186 "@" pub struct At/1 /// `@`
187 "!" pub struct Bang/1 /// `!`
188 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500189 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500190 ":" pub struct Colon/1 /// `:`
191 "::" pub struct Colon2/2 /// `::`
192 "," pub struct Comma/1 /// `,`
193 "/" pub struct Div/1 /// `/`
194 "/=" pub struct DivEq/2 /// `/=`
195 "." pub struct Dot/1 /// `.`
196 ".." pub struct Dot2/2 /// `..`
197 "..." pub struct Dot3/3 /// `...`
198 "..=" pub struct DotDotEq/3 /// `..=`
199 "=" pub struct Eq/1 /// `=`
200 "==" pub struct EqEq/2 /// `==`
201 ">=" pub struct Ge/2 /// `>=`
202 ">" pub struct Gt/1 /// `>`
203 "<=" pub struct Le/2 /// `<=`
204 "<" pub struct Lt/1 /// `<`
205 "*=" pub struct MulEq/2 /// `*=`
206 "!=" pub struct Ne/2 /// `!=`
207 "|" pub struct Or/1 /// `|`
208 "|=" pub struct OrEq/2 /// `|=`
209 "||" pub struct OrOr/2 /// `||`
210 "#" pub struct Pound/1 /// `#`
211 "?" pub struct Question/1 /// `?`
212 "->" pub struct RArrow/2 /// `->`
213 "<-" pub struct LArrow/2 /// `<-`
214 "%" pub struct Rem/1 /// `%`
215 "%=" pub struct RemEq/2 /// `%=`
216 "=>" pub struct Rocket/2 /// `=>`
217 ";" pub struct Semi/1 /// `;`
218 "<<" pub struct Shl/2 /// `<<`
219 "<<=" pub struct ShlEq/3 /// `<<=`
220 ">>" pub struct Shr/2 /// `>>`
221 ">>=" pub struct ShrEq/3 /// `>>=`
222 "*" pub struct Star/1 /// `*`
223 "-" pub struct Sub/1 /// `-`
224 "-=" pub struct SubEq/2 /// `-=`
225 "_" pub struct Underscore/1 /// `_`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700226 }
227 delim: {
David Tolnay5a20f632017-12-26 22:11:28 -0500228 "{" pub struct Brace /// `{...}`
229 "[" pub struct Bracket /// `[...]`
230 "(" pub struct Paren /// `(...)`
231 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700232 }
233 syms: {
David Tolnay5a20f632017-12-26 22:11:28 -0500234 "as" pub struct As /// `as`
235 "auto" pub struct Auto /// `auto`
236 "box" pub struct Box /// `box`
237 "break" pub struct Break /// `break`
238 "Self" pub struct CapSelf /// `Self`
239 "catch" pub struct Catch /// `catch`
240 "const" pub struct Const /// `const`
241 "continue" pub struct Continue /// `continue`
242 "crate" pub struct Crate /// `crate`
243 "default" pub struct Default /// `default`
244 "do" pub struct Do /// `do`
245 "dyn" pub struct Dyn /// `dyn`
246 "else" pub struct Else /// `else`
247 "enum" pub struct Enum /// `enum`
248 "extern" pub struct Extern /// `extern`
249 "fn" pub struct Fn /// `fn`
250 "for" pub struct For /// `for`
251 "if" pub struct If /// `if`
252 "impl" pub struct Impl /// `impl`
253 "in" pub struct In /// `in`
254 "let" pub struct Let /// `let`
255 "loop" pub struct Loop /// `loop`
256 "macro" pub struct Macro /// `macro`
257 "match" pub struct Match /// `match`
258 "mod" pub struct Mod /// `mod`
259 "move" pub struct Move /// `move`
260 "mut" pub struct Mut /// `mut`
261 "pub" pub struct Pub /// `pub`
262 "ref" pub struct Ref /// `ref`
263 "return" pub struct Return /// `return`
264 "self" pub struct Self_ /// `self`
265 "static" pub struct Static /// `static`
266 "struct" pub struct Struct /// `struct`
267 "super" pub struct Super /// `super`
268 "trait" pub struct Trait /// `trait`
269 "type" pub struct Type /// `type`
270 "union" pub struct Union /// `union`
271 "unsafe" pub struct Unsafe /// `unsafe`
272 "use" pub struct Use /// `use`
273 "where" pub struct Where /// `where`
274 "while" pub struct While /// `while`
275 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700276 }
277}
278
David Tolnayf8db7ba2017-11-11 22:52:16 -0800279// Unfortunate duplication due to a rustdoc bug.
280// https://github.com/rust-lang/rust/issues/45939
281#[macro_export]
282macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500283 (+) => { $crate::token::Add };
284 (+=) => { $crate::token::AddEq };
285 (&) => { $crate::token::And };
286 (&&) => { $crate::token::AndAnd };
287 (&=) => { $crate::token::AndEq };
288 (@) => { $crate::token::At };
289 (!) => { $crate::token::Bang };
290 (^) => { $crate::token::Caret };
291 (^=) => { $crate::token::CaretEq };
292 (:) => { $crate::token::Colon };
293 (::) => { $crate::token::Colon2 };
294 (,) => { $crate::token::Comma };
295 (/) => { $crate::token::Div };
296 (/=) => { $crate::token::DivEq };
297 (.) => { $crate::token::Dot };
298 (..) => { $crate::token::Dot2 };
299 (...) => { $crate::token::Dot3 };
300 (..=) => { $crate::token::DotDotEq };
301 (=) => { $crate::token::Eq };
302 (==) => { $crate::token::EqEq };
303 (>=) => { $crate::token::Ge };
304 (>) => { $crate::token::Gt };
305 (<=) => { $crate::token::Le };
306 (<) => { $crate::token::Lt };
307 (*=) => { $crate::token::MulEq };
308 (!=) => { $crate::token::Ne };
309 (|) => { $crate::token::Or };
310 (|=) => { $crate::token::OrEq };
311 (||) => { $crate::token::OrOr };
312 (#) => { $crate::token::Pound };
313 (?) => { $crate::token::Question };
314 (->) => { $crate::token::RArrow };
315 (<-) => { $crate::token::LArrow };
316 (%) => { $crate::token::Rem };
317 (%=) => { $crate::token::RemEq };
318 (=>) => { $crate::token::Rocket };
319 (;) => { $crate::token::Semi };
320 (<<) => { $crate::token::Shl };
321 (<<=) => { $crate::token::ShlEq };
322 (>>) => { $crate::token::Shr };
323 (>>=) => { $crate::token::ShrEq };
324 (*) => { $crate::token::Star };
325 (-) => { $crate::token::Sub };
326 (-=) => { $crate::token::SubEq };
327 (_) => { $crate::token::Underscore };
328 (as) => { $crate::token::As };
329 (auto) => { $crate::token::Auto };
330 (box) => { $crate::token::Box };
331 (break) => { $crate::token::Break };
332 (Self) => { $crate::token::CapSelf };
333 (catch) => { $crate::token::Catch };
334 (const) => { $crate::token::Const };
335 (continue) => { $crate::token::Continue };
336 (crate) => { $crate::token::Crate };
337 (default) => { $crate::token::Default };
338 (do) => { $crate::token::Do };
339 (dyn) => { $crate::token::Dyn };
340 (else) => { $crate::token::Else };
341 (enum) => { $crate::token::Enum };
342 (extern) => { $crate::token::Extern };
343 (fn) => { $crate::token::Fn };
344 (for) => { $crate::token::For };
345 (if) => { $crate::token::If };
346 (impl) => { $crate::token::Impl };
347 (in) => { $crate::token::In };
348 (let) => { $crate::token::Let };
349 (loop) => { $crate::token::Loop };
350 (macro) => { $crate::token::Macro };
351 (match) => { $crate::token::Match };
352 (mod) => { $crate::token::Mod };
353 (move) => { $crate::token::Move };
354 (mut) => { $crate::token::Mut };
355 (pub) => { $crate::token::Pub };
356 (ref) => { $crate::token::Ref };
357 (return) => { $crate::token::Return };
358 (self) => { $crate::token::Self_ };
359 (static) => { $crate::token::Static };
360 (struct) => { $crate::token::Struct };
361 (super) => { $crate::token::Super };
362 (trait) => { $crate::token::Trait };
363 (type) => { $crate::token::Type };
364 (union) => { $crate::token::Union };
365 (unsafe) => { $crate::token::Unsafe };
366 (use) => { $crate::token::Use };
367 (where) => { $crate::token::Where };
368 (while) => { $crate::token::While };
369 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370}
371
David Tolnay0fbe3282017-12-26 21:46:16 -0500372#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800373#[macro_export]
374macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500375 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
376 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
377 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
378 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
379 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
380 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
381 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
382 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
383 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
384 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
385 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
386 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
387 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
388 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
389 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
390 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
391 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
392 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
393 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
394 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
395 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
396 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
397 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
398 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
399 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
400 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
401 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
402 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
403 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
404 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
405 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
406 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
407 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
408 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
409 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
410 ($i:expr, =>) => { call!($i, <$crate::token::Rocket as $crate::synom::Synom>::parse) };
411 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
412 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
413 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
414 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
415 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
416 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
417 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
418 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
419 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800420}
421
David Tolnay0fbe3282017-12-26 21:46:16 -0500422#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800423#[macro_export]
424macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500425 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
426 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
427 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
428 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
429 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
430 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
431 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
432 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
433 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
434 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
435 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
436 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
437 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
438 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
439 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
440 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
441 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
442 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
443 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
444 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
445 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
446 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
447 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
448 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
449 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
450 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
451 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
452 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
453 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
454 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
455 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
456 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
457 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
458 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
459 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
460 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
461 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
462 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
463 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
464 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
465 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
466 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800467}
468
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700469#[cfg(feature = "parsing")]
470mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500471 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700472
David Tolnayc5ab8c62017-12-26 16:43:39 -0500473 use cursor::Cursor;
David Tolnay51382052017-12-27 13:46:21 -0500474 use {parse_error, PResult};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700475
476 pub trait FromSpans: Sized {
477 fn from_spans(spans: &[Span]) -> Self;
478 }
479
480 impl FromSpans for [Span; 1] {
481 fn from_spans(spans: &[Span]) -> Self {
482 [spans[0]]
483 }
484 }
485
486 impl FromSpans for [Span; 2] {
487 fn from_spans(spans: &[Span]) -> Self {
488 [spans[0], spans[1]]
489 }
490 }
491
492 impl FromSpans for [Span; 3] {
493 fn from_spans(spans: &[Span]) -> Self {
494 [spans[0], spans[1], spans[2]]
495 }
496 }
497
David Tolnay51382052017-12-27 13:46:21 -0500498 pub fn op<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R>
499 where
500 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700501 {
502 let mut spans = [Span::default(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700503 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700504 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700505
Alex Crichton954046c2017-05-30 21:49:42 -0700506 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400507 match tokens.op() {
508 Some((rest, span, c, kind)) if c == ch => {
509 if i != s.len() - 1 {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700510 match kind {
511 Spacing::Joint => {}
512 _ => return parse_error(),
Michael Layzell0a1a6632017-06-02 18:07:43 -0400513 }
514 }
David Tolnay98942562017-12-26 21:24:35 -0500515 *slot = span;
Michael Layzell0a1a6632017-06-02 18:07:43 -0400516 tokens = rest;
Alex Crichton954046c2017-05-30 21:49:42 -0700517 }
David Tolnay51382052017-12-27 13:46:21 -0500518 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700519 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700520 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400521 Ok((tokens, new(T::from_spans(&spans))))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700522 }
523
David Tolnay51382052017-12-27 13:46:21 -0500524 pub fn sym<'a, T>(sym: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400525 if let Some((rest, span, s)) = tokens.word() {
526 if s.as_str() == sym {
David Tolnay98942562017-12-26 21:24:35 -0500527 return Ok((rest, new(span)));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400528 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700529 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400530 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700531 }
532
David Tolnay51382052017-12-27 13:46:21 -0500533 pub fn delim<'a, F, R, T>(
534 delim: &str,
535 tokens: Cursor<'a>,
536 new: fn(Span) -> T,
537 f: F,
538 ) -> PResult<'a, (R, T)>
539 where
540 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700541 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400542 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700543 let delim = match delim {
544 "(" => Delimiter::Parenthesis,
545 "{" => Delimiter::Brace,
546 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400547 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700548 _ => panic!("unknown delimiter: {}", delim),
549 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700550
Michael Layzell0a1a6632017-06-02 18:07:43 -0400551 if let Some(seqinfo) = tokens.seq(delim) {
552 match f(seqinfo.inside) {
553 Ok((remaining, ret)) => {
554 if remaining.eof() {
David Tolnay98942562017-12-26 21:24:35 -0500555 return Ok((seqinfo.outside, (ret, new(seqinfo.span))));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400556 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700557 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400558 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700559 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700560 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400561 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700562 }
563}
564
565#[cfg(feature = "printing")]
566mod printing {
David Tolnay51382052017-12-27 13:46:21 -0500567 use proc_macro2::{Spacing, Span, Term, TokenNode, TokenTree};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700568 use quote::Tokens;
569
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700570 pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) {
571 assert_eq!(s.len(), spans.len());
572
573 let mut chars = s.chars();
574 let mut spans = spans.iter();
575 let ch = chars.next_back().unwrap();
576 let span = spans.next_back().unwrap();
577 for (ch, span) in chars.zip(spans) {
578 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500579 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700580 kind: TokenNode::Op(ch, Spacing::Joint),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700581 });
582 }
583
584 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500585 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700586 kind: TokenNode::Op(ch, Spacing::Alone),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700587 });
588 }
589
590 pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) {
591 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500592 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700593 kind: TokenNode::Term(Term::intern(s)),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700594 });
595 }
596
597 pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500598 where
599 F: FnOnce(&mut Tokens),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700600 {
David Tolnay98942562017-12-26 21:24:35 -0500601 tokens.append_delimited(s, *span, f)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700602 }
603}