Skip to content

Commit

Permalink
Merge pull request #1625 from dtolnay/peek
Browse files Browse the repository at this point in the history
Improve how None-delimited groups are counted by peek
  • Loading branch information
dtolnay committed Apr 17, 2024
2 parents 9f00b23 + 4b18c15 commit 48f99b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/buffer.rs
Expand Up @@ -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<Cursor<'a>> {
pub(crate) fn skip(mut self) -> Option<Cursor<'a>> {
self.ignore_none();

let len = match self.entry() {
Entry::End(_) => return None,

Expand Down
10 changes: 0 additions & 10 deletions src/parse.rs
Expand Up @@ -614,11 +614,6 @@ impl<'a> ParseBuffer<'a> {
/// ```
pub fn peek2<T: Peek>(&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)
}

Expand All @@ -629,11 +624,6 @@ impl<'a> ParseBuffer<'a> {
/// Looks at the third-next token in the parse stream.
pub fn peek3<T: Peek>(&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()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parse_stream.rs
Expand Up @@ -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);
Expand All @@ -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()?;

Expand Down

0 comments on commit 48f99b0

Please sign in to comment.