blob: cfff6f6f732265efff4c36b2e44b00c934e2584a [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
Nika Layzelld73a3652017-10-24 08:57:05 -040035 #[cfg(feature = "extra-traits")]
36 impl ::std::fmt::Debug for $name {
37 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
David Tolnay98942562017-12-26 21:24:35 -050038 f.write_str(stringify!($name))
Nika Layzelld73a3652017-10-24 08:57:05 -040039 }
40 }
41
David Tolnay98942562017-12-26 21:24:35 -050042 #[cfg(feature = "extra-traits")]
43 impl ::std::cmp::Eq for $name {}
44
45 #[cfg(feature = "extra-traits")]
46 impl ::std::cmp::PartialEq for $name {
47 fn eq(&self, _other: &$name) -> bool {
48 true
49 }
50 }
51
52 #[cfg(feature = "extra-traits")]
53 impl ::std::hash::Hash for $name {
54 fn hash<H>(&self, _state: &mut H)
55 where H: ::std::hash::Hasher
56 {}
57 }
58
Alex Crichton7b9e02f2017-05-30 15:54:33 -070059 #[cfg(feature = "printing")]
60 impl ::quote::ToTokens for $name {
61 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
62 printing::op($s, &self.0, tokens);
63 }
64 }
65
66 #[cfg(feature = "parsing")]
67 impl ::Synom for $name {
David Tolnayc5ab8c62017-12-26 16:43:39 -050068 fn parse(tokens: $crate::synom::Cursor) -> $crate::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070069 parsing::op($s, tokens, $name)
70 }
71 }
72 }
73}
74
75macro_rules! sym {
David Tolnay5a20f632017-12-26 22:11:28 -050076 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -070077 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070078 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -050079 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -070080 pub struct $name(pub Span);
81
David Tolnay98942562017-12-26 21:24:35 -050082 #[cfg(feature = "extra-traits")]
83 impl ::std::fmt::Debug for $name {
84 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
85 f.write_str(stringify!($name))
86 }
87 }
88
89 #[cfg(feature = "extra-traits")]
90 impl ::std::cmp::Eq for $name {}
91
92 #[cfg(feature = "extra-traits")]
93 impl ::std::cmp::PartialEq for $name {
94 fn eq(&self, _other: &$name) -> bool {
95 true
96 }
97 }
98
99 #[cfg(feature = "extra-traits")]
100 impl ::std::hash::Hash for $name {
101 fn hash<H>(&self, _state: &mut H)
102 where H: ::std::hash::Hasher
103 {}
104 }
105
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700106 #[cfg(feature = "printing")]
107 impl ::quote::ToTokens for $name {
108 fn to_tokens(&self, tokens: &mut ::quote::Tokens) {
109 printing::sym($s, &self.0, tokens);
110 }
111 }
112
113 #[cfg(feature = "parsing")]
114 impl ::Synom for $name {
David Tolnayc5ab8c62017-12-26 16:43:39 -0500115 fn parse(tokens: $crate::synom::Cursor) -> $crate::PResult<$name> {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700116 parsing::sym($s, tokens, $name)
117 }
118 }
119 }
120}
121
122macro_rules! delim {
David Tolnay5a20f632017-12-26 22:11:28 -0500123 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700124 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700125 #[derive(Default)]
David Tolnay5a20f632017-12-26 22:11:28 -0500126 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700127 pub struct $name(pub Span);
128
David Tolnay98942562017-12-26 21:24:35 -0500129 #[cfg(feature = "extra-traits")]
130 impl ::std::fmt::Debug for $name {
131 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
132 f.write_str(stringify!($name))
133 }
134 }
135
136 #[cfg(feature = "extra-traits")]
137 impl ::std::cmp::Eq for $name {}
138
139 #[cfg(feature = "extra-traits")]
140 impl ::std::cmp::PartialEq for $name {
141 fn eq(&self, _other: &$name) -> bool {
142 true
143 }
144 }
145
146 #[cfg(feature = "extra-traits")]
147 impl ::std::hash::Hash for $name {
148 fn hash<H>(&self, _state: &mut H)
149 where H: ::std::hash::Hasher
150 {}
151 }
152
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700153 impl $name {
154 #[cfg(feature = "printing")]
155 pub fn surround<F>(&self,
156 tokens: &mut ::quote::Tokens,
157 f: F)
158 where F: FnOnce(&mut ::quote::Tokens)
159 {
160 printing::delim($s, &self.0, tokens, f);
161 }
162
163 #[cfg(feature = "parsing")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500164 pub fn parse<F, R>(tokens: $crate::synom::Cursor, f: F) -> $crate::PResult<(R, $name)>
165 where F: FnOnce($crate::synom::Cursor) -> $crate::PResult<R>
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700166 {
167 parsing::delim($s, tokens, $name, f)
168 }
169 }
170 }
171}
172
173tokens! {
174 ops: {
David Tolnay5a20f632017-12-26 22:11:28 -0500175 "+" pub struct Add/1 /// `+`
176 "+=" pub struct AddEq/2 /// `+=`
177 "&" pub struct And/1 /// `&`
178 "&&" pub struct AndAnd/2 /// `&&`
179 "&=" pub struct AndEq/2 /// `&=`
180 "@" pub struct At/1 /// `@`
181 "!" pub struct Bang/1 /// `!`
182 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500183 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500184 ":" pub struct Colon/1 /// `:`
185 "::" pub struct Colon2/2 /// `::`
186 "," pub struct Comma/1 /// `,`
187 "/" pub struct Div/1 /// `/`
188 "/=" pub struct DivEq/2 /// `/=`
189 "." pub struct Dot/1 /// `.`
190 ".." pub struct Dot2/2 /// `..`
191 "..." pub struct Dot3/3 /// `...`
192 "..=" pub struct DotDotEq/3 /// `..=`
193 "=" pub struct Eq/1 /// `=`
194 "==" pub struct EqEq/2 /// `==`
195 ">=" pub struct Ge/2 /// `>=`
196 ">" pub struct Gt/1 /// `>`
197 "<=" pub struct Le/2 /// `<=`
198 "<" pub struct Lt/1 /// `<`
199 "*=" pub struct MulEq/2 /// `*=`
200 "!=" pub struct Ne/2 /// `!=`
201 "|" pub struct Or/1 /// `|`
202 "|=" pub struct OrEq/2 /// `|=`
203 "||" pub struct OrOr/2 /// `||`
204 "#" pub struct Pound/1 /// `#`
205 "?" pub struct Question/1 /// `?`
206 "->" pub struct RArrow/2 /// `->`
207 "<-" pub struct LArrow/2 /// `<-`
208 "%" pub struct Rem/1 /// `%`
209 "%=" pub struct RemEq/2 /// `%=`
210 "=>" pub struct Rocket/2 /// `=>`
211 ";" pub struct Semi/1 /// `;`
212 "<<" pub struct Shl/2 /// `<<`
213 "<<=" pub struct ShlEq/3 /// `<<=`
214 ">>" pub struct Shr/2 /// `>>`
215 ">>=" pub struct ShrEq/3 /// `>>=`
216 "*" pub struct Star/1 /// `*`
217 "-" pub struct Sub/1 /// `-`
218 "-=" pub struct SubEq/2 /// `-=`
219 "_" pub struct Underscore/1 /// `_`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700220 }
221 delim: {
David Tolnay5a20f632017-12-26 22:11:28 -0500222 "{" pub struct Brace /// `{...}`
223 "[" pub struct Bracket /// `[...]`
224 "(" pub struct Paren /// `(...)`
225 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700226 }
227 syms: {
David Tolnay5a20f632017-12-26 22:11:28 -0500228 "as" pub struct As /// `as`
229 "auto" pub struct Auto /// `auto`
230 "box" pub struct Box /// `box`
231 "break" pub struct Break /// `break`
232 "Self" pub struct CapSelf /// `Self`
233 "catch" pub struct Catch /// `catch`
234 "const" pub struct Const /// `const`
235 "continue" pub struct Continue /// `continue`
236 "crate" pub struct Crate /// `crate`
237 "default" pub struct Default /// `default`
238 "do" pub struct Do /// `do`
239 "dyn" pub struct Dyn /// `dyn`
240 "else" pub struct Else /// `else`
241 "enum" pub struct Enum /// `enum`
242 "extern" pub struct Extern /// `extern`
243 "fn" pub struct Fn /// `fn`
244 "for" pub struct For /// `for`
245 "if" pub struct If /// `if`
246 "impl" pub struct Impl /// `impl`
247 "in" pub struct In /// `in`
248 "let" pub struct Let /// `let`
249 "loop" pub struct Loop /// `loop`
250 "macro" pub struct Macro /// `macro`
251 "match" pub struct Match /// `match`
252 "mod" pub struct Mod /// `mod`
253 "move" pub struct Move /// `move`
254 "mut" pub struct Mut /// `mut`
255 "pub" pub struct Pub /// `pub`
256 "ref" pub struct Ref /// `ref`
257 "return" pub struct Return /// `return`
258 "self" pub struct Self_ /// `self`
259 "static" pub struct Static /// `static`
260 "struct" pub struct Struct /// `struct`
261 "super" pub struct Super /// `super`
262 "trait" pub struct Trait /// `trait`
263 "type" pub struct Type /// `type`
264 "union" pub struct Union /// `union`
265 "unsafe" pub struct Unsafe /// `unsafe`
266 "use" pub struct Use /// `use`
267 "where" pub struct Where /// `where`
268 "while" pub struct While /// `while`
269 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700270 }
271}
272
David Tolnayf8db7ba2017-11-11 22:52:16 -0800273// Unfortunate duplication due to a rustdoc bug.
274// https://github.com/rust-lang/rust/issues/45939
275#[macro_export]
276macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500277 (+) => { $crate::token::Add };
278 (+=) => { $crate::token::AddEq };
279 (&) => { $crate::token::And };
280 (&&) => { $crate::token::AndAnd };
281 (&=) => { $crate::token::AndEq };
282 (@) => { $crate::token::At };
283 (!) => { $crate::token::Bang };
284 (^) => { $crate::token::Caret };
285 (^=) => { $crate::token::CaretEq };
286 (:) => { $crate::token::Colon };
287 (::) => { $crate::token::Colon2 };
288 (,) => { $crate::token::Comma };
289 (/) => { $crate::token::Div };
290 (/=) => { $crate::token::DivEq };
291 (.) => { $crate::token::Dot };
292 (..) => { $crate::token::Dot2 };
293 (...) => { $crate::token::Dot3 };
294 (..=) => { $crate::token::DotDotEq };
295 (=) => { $crate::token::Eq };
296 (==) => { $crate::token::EqEq };
297 (>=) => { $crate::token::Ge };
298 (>) => { $crate::token::Gt };
299 (<=) => { $crate::token::Le };
300 (<) => { $crate::token::Lt };
301 (*=) => { $crate::token::MulEq };
302 (!=) => { $crate::token::Ne };
303 (|) => { $crate::token::Or };
304 (|=) => { $crate::token::OrEq };
305 (||) => { $crate::token::OrOr };
306 (#) => { $crate::token::Pound };
307 (?) => { $crate::token::Question };
308 (->) => { $crate::token::RArrow };
309 (<-) => { $crate::token::LArrow };
310 (%) => { $crate::token::Rem };
311 (%=) => { $crate::token::RemEq };
312 (=>) => { $crate::token::Rocket };
313 (;) => { $crate::token::Semi };
314 (<<) => { $crate::token::Shl };
315 (<<=) => { $crate::token::ShlEq };
316 (>>) => { $crate::token::Shr };
317 (>>=) => { $crate::token::ShrEq };
318 (*) => { $crate::token::Star };
319 (-) => { $crate::token::Sub };
320 (-=) => { $crate::token::SubEq };
321 (_) => { $crate::token::Underscore };
322 (as) => { $crate::token::As };
323 (auto) => { $crate::token::Auto };
324 (box) => { $crate::token::Box };
325 (break) => { $crate::token::Break };
326 (Self) => { $crate::token::CapSelf };
327 (catch) => { $crate::token::Catch };
328 (const) => { $crate::token::Const };
329 (continue) => { $crate::token::Continue };
330 (crate) => { $crate::token::Crate };
331 (default) => { $crate::token::Default };
332 (do) => { $crate::token::Do };
333 (dyn) => { $crate::token::Dyn };
334 (else) => { $crate::token::Else };
335 (enum) => { $crate::token::Enum };
336 (extern) => { $crate::token::Extern };
337 (fn) => { $crate::token::Fn };
338 (for) => { $crate::token::For };
339 (if) => { $crate::token::If };
340 (impl) => { $crate::token::Impl };
341 (in) => { $crate::token::In };
342 (let) => { $crate::token::Let };
343 (loop) => { $crate::token::Loop };
344 (macro) => { $crate::token::Macro };
345 (match) => { $crate::token::Match };
346 (mod) => { $crate::token::Mod };
347 (move) => { $crate::token::Move };
348 (mut) => { $crate::token::Mut };
349 (pub) => { $crate::token::Pub };
350 (ref) => { $crate::token::Ref };
351 (return) => { $crate::token::Return };
352 (self) => { $crate::token::Self_ };
353 (static) => { $crate::token::Static };
354 (struct) => { $crate::token::Struct };
355 (super) => { $crate::token::Super };
356 (trait) => { $crate::token::Trait };
357 (type) => { $crate::token::Type };
358 (union) => { $crate::token::Union };
359 (unsafe) => { $crate::token::Unsafe };
360 (use) => { $crate::token::Use };
361 (where) => { $crate::token::Where };
362 (while) => { $crate::token::While };
363 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800364}
365
David Tolnay0fbe3282017-12-26 21:46:16 -0500366#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800367#[macro_export]
368macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500369 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
370 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
371 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
372 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
373 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
374 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
375 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
376 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
377 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
378 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
379 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
380 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
381 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
382 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
383 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
384 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
385 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
386 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
387 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
388 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
389 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
390 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
391 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
392 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
393 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
394 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
395 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
396 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
397 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
398 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
399 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
400 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
401 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
402 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
403 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
404 ($i:expr, =>) => { call!($i, <$crate::token::Rocket as $crate::synom::Synom>::parse) };
405 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
406 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
407 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
408 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
409 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
410 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
411 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
412 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
413 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414}
415
David Tolnay0fbe3282017-12-26 21:46:16 -0500416#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800417#[macro_export]
418macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500419 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
420 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
421 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
422 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
423 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
424 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
425 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
426 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
427 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
428 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
429 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
430 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
431 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
432 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
433 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
434 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
435 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
436 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
437 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
438 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
439 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
440 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
441 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
442 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
443 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
444 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
445 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
446 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
447 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
448 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
449 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
450 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
451 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
452 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
453 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
454 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
455 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
456 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
457 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
458 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
459 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
460 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800461}
462
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700463#[cfg(feature = "parsing")]
464mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500465 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700466
David Tolnayc5ab8c62017-12-26 16:43:39 -0500467 use cursor::Cursor;
David Tolnay51382052017-12-27 13:46:21 -0500468 use {parse_error, PResult};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700469
470 pub trait FromSpans: Sized {
471 fn from_spans(spans: &[Span]) -> Self;
472 }
473
474 impl FromSpans for [Span; 1] {
475 fn from_spans(spans: &[Span]) -> Self {
476 [spans[0]]
477 }
478 }
479
480 impl FromSpans for [Span; 2] {
481 fn from_spans(spans: &[Span]) -> Self {
482 [spans[0], spans[1]]
483 }
484 }
485
486 impl FromSpans for [Span; 3] {
487 fn from_spans(spans: &[Span]) -> Self {
488 [spans[0], spans[1], spans[2]]
489 }
490 }
491
David Tolnay51382052017-12-27 13:46:21 -0500492 pub fn op<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R>
493 where
494 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700495 {
496 let mut spans = [Span::default(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700497 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700498 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700499
Alex Crichton954046c2017-05-30 21:49:42 -0700500 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400501 match tokens.op() {
502 Some((rest, span, c, kind)) if c == ch => {
503 if i != s.len() - 1 {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700504 match kind {
505 Spacing::Joint => {}
506 _ => return parse_error(),
Michael Layzell0a1a6632017-06-02 18:07:43 -0400507 }
508 }
David Tolnay98942562017-12-26 21:24:35 -0500509 *slot = span;
Michael Layzell0a1a6632017-06-02 18:07:43 -0400510 tokens = rest;
Alex Crichton954046c2017-05-30 21:49:42 -0700511 }
David Tolnay51382052017-12-27 13:46:21 -0500512 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700513 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700514 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400515 Ok((tokens, new(T::from_spans(&spans))))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700516 }
517
David Tolnay51382052017-12-27 13:46:21 -0500518 pub fn sym<'a, T>(sym: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400519 if let Some((rest, span, s)) = tokens.word() {
520 if s.as_str() == sym {
David Tolnay98942562017-12-26 21:24:35 -0500521 return Ok((rest, new(span)));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400522 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700523 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400524 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700525 }
526
David Tolnay51382052017-12-27 13:46:21 -0500527 pub fn delim<'a, F, R, T>(
528 delim: &str,
529 tokens: Cursor<'a>,
530 new: fn(Span) -> T,
531 f: F,
532 ) -> PResult<'a, (R, T)>
533 where
534 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700535 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400536 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700537 let delim = match delim {
538 "(" => Delimiter::Parenthesis,
539 "{" => Delimiter::Brace,
540 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400541 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700542 _ => panic!("unknown delimiter: {}", delim),
543 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700544
Michael Layzell0a1a6632017-06-02 18:07:43 -0400545 if let Some(seqinfo) = tokens.seq(delim) {
546 match f(seqinfo.inside) {
547 Ok((remaining, ret)) => {
548 if remaining.eof() {
David Tolnay98942562017-12-26 21:24:35 -0500549 return Ok((seqinfo.outside, (ret, new(seqinfo.span))));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400550 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700551 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400552 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700553 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700554 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400555 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700556 }
557}
558
559#[cfg(feature = "printing")]
560mod printing {
David Tolnay51382052017-12-27 13:46:21 -0500561 use proc_macro2::{Spacing, Span, Term, TokenNode, TokenTree};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700562 use quote::Tokens;
563
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700564 pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) {
565 assert_eq!(s.len(), spans.len());
566
567 let mut chars = s.chars();
568 let mut spans = spans.iter();
569 let ch = chars.next_back().unwrap();
570 let span = spans.next_back().unwrap();
571 for (ch, span) in chars.zip(spans) {
572 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500573 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700574 kind: TokenNode::Op(ch, Spacing::Joint),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700575 });
576 }
577
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::Alone),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700581 });
582 }
583
584 pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) {
585 tokens.append(TokenTree {
David Tolnay98942562017-12-26 21:24:35 -0500586 span: *span,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700587 kind: TokenNode::Term(Term::intern(s)),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700588 });
589 }
590
591 pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500592 where
593 F: FnOnce(&mut Tokens),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700594 {
David Tolnay98942562017-12-26 21:24:35 -0500595 tokens.append_delimited(s, *span, f)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700596 }
597}