Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve how None-delimited groups are counted by peek #1625

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/buffer.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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