David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 1 | use std::cell::RefCell; |
| 2 | |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame^] | 3 | use proc_macro2::{Delimiter, Span}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 4 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 5 | use buffer::Cursor; |
| 6 | use error::{self, Error}; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 7 | use span::IntoSpans; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 8 | use token::Token; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 9 | |
| 10 | /// Support for checking the next token in a stream to decide how to parse. |
| 11 | /// |
| 12 | /// Use [`ParseStream::lookahead1`] to construct this object. |
| 13 | /// |
| 14 | /// [`ParseStream::lookahead1`]: struct.ParseBuffer.html#method.lookahead1 |
| 15 | pub struct Lookahead1<'a> { |
| 16 | scope: Span, |
| 17 | cursor: Cursor<'a>, |
| 18 | comparisons: RefCell<Vec<String>>, |
| 19 | } |
| 20 | |
| 21 | impl<'a> Lookahead1<'a> { |
| 22 | // Not public API. |
| 23 | #[doc(hidden)] |
| 24 | pub fn new(scope: Span, cursor: Cursor<'a>) -> Self { |
| 25 | Lookahead1 { |
| 26 | scope: scope, |
| 27 | cursor: cursor, |
| 28 | comparisons: RefCell::new(Vec::new()), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn peek<T: Peek>(&self, token: T) -> bool { |
| 33 | let _ = token; |
| 34 | if T::Token::peek(self) { |
| 35 | return true; |
| 36 | } |
| 37 | self.comparisons.borrow_mut().push(T::Token::display()); |
| 38 | false |
| 39 | } |
| 40 | |
| 41 | pub fn error(self) -> Error { |
David Tolnay | da1dc7c | 2018-08-24 11:57:28 -0400 | [diff] [blame] | 42 | let comparisons = self.comparisons.borrow(); |
| 43 | match comparisons.len() { |
| 44 | 0 => if self.cursor.eof() { |
| 45 | Error::new(self.scope, "unexpected end of input") |
| 46 | } else { |
| 47 | Error::new(self.cursor.span(), "unexpected token") |
| 48 | }, |
| 49 | 1 => { |
| 50 | let message = format!("expected {}", comparisons[0]); |
| 51 | error::new_at(self.scope, self.cursor, message) |
| 52 | } |
| 53 | _ => { |
David Tolnay | c6e86c7 | 2018-08-24 12:28:40 -0400 | [diff] [blame] | 54 | let join = comparisons.join(", "); |
| 55 | let message = format!("expected one of: {}", join); |
David Tolnay | da1dc7c | 2018-08-24 11:57:28 -0400 | [diff] [blame] | 56 | error::new_at(self.scope, self.cursor, message) |
| 57 | } |
| 58 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Types that can be parsed by looking at just one token. |
| 63 | /// |
| 64 | /// This trait is sealed and cannot be implemented for types outside of Syn. |
| 65 | pub trait Peek: private::Sealed { |
| 66 | // Not public API. |
| 67 | #[doc(hidden)] |
| 68 | type Token: Token; |
| 69 | } |
| 70 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 71 | impl<F: FnOnce(TokenMarker) -> T, T: Token> Peek for F { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 72 | type Token = T; |
| 73 | } |
| 74 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 75 | pub enum TokenMarker {} |
| 76 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 77 | impl<S> IntoSpans<S> for TokenMarker { |
| 78 | fn into_spans(self) -> S { |
| 79 | match self {} |
| 80 | } |
| 81 | } |
| 82 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 83 | // Not public API. |
| 84 | #[doc(hidden)] |
| 85 | pub fn is_token(lookahead: &Lookahead1, repr: &'static str) -> bool { |
| 86 | if let Some((token, _rest)) = lookahead.cursor.token_tree() { |
| 87 | token.to_string() == repr |
| 88 | } else { |
| 89 | false |
| 90 | } |
| 91 | } |
| 92 | |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame^] | 93 | // Not public API. |
| 94 | #[doc(hidden)] |
| 95 | pub fn is_delimiter(lookahead: &Lookahead1, delimiter: Delimiter) -> bool { |
| 96 | lookahead.cursor.group(delimiter).is_some() |
| 97 | } |
| 98 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 99 | mod private { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 100 | use super::{Token, TokenMarker}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 101 | pub trait Sealed {} |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 102 | impl<F: FnOnce(TokenMarker) -> T, T: Token> Sealed for F {} |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 103 | } |