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 | /// |
David Tolnay | 5c70ede | 2018-09-01 10:07:56 -0700 | [diff] [blame] | 12 | /// An important advantage over [`ParseStream::peek`] is that here we |
| 13 | /// automatically construct an appropriate error message based on the token |
| 14 | /// alternatives that get peeked. If you are producing your own error message, |
| 15 | /// go ahead and use `ParseStream::peek` instead. |
| 16 | /// |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 17 | /// Use [`ParseStream::lookahead1`] to construct this object. |
| 18 | /// |
David Tolnay | 5c70ede | 2018-09-01 10:07:56 -0700 | [diff] [blame] | 19 | /// [`ParseStream::peek`]: struct.ParseBuffer.html#method.peek |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 20 | /// [`ParseStream::lookahead1`]: struct.ParseBuffer.html#method.lookahead1 |
David Tolnay | 956449d | 2018-09-01 10:17:54 -0700 | [diff] [blame] | 21 | /// |
| 22 | /// # Example |
| 23 | /// |
| 24 | /// ``` |
| 25 | /// # extern crate syn; |
| 26 | /// # |
| 27 | /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Token, TypeParam}; |
| 28 | /// use syn::parse::{Parse, ParseStream, Result}; |
| 29 | /// |
| 30 | /// // A generic parameter, a single one of the comma-separated elements inside |
| 31 | /// // angle brackets in: |
| 32 | /// // |
| 33 | /// // fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... } |
| 34 | /// // |
| 35 | /// // On invalid input, lookahead gives us a reasonable error message. |
| 36 | /// // |
| 37 | /// // error: expected one of: identifier, lifetime, `const` |
| 38 | /// // | |
| 39 | /// // 5 | fn f<!Sized>() {} |
| 40 | /// // | ^ |
| 41 | /// enum GenericParam { |
| 42 | /// Type(TypeParam), |
| 43 | /// Lifetime(LifetimeDef), |
| 44 | /// Const(ConstParam), |
| 45 | /// } |
| 46 | /// |
| 47 | /// impl Parse for GenericParam { |
| 48 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 49 | /// let lookahead = input.lookahead1(); |
| 50 | /// if lookahead.peek(Ident) { |
| 51 | /// input.parse().map(GenericParam::Type) |
| 52 | /// } else if lookahead.peek(Lifetime) { |
| 53 | /// input.parse().map(GenericParam::Lifetime) |
| 54 | /// } else if lookahead.peek(Token![const]) { |
| 55 | /// input.parse().map(GenericParam::Const) |
| 56 | /// } else { |
| 57 | /// Err(lookahead.error()) |
| 58 | /// } |
| 59 | /// } |
| 60 | /// } |
| 61 | /// # |
| 62 | /// # fn main() {} |
| 63 | /// ``` |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 64 | pub struct Lookahead1<'a> { |
| 65 | scope: Span, |
| 66 | cursor: Cursor<'a>, |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 67 | comparisons: RefCell<Vec<&'static str>>, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 68 | } |
| 69 | |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 70 | pub fn new(scope: Span, cursor: Cursor) -> Lookahead1 { |
| 71 | Lookahead1 { |
| 72 | scope: scope, |
| 73 | cursor: cursor, |
| 74 | comparisons: RefCell::new(Vec::new()), |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 75 | } |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 76 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 77 | |
David Tolnay | cae851e | 2018-09-01 10:59:22 -0700 | [diff] [blame] | 78 | fn peek_impl( |
| 79 | lookahead: &Lookahead1, |
| 80 | peek: fn(Cursor) -> bool, |
| 81 | display: fn() -> &'static str, |
| 82 | ) -> bool { |
| 83 | if peek(lookahead.cursor) { |
| 84 | return true; |
| 85 | } |
| 86 | lookahead.comparisons.borrow_mut().push(display()); |
| 87 | false |
| 88 | } |
| 89 | |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 90 | impl<'a> Lookahead1<'a> { |
David Tolnay | 6d829fc | 2018-09-01 14:20:32 -0700 | [diff] [blame^] | 91 | /// Looks at the next token in the parse stream to determine whether it |
| 92 | /// matches the requested type of token. |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 93 | pub fn peek<T: Peek>(&self, token: T) -> bool { |
| 94 | let _ = token; |
David Tolnay | cae851e | 2018-09-01 10:59:22 -0700 | [diff] [blame] | 95 | peek_impl(self, T::Token::peek, T::Token::display) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 96 | } |
| 97 | |
David Tolnay | 6d829fc | 2018-09-01 14:20:32 -0700 | [diff] [blame^] | 98 | /// Triggers an error at the current position of the parse stream. |
| 99 | /// |
| 100 | /// The error message will identify all of the expected token types that |
| 101 | /// have been peeked against this lookahead instance. |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 102 | pub fn error(self) -> Error { |
David Tolnay | da1dc7c | 2018-08-24 11:57:28 -0400 | [diff] [blame] | 103 | let comparisons = self.comparisons.borrow(); |
| 104 | match comparisons.len() { |
| 105 | 0 => if self.cursor.eof() { |
| 106 | Error::new(self.scope, "unexpected end of input") |
| 107 | } else { |
| 108 | Error::new(self.cursor.span(), "unexpected token") |
| 109 | }, |
| 110 | 1 => { |
| 111 | let message = format!("expected {}", comparisons[0]); |
| 112 | error::new_at(self.scope, self.cursor, message) |
| 113 | } |
| 114 | _ => { |
David Tolnay | c6e86c7 | 2018-08-24 12:28:40 -0400 | [diff] [blame] | 115 | let join = comparisons.join(", "); |
| 116 | let message = format!("expected one of: {}", join); |
David Tolnay | da1dc7c | 2018-08-24 11:57:28 -0400 | [diff] [blame] | 117 | error::new_at(self.scope, self.cursor, message) |
| 118 | } |
| 119 | } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /// Types that can be parsed by looking at just one token. |
| 124 | /// |
David Tolnay | 8a44dbf | 2018-09-01 10:00:43 -0700 | [diff] [blame] | 125 | /// Use [`ParseStream::peek`] to peek one of these types in a parse stream |
| 126 | /// without consuming it from the stream. |
| 127 | /// |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 128 | /// This trait is sealed and cannot be implemented for types outside of Syn. |
David Tolnay | 8a44dbf | 2018-09-01 10:00:43 -0700 | [diff] [blame] | 129 | /// |
| 130 | /// [`ParseStream::peek`]: struct.ParseBuffer.html#method.peek |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 131 | pub trait Peek: private::Sealed { |
| 132 | // Not public API. |
| 133 | #[doc(hidden)] |
| 134 | type Token: Token; |
| 135 | } |
| 136 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 137 | impl<F: FnOnce(TokenMarker) -> T, T: Token> Peek for F { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 138 | type Token = T; |
| 139 | } |
| 140 | |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 141 | pub enum TokenMarker {} |
| 142 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 143 | impl<S> IntoSpans<S> for TokenMarker { |
| 144 | fn into_spans(self) -> S { |
| 145 | match self {} |
| 146 | } |
| 147 | } |
| 148 | |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 149 | pub fn is_delimiter(cursor: Cursor, delimiter: Delimiter) -> bool { |
| 150 | cursor.group(delimiter).is_some() |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 151 | } |
| 152 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 153 | mod private { |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 154 | use super::{Token, TokenMarker}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 155 | pub trait Sealed {} |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame] | 156 | impl<F: FnOnce(TokenMarker) -> T, T: Token> Sealed for F {} |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 157 | } |