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 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 8 | use span::Span; |
| 9 | |
| 10 | macro_rules! tokens { |
| 11 | ( |
| 12 | ops: { |
| 13 | $(($($op:tt)*),)* |
| 14 | } |
| 15 | delim: { |
| 16 | $(($($delim:tt)*),)* |
| 17 | } |
| 18 | syms: { |
| 19 | $(($($sym:tt)*),)* |
| 20 | } |
| 21 | ) => ( |
| 22 | $(op! { $($op)* })* |
| 23 | $(delim! { $($delim)* })* |
| 24 | $(sym! { $($sym)* })* |
| 25 | ) |
| 26 | } |
| 27 | |
| 28 | macro_rules! op { |
| 29 | (pub struct $name:ident($($contents:tt)*) => $s:expr) => { |
| 30 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 31 | #[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash))] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 32 | #[derive(Default)] |
| 33 | pub struct $name(pub $($contents)*); |
| 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 { |
| 38 | let mut t = f.debug_tuple(stringify!($name)); |
| 39 | for span in &self.0 { |
| 40 | t.field(span); |
| 41 | } |
| 42 | t.finish() |
| 43 | } |
| 44 | } |
| 45 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 46 | #[cfg(feature = "printing")] |
| 47 | impl ::quote::ToTokens for $name { |
| 48 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
| 49 | printing::op($s, &self.0, tokens); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[cfg(feature = "parsing")] |
| 54 | impl ::Synom for $name { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 55 | fn parse(tokens: $crate::Cursor) -> $crate::PResult<$name> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 56 | parsing::op($s, tokens, $name) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | macro_rules! sym { |
| 63 | (pub struct $name:ident => $s:expr) => { |
| 64 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 65 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 66 | #[derive(Default)] |
| 67 | pub struct $name(pub Span); |
| 68 | |
| 69 | #[cfg(feature = "printing")] |
| 70 | impl ::quote::ToTokens for $name { |
| 71 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
| 72 | printing::sym($s, &self.0, tokens); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #[cfg(feature = "parsing")] |
| 77 | impl ::Synom for $name { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 78 | fn parse(tokens: $crate::Cursor) -> $crate::PResult<$name> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 79 | parsing::sym($s, tokens, $name) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | macro_rules! delim { |
| 86 | (pub struct $name:ident => $s:expr) => { |
| 87 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 88 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 89 | #[derive(Default)] |
| 90 | pub struct $name(pub Span); |
| 91 | |
| 92 | impl $name { |
| 93 | #[cfg(feature = "printing")] |
| 94 | pub fn surround<F>(&self, |
| 95 | tokens: &mut ::quote::Tokens, |
| 96 | f: F) |
| 97 | where F: FnOnce(&mut ::quote::Tokens) |
| 98 | { |
| 99 | printing::delim($s, &self.0, tokens, f); |
| 100 | } |
| 101 | |
| 102 | #[cfg(feature = "parsing")] |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 103 | pub fn parse<F, R>(tokens: $crate::Cursor, f: F) -> $crate::PResult<(R, $name)> |
| 104 | where F: FnOnce($crate::Cursor) -> $crate::PResult<R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 105 | { |
| 106 | parsing::delim($s, tokens, $name, f) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | tokens! { |
| 113 | ops: { |
| 114 | (pub struct Add([Span; 1]) => "+"), |
| 115 | (pub struct AddEq([Span; 2]) => "+="), |
| 116 | (pub struct And([Span; 1]) => "&"), |
| 117 | (pub struct AndAnd([Span; 2]) => "&&"), |
| 118 | (pub struct AndEq([Span; 2]) => "&="), |
| 119 | (pub struct At([Span; 1]) => "@"), |
| 120 | (pub struct Bang([Span; 1]) => "!"), |
| 121 | (pub struct Caret([Span; 1]) => "^"), |
| 122 | (pub struct CaretEq([Span; 2]) => "^="), |
| 123 | (pub struct Colon([Span; 1]) => ":"), |
| 124 | (pub struct Colon2([Span; 2]) => "::"), |
| 125 | (pub struct Comma([Span; 1]) => ","), |
| 126 | (pub struct Div([Span; 1]) => "/"), |
| 127 | (pub struct DivEq([Span; 2]) => "/="), |
| 128 | (pub struct Dot([Span; 1]) => "."), |
| 129 | (pub struct Dot2([Span; 2]) => ".."), |
| 130 | (pub struct Dot3([Span; 3]) => "..."), |
| 131 | (pub struct Eq([Span; 1]) => "="), |
| 132 | (pub struct EqEq([Span; 2]) => "=="), |
| 133 | (pub struct Ge([Span; 2]) => ">="), |
| 134 | (pub struct Gt([Span; 1]) => ">"), |
| 135 | (pub struct Le([Span; 2]) => "<="), |
| 136 | (pub struct Lt([Span; 1]) => "<"), |
| 137 | (pub struct MulEq([Span; 2]) => "*="), |
| 138 | (pub struct Ne([Span; 2]) => "!="), |
| 139 | (pub struct Or([Span; 1]) => "|"), |
| 140 | (pub struct OrEq([Span; 2]) => "|="), |
| 141 | (pub struct OrOr([Span; 2]) => "||"), |
| 142 | (pub struct Pound([Span; 1]) => "#"), |
| 143 | (pub struct Question([Span; 1]) => "?"), |
| 144 | (pub struct RArrow([Span; 2]) => "->"), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 145 | (pub struct LArrow([Span; 2]) => "<-"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 146 | (pub struct Rem([Span; 1]) => "%"), |
| 147 | (pub struct RemEq([Span; 2]) => "%="), |
| 148 | (pub struct Rocket([Span; 2]) => "=>"), |
| 149 | (pub struct Semi([Span; 1]) => ";"), |
| 150 | (pub struct Shl([Span; 2]) => "<<"), |
| 151 | (pub struct ShlEq([Span; 3]) => "<<="), |
| 152 | (pub struct Shr([Span; 2]) => ">>"), |
| 153 | (pub struct ShrEq([Span; 3]) => ">>="), |
| 154 | (pub struct Star([Span; 1]) => "*"), |
| 155 | (pub struct Sub([Span; 1]) => "-"), |
| 156 | (pub struct SubEq([Span; 2]) => "-="), |
| 157 | (pub struct Underscore([Span; 1]) => "_"), |
| 158 | } |
| 159 | delim: { |
| 160 | (pub struct Brace => "{"), |
| 161 | (pub struct Bracket => "["), |
| 162 | (pub struct Paren => "("), |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 163 | (pub struct Group => " "), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 164 | } |
| 165 | syms: { |
| 166 | (pub struct As => "as"), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame^] | 167 | (pub struct Box => "box"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 168 | (pub struct Break => "break"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 169 | (pub struct CapSelf => "Self"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 170 | (pub struct Catch => "catch"), |
| 171 | (pub struct Const => "const"), |
| 172 | (pub struct Continue => "continue"), |
| 173 | (pub struct Crate => "crate"), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame^] | 174 | (pub struct Default => "default"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 175 | (pub struct Do => "do"), |
| 176 | (pub struct Else => "else"), |
| 177 | (pub struct Enum => "enum"), |
| 178 | (pub struct Extern => "extern"), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame^] | 179 | (pub struct Fn => "fn"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 180 | (pub struct For => "for"), |
| 181 | (pub struct If => "if"), |
| 182 | (pub struct Impl => "impl"), |
| 183 | (pub struct In => "in"), |
| 184 | (pub struct Let => "let"), |
| 185 | (pub struct Loop => "loop"), |
| 186 | (pub struct Match => "match"), |
| 187 | (pub struct Mod => "mod"), |
| 188 | (pub struct Move => "move"), |
| 189 | (pub struct Mut => "mut"), |
| 190 | (pub struct Pub => "pub"), |
| 191 | (pub struct Ref => "ref"), |
| 192 | (pub struct Return => "return"), |
| 193 | (pub struct Self_ => "self"), |
| 194 | (pub struct Static => "static"), |
| 195 | (pub struct Struct => "struct"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 196 | (pub struct Super => "super"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 197 | (pub struct Trait => "trait"), |
| 198 | (pub struct Type => "type"), |
| 199 | (pub struct Union => "union"), |
| 200 | (pub struct Unsafe => "unsafe"), |
| 201 | (pub struct Use => "use"), |
| 202 | (pub struct Where => "where"), |
| 203 | (pub struct While => "while"), |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 204 | (pub struct Yield => "yield"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame^] | 208 | // Unfortunate duplication due to a rustdoc bug. |
| 209 | // https://github.com/rust-lang/rust/issues/45939 |
| 210 | #[macro_export] |
| 211 | macro_rules! Token { |
| 212 | (+) => { $crate::tokens::Add }; |
| 213 | (+=) => { $crate::tokens::AddEq }; |
| 214 | (&) => { $crate::tokens::And }; |
| 215 | (&&) => { $crate::tokens::AndAnd }; |
| 216 | (&=) => { $crate::tokens::AndEq }; |
| 217 | (@) => { $crate::tokens::At }; |
| 218 | (!) => { $crate::tokens::Bang }; |
| 219 | (^) => { $crate::tokens::Caret }; |
| 220 | (^=) => { $crate::tokens::CaretEq }; |
| 221 | (:) => { $crate::tokens::Colon }; |
| 222 | (::) => { $crate::tokens::Colon2 }; |
| 223 | (,) => { $crate::tokens::Comma }; |
| 224 | (/) => { $crate::tokens::Div }; |
| 225 | (/=) => { $crate::tokens::DivEq }; |
| 226 | (.) => { $crate::tokens::Dot }; |
| 227 | (..) => { $crate::tokens::Dot2 }; |
| 228 | (...) => { $crate::tokens::Dot3 }; |
| 229 | (=) => { $crate::tokens::Eq }; |
| 230 | (==) => { $crate::tokens::EqEq }; |
| 231 | (>=) => { $crate::tokens::Ge }; |
| 232 | (>) => { $crate::tokens::Gt }; |
| 233 | (<=) => { $crate::tokens::Le }; |
| 234 | (<) => { $crate::tokens::Lt }; |
| 235 | (*=) => { $crate::tokens::MulEq }; |
| 236 | (!=) => { $crate::tokens::Ne }; |
| 237 | (|) => { $crate::tokens::Or }; |
| 238 | (|=) => { $crate::tokens::OrEq }; |
| 239 | (||) => { $crate::tokens::OrOr }; |
| 240 | (#) => { $crate::tokens::Pound }; |
| 241 | (?) => { $crate::tokens::Question }; |
| 242 | (->) => { $crate::tokens::RArrow }; |
| 243 | (<-) => { $crate::tokens::LArrow }; |
| 244 | (%) => { $crate::tokens::Rem }; |
| 245 | (%=) => { $crate::tokens::RemEq }; |
| 246 | (=>) => { $crate::tokens::Rocket }; |
| 247 | (;) => { $crate::tokens::Semi }; |
| 248 | (<<) => { $crate::tokens::Shl }; |
| 249 | (<<=) => { $crate::tokens::ShlEq }; |
| 250 | (>>) => { $crate::tokens::Shr }; |
| 251 | (>>=) => { $crate::tokens::ShrEq }; |
| 252 | (*) => { $crate::tokens::Star }; |
| 253 | (-) => { $crate::tokens::Sub }; |
| 254 | (-=) => { $crate::tokens::SubEq }; |
| 255 | (_) => { $crate::tokens::Underscore }; |
| 256 | (as) => { $crate::tokens::As }; |
| 257 | (box) => { $crate::tokens::Box }; |
| 258 | (break) => { $crate::tokens::Break }; |
| 259 | (Self) => { $crate::tokens::CapSelf }; |
| 260 | (catch) => { $crate::tokens::Catch }; |
| 261 | (const) => { $crate::tokens::Const }; |
| 262 | (continue) => { $crate::tokens::Continue }; |
| 263 | (crate) => { $crate::tokens::Crate }; |
| 264 | (default) => { $crate::tokens::Default }; |
| 265 | (do) => { $crate::tokens::Do }; |
| 266 | (else) => { $crate::tokens::Else }; |
| 267 | (enum) => { $crate::tokens::Enum }; |
| 268 | (extern) => { $crate::tokens::Extern }; |
| 269 | (fn) => { $crate::tokens::Fn }; |
| 270 | (for) => { $crate::tokens::For }; |
| 271 | (if) => { $crate::tokens::If }; |
| 272 | (impl) => { $crate::tokens::Impl }; |
| 273 | (in) => { $crate::tokens::In }; |
| 274 | (let) => { $crate::tokens::Let }; |
| 275 | (loop) => { $crate::tokens::Loop }; |
| 276 | (match) => { $crate::tokens::Match }; |
| 277 | (mod) => { $crate::tokens::Mod }; |
| 278 | (move) => { $crate::tokens::Move }; |
| 279 | (mut) => { $crate::tokens::Mut }; |
| 280 | (pub) => { $crate::tokens::Pub }; |
| 281 | (ref) => { $crate::tokens::Ref }; |
| 282 | (return) => { $crate::tokens::Return }; |
| 283 | (self) => { $crate::tokens::Self_ }; |
| 284 | (static) => { $crate::tokens::Static }; |
| 285 | (struct) => { $crate::tokens::Struct }; |
| 286 | (super) => { $crate::tokens::Super }; |
| 287 | (trait) => { $crate::tokens::Trait }; |
| 288 | (type) => { $crate::tokens::Type }; |
| 289 | (union) => { $crate::tokens::Union }; |
| 290 | (unsafe) => { $crate::tokens::Unsafe }; |
| 291 | (use) => { $crate::tokens::Use }; |
| 292 | (where) => { $crate::tokens::Where }; |
| 293 | (while) => { $crate::tokens::While }; |
| 294 | (yield) => { $crate::tokens::Yield }; |
| 295 | } |
| 296 | |
| 297 | #[macro_export] |
| 298 | macro_rules! punct { |
| 299 | ($i:expr, +) => { call!($i, <$crate::tokens::Add as $crate::Synom>::parse) }; |
| 300 | ($i:expr, +=) => { call!($i, <$crate::tokens::AddEq as $crate::Synom>::parse) }; |
| 301 | ($i:expr, &) => { call!($i, <$crate::tokens::And as $crate::Synom>::parse) }; |
| 302 | ($i:expr, &&) => { call!($i, <$crate::tokens::AndAnd as $crate::Synom>::parse) }; |
| 303 | ($i:expr, &=) => { call!($i, <$crate::tokens::AndEq as $crate::Synom>::parse) }; |
| 304 | ($i:expr, @) => { call!($i, <$crate::tokens::At as $crate::Synom>::parse) }; |
| 305 | ($i:expr, !) => { call!($i, <$crate::tokens::Bang as $crate::Synom>::parse) }; |
| 306 | ($i:expr, ^) => { call!($i, <$crate::tokens::Caret as $crate::Synom>::parse) }; |
| 307 | ($i:expr, ^=) => { call!($i, <$crate::tokens::CaretEq as $crate::Synom>::parse) }; |
| 308 | ($i:expr, :) => { call!($i, <$crate::tokens::Colon as $crate::Synom>::parse) }; |
| 309 | ($i:expr, ::) => { call!($i, <$crate::tokens::Colon2 as $crate::Synom>::parse) }; |
| 310 | ($i:expr, ,) => { call!($i, <$crate::tokens::Comma as $crate::Synom>::parse) }; |
| 311 | ($i:expr, /) => { call!($i, <$crate::tokens::Div as $crate::Synom>::parse) }; |
| 312 | ($i:expr, /=) => { call!($i, <$crate::tokens::DivEq as $crate::Synom>::parse) }; |
| 313 | ($i:expr, .) => { call!($i, <$crate::tokens::Dot as $crate::Synom>::parse) }; |
| 314 | ($i:expr, ..) => { call!($i, <$crate::tokens::Dot2 as $crate::Synom>::parse) }; |
| 315 | ($i:expr, ...) => { call!($i, <$crate::tokens::Dot3 as $crate::Synom>::parse) }; |
| 316 | ($i:expr, =) => { call!($i, <$crate::tokens::Eq as $crate::Synom>::parse) }; |
| 317 | ($i:expr, ==) => { call!($i, <$crate::tokens::EqEq as $crate::Synom>::parse) }; |
| 318 | ($i:expr, >=) => { call!($i, <$crate::tokens::Ge as $crate::Synom>::parse) }; |
| 319 | ($i:expr, >) => { call!($i, <$crate::tokens::Gt as $crate::Synom>::parse) }; |
| 320 | ($i:expr, <=) => { call!($i, <$crate::tokens::Le as $crate::Synom>::parse) }; |
| 321 | ($i:expr, <) => { call!($i, <$crate::tokens::Lt as $crate::Synom>::parse) }; |
| 322 | ($i:expr, *=) => { call!($i, <$crate::tokens::MulEq as $crate::Synom>::parse) }; |
| 323 | ($i:expr, !=) => { call!($i, <$crate::tokens::Ne as $crate::Synom>::parse) }; |
| 324 | ($i:expr, |) => { call!($i, <$crate::tokens::Or as $crate::Synom>::parse) }; |
| 325 | ($i:expr, |=) => { call!($i, <$crate::tokens::OrEq as $crate::Synom>::parse) }; |
| 326 | ($i:expr, ||) => { call!($i, <$crate::tokens::OrOr as $crate::Synom>::parse) }; |
| 327 | ($i:expr, #) => { call!($i, <$crate::tokens::Pound as $crate::Synom>::parse) }; |
| 328 | ($i:expr, ?) => { call!($i, <$crate::tokens::Question as $crate::Synom>::parse) }; |
| 329 | ($i:expr, ->) => { call!($i, <$crate::tokens::RArrow as $crate::Synom>::parse) }; |
| 330 | ($i:expr, <-) => { call!($i, <$crate::tokens::LArrow as $crate::Synom>::parse) }; |
| 331 | ($i:expr, %) => { call!($i, <$crate::tokens::Rem as $crate::Synom>::parse) }; |
| 332 | ($i:expr, %=) => { call!($i, <$crate::tokens::RemEq as $crate::Synom>::parse) }; |
| 333 | ($i:expr, =>) => { call!($i, <$crate::tokens::Rocket as $crate::Synom>::parse) }; |
| 334 | ($i:expr, ;) => { call!($i, <$crate::tokens::Semi as $crate::Synom>::parse) }; |
| 335 | ($i:expr, <<) => { call!($i, <$crate::tokens::Shl as $crate::Synom>::parse) }; |
| 336 | ($i:expr, <<=) => { call!($i, <$crate::tokens::ShlEq as $crate::Synom>::parse) }; |
| 337 | ($i:expr, >>) => { call!($i, <$crate::tokens::Shr as $crate::Synom>::parse) }; |
| 338 | ($i:expr, >>=) => { call!($i, <$crate::tokens::ShrEq as $crate::Synom>::parse) }; |
| 339 | ($i:expr, *) => { call!($i, <$crate::tokens::Star as $crate::Synom>::parse) }; |
| 340 | ($i:expr, -) => { call!($i, <$crate::tokens::Sub as $crate::Synom>::parse) }; |
| 341 | ($i:expr, -=) => { call!($i, <$crate::tokens::SubEq as $crate::Synom>::parse) }; |
| 342 | ($i:expr, _) => { call!($i, <$crate::tokens::Underscore as $crate::Synom>::parse) }; |
| 343 | } |
| 344 | |
| 345 | #[macro_export] |
| 346 | macro_rules! keyword { |
| 347 | ($i:expr, as) => { call!($i, <$crate::tokens::As as $crate::Synom>::parse) }; |
| 348 | ($i:expr, box) => { call!($i, <$crate::tokens::Box as $crate::Synom>::parse) }; |
| 349 | ($i:expr, break) => { call!($i, <$crate::tokens::Break as $crate::Synom>::parse) }; |
| 350 | ($i:expr, Self) => { call!($i, <$crate::tokens::CapSelf as $crate::Synom>::parse) }; |
| 351 | ($i:expr, catch) => { call!($i, <$crate::tokens::Catch as $crate::Synom>::parse) }; |
| 352 | ($i:expr, const) => { call!($i, <$crate::tokens::Const as $crate::Synom>::parse) }; |
| 353 | ($i:expr, continue) => { call!($i, <$crate::tokens::Continue as $crate::Synom>::parse) }; |
| 354 | ($i:expr, crate) => { call!($i, <$crate::tokens::Crate as $crate::Synom>::parse) }; |
| 355 | ($i:expr, default) => { call!($i, <$crate::tokens::Default as $crate::Synom>::parse) }; |
| 356 | ($i:expr, do) => { call!($i, <$crate::tokens::Do as $crate::Synom>::parse) }; |
| 357 | ($i:expr, else) => { call!($i, <$crate::tokens::Else as $crate::Synom>::parse) }; |
| 358 | ($i:expr, enum) => { call!($i, <$crate::tokens::Enum as $crate::Synom>::parse) }; |
| 359 | ($i:expr, extern) => { call!($i, <$crate::tokens::Extern as $crate::Synom>::parse) }; |
| 360 | ($i:expr, fn) => { call!($i, <$crate::tokens::Fn as $crate::Synom>::parse) }; |
| 361 | ($i:expr, for) => { call!($i, <$crate::tokens::For as $crate::Synom>::parse) }; |
| 362 | ($i:expr, if) => { call!($i, <$crate::tokens::If as $crate::Synom>::parse) }; |
| 363 | ($i:expr, impl) => { call!($i, <$crate::tokens::Impl as $crate::Synom>::parse) }; |
| 364 | ($i:expr, in) => { call!($i, <$crate::tokens::In as $crate::Synom>::parse) }; |
| 365 | ($i:expr, let) => { call!($i, <$crate::tokens::Let as $crate::Synom>::parse) }; |
| 366 | ($i:expr, loop) => { call!($i, <$crate::tokens::Loop as $crate::Synom>::parse) }; |
| 367 | ($i:expr, match) => { call!($i, <$crate::tokens::Match as $crate::Synom>::parse) }; |
| 368 | ($i:expr, mod) => { call!($i, <$crate::tokens::Mod as $crate::Synom>::parse) }; |
| 369 | ($i:expr, move) => { call!($i, <$crate::tokens::Move as $crate::Synom>::parse) }; |
| 370 | ($i:expr, mut) => { call!($i, <$crate::tokens::Mut as $crate::Synom>::parse) }; |
| 371 | ($i:expr, pub) => { call!($i, <$crate::tokens::Pub as $crate::Synom>::parse) }; |
| 372 | ($i:expr, ref) => { call!($i, <$crate::tokens::Ref as $crate::Synom>::parse) }; |
| 373 | ($i:expr, return) => { call!($i, <$crate::tokens::Return as $crate::Synom>::parse) }; |
| 374 | ($i:expr, self) => { call!($i, <$crate::tokens::Self_ as $crate::Synom>::parse) }; |
| 375 | ($i:expr, static) => { call!($i, <$crate::tokens::Static as $crate::Synom>::parse) }; |
| 376 | ($i:expr, struct) => { call!($i, <$crate::tokens::Struct as $crate::Synom>::parse) }; |
| 377 | ($i:expr, super) => { call!($i, <$crate::tokens::Super as $crate::Synom>::parse) }; |
| 378 | ($i:expr, trait) => { call!($i, <$crate::tokens::Trait as $crate::Synom>::parse) }; |
| 379 | ($i:expr, type) => { call!($i, <$crate::tokens::Type as $crate::Synom>::parse) }; |
| 380 | ($i:expr, union) => { call!($i, <$crate::tokens::Union as $crate::Synom>::parse) }; |
| 381 | ($i:expr, unsafe) => { call!($i, <$crate::tokens::Unsafe as $crate::Synom>::parse) }; |
| 382 | ($i:expr, use) => { call!($i, <$crate::tokens::Use as $crate::Synom>::parse) }; |
| 383 | ($i:expr, where) => { call!($i, <$crate::tokens::Where as $crate::Synom>::parse) }; |
| 384 | ($i:expr, while) => { call!($i, <$crate::tokens::While as $crate::Synom>::parse) }; |
| 385 | ($i:expr, yield) => { call!($i, <$crate::tokens::Yield as $crate::Synom>::parse) }; |
| 386 | } |
| 387 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 388 | #[cfg(feature = "parsing")] |
| 389 | mod parsing { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 390 | use proc_macro2::{Delimiter, Spacing}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 391 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 392 | use {PResult, Cursor, parse_error}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 393 | use span::Span; |
| 394 | |
| 395 | pub trait FromSpans: Sized { |
| 396 | fn from_spans(spans: &[Span]) -> Self; |
| 397 | } |
| 398 | |
| 399 | impl FromSpans for [Span; 1] { |
| 400 | fn from_spans(spans: &[Span]) -> Self { |
| 401 | [spans[0]] |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | impl FromSpans for [Span; 2] { |
| 406 | fn from_spans(spans: &[Span]) -> Self { |
| 407 | [spans[0], spans[1]] |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | impl FromSpans for [Span; 3] { |
| 412 | fn from_spans(spans: &[Span]) -> Self { |
| 413 | [spans[0], spans[1], spans[2]] |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | pub fn op<'a, T, R>(s: &str, |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 418 | mut tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 419 | new: fn(T) -> R) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 420 | -> PResult<'a, R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 421 | where T: FromSpans, |
| 422 | { |
| 423 | let mut spans = [Span::default(); 3]; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 424 | assert!(s.len() <= spans.len()); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 425 | let chars = s.chars(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 426 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 427 | for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 428 | match tokens.op() { |
| 429 | Some((rest, span, c, kind)) if c == ch => { |
| 430 | if i != s.len() - 1 { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 431 | match kind { |
| 432 | Spacing::Joint => {} |
| 433 | _ => return parse_error(), |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | *slot = Span(span); |
| 437 | tokens = rest; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 438 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 439 | _ => return parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 440 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 441 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 442 | Ok((tokens, new(T::from_spans(&spans)))) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | pub fn sym<'a, T>(sym: &str, |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 446 | tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 447 | new: fn(Span) -> T) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 448 | -> PResult<'a, T> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 449 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 450 | if let Some((rest, span, s)) = tokens.word() { |
| 451 | if s.as_str() == sym { |
| 452 | return Ok((rest, new(Span(span)))); |
| 453 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 454 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 455 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | pub fn delim<'a, F, R, T>(delim: &str, |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 459 | tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 460 | new: fn(Span) -> T, |
| 461 | f: F) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 462 | -> PResult<'a, (R, T)> |
| 463 | where F: FnOnce(Cursor) -> PResult<R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 464 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 465 | // NOTE: We should support none-delimited sequences here. |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 466 | let delim = match delim { |
| 467 | "(" => Delimiter::Parenthesis, |
| 468 | "{" => Delimiter::Brace, |
| 469 | "[" => Delimiter::Bracket, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 470 | " " => Delimiter::None, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 471 | _ => panic!("unknown delimiter: {}", delim), |
| 472 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 473 | |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 474 | if let Some(seqinfo) = tokens.seq(delim) { |
| 475 | match f(seqinfo.inside) { |
| 476 | Ok((remaining, ret)) => { |
| 477 | if remaining.eof() { |
| 478 | return Ok((seqinfo.outside, (ret, new(Span(seqinfo.span))))); |
| 479 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 480 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 481 | Err(err) => return Err(err), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 482 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 483 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 484 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | #[cfg(feature = "printing")] |
| 489 | mod printing { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 490 | use proc_macro2::{TokenTree, TokenNode, Spacing, Term}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 491 | use quote::Tokens; |
| 492 | |
| 493 | use span::Span; |
| 494 | |
| 495 | pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) { |
| 496 | assert_eq!(s.len(), spans.len()); |
| 497 | |
| 498 | let mut chars = s.chars(); |
| 499 | let mut spans = spans.iter(); |
| 500 | let ch = chars.next_back().unwrap(); |
| 501 | let span = spans.next_back().unwrap(); |
| 502 | for (ch, span) in chars.zip(spans) { |
| 503 | tokens.append(TokenTree { |
| 504 | span: span.0, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 505 | kind: TokenNode::Op(ch, Spacing::Joint), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 506 | }); |
| 507 | } |
| 508 | |
| 509 | tokens.append(TokenTree { |
| 510 | span: span.0, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 511 | kind: TokenNode::Op(ch, Spacing::Alone), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 512 | }); |
| 513 | } |
| 514 | |
| 515 | pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) { |
| 516 | tokens.append(TokenTree { |
| 517 | span: span.0, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 518 | kind: TokenNode::Term(Term::intern(s)), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 519 | }); |
| 520 | } |
| 521 | |
| 522 | pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F) |
| 523 | where F: FnOnce(&mut Tokens) |
| 524 | { |
| 525 | tokens.append_delimited(s, span.0, f) |
| 526 | } |
| 527 | } |