Skip to content

Commit

Permalink
Replace destructuring of TokenEntry with functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Jan 26, 2024
1 parent 7223de9 commit 2bc95f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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()))
})
}

Expand Down Expand Up @@ -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()
Expand All @@ -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.
Expand Down
21 changes: 13 additions & 8 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion src/token/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ pub struct WalkEntry<T> {
}

impl<T> WalkEntry<T> {
pub fn into_token(self) -> T {
self.token
}

fn map<U, F>(self, f: F) -> WalkEntry<U>
where
F: FnOnce(T) -> U,
Expand All @@ -160,11 +164,16 @@ impl<T> WalkEntry<T> {
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<BranchEntry<'i, 't, A>>,
Expand Down

0 comments on commit 2bc95f7

Please sign in to comment.