Skip to content

Commit a6f1d03

Browse files
committed
syntax: rename 'Group' to 'Capture'
Now that it *only* represents a capturing group, it makes sense to give it a more specific name.
1 parent c6c518a commit a6f1d03

File tree

6 files changed

+75
-73
lines changed

6 files changed

+75
-73
lines changed

regex-syntax/src/hir/literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl Extractor {
186186
}
187187
Class(hir::Class::Bytes(ref cls)) => self.extract_class_bytes(cls),
188188
Repetition(ref rep) => self.extract_repetition(rep),
189-
Group(hir::Group { ref hir, .. }) => self.extract(hir),
189+
Capture(hir::Capture { ref hir, .. }) => self.extract(hir),
190190
Concat(ref hirs) => match self.kind {
191191
ExtractKind::Prefix => self.extract_concat(hirs.iter()),
192192
ExtractKind::Suffix => self.extract_concat(hirs.iter().rev()),

regex-syntax/src/hir/mod.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ pub enum HirKind {
185185
Look(Look),
186186
/// A repetition operation applied to a child expression.
187187
Repetition(Repetition),
188-
/// A possibly capturing group, which contains a child expression.
189-
Group(Group),
188+
/// A capturing group, which contains a child expression.
189+
Capture(Capture),
190190
/// A concatenation of expressions. A concatenation always has at least two
191191
/// child expressions.
192192
///
@@ -329,11 +329,11 @@ impl Hir {
329329
Hir { kind: HirKind::Repetition(rep), props }
330330
}
331331

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

339339
/// Returns the concatenation of the given expressions.
@@ -529,7 +529,7 @@ impl HirKind {
529529
| HirKind::Literal(_)
530530
| HirKind::Class(_)
531531
| HirKind::Look(_) => false,
532-
HirKind::Group(_)
532+
HirKind::Capture(_)
533533
| HirKind::Repetition(_)
534534
| HirKind::Concat(_)
535535
| HirKind::Alternation(_) => true,
@@ -1431,10 +1431,10 @@ impl Look {
14311431
/// in a `Hir`. Instead, non-capturing grouping is handled automatically by
14321432
/// the recursive structure of the `Hir` itself.
14331433
#[derive(Clone, Debug, Eq, PartialEq)]
1434-
pub struct Group {
1435-
/// The capture index of the group.
1434+
pub struct Capture {
1435+
/// The capture index of the capture.
14361436
pub index: u32,
1437-
/// The name of the group, if it exists.
1437+
/// The name of the capture, if it exists.
14381438
pub name: Option<Box<str>>,
14391439
/// The expression inside the capturing group, which may be empty.
14401440
pub hir: Box<Hir>,
@@ -1523,7 +1523,7 @@ impl Drop for Hir {
15231523
| HirKind::Literal(_)
15241524
| HirKind::Class(_)
15251525
| HirKind::Look(_) => return,
1526-
HirKind::Group(ref x) if !x.hir.kind.has_subexprs() => return,
1526+
HirKind::Capture(ref x) if !x.hir.kind.has_subexprs() => return,
15271527
HirKind::Repetition(ref x) if !x.hir.kind.has_subexprs() => return,
15281528
HirKind::Concat(ref x) if x.is_empty() => return,
15291529
HirKind::Alternation(ref x) if x.is_empty() => return,
@@ -1537,7 +1537,7 @@ impl Drop for Hir {
15371537
| HirKind::Literal(_)
15381538
| HirKind::Class(_)
15391539
| HirKind::Look(_) => {}
1540-
HirKind::Group(ref mut x) => {
1540+
HirKind::Capture(ref mut x) => {
15411541
stack.push(mem::replace(&mut x.hir, Hir::empty()));
15421542
}
15431543
HirKind::Repetition(ref mut x) => {
@@ -1955,13 +1955,9 @@ impl Properties {
19551955
Properties(Box::new(inner))
19561956
}
19571957

1958-
/// Create a new set of HIR properties for a group.
1959-
fn group(group: &Group) -> Properties {
1960-
// FIXME: Groups really should always have the same properties as
1961-
// their child expressions. But the literal properties somewhat
1962-
// over-constrained in what they represent in order to make downstream
1963-
// analyses a bit more straight-forward.
1964-
let p = group.hir.properties();
1958+
/// Create a new set of HIR properties for a capture.
1959+
fn capture(capture: &Capture) -> Properties {
1960+
let p = capture.hir.properties();
19651961
Properties(Box::new(PropertiesI {
19661962
captures_len: p.captures_len().saturating_add(1),
19671963
literal: false,
@@ -3055,7 +3051,7 @@ mod tests {
30553051
let run = || {
30563052
let mut expr = Hir::empty();
30573053
for _ in 0..100 {
3058-
expr = Hir::group(Group {
3054+
expr = Hir::capture(Capture {
30593055
index: 1,
30603056
name: None,
30613057
hir: Box::new(expr),

regex-syntax/src/hir/print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<W: fmt::Write> Visitor for Writer<W> {
190190
self.wtr.write_str(r"\B")?;
191191
}
192192
},
193-
HirKind::Group(hir::Group { ref name, .. }) => {
193+
HirKind::Capture(hir::Capture { ref name, .. }) => {
194194
self.wtr.write_str("(")?;
195195
if let Some(ref name) = *name {
196196
write!(self.wtr, "?P<{}>", name)?;
@@ -254,7 +254,7 @@ impl<W: fmt::Write> Visitor for Writer<W> {
254254
self.wtr.write_str("?")?;
255255
}
256256
}
257-
HirKind::Group(_)
257+
HirKind::Capture(_)
258258
| HirKind::Concat(_)
259259
| HirKind::Alternation(_) => {
260260
self.wtr.write_str(r")")?;

regex-syntax/src/hir/translate.rs

+50-44
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ enum HirFrame {
173173
/// This sentinel only exists to stop other things (like flattening
174174
/// literals) from reaching across repetition operators.
175175
Repetition,
176-
/// This is pushed on to the stack upon first seeing any kind of group,
176+
/// This is pushed on to the stack upon first seeing any kind of capture,
177177
/// indicated by parentheses (including non-capturing groups). It is popped
178178
/// upon leaving a group.
179179
Group {
@@ -414,7 +414,7 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
414414
let expr = self.pop().unwrap().unwrap_expr();
415415
let old_flags = self.pop().unwrap().unwrap_group();
416416
self.trans().flags.set(old_flags);
417-
self.push(HirFrame::Expr(self.hir_group(x, expr)));
417+
self.push(HirFrame::Expr(self.hir_capture(x, expr)));
418418
}
419419
Ast::Concat(_) => {
420420
let mut exprs = vec![];
@@ -902,7 +902,7 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
902902
})
903903
}
904904

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

918918
fn hir_repetition(&self, rep: &ast::Repetition, expr: Hir) -> Hir {
@@ -1352,12 +1352,12 @@ mod tests {
13521352
Hir::literal(s)
13531353
}
13541354

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

1359-
fn hir_group_name(index: u32, name: &str, expr: Hir) -> Hir {
1360-
Hir::group(hir::Group {
1359+
fn hir_capture_name(index: u32, name: &str, expr: Hir) -> Hir {
1360+
Hir::capture(hir::Capture {
13611361
index,
13621362
name: Some(name.into()),
13631363
hir: Box::new(expr),
@@ -1528,35 +1528,35 @@ mod tests {
15281528
fn empty() {
15291529
assert_eq!(t(""), Hir::empty());
15301530
assert_eq!(t("(?i)"), Hir::empty());
1531-
assert_eq!(t("()"), hir_group(1, Hir::empty()));
1531+
assert_eq!(t("()"), hir_capture(1, Hir::empty()));
15321532
assert_eq!(t("(?:)"), Hir::empty());
1533-
assert_eq!(t("(?P<wat>)"), hir_group_name(1, "wat", Hir::empty()));
1533+
assert_eq!(t("(?P<wat>)"), hir_capture_name(1, "wat", Hir::empty()));
15341534
assert_eq!(t("|"), hir_alt(vec![Hir::empty(), Hir::empty()]));
15351535
assert_eq!(
15361536
t("()|()"),
15371537
hir_alt(vec![
1538-
hir_group(1, Hir::empty()),
1539-
hir_group(2, Hir::empty()),
1538+
hir_capture(1, Hir::empty()),
1539+
hir_capture(2, Hir::empty()),
15401540
])
15411541
);
15421542
assert_eq!(
15431543
t("(|b)"),
1544-
hir_group(1, hir_alt(vec![Hir::empty(), hir_lit("b"),]))
1544+
hir_capture(1, hir_alt(vec![Hir::empty(), hir_lit("b"),]))
15451545
);
15461546
assert_eq!(
15471547
t("(a|)"),
1548-
hir_group(1, hir_alt(vec![hir_lit("a"), Hir::empty(),]))
1548+
hir_capture(1, hir_alt(vec![hir_lit("a"), Hir::empty(),]))
15491549
);
15501550
assert_eq!(
15511551
t("(a||c)"),
1552-
hir_group(
1552+
hir_capture(
15531553
1,
15541554
hir_alt(vec![hir_lit("a"), Hir::empty(), hir_lit("c"),])
15551555
)
15561556
);
15571557
assert_eq!(
15581558
t("(||)"),
1559-
hir_group(
1559+
hir_capture(
15601560
1,
15611561
hir_alt(vec![Hir::empty(), Hir::empty(), Hir::empty(),])
15621562
)
@@ -1740,56 +1740,59 @@ mod tests {
17401740

17411741
#[test]
17421742
fn group() {
1743-
assert_eq!(t("(a)"), hir_group(1, hir_lit("a")));
1743+
assert_eq!(t("(a)"), hir_capture(1, hir_lit("a")));
17441744
assert_eq!(
17451745
t("(a)(b)"),
17461746
hir_cat(vec![
1747-
hir_group(1, hir_lit("a")),
1748-
hir_group(2, hir_lit("b")),
1747+
hir_capture(1, hir_lit("a")),
1748+
hir_capture(2, hir_lit("b")),
17491749
])
17501750
);
17511751
assert_eq!(
17521752
t("(a)|(b)"),
17531753
hir_alt(vec![
1754-
hir_group(1, hir_lit("a")),
1755-
hir_group(2, hir_lit("b")),
1754+
hir_capture(1, hir_lit("a")),
1755+
hir_capture(2, hir_lit("b")),
17561756
])
17571757
);
1758-
assert_eq!(t("(?P<foo>)"), hir_group_name(1, "foo", Hir::empty()));
1759-
assert_eq!(t("(?P<foo>a)"), hir_group_name(1, "foo", hir_lit("a")));
1758+
assert_eq!(t("(?P<foo>)"), hir_capture_name(1, "foo", Hir::empty()));
1759+
assert_eq!(t("(?P<foo>a)"), hir_capture_name(1, "foo", hir_lit("a")));
17601760
assert_eq!(
17611761
t("(?P<foo>a)(?P<bar>b)"),
17621762
hir_cat(vec![
1763-
hir_group_name(1, "foo", hir_lit("a")),
1764-
hir_group_name(2, "bar", hir_lit("b")),
1763+
hir_capture_name(1, "foo", hir_lit("a")),
1764+
hir_capture_name(2, "bar", hir_lit("b")),
17651765
])
17661766
);
17671767
assert_eq!(t("(?:)"), Hir::empty());
17681768
assert_eq!(t("(?:a)"), hir_lit("a"));
17691769
assert_eq!(
17701770
t("(?:a)(b)"),
1771-
hir_cat(vec![hir_lit("a"), hir_group(1, hir_lit("b")),])
1771+
hir_cat(vec![hir_lit("a"), hir_capture(1, hir_lit("b")),])
17721772
);
17731773
assert_eq!(
17741774
t("(a)(?:b)(c)"),
17751775
hir_cat(vec![
1776-
hir_group(1, hir_lit("a")),
1776+
hir_capture(1, hir_lit("a")),
17771777
hir_lit("b"),
1778-
hir_group(2, hir_lit("c")),
1778+
hir_capture(2, hir_lit("c")),
17791779
])
17801780
);
17811781
assert_eq!(
17821782
t("(a)(?P<foo>b)(c)"),
17831783
hir_cat(vec![
1784-
hir_group(1, hir_lit("a")),
1785-
hir_group_name(2, "foo", hir_lit("b")),
1786-
hir_group(3, hir_lit("c")),
1784+
hir_capture(1, hir_lit("a")),
1785+
hir_capture_name(2, "foo", hir_lit("b")),
1786+
hir_capture(3, hir_lit("c")),
17871787
])
17881788
);
1789-
assert_eq!(t("()"), hir_group(1, Hir::empty()));
1790-
assert_eq!(t("((?i))"), hir_group(1, Hir::empty()));
1791-
assert_eq!(t("((?x))"), hir_group(1, Hir::empty()));
1792-
assert_eq!(t("(((?x)))"), hir_group(1, hir_group(2, Hir::empty())));
1789+
assert_eq!(t("()"), hir_capture(1, Hir::empty()));
1790+
assert_eq!(t("((?i))"), hir_capture(1, Hir::empty()));
1791+
assert_eq!(t("((?x))"), hir_capture(1, Hir::empty()));
1792+
assert_eq!(
1793+
t("(((?x)))"),
1794+
hir_capture(1, hir_capture(2, Hir::empty()))
1795+
);
17931796
}
17941797

17951798
#[test]
@@ -1818,7 +1821,7 @@ mod tests {
18181821
assert_eq!(
18191822
t("((?i-u)a)b"),
18201823
hir_cat(vec![
1821-
hir_group(1, hir_bclass(&[(b'A', b'A'), (b'a', b'a')])),
1824+
hir_capture(1, hir_bclass(&[(b'A', b'A'), (b'a', b'a')])),
18221825
hir_lit("b"),
18231826
])
18241827
);
@@ -1908,7 +1911,7 @@ mod tests {
19081911
t("ab?"),
19091912
hir_cat(vec![hir_lit("a"), hir_quest(true, hir_lit("b")),])
19101913
);
1911-
assert_eq!(t("(ab)?"), hir_quest(true, hir_group(1, hir_lit("ab"))));
1914+
assert_eq!(t("(ab)?"), hir_quest(true, hir_capture(1, hir_lit("ab"))));
19121915
assert_eq!(
19131916
t("a|b?"),
19141917
hir_alt(vec![hir_lit("a"), hir_quest(true, hir_lit("b")),])
@@ -1922,7 +1925,7 @@ mod tests {
19221925
let c = || hir_look(hir::Look::WordUnicode);
19231926
let d = || hir_look(hir::Look::WordUnicodeNegate);
19241927

1925-
assert_eq!(t("(^$)"), hir_group(1, hir_cat(vec![a(), b()])));
1928+
assert_eq!(t("(^$)"), hir_capture(1, hir_cat(vec![a(), b()])));
19261929
assert_eq!(t("^|$"), hir_alt(vec![a(), b()]));
19271930
assert_eq!(t(r"^|$|\b"), hir_alt(vec![a(), b(), c()]));
19281931
assert_eq!(
@@ -1933,11 +1936,14 @@ mod tests {
19331936
hir_cat(vec![c(), d()]),
19341937
])
19351938
);
1936-
assert_eq!(t("(^|$)"), hir_group(1, hir_alt(vec![a(), b()])));
1937-
assert_eq!(t(r"(^|$|\b)"), hir_group(1, hir_alt(vec![a(), b(), c()])));
1939+
assert_eq!(t("(^|$)"), hir_capture(1, hir_alt(vec![a(), b()])));
1940+
assert_eq!(
1941+
t(r"(^|$|\b)"),
1942+
hir_capture(1, hir_alt(vec![a(), b(), c()]))
1943+
);
19381944
assert_eq!(
19391945
t(r"(^$|$\b|\b\B)"),
1940-
hir_group(
1946+
hir_capture(
19411947
1,
19421948
hir_alt(vec![
19431949
hir_cat(vec![a(), b()]),
@@ -1948,15 +1954,15 @@ mod tests {
19481954
);
19491955
assert_eq!(
19501956
t(r"(^$|($\b|(\b\B)))"),
1951-
hir_group(
1957+
hir_capture(
19521958
1,
19531959
hir_alt(vec![
19541960
hir_cat(vec![a(), b()]),
1955-
hir_group(
1961+
hir_capture(
19561962
2,
19571963
hir_alt(vec![
19581964
hir_cat(vec![b(), c()]),
1959-
hir_group(3, hir_cat(vec![c(), d()])),
1965+
hir_capture(3, hir_cat(vec![c(), d()])),
19601966
])
19611967
),
19621968
])

regex-syntax/src/hir/visitor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ enum Frame<'a> {
7575
/// A stack frame allocated just before descending into a repetition
7676
/// operator's child node.
7777
Repetition(&'a hir::Repetition),
78-
/// A stack frame allocated just before descending into a group's child
78+
/// A stack frame allocated just before descending into a capture's child
7979
/// node.
80-
Group(&'a hir::Group),
80+
Capture(&'a hir::Capture),
8181
/// The stack frame used while visiting every child node of a concatenation
8282
/// of expressions.
8383
Concat {
@@ -150,7 +150,7 @@ impl<'a> HeapVisitor<'a> {
150150
fn induct(&mut self, hir: &'a Hir) -> Option<Frame<'a>> {
151151
match *hir.kind() {
152152
HirKind::Repetition(ref x) => Some(Frame::Repetition(x)),
153-
HirKind::Group(ref x) => Some(Frame::Group(x)),
153+
HirKind::Capture(ref x) => Some(Frame::Capture(x)),
154154
HirKind::Concat(ref x) if x.is_empty() => None,
155155
HirKind::Concat(ref x) => {
156156
Some(Frame::Concat { head: &x[0], tail: &x[1..] })
@@ -168,7 +168,7 @@ impl<'a> HeapVisitor<'a> {
168168
fn pop(&self, induct: Frame<'a>) -> Option<Frame<'a>> {
169169
match induct {
170170
Frame::Repetition(_) => None,
171-
Frame::Group(_) => None,
171+
Frame::Capture(_) => None,
172172
Frame::Concat { tail, .. } => {
173173
if tail.is_empty() {
174174
None
@@ -196,7 +196,7 @@ impl<'a> Frame<'a> {
196196
fn child(&self) -> &'a Hir {
197197
match *self {
198198
Frame::Repetition(rep) => &rep.hir,
199-
Frame::Group(group) => &group.hir,
199+
Frame::Capture(capture) => &capture.hir,
200200
Frame::Concat { head, .. } => head,
201201
Frame::Alternation { head, .. } => head,
202202
}

0 commit comments

Comments
 (0)