diff --git a/src/rule.rs b/src/rule.rs index 43af0c7..e7d1bd5 100644 --- a/src/rule.rs +++ b/src/rule.rs @@ -384,11 +384,11 @@ where A: Spanned, { if let Some((left, right)) = walk::forward(tree) - .group_by(|TokenEntry { position, .. }| *position) + .group_by(TokenEntry::position) .into_iter() .flat_map(|(_, group)| { group - .map(|TokenEntry { token, .. }| token) + .map(TokenEntry::into_token) .tuple_windows::<(_, _)>() .filter(|(left, right)| left.boundary().and(right.boundary()).is_some()) .map(|(left, right)| (*left.annotation().span(), *right.annotation().span())) @@ -487,7 +487,7 @@ where P: FnMut(&'i Token<'t, A>) -> bool, { token.map_or(false, |token| { - traversal(token).any(move |TokenEntry { token, .. }| predicate(token)) + traversal(token).any(move |entry| predicate(entry.into_token())) }) } @@ -786,7 +786,7 @@ where A: Spanned, { if let Some(token) = walk::forward(tree) - .map(|TokenEntry { token, .. }| token) + .map(TokenEntry::into_token) .find(|token| { token .as_repetition() @@ -811,7 +811,7 @@ where A: Spanned, { if let Some(token) = walk::forward(tree) - .map(|TokenEntry { token, .. }| token) + .map(TokenEntry::into_token) // TODO: This is expensive. For each token tree encountered, the tree is traversed to // determine its variance. If variant, the tree is traversed and queried again, // revisiting the same tokens to recompute their local variance. diff --git a/src/token/mod.rs b/src/token/mod.rs index 6b533f3..b64383b 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -692,18 +692,23 @@ impl<'t, A> Token<'t, A> { // // Implement this via `fold`. pub fn has_root(&self) -> bool { - walk::starting(self).any(|TokenEntry { token, .. }| { - token.as_leaf().map_or(false, |leaf| { - matches!( - leaf, - LeafKind::Separator(_) | LeafKind::Wildcard(Wildcard::Tree { has_root: true }), - ) + walk::starting(self) + .map(TokenEntry::into_token) + .any(|token| { + token.as_leaf().map_or(false, |leaf| { + matches!( + leaf, + LeafKind::Separator(_) + | LeafKind::Wildcard(Wildcard::Tree { has_root: true }), + ) + }) }) - }) } pub fn has_boundary(&self) -> bool { - walk::forward(self).any(|TokenEntry { token, .. }| token.boundary().is_some()) + walk::forward(self) + .map(TokenEntry::into_token) + .any(|token| token.boundary().is_some()) } pub fn is_capturing(&self) -> bool { diff --git a/src/token/walk.rs b/src/token/walk.rs index df09f83..123bc07 100644 --- a/src/token/walk.rs +++ b/src/token/walk.rs @@ -150,6 +150,10 @@ pub struct WalkEntry { } impl WalkEntry { + pub fn into_token(self) -> T { + self.token + } + fn map(self, f: F) -> WalkEntry where F: FnOnce(T) -> U, @@ -160,11 +164,16 @@ impl WalkEntry { token: f(token), } } + + pub fn position(&self) -> Position { + self.position + } } -pub type TokenEntry<'i, 't, A> = WalkEntry<&'i Token<'t, A>>; type BranchEntry<'i, 't, A> = WalkEntry<&'i BranchKind<'t, A>>; +pub type TokenEntry<'i, 't, A> = WalkEntry<&'i Token<'t, A>>; + #[derive(Clone, Debug)] struct Walk<'i, 't, A, E> { branch: Option>,