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 | |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame^] | 21 | pub fn new(scope: Span, cursor: Cursor) -> Lookahead1 { |
| 22 | Lookahead1 { |
| 23 | scope: scope, |
| 24 | cursor: cursor, |
| 25 | comparisons: RefCell::new(Vec::new()), |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 26 | } |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame^] | 27 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 28 | |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame^] | 29 | impl<'a> Lookahead1<'a> { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 30 | pub fn peek<T: Peek>(&self, token: T) -> bool { |
| 31 | let _ = token; |
| 32 | if T::Token::peek(self) { |
| 33 | return true; |
| 34 | } |
| 35 | self.comparisons.borrow_mut().push(T::Token::display()); |
| 36 | false |
| 37 | } |
| 38 | |
| 39 | pub fn error(self) -> Error { |
David Tolnay | da1dc7c | 2018-08-24 11:57:28 -0400 | [diff] [blame] | 40 | let comparisons = self.comparisons.borrow(); |
| 41 | match comparisons.len() { |
| 42 | 0 => if self.cursor.eof() { |
| 43 | Error::new(self.scope, "unexpected end of input") |
| 44 | } else { |
| 45 | Error::new(self.cursor.span(), "unexpected token") |
| 46 | }, |
| 47 | 1 => { |
| 48 | let message = format!("expected {}", comparisons[0]); |
| 49 | error::new_at(self.scope, self.cursor, message) |
| 50 | } |
| 51 | _ => { |
David Tolnay | c6e86c7 | 2018-08-24 12:28:40 -0400 | [diff] [blame] | 52 | let join = comparisons.join(", "); |
| 53 | let message = format!("expected one of: {}", join); |
David Tolnay | da1dc7c | 2018-08-24 11:57:28 -0400 | [diff] [blame] | 54 | error::new_at(self.scope, self.cursor, message) |
| 55 | } |
| 56 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 57 | } |
David Tolnay | d983692 | 2018-08-25 18:05:36 -0400 | [diff] [blame] | 58 | |
David Tolnay | d983692 | 2018-08-25 18:05:36 -0400 | [diff] [blame] | 59 | pub fn cursor(&self) -> Cursor<'a> { |
| 60 | self.cursor |
| 61 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /// Types that can be parsed by looking at just one token. |
| 65 | /// |
| 66 | /// This trait is sealed and cannot be implemented for types outside of Syn. |
| 67 | pub trait Peek: private::Sealed { |
| 68 | // Not public API. |
| 69 | #[doc(hidden)] |
| 70 | type Token: Token; |
| 71 | } |
| 72 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 73 | impl<F: FnOnce(TokenMarker) -> T, T: Token> Peek for F { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 74 | type Token = T; |
| 75 | } |
| 76 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 77 | pub enum TokenMarker {} |
| 78 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 79 | impl<S> IntoSpans<S> for TokenMarker { |
| 80 | fn into_spans(self) -> S { |
| 81 | match self {} |
| 82 | } |
| 83 | } |
| 84 | |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 85 | pub fn is_delimiter(lookahead: &Lookahead1, delimiter: Delimiter) -> bool { |
| 86 | lookahead.cursor.group(delimiter).is_some() |
| 87 | } |
| 88 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 89 | mod private { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 90 | use super::{Token, TokenMarker}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 91 | pub trait Sealed {} |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 92 | impl<F: FnOnce(TokenMarker) -> T, T: Token> Sealed for F {} |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 93 | } |