Skip to content

Commit 6f7072a

Browse files
committed
Collapse bound and invariant::natural into natural.
This change collapses modules concerning natural quantities and bounds into the `token::variance::natural` module. Misused `macro_export` attributes have also been removed (macros are not meant to be exported in public APIs).
1 parent 4fdc5ec commit 6f7072a

File tree

10 files changed

+261
-246
lines changed

10 files changed

+261
-246
lines changed

src/token/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,17 @@ use std::str;
1515
use crate::diagnostics::{Span, Spanned};
1616
use crate::query::When;
1717
use crate::token::variance::invariant::{
18-
BoundaryTerm, IntoNominalText, IntoStructuralText, One, Zero,
18+
BoundaryTerm, Finalize, IntoNominalText, IntoStructuralText, InvariantTerm, One, Zero,
1919
};
2020
use crate::token::variance::ops;
2121
use crate::token::variance::{TreeExhaustiveness, TreeVariance, VarianceFold, VarianceTerm};
2222
use crate::token::walk::{BranchFold, Fold, FoldMap, Starting, TokenEntry};
2323
use crate::{StrExt as _, PATHS_ARE_CASE_INSENSITIVE};
2424

2525
pub use crate::token::parse::{parse, ParseError, ROOT_SEPARATOR_EXPRESSION};
26-
pub use crate::token::variance::bound::{
27-
BoundedVariantRange, Boundedness, NaturalRange, VariantRange,
28-
};
29-
pub use crate::token::variance::invariant::{
30-
Breadth, Depth, Finalize, Invariant, InvariantTerm, Size, Text,
31-
};
32-
pub use crate::token::variance::{TokenVariance, Variance};
26+
pub use crate::token::variance::invariant::{Breadth, Depth, Invariant, Size, Text};
27+
pub use crate::token::variance::natural::{BoundedVariantRange, NaturalRange, VariantRange};
28+
pub use crate::token::variance::{Boundedness, TokenVariance, Variance};
3329

3430
// TODO: Tree representations of expressions are intrusive and only differ in their annotations.
3531
// This supports some distinctions between tree types, but greatly limits the ability to

src/token/variance/invariant/mod.rs

Lines changed: 82 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
mod natural;
21
mod term;
32
mod text;
43

54
use std::num::NonZeroUsize;
65

7-
use crate::token::variance::bound::{BoundedVariantRange, Boundedness, OpenedUpperBound};
8-
use crate::token::variance::ops::{Conjunction, Disjunction, Product};
9-
use crate::token::variance::TokenVariance;
6+
use crate::token::variance::natural::{
7+
define_natural_invariant, BoundedVariantRange, OpenedUpperBound,
8+
};
9+
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
10+
use crate::token::variance::{Boundedness, TokenVariance};
1011

11-
pub use crate::token::variance::invariant::natural::{Depth, Size};
1212
pub use crate::token::variance::invariant::term::{BoundaryTerm, SeparatedTerm, Termination};
1313
pub use crate::token::variance::invariant::text::{IntoNominalText, IntoStructuralText, Text};
1414

@@ -38,6 +38,59 @@ pub trait Invariant: Sized + Zero {
3838
fn into_lower_bound(self) -> Boundedness<Self::Bound>;
3939
}
4040

41+
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
42+
pub struct UnitBound;
43+
44+
impl<T> Conjunction<T> for UnitBound {
45+
type Output = Self;
46+
47+
fn conjunction(self, _: T) -> Self::Output {
48+
self
49+
}
50+
}
51+
52+
impl Disjunction for UnitBound {
53+
type Output = Boundedness<Self>;
54+
55+
fn disjunction(self, _: Self) -> Self::Output {
56+
self.into()
57+
}
58+
}
59+
60+
impl From<()> for UnitBound {
61+
fn from(_: ()) -> Self {
62+
UnitBound
63+
}
64+
}
65+
66+
impl From<BoundedVariantRange> for UnitBound {
67+
fn from(_: BoundedVariantRange) -> Self {
68+
UnitBound
69+
}
70+
}
71+
72+
impl OpenedUpperBound for UnitBound {
73+
fn opened_upper_bound(self) -> Boundedness<Self> {
74+
UnitBound.into()
75+
}
76+
}
77+
78+
impl Product<BoundedVariantRange> for UnitBound {
79+
type Output = Boundedness<Self>;
80+
81+
fn product(self, _: BoundedVariantRange) -> Self::Output {
82+
self.into()
83+
}
84+
}
85+
86+
impl Product<NonZeroUsize> for Boundedness<UnitBound> {
87+
type Output = Self;
88+
89+
fn product(self, _: NonZeroUsize) -> Self::Output {
90+
self
91+
}
92+
}
93+
4194
// Breadth is the maximum size (UTF-8 bytes) of component text in a glob expression. For example,
4295
// the breadth of `{a/,a/b/}<c/d:2>` is two (bytes).
4396
//
@@ -82,55 +135,39 @@ impl Invariant for Breadth {
82135
}
83136
}
84137

85-
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
86-
pub struct UnitBound;
87-
88-
impl<T> Conjunction<T> for UnitBound {
89-
type Output = Self;
90-
91-
fn conjunction(self, _: T) -> Self::Output {
92-
self
93-
}
94-
}
95-
96-
impl Disjunction for UnitBound {
97-
type Output = Boundedness<Self>;
98-
99-
fn disjunction(self, _: Self) -> Self::Output {
100-
self.into()
101-
}
102-
}
138+
define_natural_invariant!(
139+
/// Depth of a glob expression.
140+
Depth => BoundaryTerm,
141+
);
103142

104-
impl From<()> for UnitBound {
105-
fn from(_: ()) -> Self {
106-
UnitBound
143+
impl TokenVariance<Depth> {
144+
pub fn is_exhaustive(&self) -> bool {
145+
!self.has_upper_bound()
107146
}
108147
}
109148

110-
impl From<BoundedVariantRange> for UnitBound {
111-
fn from(_: BoundedVariantRange) -> Self {
112-
UnitBound
149+
impl BoundaryTerm<Depth> {
150+
pub fn is_exhaustive(&self) -> bool {
151+
self.clone().finalize().is_exhaustive()
113152
}
114153
}
115154

116-
impl OpenedUpperBound for UnitBound {
117-
fn opened_upper_bound(self) -> Boundedness<Self> {
118-
UnitBound.into()
119-
}
120-
}
155+
impl Finalize for SeparatedTerm<TokenVariance<Depth>> {
156+
type Output = TokenVariance<Depth>;
121157

122-
impl Product<BoundedVariantRange> for UnitBound {
123-
type Output = Boundedness<Self>;
158+
fn finalize(self) -> Self::Output {
159+
use Termination::{Closed, Open};
124160

125-
fn product(self, _: BoundedVariantRange) -> Self::Output {
126-
self.into()
161+
let SeparatedTerm(termination, term) = self;
162+
match termination {
163+
Open => ops::conjunction(term, One::one()),
164+
Closed => term.map_invariant(|term| term.map(|term| term.saturating_sub(1))),
165+
_ => term,
166+
}
127167
}
128168
}
129169

130-
impl Product<NonZeroUsize> for Boundedness<UnitBound> {
131-
type Output = Self;
132-
133-
fn product(self, _: NonZeroUsize) -> Self::Output {
134-
self
135-
}
136-
}
170+
define_natural_invariant!(
171+
/// Size of a glob expression.
172+
Size,
173+
);

src/token/variance/invariant/natural.rs

Lines changed: 0 additions & 165 deletions
This file was deleted.

src/token/variance/invariant/term.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use itertools::Itertools;
22
use std::collections::HashSet;
33
use std::hash::Hash;
44

5-
use crate::token::variance::bound::Boundedness;
65
use crate::token::variance::invariant::{Finalize, Invariant, One, Zero};
76
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
8-
use crate::token::variance::{TokenVariance, Variance};
7+
use crate::token::variance::{Boundedness, TokenVariance, Variance};
98
use crate::token::Composition;
109

1110
pub type BoundaryTerm<T> = TreeTerm<SeparatedTerm<TokenVariance<T>>>;

src/token/variance/invariant/text.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use std::collections::VecDeque;
33
use std::num::NonZeroUsize;
44

55
use crate::encode;
6-
use crate::token::variance::bound::{Boundedness, VariantRange};
76
use crate::token::variance::invariant::{Invariant, UnitBound, Zero};
7+
use crate::token::variance::natural::VariantRange;
88
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
9-
use crate::token::variance::{TokenVariance, Variance};
9+
use crate::token::variance::{Boundedness, TokenVariance, Variance};
1010
use crate::PATHS_ARE_CASE_INSENSITIVE;
1111

1212
use Fragment::{Nominal, Structural};

0 commit comments

Comments
 (0)