From 4b18c155580a32b3e8888ddb56fa9c192b188658 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 16 Apr 2024 16:57:08 -0700 Subject: [PATCH] Improve how None-delimited groups are counted by peek --- src/buffer.rs | 8 +++++--- src/parse.rs | 10 ---------- tests/test_parse_stream.rs | 8 ++++---- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 86dec46afd..d4b0dd11ad 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -346,11 +346,13 @@ impl<'a> Cursor<'a> { self.span() } - /// Skip over the next token without cloning it. Returns `None` if this - /// cursor points to eof. + /// Skip over the next token that is not a None-delimited group, without + /// cloning it. Returns `None` if this cursor points to eof. /// /// This method treats `'lifetimes` as a single token. - pub(crate) fn skip(self) -> Option> { + pub(crate) fn skip(mut self) -> Option> { + self.ignore_none(); + let len = match self.entry() { Entry::End(_) => return None, diff --git a/src/parse.rs b/src/parse.rs index aafc1a9649..5d0fbc43b8 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -614,11 +614,6 @@ impl<'a> ParseBuffer<'a> { /// ``` pub fn peek2(&self, token: T) -> bool { fn peek2(buffer: &ParseBuffer, peek: fn(Cursor) -> bool) -> bool { - if let Some(group) = buffer.cursor().group(Delimiter::None) { - if group.0.skip().map_or(false, peek) { - return true; - } - } buffer.cursor().skip().map_or(false, peek) } @@ -629,11 +624,6 @@ impl<'a> ParseBuffer<'a> { /// Looks at the third-next token in the parse stream. pub fn peek3(&self, token: T) -> bool { fn peek3(buffer: &ParseBuffer, peek: fn(Cursor) -> bool) -> bool { - if let Some(group) = buffer.cursor().group(Delimiter::None) { - if group.0.skip().and_then(Cursor::skip).map_or(false, peek) { - return true; - } - } buffer .cursor() .skip() diff --git a/tests/test_parse_stream.rs b/tests/test_parse_stream.rs index 11dc1a6630..3a009b4921 100644 --- a/tests/test_parse_stream.rs +++ b/tests/test_parse_stream.rs @@ -136,8 +136,8 @@ fn test_peek_groups() { assert!(!input.peek2(Token![::])); assert!(input.peek2(Token![!])); assert!(input.peek2(token::Group)); - assert!(!input.peek3(Token![=])); // FIXME - assert!(input.peek3(Token![static])); // FIXME + assert!(input.peek3(Token![=])); + assert!(!input.peek3(Token![static])); let content; parenthesized!(content in input); @@ -155,8 +155,8 @@ fn test_peek_groups() { assert!(input.peek(token::Group)); assert!(input.peek(Token![!])); assert!(input.peek2(Token![=])); - assert!(!input.peek3(Token![static])); // FIXME - assert!(input.peek2(Token![static])); // FIXME + assert!(input.peek3(Token![static])); + assert!(!input.peek2(Token![static])); let _: Token![!] = input.parse()?;