blob: 5049f28b51add9e1d1349d0b5f5ec115f4476544 [file] [log] [blame]
David Tolnay18c754c2018-08-21 23:26:58 -04001use std::cell::RefCell;
2
David Tolnay2d84a082018-08-25 16:31:38 -04003use proc_macro2::{Delimiter, Span};
David Tolnay18c754c2018-08-21 23:26:58 -04004
David Tolnayad4b2472018-08-25 08:25:24 -04005use buffer::Cursor;
6use error::{self, Error};
David Tolnay776f8e02018-08-24 22:32:10 -04007use span::IntoSpans;
David Tolnay776f8e02018-08-24 22:32:10 -04008use token::Token;
David Tolnay18c754c2018-08-21 23:26:58 -04009
10/// Support for checking the next token in a stream to decide how to parse.
11///
David Tolnay5c70ede2018-09-01 10:07:56 -070012/// 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 Tolnay18c754c2018-08-21 23:26:58 -040017/// Use [`ParseStream::lookahead1`] to construct this object.
18///
David Tolnay5c70ede2018-09-01 10:07:56 -070019/// [`ParseStream::peek`]: struct.ParseBuffer.html#method.peek
David Tolnay18c754c2018-08-21 23:26:58 -040020/// [`ParseStream::lookahead1`]: struct.ParseBuffer.html#method.lookahead1
21pub struct Lookahead1<'a> {
22 scope: Span,
23 cursor: Cursor<'a>,
24 comparisons: RefCell<Vec<String>>,
25}
26
David Tolnay94f06632018-08-31 10:17:17 -070027pub fn new(scope: Span, cursor: Cursor) -> Lookahead1 {
28 Lookahead1 {
29 scope: scope,
30 cursor: cursor,
31 comparisons: RefCell::new(Vec::new()),
David Tolnay18c754c2018-08-21 23:26:58 -040032 }
David Tolnay94f06632018-08-31 10:17:17 -070033}
David Tolnay18c754c2018-08-21 23:26:58 -040034
David Tolnay94f06632018-08-31 10:17:17 -070035impl<'a> Lookahead1<'a> {
David Tolnay18c754c2018-08-21 23:26:58 -040036 pub fn peek<T: Peek>(&self, token: T) -> bool {
37 let _ = token;
38 if T::Token::peek(self) {
39 return true;
40 }
41 self.comparisons.borrow_mut().push(T::Token::display());
42 false
43 }
44
45 pub fn error(self) -> Error {
David Tolnayda1dc7c2018-08-24 11:57:28 -040046 let comparisons = self.comparisons.borrow();
47 match comparisons.len() {
48 0 => if self.cursor.eof() {
49 Error::new(self.scope, "unexpected end of input")
50 } else {
51 Error::new(self.cursor.span(), "unexpected token")
52 },
53 1 => {
54 let message = format!("expected {}", comparisons[0]);
55 error::new_at(self.scope, self.cursor, message)
56 }
57 _ => {
David Tolnayc6e86c72018-08-24 12:28:40 -040058 let join = comparisons.join(", ");
59 let message = format!("expected one of: {}", join);
David Tolnayda1dc7c2018-08-24 11:57:28 -040060 error::new_at(self.scope, self.cursor, message)
61 }
62 }
David Tolnay18c754c2018-08-21 23:26:58 -040063 }
David Tolnayd9836922018-08-25 18:05:36 -040064
David Tolnayd9836922018-08-25 18:05:36 -040065 pub fn cursor(&self) -> Cursor<'a> {
66 self.cursor
67 }
David Tolnay18c754c2018-08-21 23:26:58 -040068}
69
70/// Types that can be parsed by looking at just one token.
71///
David Tolnay8a44dbf2018-09-01 10:00:43 -070072/// Use [`ParseStream::peek`] to peek one of these types in a parse stream
73/// without consuming it from the stream.
74///
David Tolnay18c754c2018-08-21 23:26:58 -040075/// This trait is sealed and cannot be implemented for types outside of Syn.
David Tolnay8a44dbf2018-09-01 10:00:43 -070076///
77/// [`ParseStream::peek`]: struct.ParseBuffer.html#method.peek
David Tolnay18c754c2018-08-21 23:26:58 -040078pub trait Peek: private::Sealed {
79 // Not public API.
80 #[doc(hidden)]
81 type Token: Token;
82}
83
David Tolnay2b687b02018-08-24 13:36:36 -040084impl<F: FnOnce(TokenMarker) -> T, T: Token> Peek for F {
David Tolnay18c754c2018-08-21 23:26:58 -040085 type Token = T;
86}
87
David Tolnay2b687b02018-08-24 13:36:36 -040088pub enum TokenMarker {}
89
David Tolnay776f8e02018-08-24 22:32:10 -040090impl<S> IntoSpans<S> for TokenMarker {
91 fn into_spans(self) -> S {
92 match self {}
93 }
94}
95
David Tolnay2d84a082018-08-25 16:31:38 -040096pub fn is_delimiter(lookahead: &Lookahead1, delimiter: Delimiter) -> bool {
97 lookahead.cursor.group(delimiter).is_some()
98}
99
David Tolnay18c754c2018-08-21 23:26:58 -0400100mod private {
David Tolnay2b687b02018-08-24 13:36:36 -0400101 use super::{Token, TokenMarker};
David Tolnay18c754c2018-08-21 23:26:58 -0400102 pub trait Sealed {}
David Tolnay2b687b02018-08-24 13:36:36 -0400103 impl<F: FnOnce(TokenMarker) -> T, T: Token> Sealed for F {}
David Tolnay18c754c2018-08-21 23:26:58 -0400104}