Skip to content

Commit f48d417

Browse files
committed
syntax: simplify hir::GroupKind
I'm not sure exactly why I used three variants instead of two like how I've defined it in this patch. Possibly because the AST uses three variants? (The AST needs to do a little more work to store a span associated with where the name actually is in the expression, so it maybe makes a little more sense there.) In any case, this is the first step of many in simplifying the HIR.
1 parent f4ee2fb commit f48d417

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

regex-syntax/src/hir/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,19 +1383,17 @@ pub struct Group {
13831383
/// The kind of group.
13841384
#[derive(Clone, Debug, Eq, PartialEq)]
13851385
pub enum GroupKind {
1386-
/// A normal unnamed capturing group.
1386+
/// A non-capturing group.
1387+
NonCapturing,
1388+
/// A capturing group with an optional name.
13871389
///
13881390
/// The value is the capture index of the group.
1389-
CaptureIndex(u32),
1390-
/// A named capturing group.
1391-
CaptureName {
1392-
/// The name of the group.
1393-
name: String,
1391+
Capture {
13941392
/// The capture index of the group.
13951393
index: u32,
1394+
/// The name of the group, if it exists.
1395+
name: Option<String>,
13961396
},
1397-
/// A non-capturing group.
1398-
NonCapturing,
13991397
}
14001398

14011399
/// The high-level intermediate representation of a repetition operator.

regex-syntax/src/hir/print.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ impl<W: fmt::Write> Visitor for Writer<W> {
150150
self.wtr.write_str(r"(?-u:\B)")?;
151151
}
152152
HirKind::Group(ref x) => match x.kind {
153-
hir::GroupKind::CaptureIndex(_) => {
153+
hir::GroupKind::Capture { ref name, .. } => {
154154
self.wtr.write_str("(")?;
155-
}
156-
hir::GroupKind::CaptureName { ref name, .. } => {
157-
write!(self.wtr, "(?P<{}>", name)?;
155+
if let Some(ref name) = *name {
156+
write!(self.wtr, "?P<{}>", name)?;
157+
}
158158
}
159159
hir::GroupKind::NonCapturing => {
160160
self.wtr.write_str("(?:")?;

regex-syntax/src/hir/translate.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,13 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
763763

764764
fn hir_group(&self, group: &ast::Group, expr: Hir) -> Hir {
765765
let kind = match group.kind {
766-
ast::GroupKind::CaptureIndex(idx) => {
767-
hir::GroupKind::CaptureIndex(idx)
768-
}
769-
ast::GroupKind::CaptureName(ref capname) => {
770-
hir::GroupKind::CaptureName {
771-
name: capname.name.clone(),
772-
index: capname.index,
773-
}
766+
ast::GroupKind::CaptureIndex(index) => {
767+
hir::GroupKind::Capture { index, name: None }
774768
}
769+
ast::GroupKind::CaptureName(ref cap) => hir::GroupKind::Capture {
770+
index: cap.index,
771+
name: Some(cap.name.clone()),
772+
},
775773
ast::GroupKind::NonCapturing(_) => hir::GroupKind::NonCapturing,
776774
};
777775
Hir::group(hir::Group { kind, hir: Box::new(expr) })
@@ -1211,16 +1209,16 @@ mod tests {
12111209

12121210
fn hir_group(i: u32, expr: Hir) -> Hir {
12131211
Hir::group(hir::Group {
1214-
kind: hir::GroupKind::CaptureIndex(i),
1212+
kind: hir::GroupKind::Capture { index: i, name: None },
12151213
hir: Box::new(expr),
12161214
})
12171215
}
12181216

12191217
fn hir_group_name(i: u32, name: &str, expr: Hir) -> Hir {
12201218
Hir::group(hir::Group {
1221-
kind: hir::GroupKind::CaptureName {
1222-
name: name.to_string(),
1219+
kind: hir::GroupKind::Capture {
12231220
index: i,
1221+
name: Some(name.to_string()),
12241222
},
12251223
hir: Box::new(expr),
12261224
})

src/compile.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,13 @@ impl Compiler {
362362
}
363363
Group(ref g) => match g.kind {
364364
hir::GroupKind::NonCapturing => self.c(&g.hir),
365-
hir::GroupKind::CaptureIndex(index) => {
365+
hir::GroupKind::Capture { index, ref name } => {
366366
if index as usize >= self.compiled.captures.len() {
367-
self.compiled.captures.push(None);
368-
}
369-
self.c_capture(2 * index as usize, &g.hir)
370-
}
371-
hir::GroupKind::CaptureName { index, ref name } => {
372-
if index as usize >= self.compiled.captures.len() {
373-
let n = name.to_string();
374-
self.compiled.captures.push(Some(n.clone()));
375-
self.capture_name_idx.insert(n, index as usize);
367+
self.compiled.captures.push(name.clone());
368+
if let Some(ref name) = *name {
369+
self.capture_name_idx
370+
.insert(name.clone(), index as usize);
371+
}
376372
}
377373
self.c_capture(2 * index as usize, &g.hir)
378374
}

0 commit comments

Comments
 (0)