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