Skip to content

Commit

Permalink
Rename GlobVariance to TokenVariance.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Mar 16, 2024
1 parent 1a9f767 commit a6cdb88
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 79 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Expand Up @@ -79,8 +79,8 @@ use crate::encode::CompileError;
use crate::query::{CapturingToken, When};
use crate::rule::{Checked, RuleError};
use crate::token::{
ConcatenationTree, Depth, ExpressionMetadata, GlobVariance, ParseError, Text, Token, TokenTree,
Tokenized,
ConcatenationTree, Depth, ExpressionMetadata, ParseError, Text, Token, TokenTree,
TokenVariance, Tokenized,
};
#[cfg(feature = "walk")]
use crate::walk::WalkError;
Expand Down Expand Up @@ -174,9 +174,9 @@ pub trait Program<'t>: Pattern<'t, Error = Infallible> {
/// [`MatchedText`]: crate::MatchedText
fn matched<'p>(&self, path: &'p CandidatePath<'_>) -> Option<MatchedText<'p>>;

fn depth(&self) -> GlobVariance<Depth>;
fn depth(&self) -> TokenVariance<Depth>;

fn text(&self) -> GlobVariance<Text<'t>>;
fn text(&self) -> TokenVariance<Text<'t>>;

/// Returns `true` if the glob has a root.
///
Expand Down Expand Up @@ -699,11 +699,11 @@ impl<'t> Program<'t> for Glob<'t> {
self.program.captures(path.as_ref()).map(From::from)
}

fn depth(&self) -> GlobVariance<Depth> {
fn depth(&self) -> TokenVariance<Depth> {
self.tree.as_ref().as_token().variance()
}

fn text(&self) -> GlobVariance<Text<'t>> {
fn text(&self) -> TokenVariance<Text<'t>> {
self.tree.as_ref().as_token().variance()
}

Expand Down Expand Up @@ -758,11 +758,11 @@ impl<'t> Program<'t> for Any<'t> {
self.program.captures(path.as_ref()).map(From::from)
}

fn depth(&self) -> GlobVariance<Depth> {
fn depth(&self) -> TokenVariance<Depth> {
self.tree.as_ref().as_token().variance()
}

fn text(&self) -> GlobVariance<Text<'t>> {
fn text(&self) -> TokenVariance<Text<'t>> {
self.tree.as_ref().as_token().variance()
}

Expand Down
2 changes: 1 addition & 1 deletion src/query.rs
@@ -1,7 +1,7 @@
use crate::diagnostics::Span;

pub use crate::token::{
BoundedVariantRange, Boundedness, Depth, GlobVariance, Invariant, Text, UnitBound, Variance,
BoundedVariantRange, Boundedness, Depth, Invariant, Text, TokenVariance, UnitBound, Variance,
VariantRange,
};

Expand Down
78 changes: 39 additions & 39 deletions src/token/mod.rs
Expand Up @@ -25,7 +25,7 @@ pub use crate::token::variance::bound::{
BoundedVariantRange, Boundedness, NaturalRange, VariantRange,
};
pub use crate::token::variance::invariant::{Breadth, Depth, Invariant, Size, Text, UnitBound};
pub use crate::token::variance::{GlobVariance, Variance};
pub use crate::token::variance::{TokenVariance, Variance};

// TODO: Tree representations of expressions are intrusive and only differ in their annotations.
// This supports some distinctions between tree types, but greatly limits the ability to
Expand Down Expand Up @@ -445,9 +445,9 @@ impl<'t, A> Token<'t, A> {
self.as_leaf().and_then(LeafKind::boundary)
}

pub fn variance<T>(&self) -> GlobVariance<T>
pub fn variance<T>(&self) -> TokenVariance<T>
where
TreeVariance<T>: Fold<'t, A, Term = GlobVariance<T>>,
TreeVariance<T>: Fold<'t, A, Term = TokenVariance<T>>,
T: Conjunction<Output = T> + Invariant,
{
self.fold(TreeVariance::default())
Expand Down Expand Up @@ -830,7 +830,7 @@ where
Repetition<'t, A>: VarianceFold<T>,
T: Invariant,
{
fn fold(&self, terms: Vec<GlobVariance<T>>) -> Option<GlobVariance<T>> {
fn fold(&self, terms: Vec<TokenVariance<T>>) -> Option<TokenVariance<T>> {
use BranchKind::{Alternation, Concatenation, Repetition};

match self {
Expand All @@ -840,7 +840,7 @@ where
}
}

fn finalize(&self, term: GlobVariance<T>) -> GlobVariance<T> {
fn finalize(&self, term: TokenVariance<T>) -> TokenVariance<T> {
use BranchKind::{Alternation, Concatenation, Repetition};

match self {
Expand Down Expand Up @@ -942,7 +942,7 @@ where
Wildcard: VarianceTerm<T>,
T: Invariant,
{
fn term(&self) -> GlobVariance<T> {
fn term(&self) -> TokenVariance<T> {
use LeafKind::{Class, Literal, Separator, Wildcard};

match self {
Expand Down Expand Up @@ -983,19 +983,19 @@ impl<'t, A> From<Vec<Token<'t, A>>> for Alternation<'t, A> {
}

impl<'t, A> VarianceFold<Depth> for Alternation<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Depth>>) -> Option<GlobVariance<Depth>> {
fn fold(&self, terms: Vec<TokenVariance<Depth>>) -> Option<TokenVariance<Depth>> {
terms.into_iter().reduce(ops::disjunction)
}
}

impl<'t, A> VarianceFold<Size> for Alternation<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Size>>) -> Option<GlobVariance<Size>> {
fn fold(&self, terms: Vec<TokenVariance<Size>>) -> Option<TokenVariance<Size>> {
terms.into_iter().reduce(ops::disjunction)
}
}

impl<'t, A> VarianceFold<Text<'t>> for Alternation<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Text<'t>>>) -> Option<GlobVariance<Text<'t>>> {
fn fold(&self, terms: Vec<TokenVariance<Text<'t>>>) -> Option<TokenVariance<Text<'t>>> {
terms.into_iter().reduce(ops::disjunction)
}
}
Expand Down Expand Up @@ -1028,7 +1028,7 @@ impl From<(char, char)> for Archetype {
}

impl VarianceTerm<Size> for Archetype {
fn term(&self) -> GlobVariance<Size> {
fn term(&self) -> TokenVariance<Size> {
// TODO: Examine the archetype instead of blindly assuming a constant size. This becomes
// especially important if character classes gain support for named classes, as these
// may contain graphemes that cannot be encoded as a single code point. See related
Expand All @@ -1040,7 +1040,7 @@ impl VarianceTerm<Size> for Archetype {
}

impl<'t> VarianceTerm<Text<'t>> for Archetype {
fn term(&self) -> GlobVariance<Text<'t>> {
fn term(&self) -> TokenVariance<Text<'t>> {
match self {
Archetype::Character(x) => {
if PATHS_ARE_CASE_INSENSITIVE {
Expand Down Expand Up @@ -1078,11 +1078,11 @@ impl Class {
self.is_negated
}

fn fold<T, F>(&self, f: F) -> GlobVariance<T>
fn fold<T, F>(&self, f: F) -> TokenVariance<T>
where
Archetype: VarianceTerm<T>,
T: Invariant,
F: FnMut(GlobVariance<T>, GlobVariance<T>) -> GlobVariance<T>,
F: FnMut(TokenVariance<T>, TokenVariance<T>) -> TokenVariance<T>,
{
self.archetypes()
.iter()
Expand All @@ -1093,25 +1093,25 @@ impl Class {
}

impl VarianceTerm<Breadth> for Class {
fn term(&self) -> GlobVariance<Breadth> {
fn term(&self) -> TokenVariance<Breadth> {
Variance::zero()
}
}

impl VarianceTerm<Depth> for Class {
fn term(&self) -> GlobVariance<Depth> {
fn term(&self) -> TokenVariance<Depth> {
Variance::zero()
}
}

impl VarianceTerm<Size> for Class {
fn term(&self) -> GlobVariance<Size> {
fn term(&self) -> TokenVariance<Size> {
self.fold(ops::disjunction)
}
}

impl<'t> VarianceTerm<Text<'t>> for Class {
fn term(&self) -> GlobVariance<Text<'t>> {
fn term(&self) -> TokenVariance<Text<'t>> {
if self.is_negated {
// It is not feasible to encode a character class that matches all UTF-8 text and
// therefore nothing when negated, and so a character class must be variant if it is
Expand Down Expand Up @@ -1154,11 +1154,11 @@ impl<'t, A> From<Vec<Token<'t, A>>> for Concatenation<'t, A> {
}

impl<'t, A> VarianceFold<Depth> for Concatenation<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Depth>>) -> Option<GlobVariance<Depth>> {
fn fold(&self, terms: Vec<TokenVariance<Depth>>) -> Option<TokenVariance<Depth>> {
terms.into_iter().reduce(ops::conjunction)
}

fn finalize(&self, term: GlobVariance<Depth>) -> GlobVariance<Depth> {
fn finalize(&self, term: TokenVariance<Depth>) -> TokenVariance<Depth> {
// Depth must consider boundary component tokens (i.e., tree wildcards). Boundary component
// tokens do not have explict separators, so additional depth terms are needed for any
// adjacent non-boundary tokens. For example, consider the pattern `a/**/b`. The `a` and
Expand Down Expand Up @@ -1193,13 +1193,13 @@ impl<'t, A> VarianceFold<Depth> for Concatenation<'t, A> {
}

impl<'t, A> VarianceFold<Size> for Concatenation<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Size>>) -> Option<GlobVariance<Size>> {
fn fold(&self, terms: Vec<TokenVariance<Size>>) -> Option<TokenVariance<Size>> {
terms.into_iter().reduce(ops::conjunction)
}
}

impl<'t, A> VarianceFold<Text<'t>> for Concatenation<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Text<'t>>>) -> Option<GlobVariance<Text<'t>>> {
fn fold(&self, terms: Vec<TokenVariance<Text<'t>>>) -> Option<TokenVariance<Text<'t>>> {
terms.into_iter().reduce(ops::conjunction)
}
}
Expand Down Expand Up @@ -1260,27 +1260,27 @@ impl<'t> Literal<'t> {
}

impl<'t> VarianceTerm<Breadth> for Literal<'t> {
fn term(&self) -> GlobVariance<Breadth> {
fn term(&self) -> TokenVariance<Breadth> {
Variance::zero()
}
}

impl<'t> VarianceTerm<Depth> for Literal<'t> {
fn term(&self) -> GlobVariance<Depth> {
fn term(&self) -> TokenVariance<Depth> {
Variance::zero()
}
}

impl<'t> VarianceTerm<Size> for Literal<'t> {
fn term(&self) -> GlobVariance<Size> {
fn term(&self) -> TokenVariance<Size> {
// TODO: This assumes that the size of graphemes in a casing set are the same. Is that
// correct?
Variance::Invariant(self.text.as_bytes().len().into())
}
}

impl<'t> VarianceTerm<Text<'t>> for Literal<'t> {
fn term(&self) -> GlobVariance<Text<'t>> {
fn term(&self) -> TokenVariance<Text<'t>> {
self.variance()
.map_invariant(|invariant| invariant.clone().into_nominal_text())
}
Expand Down Expand Up @@ -1338,31 +1338,31 @@ impl<'t, A> BranchComposition<'t> for Repetition<'t, A> {
}

impl<'t, A> VarianceFold<Depth> for Repetition<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Depth>>) -> Option<GlobVariance<Depth>> {
fn fold(&self, terms: Vec<TokenVariance<Depth>>) -> Option<TokenVariance<Depth>> {
terms.into_iter().reduce(ops::conjunction)
}

fn finalize(&self, term: GlobVariance<Depth>) -> GlobVariance<Depth> {
fn finalize(&self, term: TokenVariance<Depth>) -> TokenVariance<Depth> {
ops::product(term, self.variance())
}
}

impl<'t, A> VarianceFold<Size> for Repetition<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Size>>) -> Option<GlobVariance<Size>> {
fn fold(&self, terms: Vec<TokenVariance<Size>>) -> Option<TokenVariance<Size>> {
terms.into_iter().reduce(ops::conjunction)
}

fn finalize(&self, term: GlobVariance<Size>) -> GlobVariance<Size> {
fn finalize(&self, term: TokenVariance<Size>) -> TokenVariance<Size> {
ops::product(term, self.variance())
}
}

impl<'t, A> VarianceFold<Text<'t>> for Repetition<'t, A> {
fn fold(&self, terms: Vec<GlobVariance<Text<'t>>>) -> Option<GlobVariance<Text<'t>>> {
fn fold(&self, terms: Vec<TokenVariance<Text<'t>>>) -> Option<TokenVariance<Text<'t>>> {
terms.into_iter().reduce(ops::conjunction)
}

fn finalize(&self, term: GlobVariance<Text<'t>>) -> GlobVariance<Text<'t>> {
fn finalize(&self, term: TokenVariance<Text<'t>>) -> TokenVariance<Text<'t>> {
ops::product(term, self.variance())
}
}
Expand All @@ -1375,27 +1375,27 @@ impl Separator {
}

impl VarianceTerm<Breadth> for Separator {
fn term(&self) -> GlobVariance<Breadth> {
fn term(&self) -> TokenVariance<Breadth> {
Variance::zero()
}
}

impl VarianceTerm<Depth> for Separator {
fn term(&self) -> GlobVariance<Depth> {
fn term(&self) -> TokenVariance<Depth> {
Variance::Invariant(1.into())
}
}

impl VarianceTerm<Size> for Separator {
fn term(&self) -> GlobVariance<Size> {
fn term(&self) -> TokenVariance<Size> {
// TODO: This is incorrect. The compiled regular expression may ignore a terminating
// separator, in which case the size is a bounded range.
Variance::Invariant(Separator::INVARIANT_TEXT.as_bytes().len().into())
}
}

impl<'t> VarianceTerm<Text<'t>> for Separator {
fn term(&self) -> GlobVariance<Text<'t>> {
fn term(&self) -> TokenVariance<Text<'t>> {
Variance::Invariant(Separator::INVARIANT_TEXT.into_structural_text())
}
}
Expand Down Expand Up @@ -1439,7 +1439,7 @@ where
}

impl VarianceTerm<Breadth> for Wildcard {
fn term(&self) -> GlobVariance<Breadth> {
fn term(&self) -> TokenVariance<Breadth> {
match self {
Wildcard::One => Variance::zero(),
_ => Variance::unbounded(),
Expand All @@ -1448,7 +1448,7 @@ impl VarianceTerm<Breadth> for Wildcard {
}

impl VarianceTerm<Depth> for Wildcard {
fn term(&self) -> GlobVariance<Depth> {
fn term(&self) -> TokenVariance<Depth> {
match self {
Wildcard::Tree { .. } => Variance::unbounded(),
_ => Variance::zero(),
Expand All @@ -1457,7 +1457,7 @@ impl VarianceTerm<Depth> for Wildcard {
}

impl VarianceTerm<Size> for Wildcard {
fn term(&self) -> GlobVariance<Size> {
fn term(&self) -> TokenVariance<Size> {
match self {
// TODO: This is a bit pessimistic and, more importantly, incorrect! Clarity is needed
// on what exactly an exactly-one wildcard matches in text. If it is a grapheme,
Expand All @@ -1471,7 +1471,7 @@ impl VarianceTerm<Size> for Wildcard {
}

impl<'t> VarianceTerm<Text<'t>> for Wildcard {
fn term(&self) -> GlobVariance<Text<'t>> {
fn term(&self) -> TokenVariance<Text<'t>> {
Variance::unbounded()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/token/variance/invariant/natural.rs
Expand Up @@ -5,7 +5,7 @@ use crate::token::variance::bound::{
};
use crate::token::variance::invariant::{Identity, Invariant};
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
use crate::token::variance::{GlobVariance, Variance};
use crate::token::variance::{TokenVariance, Variance};

macro_rules! impl_invariant_natural {
($name:ident $(,)?) => {
Expand Down Expand Up @@ -107,12 +107,12 @@ macro_rules! impl_invariant_natural {
}

impl Product<VariantRange> for $name {
type Output = GlobVariance<Self>;
type Output = TokenVariance<Self>;

fn product(self, rhs: VariantRange) -> Self::Output {
NonZeroUsize::new(self.0)
.map_or_else(
GlobVariance::<Self>::zero,
TokenVariance::<Self>::zero,
|lhs| Variance::Variant(rhs.map_bounded(|rhs| ops::product(rhs, lhs))),
)
}
Expand All @@ -138,7 +138,7 @@ macro_rules! impl_invariant_natural {
impl_invariant_natural!(Depth, once => 1);
impl_invariant_natural!(Size);

impl GlobVariance<Depth> {
impl TokenVariance<Depth> {
pub fn is_exhaustive(&self) -> bool {
!self.has_upper_bound()
}
Expand Down

0 comments on commit a6cdb88

Please sign in to comment.