David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // 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 Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 9 | //! 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 Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 16 | use proc_macro2::Span; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 17 | |
| 18 | macro_rules! tokens { |
| 19 | ( |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 20 | punct: { |
| 21 | $($punct:tt pub struct $punct_name:ident/$len:tt #[$punct_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 22 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 23 | delimiter: { |
| 24 | $($delimiter:tt pub struct $delimiter_name:ident #[$delimiter_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 25 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 26 | keyword: { |
| 27 | $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 28 | } |
| 29 | ) => ( |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 30 | $(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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 33 | ) |
| 34 | } |
| 35 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 36 | macro_rules! token_punct { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 37 | (#[$doc:meta] $s:tt pub struct $name:ident/$len:tt) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 38 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 39 | #[derive(Default)] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 40 | #[$doc] |
| 41 | pub struct $name(pub [Span; $len]); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 42 | |
David Tolnay | 0bdb055 | 2017-12-27 21:31:51 -0500 | [diff] [blame] | 43 | impl $name { |
| 44 | pub fn new(span: Span) -> Self { |
| 45 | $name([span; $len]) |
| 46 | } |
| 47 | } |
| 48 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 49 | #[cfg(feature = "extra-traits")] |
| 50 | impl ::std::fmt::Debug for $name { |
| 51 | fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 52 | f.write_str(stringify!($name)) |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 56 | #[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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 73 | #[cfg(feature = "printing")] |
| 74 | impl ::quote::ToTokens for $name { |
| 75 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 76 | printing::punct($s, &self.0, tokens); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | #[cfg(feature = "parsing")] |
| 81 | impl ::Synom for $name { |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame^] | 82 | fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 83 | parsing::punct($s, tokens, $name) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 84 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 85 | |
| 86 | fn description() -> Option<&'static str> { |
| 87 | Some(concat!("`", $s, "`")) |
| 88 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 93 | macro_rules! token_keyword { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 94 | (#[$doc:meta] $s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 95 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 96 | #[derive(Default)] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 97 | #[$doc] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 98 | pub struct $name(pub Span); |
| 99 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 100 | #[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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 124 | #[cfg(feature = "printing")] |
| 125 | impl ::quote::ToTokens for $name { |
| 126 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 127 | printing::keyword($s, &self.0, tokens); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | #[cfg(feature = "parsing")] |
| 132 | impl ::Synom for $name { |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame^] | 133 | fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 134 | parsing::keyword($s, tokens, $name) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 140 | macro_rules! token_delimiter { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 141 | (#[$doc:meta] $s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 142 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 143 | #[derive(Default)] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 144 | #[$doc] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 145 | pub struct $name(pub Span); |
| 146 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 147 | #[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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 171 | 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 Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame^] | 182 | 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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 184 | { |
| 185 | parsing::delim($s, tokens, $name, f) |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | tokens! { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 192 | punct: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 193 | "+" 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 Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 201 | "^=" pub struct CaretEq/2 /// `^=` |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 202 | ":" 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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 238 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 239 | delimiter: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 240 | "{" pub struct Brace /// `{...}` |
| 241 | "[" pub struct Bracket /// `[...]` |
| 242 | "(" pub struct Paren /// `(...)` |
| 243 | " " pub struct Group /// None-delimited group |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 244 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 245 | keyword: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 246 | "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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 291 | // Unfortunate duplication due to a rustdoc bug. |
| 292 | // https://github.com/rust-lang/rust/issues/45939 |
| 293 | #[macro_export] |
| 294 | macro_rules! Token { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 295 | (+) => { $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 Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 382 | } |
| 383 | |
David Tolnay | 0fbe328 | 2017-12-26 21:46:16 -0500 | [diff] [blame] | 384 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 385 | #[macro_export] |
| 386 | macro_rules! punct { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 387 | ($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 Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 432 | } |
| 433 | |
David Tolnay | 0fbe328 | 2017-12-26 21:46:16 -0500 | [diff] [blame] | 434 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 435 | #[macro_export] |
| 436 | macro_rules! keyword { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 437 | ($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 Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 481 | #[cfg(feature = "parsing")] |
| 482 | mod parsing { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 483 | use proc_macro2::{Delimiter, Spacing, Span}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 484 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame^] | 485 | use buffer::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 486 | use parse_error; |
| 487 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 488 | |
| 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 Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 511 | pub fn punct<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 512 | where |
| 513 | T: FromSpans, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 514 | { |
| 515 | let mut spans = [Span::default(); 3]; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 516 | assert!(s.len() <= spans.len()); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 517 | let chars = s.chars(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 518 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 519 | for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 520 | match tokens.op() { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 521 | Some((span, op, kind, rest)) if op == ch => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 522 | if i != s.len() - 1 { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 523 | match kind { |
| 524 | Spacing::Joint => {} |
| 525 | _ => return parse_error(), |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 526 | } |
| 527 | } |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 528 | *slot = span; |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 529 | tokens = rest; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 530 | } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 531 | _ => return parse_error(), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 532 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 533 | } |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 534 | Ok((new(T::from_spans(&spans)), tokens)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 535 | } |
| 536 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 537 | pub fn keyword<'a, T>(keyword: &str, tokens: Cursor<'a>, new: fn(Span) -> T) -> PResult<'a, T> { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 538 | if let Some((span, term, rest)) = tokens.term() { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 539 | if term.as_str() == keyword { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 540 | return Ok((new(span), rest)); |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 541 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 542 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 543 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 544 | } |
| 545 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 546 | pub fn delim<'a, F, R, T>( |
| 547 | delim: &str, |
| 548 | tokens: Cursor<'a>, |
| 549 | new: fn(Span) -> T, |
| 550 | f: F, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 551 | ) -> PResult<'a, (T, R)> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 552 | where |
| 553 | F: FnOnce(Cursor) -> PResult<R>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 554 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 555 | // NOTE: We should support none-delimited sequences here. |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 556 | let delim = match delim { |
| 557 | "(" => Delimiter::Parenthesis, |
| 558 | "{" => Delimiter::Brace, |
| 559 | "[" => Delimiter::Bracket, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 560 | " " => Delimiter::None, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 561 | _ => panic!("unknown delimiter: {}", delim), |
| 562 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 563 | |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 564 | if let Some((inside, span, rest)) = tokens.group(delim) { |
| 565 | match f(inside) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 566 | Ok((ret, remaining)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 567 | if remaining.eof() { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 568 | return Ok(((new(span), ret), rest)); |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 569 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 570 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 571 | Err(err) => return Err(err), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 572 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 573 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 574 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
| 578 | #[cfg(feature = "printing")] |
| 579 | mod printing { |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 580 | use proc_macro2::{Delimiter, Spacing, Span, Term, TokenNode, TokenTree}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 581 | use quote::Tokens; |
| 582 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 583 | pub fn punct(s: &str, spans: &[Span], tokens: &mut Tokens) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 584 | 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 Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 592 | span: *span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 593 | kind: TokenNode::Op(ch, Spacing::Joint), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 594 | }); |
| 595 | } |
| 596 | |
| 597 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 598 | span: *span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 599 | kind: TokenNode::Op(ch, Spacing::Alone), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 600 | }); |
| 601 | } |
| 602 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 603 | pub fn keyword(s: &str, span: &Span, tokens: &mut Tokens) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 604 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 605 | span: *span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 606 | kind: TokenNode::Term(Term::intern(s)), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 607 | }); |
| 608 | } |
| 609 | |
| 610 | pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 611 | where |
| 612 | F: FnOnce(&mut Tokens), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 613 | { |
David Tolnay | 00ab698 | 2017-12-31 18:15:06 -0500 | [diff] [blame] | 614 | 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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 627 | } |
| 628 | } |