Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1 | //! 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 Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 8 | use proc_macro2::Span; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 9 | |
| 10 | macro_rules! tokens { |
| 11 | ( |
| 12 | ops: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 13 | $($op:tt pub struct $op_name:ident/$len:tt #[$op_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 14 | } |
| 15 | delim: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 16 | $($delim:tt pub struct $delim_name:ident #[$delim_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 17 | } |
| 18 | syms: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 19 | $($sym:tt pub struct $sym_name:ident #[$sym_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 20 | } |
| 21 | ) => ( |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 22 | $(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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 25 | ) |
| 26 | } |
| 27 | |
| 28 | macro_rules! op { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 29 | (#[$doc:meta] $s:tt pub struct $name:ident/$len:tt) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 30 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 31 | #[derive(Default)] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 32 | #[$doc] |
| 33 | pub struct $name(pub [Span; $len]); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 34 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 35 | #[cfg(feature = "extra-traits")] |
| 36 | impl ::std::fmt::Debug for $name { |
| 37 | fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 38 | f.write_str(stringify!($name)) |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 42 | #[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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 59 | #[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 Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 68 | fn parse(tokens: $crate::synom::Cursor) -> $crate::PResult<$name> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 69 | parsing::op($s, tokens, $name) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | macro_rules! sym { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 76 | (#[$doc:meta] $s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 77 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 78 | #[derive(Default)] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 79 | #[$doc] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 80 | pub struct $name(pub Span); |
| 81 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 82 | #[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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 106 | #[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 Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 115 | fn parse(tokens: $crate::synom::Cursor) -> $crate::PResult<$name> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 116 | parsing::sym($s, tokens, $name) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | macro_rules! delim { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 123 | (#[$doc:meta] $s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 124 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 125 | #[derive(Default)] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 126 | #[$doc] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 127 | pub struct $name(pub Span); |
| 128 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 129 | #[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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 153 | 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 Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 164 | 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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 166 | { |
| 167 | parsing::delim($s, tokens, $name, f) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | tokens! { |
| 174 | ops: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 175 | "+" 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 /// `^` |
| 183 | "^-" pub struct CaretEq/2 /// `^=` |
| 184 | ":" 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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 220 | } |
| 221 | delim: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 222 | "{" pub struct Brace /// `{...}` |
| 223 | "[" pub struct Bracket /// `[...]` |
| 224 | "(" pub struct Paren /// `(...)` |
| 225 | " " pub struct Group /// None-delimited group |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 226 | } |
| 227 | syms: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame^] | 228 | "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 Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 273 | // Unfortunate duplication due to a rustdoc bug. |
| 274 | // https://github.com/rust-lang/rust/issues/45939 |
| 275 | #[macro_export] |
| 276 | macro_rules! Token { |
| 277 | (+) => { $crate::tokens::Add }; |
| 278 | (+=) => { $crate::tokens::AddEq }; |
| 279 | (&) => { $crate::tokens::And }; |
| 280 | (&&) => { $crate::tokens::AndAnd }; |
| 281 | (&=) => { $crate::tokens::AndEq }; |
| 282 | (@) => { $crate::tokens::At }; |
| 283 | (!) => { $crate::tokens::Bang }; |
| 284 | (^) => { $crate::tokens::Caret }; |
| 285 | (^=) => { $crate::tokens::CaretEq }; |
| 286 | (:) => { $crate::tokens::Colon }; |
| 287 | (::) => { $crate::tokens::Colon2 }; |
| 288 | (,) => { $crate::tokens::Comma }; |
| 289 | (/) => { $crate::tokens::Div }; |
| 290 | (/=) => { $crate::tokens::DivEq }; |
| 291 | (.) => { $crate::tokens::Dot }; |
| 292 | (..) => { $crate::tokens::Dot2 }; |
| 293 | (...) => { $crate::tokens::Dot3 }; |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 294 | (..=) => { $crate::tokens::DotDotEq }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 295 | (=) => { $crate::tokens::Eq }; |
| 296 | (==) => { $crate::tokens::EqEq }; |
| 297 | (>=) => { $crate::tokens::Ge }; |
| 298 | (>) => { $crate::tokens::Gt }; |
| 299 | (<=) => { $crate::tokens::Le }; |
| 300 | (<) => { $crate::tokens::Lt }; |
| 301 | (*=) => { $crate::tokens::MulEq }; |
| 302 | (!=) => { $crate::tokens::Ne }; |
| 303 | (|) => { $crate::tokens::Or }; |
| 304 | (|=) => { $crate::tokens::OrEq }; |
| 305 | (||) => { $crate::tokens::OrOr }; |
| 306 | (#) => { $crate::tokens::Pound }; |
| 307 | (?) => { $crate::tokens::Question }; |
| 308 | (->) => { $crate::tokens::RArrow }; |
| 309 | (<-) => { $crate::tokens::LArrow }; |
| 310 | (%) => { $crate::tokens::Rem }; |
| 311 | (%=) => { $crate::tokens::RemEq }; |
| 312 | (=>) => { $crate::tokens::Rocket }; |
| 313 | (;) => { $crate::tokens::Semi }; |
| 314 | (<<) => { $crate::tokens::Shl }; |
| 315 | (<<=) => { $crate::tokens::ShlEq }; |
| 316 | (>>) => { $crate::tokens::Shr }; |
| 317 | (>>=) => { $crate::tokens::ShrEq }; |
| 318 | (*) => { $crate::tokens::Star }; |
| 319 | (-) => { $crate::tokens::Sub }; |
| 320 | (-=) => { $crate::tokens::SubEq }; |
| 321 | (_) => { $crate::tokens::Underscore }; |
| 322 | (as) => { $crate::tokens::As }; |
Nika Layzell | 0dc6e63 | 2017-11-18 12:55:25 -0500 | [diff] [blame] | 323 | (auto) => { $crate::tokens::Auto }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 324 | (box) => { $crate::tokens::Box }; |
| 325 | (break) => { $crate::tokens::Break }; |
| 326 | (Self) => { $crate::tokens::CapSelf }; |
| 327 | (catch) => { $crate::tokens::Catch }; |
| 328 | (const) => { $crate::tokens::Const }; |
| 329 | (continue) => { $crate::tokens::Continue }; |
| 330 | (crate) => { $crate::tokens::Crate }; |
| 331 | (default) => { $crate::tokens::Default }; |
| 332 | (do) => { $crate::tokens::Do }; |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 333 | (dyn) => { $crate::tokens::Dyn }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 334 | (else) => { $crate::tokens::Else }; |
| 335 | (enum) => { $crate::tokens::Enum }; |
| 336 | (extern) => { $crate::tokens::Extern }; |
| 337 | (fn) => { $crate::tokens::Fn }; |
| 338 | (for) => { $crate::tokens::For }; |
| 339 | (if) => { $crate::tokens::If }; |
| 340 | (impl) => { $crate::tokens::Impl }; |
| 341 | (in) => { $crate::tokens::In }; |
| 342 | (let) => { $crate::tokens::Let }; |
| 343 | (loop) => { $crate::tokens::Loop }; |
David Tolnay | 500d832 | 2017-12-18 00:32:51 -0800 | [diff] [blame] | 344 | (macro) => { $crate::tokens::Macro }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 345 | (match) => { $crate::tokens::Match }; |
| 346 | (mod) => { $crate::tokens::Mod }; |
| 347 | (move) => { $crate::tokens::Move }; |
| 348 | (mut) => { $crate::tokens::Mut }; |
| 349 | (pub) => { $crate::tokens::Pub }; |
| 350 | (ref) => { $crate::tokens::Ref }; |
| 351 | (return) => { $crate::tokens::Return }; |
| 352 | (self) => { $crate::tokens::Self_ }; |
| 353 | (static) => { $crate::tokens::Static }; |
| 354 | (struct) => { $crate::tokens::Struct }; |
| 355 | (super) => { $crate::tokens::Super }; |
| 356 | (trait) => { $crate::tokens::Trait }; |
| 357 | (type) => { $crate::tokens::Type }; |
| 358 | (union) => { $crate::tokens::Union }; |
| 359 | (unsafe) => { $crate::tokens::Unsafe }; |
| 360 | (use) => { $crate::tokens::Use }; |
| 361 | (where) => { $crate::tokens::Where }; |
| 362 | (while) => { $crate::tokens::While }; |
| 363 | (yield) => { $crate::tokens::Yield }; |
| 364 | } |
| 365 | |
David Tolnay | 0fbe328 | 2017-12-26 21:46:16 -0500 | [diff] [blame] | 366 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 367 | #[macro_export] |
| 368 | macro_rules! punct { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 369 | ($i:expr, +) => { call!($i, <$crate::tokens::Add as $crate::synom::Synom>::parse) }; |
| 370 | ($i:expr, +=) => { call!($i, <$crate::tokens::AddEq as $crate::synom::Synom>::parse) }; |
| 371 | ($i:expr, &) => { call!($i, <$crate::tokens::And as $crate::synom::Synom>::parse) }; |
| 372 | ($i:expr, &&) => { call!($i, <$crate::tokens::AndAnd as $crate::synom::Synom>::parse) }; |
| 373 | ($i:expr, &=) => { call!($i, <$crate::tokens::AndEq as $crate::synom::Synom>::parse) }; |
| 374 | ($i:expr, @) => { call!($i, <$crate::tokens::At as $crate::synom::Synom>::parse) }; |
| 375 | ($i:expr, !) => { call!($i, <$crate::tokens::Bang as $crate::synom::Synom>::parse) }; |
| 376 | ($i:expr, ^) => { call!($i, <$crate::tokens::Caret as $crate::synom::Synom>::parse) }; |
| 377 | ($i:expr, ^=) => { call!($i, <$crate::tokens::CaretEq as $crate::synom::Synom>::parse) }; |
| 378 | ($i:expr, :) => { call!($i, <$crate::tokens::Colon as $crate::synom::Synom>::parse) }; |
| 379 | ($i:expr, ::) => { call!($i, <$crate::tokens::Colon2 as $crate::synom::Synom>::parse) }; |
| 380 | ($i:expr, ,) => { call!($i, <$crate::tokens::Comma as $crate::synom::Synom>::parse) }; |
| 381 | ($i:expr, /) => { call!($i, <$crate::tokens::Div as $crate::synom::Synom>::parse) }; |
| 382 | ($i:expr, /=) => { call!($i, <$crate::tokens::DivEq as $crate::synom::Synom>::parse) }; |
| 383 | ($i:expr, .) => { call!($i, <$crate::tokens::Dot as $crate::synom::Synom>::parse) }; |
| 384 | ($i:expr, ..) => { call!($i, <$crate::tokens::Dot2 as $crate::synom::Synom>::parse) }; |
| 385 | ($i:expr, ...) => { call!($i, <$crate::tokens::Dot3 as $crate::synom::Synom>::parse) }; |
| 386 | ($i:expr, ..=) => { call!($i, <$crate::tokens::DotDotEq as $crate::synom::Synom>::parse) }; |
| 387 | ($i:expr, =) => { call!($i, <$crate::tokens::Eq as $crate::synom::Synom>::parse) }; |
| 388 | ($i:expr, ==) => { call!($i, <$crate::tokens::EqEq as $crate::synom::Synom>::parse) }; |
| 389 | ($i:expr, >=) => { call!($i, <$crate::tokens::Ge as $crate::synom::Synom>::parse) }; |
| 390 | ($i:expr, >) => { call!($i, <$crate::tokens::Gt as $crate::synom::Synom>::parse) }; |
| 391 | ($i:expr, <=) => { call!($i, <$crate::tokens::Le as $crate::synom::Synom>::parse) }; |
| 392 | ($i:expr, <) => { call!($i, <$crate::tokens::Lt as $crate::synom::Synom>::parse) }; |
| 393 | ($i:expr, *=) => { call!($i, <$crate::tokens::MulEq as $crate::synom::Synom>::parse) }; |
| 394 | ($i:expr, !=) => { call!($i, <$crate::tokens::Ne as $crate::synom::Synom>::parse) }; |
| 395 | ($i:expr, |) => { call!($i, <$crate::tokens::Or as $crate::synom::Synom>::parse) }; |
| 396 | ($i:expr, |=) => { call!($i, <$crate::tokens::OrEq as $crate::synom::Synom>::parse) }; |
| 397 | ($i:expr, ||) => { call!($i, <$crate::tokens::OrOr as $crate::synom::Synom>::parse) }; |
| 398 | ($i:expr, #) => { call!($i, <$crate::tokens::Pound as $crate::synom::Synom>::parse) }; |
| 399 | ($i:expr, ?) => { call!($i, <$crate::tokens::Question as $crate::synom::Synom>::parse) }; |
| 400 | ($i:expr, ->) => { call!($i, <$crate::tokens::RArrow as $crate::synom::Synom>::parse) }; |
| 401 | ($i:expr, <-) => { call!($i, <$crate::tokens::LArrow as $crate::synom::Synom>::parse) }; |
| 402 | ($i:expr, %) => { call!($i, <$crate::tokens::Rem as $crate::synom::Synom>::parse) }; |
| 403 | ($i:expr, %=) => { call!($i, <$crate::tokens::RemEq as $crate::synom::Synom>::parse) }; |
| 404 | ($i:expr, =>) => { call!($i, <$crate::tokens::Rocket as $crate::synom::Synom>::parse) }; |
| 405 | ($i:expr, ;) => { call!($i, <$crate::tokens::Semi as $crate::synom::Synom>::parse) }; |
| 406 | ($i:expr, <<) => { call!($i, <$crate::tokens::Shl as $crate::synom::Synom>::parse) }; |
| 407 | ($i:expr, <<=) => { call!($i, <$crate::tokens::ShlEq as $crate::synom::Synom>::parse) }; |
| 408 | ($i:expr, >>) => { call!($i, <$crate::tokens::Shr as $crate::synom::Synom>::parse) }; |
| 409 | ($i:expr, >>=) => { call!($i, <$crate::tokens::ShrEq as $crate::synom::Synom>::parse) }; |
| 410 | ($i:expr, *) => { call!($i, <$crate::tokens::Star as $crate::synom::Synom>::parse) }; |
| 411 | ($i:expr, -) => { call!($i, <$crate::tokens::Sub as $crate::synom::Synom>::parse) }; |
| 412 | ($i:expr, -=) => { call!($i, <$crate::tokens::SubEq as $crate::synom::Synom>::parse) }; |
| 413 | ($i:expr, _) => { call!($i, <$crate::tokens::Underscore as $crate::synom::Synom>::parse) }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 414 | } |
| 415 | |
David Tolnay | 0fbe328 | 2017-12-26 21:46:16 -0500 | [diff] [blame] | 416 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 417 | #[macro_export] |
| 418 | macro_rules! keyword { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 419 | ($i:expr, as) => { call!($i, <$crate::tokens::As as $crate::synom::Synom>::parse) }; |
| 420 | ($i:expr, auto) => { call!($i, <$crate::tokens::Auto as $crate::synom::Synom>::parse) }; |
| 421 | ($i:expr, box) => { call!($i, <$crate::tokens::Box as $crate::synom::Synom>::parse) }; |
| 422 | ($i:expr, break) => { call!($i, <$crate::tokens::Break as $crate::synom::Synom>::parse) }; |
| 423 | ($i:expr, Self) => { call!($i, <$crate::tokens::CapSelf as $crate::synom::Synom>::parse) }; |
| 424 | ($i:expr, catch) => { call!($i, <$crate::tokens::Catch as $crate::synom::Synom>::parse) }; |
| 425 | ($i:expr, const) => { call!($i, <$crate::tokens::Const as $crate::synom::Synom>::parse) }; |
| 426 | ($i:expr, continue) => { call!($i, <$crate::tokens::Continue as $crate::synom::Synom>::parse) }; |
| 427 | ($i:expr, crate) => { call!($i, <$crate::tokens::Crate as $crate::synom::Synom>::parse) }; |
| 428 | ($i:expr, default) => { call!($i, <$crate::tokens::Default as $crate::synom::Synom>::parse) }; |
| 429 | ($i:expr, do) => { call!($i, <$crate::tokens::Do as $crate::synom::Synom>::parse) }; |
| 430 | ($i:expr, dyn) => { call!($i, <$crate::tokens::Dyn as $crate::synom::Synom>::parse) }; |
| 431 | ($i:expr, else) => { call!($i, <$crate::tokens::Else as $crate::synom::Synom>::parse) }; |
| 432 | ($i:expr, enum) => { call!($i, <$crate::tokens::Enum as $crate::synom::Synom>::parse) }; |
| 433 | ($i:expr, extern) => { call!($i, <$crate::tokens::Extern as $crate::synom::Synom>::parse) }; |
| 434 | ($i:expr, fn) => { call!($i, <$crate::tokens::Fn as $crate::synom::Synom>::parse) }; |
| 435 | ($i:expr, for) => { call!($i, <$crate::tokens::For as $crate::synom::Synom>::parse) }; |
| 436 | ($i:expr, if) => { call!($i, <$crate::tokens::If as $crate::synom::Synom>::parse) }; |
| 437 | ($i:expr, impl) => { call!($i, <$crate::tokens::Impl as $crate::synom::Synom>::parse) }; |
| 438 | ($i:expr, in) => { call!($i, <$crate::tokens::In as $crate::synom::Synom>::parse) }; |
| 439 | ($i:expr, let) => { call!($i, <$crate::tokens::Let as $crate::synom::Synom>::parse) }; |
| 440 | ($i:expr, loop) => { call!($i, <$crate::tokens::Loop as $crate::synom::Synom>::parse) }; |
| 441 | ($i:expr, macro) => { call!($i, <$crate::tokens::Macro as $crate::synom::Synom>::parse) }; |
| 442 | ($i:expr, match) => { call!($i, <$crate::tokens::Match as $crate::synom::Synom>::parse) }; |
| 443 | ($i:expr, mod) => { call!($i, <$crate::tokens::Mod as $crate::synom::Synom>::parse) }; |
| 444 | ($i:expr, move) => { call!($i, <$crate::tokens::Move as $crate::synom::Synom>::parse) }; |
| 445 | ($i:expr, mut) => { call!($i, <$crate::tokens::Mut as $crate::synom::Synom>::parse) }; |
| 446 | ($i:expr, pub) => { call!($i, <$crate::tokens::Pub as $crate::synom::Synom>::parse) }; |
| 447 | ($i:expr, ref) => { call!($i, <$crate::tokens::Ref as $crate::synom::Synom>::parse) }; |
| 448 | ($i:expr, return) => { call!($i, <$crate::tokens::Return as $crate::synom::Synom>::parse) }; |
| 449 | ($i:expr, self) => { call!($i, <$crate::tokens::Self_ as $crate::synom::Synom>::parse) }; |
| 450 | ($i:expr, static) => { call!($i, <$crate::tokens::Static as $crate::synom::Synom>::parse) }; |
| 451 | ($i:expr, struct) => { call!($i, <$crate::tokens::Struct as $crate::synom::Synom>::parse) }; |
| 452 | ($i:expr, super) => { call!($i, <$crate::tokens::Super as $crate::synom::Synom>::parse) }; |
| 453 | ($i:expr, trait) => { call!($i, <$crate::tokens::Trait as $crate::synom::Synom>::parse) }; |
| 454 | ($i:expr, type) => { call!($i, <$crate::tokens::Type as $crate::synom::Synom>::parse) }; |
| 455 | ($i:expr, union) => { call!($i, <$crate::tokens::Union as $crate::synom::Synom>::parse) }; |
| 456 | ($i:expr, unsafe) => { call!($i, <$crate::tokens::Unsafe as $crate::synom::Synom>::parse) }; |
| 457 | ($i:expr, use) => { call!($i, <$crate::tokens::Use as $crate::synom::Synom>::parse) }; |
| 458 | ($i:expr, where) => { call!($i, <$crate::tokens::Where as $crate::synom::Synom>::parse) }; |
| 459 | ($i:expr, while) => { call!($i, <$crate::tokens::While as $crate::synom::Synom>::parse) }; |
| 460 | ($i:expr, yield) => { call!($i, <$crate::tokens::Yield as $crate::synom::Synom>::parse) }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 463 | #[cfg(feature = "parsing")] |
| 464 | mod parsing { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 465 | use proc_macro2::{Span, Delimiter, Spacing}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 466 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 467 | use cursor::Cursor; |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 468 | use {PResult, parse_error}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 469 | |
| 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 | |
| 492 | pub fn op<'a, T, R>(s: &str, |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 493 | mut tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 494 | new: fn(T) -> R) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 495 | -> PResult<'a, R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 496 | where T: FromSpans, |
| 497 | { |
| 498 | let mut spans = [Span::default(); 3]; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 499 | assert!(s.len() <= spans.len()); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 500 | let chars = s.chars(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 501 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 502 | for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 503 | match tokens.op() { |
| 504 | Some((rest, span, c, kind)) if c == ch => { |
| 505 | if i != s.len() - 1 { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 506 | match kind { |
| 507 | Spacing::Joint => {} |
| 508 | _ => return parse_error(), |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 509 | } |
| 510 | } |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 511 | *slot = span; |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 512 | tokens = rest; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 513 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 514 | _ => return parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 515 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 516 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 517 | Ok((tokens, new(T::from_spans(&spans)))) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | pub fn sym<'a, T>(sym: &str, |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 521 | tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 522 | new: fn(Span) -> T) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 523 | -> PResult<'a, T> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 524 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 525 | if let Some((rest, span, s)) = tokens.word() { |
| 526 | if s.as_str() == sym { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 527 | return Ok((rest, new(span))); |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 528 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 529 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 530 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | pub fn delim<'a, F, R, T>(delim: &str, |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 534 | tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 535 | new: fn(Span) -> T, |
| 536 | f: F) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 537 | -> PResult<'a, (R, T)> |
| 538 | where F: FnOnce(Cursor) -> PResult<R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 539 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 540 | // NOTE: We should support none-delimited sequences here. |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 541 | let delim = match delim { |
| 542 | "(" => Delimiter::Parenthesis, |
| 543 | "{" => Delimiter::Brace, |
| 544 | "[" => Delimiter::Bracket, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 545 | " " => Delimiter::None, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 546 | _ => panic!("unknown delimiter: {}", delim), |
| 547 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 548 | |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 549 | if let Some(seqinfo) = tokens.seq(delim) { |
| 550 | match f(seqinfo.inside) { |
| 551 | Ok((remaining, ret)) => { |
| 552 | if remaining.eof() { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 553 | return Ok((seqinfo.outside, (ret, new(seqinfo.span)))); |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 554 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 555 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 556 | Err(err) => return Err(err), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 557 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 558 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 559 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | #[cfg(feature = "printing")] |
| 564 | mod printing { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 565 | use proc_macro2::{Span, TokenTree, TokenNode, Spacing, Term}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 566 | use quote::Tokens; |
| 567 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 568 | pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) { |
| 569 | assert_eq!(s.len(), spans.len()); |
| 570 | |
| 571 | let mut chars = s.chars(); |
| 572 | let mut spans = spans.iter(); |
| 573 | let ch = chars.next_back().unwrap(); |
| 574 | let span = spans.next_back().unwrap(); |
| 575 | for (ch, span) in chars.zip(spans) { |
| 576 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 577 | span: *span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 578 | kind: TokenNode::Op(ch, Spacing::Joint), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 579 | }); |
| 580 | } |
| 581 | |
| 582 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 583 | span: *span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 584 | kind: TokenNode::Op(ch, Spacing::Alone), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 585 | }); |
| 586 | } |
| 587 | |
| 588 | pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) { |
| 589 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 590 | span: *span, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 591 | kind: TokenNode::Term(Term::intern(s)), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 592 | }); |
| 593 | } |
| 594 | |
| 595 | pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F) |
| 596 | where F: FnOnce(&mut Tokens) |
| 597 | { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 598 | tokens.append_delimited(s, *span, f) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 599 | } |
| 600 | } |