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))] |
| 31 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 32 | #[derive(Default)] |
| 33 | pub struct $name(pub $($contents)*); |
| 34 | |
| 35 | #[cfg(feature = "printing")] |
| 36 | impl ::quote::ToTokens for $name { |
| 37 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
| 38 | printing::op($s, &self.0, tokens); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[cfg(feature = "parsing")] |
| 43 | impl ::Synom for $name { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 44 | fn parse(tokens: $crate::Cursor) -> $crate::PResult<$name> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 45 | parsing::op($s, tokens, $name) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | macro_rules! sym { |
| 52 | (pub struct $name:ident => $s:expr) => { |
| 53 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 54 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 55 | #[derive(Default)] |
| 56 | pub struct $name(pub Span); |
| 57 | |
| 58 | #[cfg(feature = "printing")] |
| 59 | impl ::quote::ToTokens for $name { |
| 60 | fn to_tokens(&self, tokens: &mut ::quote::Tokens) { |
| 61 | printing::sym($s, &self.0, tokens); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[cfg(feature = "parsing")] |
| 66 | impl ::Synom for $name { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 67 | fn parse(tokens: $crate::Cursor) -> $crate::PResult<$name> { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 68 | parsing::sym($s, tokens, $name) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | macro_rules! delim { |
| 75 | (pub struct $name:ident => $s:expr) => { |
| 76 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 77 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 78 | #[derive(Default)] |
| 79 | pub struct $name(pub Span); |
| 80 | |
| 81 | impl $name { |
| 82 | #[cfg(feature = "printing")] |
| 83 | pub fn surround<F>(&self, |
| 84 | tokens: &mut ::quote::Tokens, |
| 85 | f: F) |
| 86 | where F: FnOnce(&mut ::quote::Tokens) |
| 87 | { |
| 88 | printing::delim($s, &self.0, tokens, f); |
| 89 | } |
| 90 | |
| 91 | #[cfg(feature = "parsing")] |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 92 | pub fn parse<F, R>(tokens: $crate::Cursor, f: F) -> $crate::PResult<(R, $name)> |
| 93 | where F: FnOnce($crate::Cursor) -> $crate::PResult<R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 94 | { |
| 95 | parsing::delim($s, tokens, $name, f) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | tokens! { |
| 102 | ops: { |
| 103 | (pub struct Add([Span; 1]) => "+"), |
| 104 | (pub struct AddEq([Span; 2]) => "+="), |
| 105 | (pub struct And([Span; 1]) => "&"), |
| 106 | (pub struct AndAnd([Span; 2]) => "&&"), |
| 107 | (pub struct AndEq([Span; 2]) => "&="), |
| 108 | (pub struct At([Span; 1]) => "@"), |
| 109 | (pub struct Bang([Span; 1]) => "!"), |
| 110 | (pub struct Caret([Span; 1]) => "^"), |
| 111 | (pub struct CaretEq([Span; 2]) => "^="), |
| 112 | (pub struct Colon([Span; 1]) => ":"), |
| 113 | (pub struct Colon2([Span; 2]) => "::"), |
| 114 | (pub struct Comma([Span; 1]) => ","), |
| 115 | (pub struct Div([Span; 1]) => "/"), |
| 116 | (pub struct DivEq([Span; 2]) => "/="), |
| 117 | (pub struct Dot([Span; 1]) => "."), |
| 118 | (pub struct Dot2([Span; 2]) => ".."), |
| 119 | (pub struct Dot3([Span; 3]) => "..."), |
| 120 | (pub struct Eq([Span; 1]) => "="), |
| 121 | (pub struct EqEq([Span; 2]) => "=="), |
| 122 | (pub struct Ge([Span; 2]) => ">="), |
| 123 | (pub struct Gt([Span; 1]) => ">"), |
| 124 | (pub struct Le([Span; 2]) => "<="), |
| 125 | (pub struct Lt([Span; 1]) => "<"), |
| 126 | (pub struct MulEq([Span; 2]) => "*="), |
| 127 | (pub struct Ne([Span; 2]) => "!="), |
| 128 | (pub struct Or([Span; 1]) => "|"), |
| 129 | (pub struct OrEq([Span; 2]) => "|="), |
| 130 | (pub struct OrOr([Span; 2]) => "||"), |
| 131 | (pub struct Pound([Span; 1]) => "#"), |
| 132 | (pub struct Question([Span; 1]) => "?"), |
| 133 | (pub struct RArrow([Span; 2]) => "->"), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 134 | (pub struct LArrow([Span; 2]) => "<-"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 135 | (pub struct Rem([Span; 1]) => "%"), |
| 136 | (pub struct RemEq([Span; 2]) => "%="), |
| 137 | (pub struct Rocket([Span; 2]) => "=>"), |
| 138 | (pub struct Semi([Span; 1]) => ";"), |
| 139 | (pub struct Shl([Span; 2]) => "<<"), |
| 140 | (pub struct ShlEq([Span; 3]) => "<<="), |
| 141 | (pub struct Shr([Span; 2]) => ">>"), |
| 142 | (pub struct ShrEq([Span; 3]) => ">>="), |
| 143 | (pub struct Star([Span; 1]) => "*"), |
| 144 | (pub struct Sub([Span; 1]) => "-"), |
| 145 | (pub struct SubEq([Span; 2]) => "-="), |
| 146 | (pub struct Underscore([Span; 1]) => "_"), |
| 147 | } |
| 148 | delim: { |
| 149 | (pub struct Brace => "{"), |
| 150 | (pub struct Bracket => "["), |
| 151 | (pub struct Paren => "("), |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame^] | 152 | (pub struct Group => " "), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 153 | } |
| 154 | syms: { |
| 155 | (pub struct As => "as"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 156 | (pub struct Box_ => "box"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 157 | (pub struct Break => "break"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 158 | (pub struct CapSelf => "Self"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 159 | (pub struct Catch => "catch"), |
| 160 | (pub struct Const => "const"), |
| 161 | (pub struct Continue => "continue"), |
| 162 | (pub struct Crate => "crate"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 163 | (pub struct Default_ => "default"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 164 | (pub struct Do => "do"), |
| 165 | (pub struct Else => "else"), |
| 166 | (pub struct Enum => "enum"), |
| 167 | (pub struct Extern => "extern"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 168 | (pub struct Fn_ => "fn"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 169 | (pub struct For => "for"), |
| 170 | (pub struct If => "if"), |
| 171 | (pub struct Impl => "impl"), |
| 172 | (pub struct In => "in"), |
| 173 | (pub struct Let => "let"), |
| 174 | (pub struct Loop => "loop"), |
| 175 | (pub struct Match => "match"), |
| 176 | (pub struct Mod => "mod"), |
| 177 | (pub struct Move => "move"), |
| 178 | (pub struct Mut => "mut"), |
| 179 | (pub struct Pub => "pub"), |
| 180 | (pub struct Ref => "ref"), |
| 181 | (pub struct Return => "return"), |
| 182 | (pub struct Self_ => "self"), |
| 183 | (pub struct Static => "static"), |
| 184 | (pub struct Struct => "struct"), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 185 | (pub struct Super => "super"), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 186 | (pub struct Trait => "trait"), |
| 187 | (pub struct Type => "type"), |
| 188 | (pub struct Union => "union"), |
| 189 | (pub struct Unsafe => "unsafe"), |
| 190 | (pub struct Use => "use"), |
| 191 | (pub struct Where => "where"), |
| 192 | (pub struct While => "while"), |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #[cfg(feature = "parsing")] |
| 197 | mod parsing { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 198 | use proc_macro2::{Delimiter, OpKind}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 199 | |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 200 | use {PResult, Cursor, parse_error}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 201 | use span::Span; |
| 202 | |
| 203 | pub trait FromSpans: Sized { |
| 204 | fn from_spans(spans: &[Span]) -> Self; |
| 205 | } |
| 206 | |
| 207 | impl FromSpans for [Span; 1] { |
| 208 | fn from_spans(spans: &[Span]) -> Self { |
| 209 | [spans[0]] |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | impl FromSpans for [Span; 2] { |
| 214 | fn from_spans(spans: &[Span]) -> Self { |
| 215 | [spans[0], spans[1]] |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl FromSpans for [Span; 3] { |
| 220 | fn from_spans(spans: &[Span]) -> Self { |
| 221 | [spans[0], spans[1], spans[2]] |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | pub fn op<'a, T, R>(s: &str, |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 226 | mut tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 227 | new: fn(T) -> R) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 228 | -> PResult<'a, R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 229 | where T: FromSpans, |
| 230 | { |
| 231 | let mut spans = [Span::default(); 3]; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 232 | assert!(s.len() <= spans.len()); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 233 | let chars = s.chars(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 234 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 235 | for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 236 | match tokens.op() { |
| 237 | Some((rest, span, c, kind)) if c == ch => { |
| 238 | if i != s.len() - 1 { |
| 239 | if kind != OpKind::Joint { |
| 240 | return parse_error(); |
| 241 | } |
| 242 | } |
| 243 | *slot = Span(span); |
| 244 | tokens = rest; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 245 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 246 | _ => return parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 247 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 248 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 249 | Ok((tokens, new(T::from_spans(&spans)))) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | pub fn sym<'a, T>(sym: &str, |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 253 | tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 254 | new: fn(Span) -> T) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 255 | -> PResult<'a, T> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 256 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 257 | if let Some((rest, span, s)) = tokens.word() { |
| 258 | if s.as_str() == sym { |
| 259 | return Ok((rest, new(Span(span)))); |
| 260 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 261 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 262 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | pub fn delim<'a, F, R, T>(delim: &str, |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 266 | tokens: Cursor<'a>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 267 | new: fn(Span) -> T, |
| 268 | f: F) |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 269 | -> PResult<'a, (R, T)> |
| 270 | where F: FnOnce(Cursor) -> PResult<R> |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 271 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 272 | // NOTE: We should support none-delimited sequences here. |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 273 | let delim = match delim { |
| 274 | "(" => Delimiter::Parenthesis, |
| 275 | "{" => Delimiter::Brace, |
| 276 | "[" => Delimiter::Bracket, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame^] | 277 | " " => Delimiter::None, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 278 | _ => panic!("unknown delimiter: {}", delim), |
| 279 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 280 | |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 281 | if let Some(seqinfo) = tokens.seq(delim) { |
| 282 | match f(seqinfo.inside) { |
| 283 | Ok((remaining, ret)) => { |
| 284 | if remaining.eof() { |
| 285 | return Ok((seqinfo.outside, (ret, new(Span(seqinfo.span))))); |
| 286 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 287 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 288 | Err(err) => return Err(err), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 289 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 290 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 291 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | #[cfg(feature = "printing")] |
| 296 | mod printing { |
| 297 | use proc_macro2::{TokenTree, TokenKind, OpKind}; |
| 298 | use quote::Tokens; |
| 299 | |
| 300 | use span::Span; |
| 301 | |
| 302 | pub fn op(s: &str, spans: &[Span], tokens: &mut Tokens) { |
| 303 | assert_eq!(s.len(), spans.len()); |
| 304 | |
| 305 | let mut chars = s.chars(); |
| 306 | let mut spans = spans.iter(); |
| 307 | let ch = chars.next_back().unwrap(); |
| 308 | let span = spans.next_back().unwrap(); |
| 309 | for (ch, span) in chars.zip(spans) { |
| 310 | tokens.append(TokenTree { |
| 311 | span: span.0, |
| 312 | kind: TokenKind::Op(ch, OpKind::Joint), |
| 313 | }); |
| 314 | } |
| 315 | |
| 316 | tokens.append(TokenTree { |
| 317 | span: span.0, |
| 318 | kind: TokenKind::Op(ch, OpKind::Alone), |
| 319 | }); |
| 320 | } |
| 321 | |
| 322 | pub fn sym(s: &str, span: &Span, tokens: &mut Tokens) { |
| 323 | tokens.append(TokenTree { |
| 324 | span: span.0, |
| 325 | kind: TokenKind::Word(s.into()), |
| 326 | }); |
| 327 | } |
| 328 | |
| 329 | pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F) |
| 330 | where F: FnOnce(&mut Tokens) |
| 331 | { |
| 332 | tokens.append_delimited(s, span.0, f) |
| 333 | } |
| 334 | } |