Skip to content

Commit

Permalink
syntax: rename 'Group' to 'Capture'
Browse files Browse the repository at this point in the history
Now that it *only* represents a capturing group, it makes sense to give
it a more specific name.
  • Loading branch information
BurntSushi committed Apr 17, 2023
1 parent 8be8092 commit ba6ce32
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 73 deletions.
2 changes: 1 addition & 1 deletion regex-syntax/src/hir/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Extractor {
}
Class(hir::Class::Bytes(ref cls)) => self.extract_class_bytes(cls),
Repetition(ref rep) => self.extract_repetition(rep),
Group(hir::Group { ref hir, .. }) => self.extract(hir),
Capture(hir::Capture { ref hir, .. }) => self.extract(hir),
Concat(ref hirs) => match self.kind {
ExtractKind::Prefix => self.extract_concat(hirs.iter()),
ExtractKind::Suffix => self.extract_concat(hirs.iter().rev()),
Expand Down
36 changes: 16 additions & 20 deletions regex-syntax/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub enum HirKind {
Look(Look),
/// A repetition operation applied to a child expression.
Repetition(Repetition),
/// A possibly capturing group, which contains a child expression.
Group(Group),
/// A capturing group, which contains a child expression.
Capture(Capture),
/// A concatenation of expressions. A concatenation always has at least two
/// child expressions.
///
Expand Down Expand Up @@ -329,11 +329,11 @@ impl Hir {
Hir { kind: HirKind::Repetition(rep), props }
}

/// Creates a group HIR expression.
/// Creates a capture HIR expression.
#[inline]
pub fn group(group: Group) -> Hir {
let props = Properties::group(&group);
Hir { kind: HirKind::Group(group), props }
pub fn capture(capture: Capture) -> Hir {
let props = Properties::capture(&capture);
Hir { kind: HirKind::Capture(capture), props }
}

/// Returns the concatenation of the given expressions.
Expand Down Expand Up @@ -529,7 +529,7 @@ impl HirKind {
| HirKind::Literal(_)
| HirKind::Class(_)
| HirKind::Look(_) => false,
HirKind::Group(_)
HirKind::Capture(_)
| HirKind::Repetition(_)
| HirKind::Concat(_)
| HirKind::Alternation(_) => true,
Expand Down Expand Up @@ -1431,10 +1431,10 @@ impl Look {
/// in a `Hir`. Instead, non-capturing grouping is handled automatically by
/// the recursive structure of the `Hir` itself.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Group {
/// The capture index of the group.
pub struct Capture {
/// The capture index of the capture.
pub index: u32,
/// The name of the group, if it exists.
/// The name of the capture, if it exists.
pub name: Option<Box<str>>,
/// The expression inside the capturing group, which may be empty.
pub hir: Box<Hir>,
Expand Down Expand Up @@ -1523,7 +1523,7 @@ impl Drop for Hir {
| HirKind::Literal(_)
| HirKind::Class(_)
| HirKind::Look(_) => return,
HirKind::Group(ref x) if !x.hir.kind.has_subexprs() => return,
HirKind::Capture(ref x) if !x.hir.kind.has_subexprs() => return,
HirKind::Repetition(ref x) if !x.hir.kind.has_subexprs() => return,
HirKind::Concat(ref x) if x.is_empty() => return,
HirKind::Alternation(ref x) if x.is_empty() => return,
Expand All @@ -1537,7 +1537,7 @@ impl Drop for Hir {
| HirKind::Literal(_)
| HirKind::Class(_)
| HirKind::Look(_) => {}
HirKind::Group(ref mut x) => {
HirKind::Capture(ref mut x) => {
stack.push(mem::replace(&mut x.hir, Hir::empty()));
}
HirKind::Repetition(ref mut x) => {
Expand Down Expand Up @@ -1955,13 +1955,9 @@ impl Properties {
Properties(Box::new(inner))
}

/// Create a new set of HIR properties for a group.
fn group(group: &Group) -> Properties {
// FIXME: Groups really should always have the same properties as
// their child expressions. But the literal properties somewhat
// over-constrained in what they represent in order to make downstream
// analyses a bit more straight-forward.
let p = group.hir.properties();
/// Create a new set of HIR properties for a capture.
fn capture(capture: &Capture) -> Properties {
let p = capture.hir.properties();
Properties(Box::new(PropertiesI {
captures_len: p.captures_len().saturating_add(1),
literal: false,
Expand Down Expand Up @@ -3055,7 +3051,7 @@ mod tests {
let run = || {
let mut expr = Hir::empty();
for _ in 0..100 {
expr = Hir::group(Group {
expr = Hir::capture(Capture {
index: 1,
name: None,
hir: Box::new(expr),
Expand Down
4 changes: 2 additions & 2 deletions regex-syntax/src/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<W: fmt::Write> Visitor for Writer<W> {
self.wtr.write_str(r"\B")?;
}
},
HirKind::Group(hir::Group { ref name, .. }) => {
HirKind::Capture(hir::Capture { ref name, .. }) => {
self.wtr.write_str("(")?;
if let Some(ref name) = *name {
write!(self.wtr, "?P<{}>", name)?;
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<W: fmt::Write> Visitor for Writer<W> {
self.wtr.write_str("?")?;
}
}
HirKind::Group(_)
HirKind::Capture(_)
| HirKind::Concat(_)
| HirKind::Alternation(_) => {
self.wtr.write_str(r")")?;
Expand Down
94 changes: 50 additions & 44 deletions regex-syntax/src/hir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ enum HirFrame {
/// This sentinel only exists to stop other things (like flattening
/// literals) from reaching across repetition operators.
Repetition,
/// This is pushed on to the stack upon first seeing any kind of group,
/// This is pushed on to the stack upon first seeing any kind of capture,
/// indicated by parentheses (including non-capturing groups). It is popped
/// upon leaving a group.
Group {
Expand Down Expand Up @@ -414,7 +414,7 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
let expr = self.pop().unwrap().unwrap_expr();
let old_flags = self.pop().unwrap().unwrap_group();
self.trans().flags.set(old_flags);
self.push(HirFrame::Expr(self.hir_group(x, expr)));
self.push(HirFrame::Expr(self.hir_capture(x, expr)));
}
Ast::Concat(_) => {
let mut exprs = vec![];
Expand Down Expand Up @@ -902,7 +902,7 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
})
}

fn hir_group(&self, group: &ast::Group, expr: Hir) -> Hir {
fn hir_capture(&self, group: &ast::Group, expr: Hir) -> Hir {
let (index, name) = match group.kind {
ast::GroupKind::CaptureIndex(index) => (index, None),
ast::GroupKind::CaptureName { ref name, .. } => {
Expand All @@ -912,7 +912,7 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
// in which the data type is defined handles this automatically.
ast::GroupKind::NonCapturing(_) => return expr,
};
Hir::group(hir::Group { index, name, hir: Box::new(expr) })
Hir::capture(hir::Capture { index, name, hir: Box::new(expr) })
}

fn hir_repetition(&self, rep: &ast::Repetition, expr: Hir) -> Hir {
Expand Down Expand Up @@ -1352,12 +1352,12 @@ mod tests {
Hir::literal(s)
}

fn hir_group(index: u32, expr: Hir) -> Hir {
Hir::group(hir::Group { index, name: None, hir: Box::new(expr) })
fn hir_capture(index: u32, expr: Hir) -> Hir {
Hir::capture(hir::Capture { index, name: None, hir: Box::new(expr) })
}

fn hir_group_name(index: u32, name: &str, expr: Hir) -> Hir {
Hir::group(hir::Group {
fn hir_capture_name(index: u32, name: &str, expr: Hir) -> Hir {
Hir::capture(hir::Capture {
index,
name: Some(name.into()),
hir: Box::new(expr),
Expand Down Expand Up @@ -1528,35 +1528,35 @@ mod tests {
fn empty() {
assert_eq!(t(""), Hir::empty());
assert_eq!(t("(?i)"), Hir::empty());
assert_eq!(t("()"), hir_group(1, Hir::empty()));
assert_eq!(t("()"), hir_capture(1, Hir::empty()));
assert_eq!(t("(?:)"), Hir::empty());
assert_eq!(t("(?P<wat>)"), hir_group_name(1, "wat", Hir::empty()));
assert_eq!(t("(?P<wat>)"), hir_capture_name(1, "wat", Hir::empty()));
assert_eq!(t("|"), hir_alt(vec![Hir::empty(), Hir::empty()]));
assert_eq!(
t("()|()"),
hir_alt(vec![
hir_group(1, Hir::empty()),
hir_group(2, Hir::empty()),
hir_capture(1, Hir::empty()),
hir_capture(2, Hir::empty()),
])
);
assert_eq!(
t("(|b)"),
hir_group(1, hir_alt(vec![Hir::empty(), hir_lit("b"),]))
hir_capture(1, hir_alt(vec![Hir::empty(), hir_lit("b"),]))
);
assert_eq!(
t("(a|)"),
hir_group(1, hir_alt(vec![hir_lit("a"), Hir::empty(),]))
hir_capture(1, hir_alt(vec![hir_lit("a"), Hir::empty(),]))
);
assert_eq!(
t("(a||c)"),
hir_group(
hir_capture(
1,
hir_alt(vec![hir_lit("a"), Hir::empty(), hir_lit("c"),])
)
);
assert_eq!(
t("(||)"),
hir_group(
hir_capture(
1,
hir_alt(vec![Hir::empty(), Hir::empty(), Hir::empty(),])
)
Expand Down Expand Up @@ -1740,56 +1740,59 @@ mod tests {

#[test]
fn group() {
assert_eq!(t("(a)"), hir_group(1, hir_lit("a")));
assert_eq!(t("(a)"), hir_capture(1, hir_lit("a")));
assert_eq!(
t("(a)(b)"),
hir_cat(vec![
hir_group(1, hir_lit("a")),
hir_group(2, hir_lit("b")),
hir_capture(1, hir_lit("a")),
hir_capture(2, hir_lit("b")),
])
);
assert_eq!(
t("(a)|(b)"),
hir_alt(vec![
hir_group(1, hir_lit("a")),
hir_group(2, hir_lit("b")),
hir_capture(1, hir_lit("a")),
hir_capture(2, hir_lit("b")),
])
);
assert_eq!(t("(?P<foo>)"), hir_group_name(1, "foo", Hir::empty()));
assert_eq!(t("(?P<foo>a)"), hir_group_name(1, "foo", hir_lit("a")));
assert_eq!(t("(?P<foo>)"), hir_capture_name(1, "foo", Hir::empty()));
assert_eq!(t("(?P<foo>a)"), hir_capture_name(1, "foo", hir_lit("a")));
assert_eq!(
t("(?P<foo>a)(?P<bar>b)"),
hir_cat(vec![
hir_group_name(1, "foo", hir_lit("a")),
hir_group_name(2, "bar", hir_lit("b")),
hir_capture_name(1, "foo", hir_lit("a")),
hir_capture_name(2, "bar", hir_lit("b")),
])
);
assert_eq!(t("(?:)"), Hir::empty());
assert_eq!(t("(?:a)"), hir_lit("a"));
assert_eq!(
t("(?:a)(b)"),
hir_cat(vec![hir_lit("a"), hir_group(1, hir_lit("b")),])
hir_cat(vec![hir_lit("a"), hir_capture(1, hir_lit("b")),])
);
assert_eq!(
t("(a)(?:b)(c)"),
hir_cat(vec![
hir_group(1, hir_lit("a")),
hir_capture(1, hir_lit("a")),
hir_lit("b"),
hir_group(2, hir_lit("c")),
hir_capture(2, hir_lit("c")),
])
);
assert_eq!(
t("(a)(?P<foo>b)(c)"),
hir_cat(vec![
hir_group(1, hir_lit("a")),
hir_group_name(2, "foo", hir_lit("b")),
hir_group(3, hir_lit("c")),
hir_capture(1, hir_lit("a")),
hir_capture_name(2, "foo", hir_lit("b")),
hir_capture(3, hir_lit("c")),
])
);
assert_eq!(t("()"), hir_group(1, Hir::empty()));
assert_eq!(t("((?i))"), hir_group(1, Hir::empty()));
assert_eq!(t("((?x))"), hir_group(1, Hir::empty()));
assert_eq!(t("(((?x)))"), hir_group(1, hir_group(2, Hir::empty())));
assert_eq!(t("()"), hir_capture(1, Hir::empty()));
assert_eq!(t("((?i))"), hir_capture(1, Hir::empty()));
assert_eq!(t("((?x))"), hir_capture(1, Hir::empty()));
assert_eq!(
t("(((?x)))"),
hir_capture(1, hir_capture(2, Hir::empty()))
);
}

#[test]
Expand Down Expand Up @@ -1818,7 +1821,7 @@ mod tests {
assert_eq!(
t("((?i-u)a)b"),
hir_cat(vec![
hir_group(1, hir_bclass(&[(b'A', b'A'), (b'a', b'a')])),
hir_capture(1, hir_bclass(&[(b'A', b'A'), (b'a', b'a')])),
hir_lit("b"),
])
);
Expand Down Expand Up @@ -1908,7 +1911,7 @@ mod tests {
t("ab?"),
hir_cat(vec![hir_lit("a"), hir_quest(true, hir_lit("b")),])
);
assert_eq!(t("(ab)?"), hir_quest(true, hir_group(1, hir_lit("ab"))));
assert_eq!(t("(ab)?"), hir_quest(true, hir_capture(1, hir_lit("ab"))));
assert_eq!(
t("a|b?"),
hir_alt(vec![hir_lit("a"), hir_quest(true, hir_lit("b")),])
Expand All @@ -1922,7 +1925,7 @@ mod tests {
let c = || hir_look(hir::Look::WordUnicode);
let d = || hir_look(hir::Look::WordUnicodeNegate);

assert_eq!(t("(^$)"), hir_group(1, hir_cat(vec![a(), b()])));
assert_eq!(t("(^$)"), hir_capture(1, hir_cat(vec![a(), b()])));
assert_eq!(t("^|$"), hir_alt(vec![a(), b()]));
assert_eq!(t(r"^|$|\b"), hir_alt(vec![a(), b(), c()]));
assert_eq!(
Expand All @@ -1933,11 +1936,14 @@ mod tests {
hir_cat(vec![c(), d()]),
])
);
assert_eq!(t("(^|$)"), hir_group(1, hir_alt(vec![a(), b()])));
assert_eq!(t(r"(^|$|\b)"), hir_group(1, hir_alt(vec![a(), b(), c()])));
assert_eq!(t("(^|$)"), hir_capture(1, hir_alt(vec![a(), b()])));
assert_eq!(
t(r"(^|$|\b)"),
hir_capture(1, hir_alt(vec![a(), b(), c()]))
);
assert_eq!(
t(r"(^$|$\b|\b\B)"),
hir_group(
hir_capture(
1,
hir_alt(vec![
hir_cat(vec![a(), b()]),
Expand All @@ -1948,15 +1954,15 @@ mod tests {
);
assert_eq!(
t(r"(^$|($\b|(\b\B)))"),
hir_group(
hir_capture(
1,
hir_alt(vec![
hir_cat(vec![a(), b()]),
hir_group(
hir_capture(
2,
hir_alt(vec![
hir_cat(vec![b(), c()]),
hir_group(3, hir_cat(vec![c(), d()])),
hir_capture(3, hir_cat(vec![c(), d()])),
])
),
])
Expand Down
10 changes: 5 additions & 5 deletions regex-syntax/src/hir/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ enum Frame<'a> {
/// A stack frame allocated just before descending into a repetition
/// operator's child node.
Repetition(&'a hir::Repetition),
/// A stack frame allocated just before descending into a group's child
/// A stack frame allocated just before descending into a capture's child
/// node.
Group(&'a hir::Group),
Capture(&'a hir::Capture),
/// The stack frame used while visiting every child node of a concatenation
/// of expressions.
Concat {
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<'a> HeapVisitor<'a> {
fn induct(&mut self, hir: &'a Hir) -> Option<Frame<'a>> {
match *hir.kind() {
HirKind::Repetition(ref x) => Some(Frame::Repetition(x)),
HirKind::Group(ref x) => Some(Frame::Group(x)),
HirKind::Capture(ref x) => Some(Frame::Capture(x)),
HirKind::Concat(ref x) if x.is_empty() => None,
HirKind::Concat(ref x) => {
Some(Frame::Concat { head: &x[0], tail: &x[1..] })
Expand All @@ -168,7 +168,7 @@ impl<'a> HeapVisitor<'a> {
fn pop(&self, induct: Frame<'a>) -> Option<Frame<'a>> {
match induct {
Frame::Repetition(_) => None,
Frame::Group(_) => None,
Frame::Capture(_) => None,
Frame::Concat { tail, .. } => {
if tail.is_empty() {
None
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<'a> Frame<'a> {
fn child(&self) -> &'a Hir {
match *self {
Frame::Repetition(rep) => &rep.hir,
Frame::Group(group) => &group.hir,
Frame::Capture(capture) => &capture.hir,
Frame::Concat { head, .. } => head,
Frame::Alternation { head, .. } => head,
}
Expand Down
Loading

0 comments on commit ba6ce32

Please sign in to comment.