Skip to content

Commit e7c2584

Browse files
committed
Change how flatten works on hir::Lookaround
The lack of recursing into the inner expression of a lookaround is correct under the current assumption that lookarounds cannot have capture groups. But once the restriction is lifted, this wrong implementation can be very subtle to find. Instead, we can already do the filtering and accept it being a no-op for now.
1 parent 9f1a5e1 commit e7c2584

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

regex-automata/src/meta/reverse_inner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ fn flatten(hir: &Hir) -> Hir {
207207
HirKind::Literal(hir::Literal(ref x)) => Hir::literal(x.clone()),
208208
HirKind::Class(ref x) => Hir::class(x.clone()),
209209
HirKind::Look(ref x) => Hir::look(x.clone()),
210-
HirKind::Lookaround(ref x) => Hir::lookaround(x.clone()),
210+
HirKind::Lookaround(ref x) => {
211+
Hir::lookaround(x.with(flatten(x.sub())))
212+
}
211213
HirKind::Repetition(ref x) => Hir::repetition(x.with(flatten(&x.sub))),
212214
// This is the interesting case. We just drop the group information
213215
// entirely and use the child HIR itself.

regex-syntax/src/hir/mod.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,15 +1796,15 @@ impl Look {
17961796
}
17971797
}
17981798

1799-
/// Represents a general lookaround assertion
1799+
/// Represents a general lookaround assertion.
18001800
///
18011801
/// Currently, only lookbehind assertions are supported.
18021802
/// Furthermore, capture groups inside assertions are not supported.
18031803
#[derive(Clone, Debug, Eq, PartialEq)]
18041804
pub enum Lookaround {
1805-
/// A positive lookbehind assertion
1805+
/// A positive lookbehind assertion.
18061806
PositiveLookBehind(Box<Hir>),
1807-
/// A negative lookbehind assertion
1807+
/// A negative lookbehind assertion.
18081808
NegativeLookBehind(Box<Hir>),
18091809
}
18101810

@@ -1813,16 +1813,31 @@ impl Lookaround {
18131813
/// lookaround assertion to hold.
18141814
pub fn sub(&self) -> &Hir {
18151815
match self {
1816-
Lookaround::PositiveLookBehind(sub)
1817-
| Lookaround::NegativeLookBehind(sub) => sub,
1816+
Self::PositiveLookBehind(sub) | Self::NegativeLookBehind(sub) => {
1817+
sub
1818+
}
18181819
}
18191820
}
18201821

18211822
/// Returns a mutable reference to the inner expression
18221823
pub fn sub_mut(&mut self) -> &mut Hir {
18231824
match self {
1824-
Lookaround::PositiveLookBehind(sub)
1825-
| Lookaround::NegativeLookBehind(sub) => sub,
1825+
Self::PositiveLookBehind(sub) | Self::NegativeLookBehind(sub) => {
1826+
sub
1827+
}
1828+
}
1829+
}
1830+
1831+
/// Returns a new lookaround of the same kind, but with its
1832+
/// sub-expression replaced with the one given.
1833+
pub fn with(&self, sub: Hir) -> Lookaround {
1834+
match self {
1835+
Self::PositiveLookBehind(_) => {
1836+
Self::PositiveLookBehind(Box::new(sub))
1837+
}
1838+
Self::NegativeLookBehind(_) => {
1839+
Self::NegativeLookBehind(Box::new(sub))
1840+
}
18261841
}
18271842
}
18281843
}

0 commit comments

Comments
 (0)