David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 1 | //! Tokens representing Rust punctuation, keywords, and delimiters. |
| 2 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 3 | use std::ops::{Deref, DerefMut}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 4 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 5 | use proc_macro2::{Spacing, Span}; |
| 6 | |
David Tolnay | 544a90f | 2018-08-24 20:34:03 -0400 | [diff] [blame^] | 7 | use super::error::Error; |
| 8 | use super::lookahead; |
| 9 | use super::parse::{Lookahead1, Parse, ParseStream, Result}; |
| 10 | use super::span::{FromSpans, IntoSpans}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 11 | |
| 12 | /// Marker trait for types that represent single tokens. |
| 13 | /// |
| 14 | /// This trait is sealed and cannot be implemented for types outside of Syn. |
| 15 | pub trait Token: private::Sealed { |
| 16 | // Not public API. |
| 17 | #[doc(hidden)] |
| 18 | fn peek(lookahead: &Lookahead1) -> bool; |
| 19 | |
| 20 | // Not public API. |
| 21 | #[doc(hidden)] |
| 22 | fn display() -> String; |
| 23 | } |
| 24 | |
| 25 | mod private { |
| 26 | pub trait Sealed {} |
| 27 | } |
| 28 | |
| 29 | /// A type-macro that expands to the name of the Rust type representation of a |
| 30 | /// given token. |
| 31 | #[macro_export] |
| 32 | #[cfg_attr(rustfmt, rustfmt_skip)] |
| 33 | macro_rules! Token { |
David Tolnay | 544a90f | 2018-08-24 20:34:03 -0400 | [diff] [blame^] | 34 | (struct) => { $crate::next::token::Struct }; |
| 35 | (enum) => { $crate::next::token::Enum }; |
| 36 | (:) => { $crate::next::token::Colon }; |
| 37 | (,) => { $crate::next::token::Comma }; |
| 38 | (..) => { $crate::next::token::Dot2 }; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 39 | } |
| 40 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 41 | macro_rules! impl_token { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 42 | ($token:tt $name:ident #[$doc:meta]) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 43 | impl Token for $name { |
| 44 | fn peek(lookahead: &Lookahead1) -> bool { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 45 | lookahead::is_token(lookahead, $token) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | fn display() -> String { |
| 49 | concat!("`", $token, "`").to_owned() |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl private::Sealed for $name {} |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | macro_rules! define_keywords { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 58 | ($($token:tt pub struct $name:ident #[$doc:meta])*) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 59 | $( |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 60 | #[$doc] |
| 61 | #[derive(Debug)] |
| 62 | pub struct $name { |
| 63 | pub span: Span, |
| 64 | } |
| 65 | |
| 66 | #[doc(hidden)] |
| 67 | #[allow(non_snake_case)] |
| 68 | pub fn $name<T: IntoSpans<[Span; 1]>>(span: T) -> $name { |
| 69 | $name { |
| 70 | span: span.into_spans()[0], |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl_token!($token $name #[$doc]); |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 75 | |
| 76 | impl Parse for $name { |
| 77 | fn parse(input: ParseStream) -> Result<Self> { |
| 78 | parse_keyword(input, $token).map($name) |
| 79 | } |
| 80 | } |
| 81 | )* |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | macro_rules! define_punctuation { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 86 | ($($token:tt pub struct $name:ident/$len:tt #[$doc:meta])*) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 87 | $( |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 88 | #[$doc] |
| 89 | #[derive(Debug)] |
| 90 | pub struct $name { |
| 91 | pub spans: [Span; $len], |
| 92 | } |
| 93 | |
| 94 | impl Deref for $name { |
| 95 | type Target = [Span; $len]; |
| 96 | |
| 97 | fn deref(&self) -> &Self::Target { |
| 98 | &self.spans |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | impl DerefMut for $name { |
| 103 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 104 | &mut self.spans |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[doc(hidden)] |
| 109 | #[allow(non_snake_case)] |
| 110 | pub fn $name<T: IntoSpans<[Span; $len]>>(spans: T) -> $name { |
| 111 | $name { |
| 112 | spans: spans.into_spans(), |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl_token!($token $name #[$doc]); |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 117 | |
| 118 | impl Parse for $name { |
| 119 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 120 | parse_punctuation(input, $token).map($name::<[Span; $len]>) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | )* |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | define_keywords! { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 128 | "struct" pub struct Struct /// `struct` |
| 129 | "enum" pub struct Enum /// `enum` |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | define_punctuation! { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 133 | ":" pub struct Colon/1 /// `:` |
| 134 | "," pub struct Comma/1 /// `,` |
| 135 | ".." pub struct Dot2/2 /// `..` |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /// `{...}` |
| 139 | #[derive(Debug)] |
| 140 | pub struct Brace(pub Span); |
| 141 | |
| 142 | fn parse_keyword(input: ParseStream, token: &str) -> Result<Span> { |
| 143 | input.step_cursor(|cursor| { |
| 144 | if let Some((ident, rest)) = cursor.ident() { |
| 145 | if ident == token { |
| 146 | return Ok((ident.span(), rest)); |
| 147 | } |
| 148 | } |
| 149 | Err(cursor.error(format!("expected `{}`", token))) |
| 150 | }) |
| 151 | } |
| 152 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 153 | fn parse_punctuation<S: FromSpans>(input: ParseStream, token: &str) -> Result<S> { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 154 | input.step_cursor(|cursor| { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 155 | let mut cursor = *cursor; |
| 156 | let mut spans = [cursor.span(); 3]; |
| 157 | assert!(token.len() <= spans.len()); |
| 158 | |
| 159 | for (i, ch) in token.chars().enumerate() { |
| 160 | match cursor.punct() { |
| 161 | Some((punct, rest)) => { |
| 162 | spans[i] = punct.span(); |
| 163 | if punct.as_char() != ch { |
| 164 | break; |
| 165 | } else if i == token.len() - 1 { |
| 166 | return Ok((S::from_spans(&spans), rest)); |
| 167 | } else if punct.spacing() != Spacing::Joint { |
| 168 | break; |
| 169 | } |
| 170 | cursor = rest; |
| 171 | } |
| 172 | None => break, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 175 | |
| 176 | Err(Error::new(spans[0], format!("expected `{}`", token))) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 177 | }) |
| 178 | } |