blob: 47ac71c9797012ac4aa7b20dccd340311927a6ed [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///
12/// Use [`ParseStream::lookahead1`] to construct this object.
13///
14/// [`ParseStream::lookahead1`]: struct.ParseBuffer.html#method.lookahead1
15pub struct Lookahead1<'a> {
16 scope: Span,
17 cursor: Cursor<'a>,
18 comparisons: RefCell<Vec<String>>,
19}
20
David Tolnay94f06632018-08-31 10:17:17 -070021pub fn new(scope: Span, cursor: Cursor) -> Lookahead1 {
22 Lookahead1 {
23 scope: scope,
24 cursor: cursor,
25 comparisons: RefCell::new(Vec::new()),
David Tolnay18c754c2018-08-21 23:26:58 -040026 }
David Tolnay94f06632018-08-31 10:17:17 -070027}
David Tolnay18c754c2018-08-21 23:26:58 -040028
David Tolnay94f06632018-08-31 10:17:17 -070029impl<'a> Lookahead1<'a> {
David Tolnay18c754c2018-08-21 23:26:58 -040030 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 Tolnayda1dc7c2018-08-24 11:57:28 -040040 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 Tolnayc6e86c72018-08-24 12:28:40 -040052 let join = comparisons.join(", ");
53 let message = format!("expected one of: {}", join);
David Tolnayda1dc7c2018-08-24 11:57:28 -040054 error::new_at(self.scope, self.cursor, message)
55 }
56 }
David Tolnay18c754c2018-08-21 23:26:58 -040057 }
David Tolnayd9836922018-08-25 18:05:36 -040058
David Tolnayd9836922018-08-25 18:05:36 -040059 pub fn cursor(&self) -> Cursor<'a> {
60 self.cursor
61 }
David Tolnay18c754c2018-08-21 23:26:58 -040062}
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.
67pub trait Peek: private::Sealed {
68 // Not public API.
69 #[doc(hidden)]
70 type Token: Token;
71}
72
David Tolnay2b687b02018-08-24 13:36:36 -040073impl<F: FnOnce(TokenMarker) -> T, T: Token> Peek for F {
David Tolnay18c754c2018-08-21 23:26:58 -040074 type Token = T;
75}
76
David Tolnay2b687b02018-08-24 13:36:36 -040077pub enum TokenMarker {}
78
David Tolnay776f8e02018-08-24 22:32:10 -040079impl<S> IntoSpans<S> for TokenMarker {
80 fn into_spans(self) -> S {
81 match self {}
82 }
83}
84
David Tolnay2d84a082018-08-25 16:31:38 -040085pub fn is_delimiter(lookahead: &Lookahead1, delimiter: Delimiter) -> bool {
86 lookahead.cursor.group(delimiter).is_some()
87}
88
David Tolnay18c754c2018-08-21 23:26:58 -040089mod private {
David Tolnay2b687b02018-08-24 13:36:36 -040090 use super::{Token, TokenMarker};
David Tolnay18c754c2018-08-21 23:26:58 -040091 pub trait Sealed {}
David Tolnay2b687b02018-08-24 13:36:36 -040092 impl<F: FnOnce(TokenMarker) -> T, T: Token> Sealed for F {}
David Tolnay18c754c2018-08-21 23:26:58 -040093}