-
-
Notifications
You must be signed in to change notification settings - Fork 315
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Is it possible to peek for EOF
?
#1161
Comments
#889 describes some ways to accomplish this. |
For anyone stumbling on this and looking for something that does not require forking everytime one wants to peek if the token is the last one, I found that there is a way to define ones own #[derive(Clone, Copy, Debug, Default)]
struct Eof {}
impl syn::token::CustomToken for Eof {
fn peek(cursor: syn::buffer::Cursor) -> bool {
cursor.eof()
}
fn display() -> &'static str {
"EOF"
}
}
#[allow(non_snake_case)]
fn Eof(_: impl syn::__private::IntoSpans<Span>) -> Eof {
Eof {}
}
// usage
input.peek2(Eof); note though that this uses the |
The solution I proposed doesn not seem to work anymore (shouldn't use hidden API I guess), alternative is to create a helper function that just steps a bit to check for eof: fn peek2_eof(input: ParseStream) -> bool {
let mut ts = input.fork().parse::<TokenStream>().unwrap().into_iter();
ts.next();
ts.next().is_none()
} |
I need to check if I'm at the end of the input or in front of some delimiter, to parse in a certain way. Checking for the delimiter works perfectly with
peek2
but I cannot check if I'm at the end of the input with peeking.The text was updated successfully, but these errors were encountered: