]>
git.proxmox.com Git - rustc.git/blob - vendor/syn-0.15.35/src/lookahead.rs
1 use std
::cell
::RefCell
;
3 use proc_macro2
::{Delimiter, Span}
;
6 use error
::{self, Error}
;
7 use sealed
::lookahead
::Sealed
;
11 /// Support for checking the next token in a stream to decide how to parse.
13 /// An important advantage over [`ParseStream::peek`] is that here we
14 /// automatically construct an appropriate error message based on the token
15 /// alternatives that get peeked. If you are producing your own error message,
16 /// go ahead and use `ParseStream::peek` instead.
18 /// Use [`ParseStream::lookahead1`] to construct this object.
20 /// [`ParseStream::peek`]: struct.ParseBuffer.html#method.peek
21 /// [`ParseStream::lookahead1`]: struct.ParseBuffer.html#method.lookahead1
26 /// use syn::{ConstParam, Ident, Lifetime, LifetimeDef, Result, Token, TypeParam};
27 /// use syn::parse::{Parse, ParseStream};
29 /// // A generic parameter, a single one of the comma-separated elements inside
30 /// // angle brackets in:
32 /// // fn f<T: Clone, 'a, 'b: 'a, const N: usize>() { ... }
34 /// // On invalid input, lookahead gives us a reasonable error message.
36 /// // error: expected one of: identifier, lifetime, `const`
38 /// // 5 | fn f<!Sized>() {}
40 /// enum GenericParam {
42 /// Lifetime(LifetimeDef),
43 /// Const(ConstParam),
46 /// impl Parse for GenericParam {
47 /// fn parse(input: ParseStream) -> Result<Self> {
48 /// let lookahead = input.lookahead1();
49 /// if lookahead.peek(Ident) {
50 /// input.parse().map(GenericParam::Type)
51 /// } else if lookahead.peek(Lifetime) {
52 /// input.parse().map(GenericParam::Lifetime)
53 /// } else if lookahead.peek(Token![const]) {
54 /// input.parse().map(GenericParam::Const)
56 /// Err(lookahead.error())
61 pub struct Lookahead1
<'a
> {
64 comparisons
: RefCell
<Vec
<&'
static str>>,
67 pub fn new(scope
: Span
, cursor
: Cursor
) -> Lookahead1
{
71 comparisons
: RefCell
::new(Vec
::new()),
76 lookahead
: &Lookahead1
,
77 peek
: fn(Cursor
) -> bool
,
78 display
: fn() -> &'
static str,
80 if peek(lookahead
.cursor
) {
83 lookahead
.comparisons
.borrow_mut().push(display());
87 impl<'a
> Lookahead1
<'a
> {
88 /// Looks at the next token in the parse stream to determine whether it
89 /// matches the requested type of token.
93 /// Note that this method does not use turbofish syntax. Pass the peek type
94 /// inside of parentheses.
96 /// - `input.peek(Token![struct])`
97 /// - `input.peek(Token![==])`
98 /// - `input.peek(Ident)` *(does not accept keywords)*
99 /// - `input.peek(Ident::peek_any)`
100 /// - `input.peek(Lifetime)`
101 /// - `input.peek(token::Brace)`
102 pub fn peek
<T
: Peek
>(&self, token
: T
) -> bool
{
104 peek_impl(self, T
::Token
::peek
, T
::Token
::display
)
107 /// Triggers an error at the current position of the parse stream.
109 /// The error message will identify all of the expected token types that
110 /// have been peeked against this lookahead instance.
111 pub fn error(self) -> Error
{
112 let comparisons
= self.comparisons
.borrow();
113 match comparisons
.len() {
115 if self.cursor
.eof() {
116 Error
::new(self.scope
, "unexpected end of input")
118 Error
::new(self.cursor
.span(), "unexpected token")
122 let message
= format
!("expected {}", comparisons
[0]);
123 error
::new_at(self.scope
, self.cursor
, message
)
126 let message
= format
!("expected {} or {}", comparisons
[0], comparisons
[1]);
127 error
::new_at(self.scope
, self.cursor
, message
)
130 let join
= comparisons
.join(", ");
131 let message
= format
!("expected one of: {}", join
);
132 error
::new_at(self.scope
, self.cursor
, message
)
138 /// Types that can be parsed by looking at just one token.
140 /// Use [`ParseStream::peek`] to peek one of these types in a parse stream
141 /// without consuming it from the stream.
143 /// This trait is sealed and cannot be implemented for types outside of Syn.
145 /// [`ParseStream::peek`]: struct.ParseBuffer.html#method.peek
146 pub trait Peek
: Sealed
{
152 impl<F
: Copy
+ FnOnce(TokenMarker
) -> T
, T
: Token
> Peek
for F
{
156 pub enum TokenMarker {}
158 impl<S
> IntoSpans
<S
> for TokenMarker
{
159 fn into_spans(self) -> S
{
164 pub fn is_delimiter(cursor
: Cursor
, delimiter
: Delimiter
) -> bool
{
165 cursor
.group(delimiter
).is_some()
168 impl<F
: Copy
+ FnOnce(TokenMarker
) -> T
, T
: Token
> Sealed
for F {}