Skip to content

Commit

Permalink
Abstract fold position parameters with a trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Mar 30, 2024
1 parent 37f4f07 commit 9103b60
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 78 deletions.
10 changes: 3 additions & 7 deletions src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use thiserror::Error;

pub use regex_automata::meta::Regex;

use crate::token::walk::{Fold, FoldPosition, Forward, TokenPath};
use crate::token::walk::{Fold, FoldPosition, Forward};
use crate::token::{self, Archetype, BranchKind, ConcatenationTree, LeafKind};
use crate::IteratorExt as _;

Expand Down Expand Up @@ -103,7 +103,7 @@ where

fn fold(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
_: impl FoldPosition<'t, A>,
branch: &BranchKind<'t, A>,
terms: Vec<Self::Term>,
) -> Option<Self::Term> {
Expand All @@ -128,11 +128,7 @@ where
term
}

fn term(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
leaf: &LeafKind<'t>,
) -> Self::Term {
fn term(&mut self, _: impl FoldPosition<'t, A>, leaf: &LeafKind<'t>) -> Self::Term {
use token::Wildcard::{One, Tree, ZeroOrMore};
use Archetype::{Character, Range};
use LeafKind::{Class, Literal, Separator, Wildcard};
Expand Down
12 changes: 3 additions & 9 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use crate::token::variance::invariant::{
};
use crate::token::variance::ops;
use crate::token::variance::{TreeExhaustiveness, TreeVariance, VarianceFold, VarianceTerm};
use crate::token::walk::{
BranchFold, Fold, FoldMap, FoldPosition, Starting, TokenEntry, TokenPath,
};
use crate::token::walk::{BranchFold, Fold, FoldMap, FoldPosition, Starting, TokenEntry};
use crate::{StrExt as _, PATHS_ARE_CASE_INSENSITIVE};

pub use crate::token::parse::{parse, ParseError, ROOT_SEPARATOR_EXPRESSION};
Expand Down Expand Up @@ -520,7 +518,7 @@ impl<'t, A> Token<'t, A> {

fn fold(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
_: impl FoldPosition<'t, A>,
branch: &BranchKind<'t, A>,
terms: Vec<Self::Term>,
) -> Option<Self::Term> {
Expand All @@ -540,11 +538,7 @@ impl<'t, A> Token<'t, A> {
})
}

fn term(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
leaf: &LeafKind<'t>,
) -> Self::Term {
fn term(&mut self, _: impl FoldPosition<'t, A>, leaf: &LeafKind<'t>) -> Self::Term {
leaf.is_rooting().into()
}
}
Expand Down
20 changes: 5 additions & 15 deletions src/token/variance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use crate::token::variance::natural::{
BoundedVariantRange, NaturalRange, OpenedUpperBound, VariantRange,
};
use crate::token::variance::ops::{Conjunction, Disjunction, Product};
use crate::token::walk::{
ChildToken, Fold, FoldPosition, Forward, ParentToken, Sequencer, TokenPath,
};
use crate::token::walk::{ChildToken, Fold, FoldPosition, Forward, ParentToken, Sequencer};
use crate::token::{Boundary, BranchKind, LeafKind};

pub use Boundedness::{Bounded, Unbounded};
Expand Down Expand Up @@ -298,7 +296,7 @@ where

fn fold(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
_: impl FoldPosition<'t, A>,
branch: &BranchKind<'t, A>,
terms: Vec<Self::Term>,
) -> Option<Self::Term> {
Expand All @@ -309,11 +307,7 @@ where
branch.finalize(term)
}

fn term(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
leaf: &LeafKind<'t>,
) -> Self::Term {
fn term(&mut self, _: impl FoldPosition<'t, A>, leaf: &LeafKind<'t>) -> Self::Term {
leaf.term()
}
}
Expand Down Expand Up @@ -351,7 +345,7 @@ impl<'t, A> Fold<'t, A> for TreeExhaustiveness {

fn fold(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
_: impl FoldPosition<'t, A>,
branch: &BranchKind<'t, A>,
terms: Vec<Self::Term>,
) -> Option<Self::Term> {
Expand Down Expand Up @@ -399,11 +393,7 @@ impl<'t, A> Fold<'t, A> for TreeExhaustiveness {
}
}

fn term(
&mut self,
_: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
leaf: &LeafKind<'t>,
) -> Self::Term {
fn term(&mut self, _: impl FoldPosition<'t, A>, leaf: &LeafKind<'t>) -> Self::Term {
self::term::<Depth>(leaf)
}
}
Expand Down
75 changes: 28 additions & 47 deletions src/token/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,43 +108,11 @@ impl<'i, 't, A> From<Adjacency<'i, 't, A>>
}
}

// TODO: This is effectively an "alias". Replace it with a trait alias when that or a similar
// feature is stabilized.
pub trait TokenPath<'t, A>: SliceProjection<Item = BranchKind<'t, A>> {}

impl<'t, A, P> TokenPath<'t, A> for P where P: SliceProjection<Item = BranchKind<'t, A>> {}

#[derive(Clone, Copy, Debug)]
pub struct FoldPosition<'i, 't, A, P>
where
P: SliceProjection<Item = BranchKind<'t, A>>,
{
adjacency: Adjacency<'i, 't, A>,
path: P,
}

impl<'i, 't, A, P> FoldPosition<'i, 't, A, P>
where
P: SliceProjection<Item = BranchKind<'t, A>>,
{
pub fn adjacency(&self) -> &Adjacency<'i, 't, A> {
&self.adjacency
}

pub fn path(&self) -> &P {
&self.path
}
}

// TODO: Represent `FoldPosition` via a trait like this and remove the `TokenPath` "alias".
pub trait Positional<'t, A> {
type Token;
pub trait FoldPosition<'t, A> {
type Path: SliceProjection<Item = BranchKind<'t, A>>;

fn adjacency(&self) -> &Adjacency<'_, 't, A>;

fn token(&self) -> &Self::Token;

fn path(&self) -> &Self::Path;
}

Expand All @@ -168,7 +136,7 @@ pub trait Fold<'t, A> {

fn fold(
&mut self,
position: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
position: impl FoldPosition<'t, A>,
branch: &BranchKind<'t, A>,
terms: Vec<Self::Term>,
) -> Option<Self::Term>;
Expand All @@ -177,11 +145,7 @@ pub trait Fold<'t, A> {
term
}

fn term(
&mut self,
position: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
leaf: &LeafKind<'t>,
) -> Self::Term;
fn term(&mut self, position: impl FoldPosition<'t, A>, leaf: &LeafKind<'t>) -> Self::Term;
}

impl<'f, 't, A, F> Fold<'t, A> for &'f mut F
Expand All @@ -205,7 +169,7 @@ where

fn fold(
&mut self,
position: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
position: impl FoldPosition<'t, A>,
branch: &BranchKind<'t, A>,
terms: Vec<Self::Term>,
) -> Option<Self::Term> {
Expand All @@ -216,11 +180,7 @@ where
F::finalize(self, branch, term)
}

fn term(
&mut self,
position: FoldPosition<'_, 't, A, impl TokenPath<'t, A>>,
leaf: &LeafKind<'t>,
) -> Self::Term {
fn term(&mut self, position: impl FoldPosition<'t, A>, leaf: &LeafKind<'t>) -> Self::Term {
F::term(self, position, leaf)
}
}
Expand Down Expand Up @@ -342,6 +302,27 @@ impl<'t, A> Token<'t, A> {
}
}

#[derive(Clone, Copy, Debug)]
pub struct Position<'i, 't, A, P> {
adjacency: Adjacency<'i, 't, A>,
path: P,
}

impl<'i, 't, A, P> FoldPosition<'t, A> for Position<'i, 't, A, P>
where
P: SliceProjection<Item = BranchKind<'t, A>>,
{
type Path = P;

fn adjacency(&self) -> &Adjacency<'_, 't, A> {
&self.adjacency
}

fn path(&self) -> &Self::Path {
&self.path
}
}

struct TokenPath<'i, 't, A, F>
where
F: Fold<'t, A>,
Expand Down Expand Up @@ -378,7 +359,7 @@ impl<'t, A> Token<'t, A> {
adjacency: Adjacency<'i, 't, A>,
) -> Result<(), F::Term> {
let (f, path) = self.as_fold_and_path_slice();
let term = f.term(FoldPosition { adjacency, path }, leaf);
let term = f.term(Position { adjacency, path }, leaf);
match self.branches.last_mut() {
Some(branch) => {
branch.push(term);
Expand Down Expand Up @@ -478,7 +459,7 @@ impl<'t, A> Token<'t, A> {
terms,
} = self;
let (terms, f) = (map_terms_and_get_fold)(terms);
f.fold(FoldPosition { adjacency, path }, branch, terms)
f.fold(Position { adjacency, path }, branch, terms)
.map(|term| f.finalize(branch, term))
}
}
Expand Down

0 comments on commit 9103b60

Please sign in to comment.