Skip to content

Commit 3f09a5f

Browse files
committed
Switch CaptureName to use fields to simplify the code.
1 parent cf39a71 commit 3f09a5f

File tree

4 files changed

+70
-64
lines changed

4 files changed

+70
-64
lines changed

regex-syntax/src/ast/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1203,9 +1203,7 @@ impl Group {
12031203
/// Returns true if and only if this group is capturing.
12041204
pub fn is_capturing(&self) -> bool {
12051205
match self.kind {
1206-
GroupKind::CaptureIndex(_)
1207-
| GroupKind::CaptureName(_)
1208-
| GroupKind::CapturePName(_) => true,
1206+
GroupKind::CaptureIndex(_) | GroupKind::CaptureName { .. } => true,
12091207
GroupKind::NonCapturing(_) => false,
12101208
}
12111209
}
@@ -1216,9 +1214,7 @@ impl Group {
12161214
pub fn capture_index(&self) -> Option<u32> {
12171215
match self.kind {
12181216
GroupKind::CaptureIndex(i) => Some(i),
1219-
GroupKind::CaptureName(ref x) | GroupKind::CapturePName(ref x) => {
1220-
Some(x.index)
1221-
}
1217+
GroupKind::CaptureName { ref name, .. } => Some(name.index),
12221218
GroupKind::NonCapturing(_) => None,
12231219
}
12241220
}
@@ -1229,10 +1225,13 @@ impl Group {
12291225
pub enum GroupKind {
12301226
/// `(a)`
12311227
CaptureIndex(u32),
1232-
/// `(?<name>a)`
1233-
CaptureName(CaptureName),
1234-
/// `(?P<name>a)`
1235-
CapturePName(CaptureName),
1228+
/// `(?<name>a)` or `(?P<name>a)`
1229+
CaptureName {
1230+
/// True if the `?P<` syntax is used and false if the `?<` syntax is used.
1231+
starts_with_p: bool,
1232+
/// The capture name.
1233+
name: CaptureName,
1234+
},
12361235
/// `(?:a)` and `(?i:a)`
12371236
NonCapturing(Flags),
12381237
}

regex-syntax/src/ast/parse.rs

+54-41
Original file line numberDiff line numberDiff line change
@@ -1195,21 +1195,16 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
11951195
));
11961196
}
11971197
let inner_span = self.span();
1198-
let mut has_p = false;
1199-
if self.bump_if("?<") || {
1200-
has_p = true;
1201-
self.bump_if("?P<")
1198+
let mut starts_with_p = true;
1199+
if self.bump_if("?P<") || {
1200+
starts_with_p = false;
1201+
self.bump_if("?<")
12021202
} {
12031203
let capture_index = self.next_capture_index(open_span)?;
1204-
let cap = self.parse_capture_name(capture_index)?;
1205-
let kind = if has_p {
1206-
ast::GroupKind::CapturePName
1207-
} else {
1208-
ast::GroupKind::CaptureName
1209-
};
1204+
let name = self.parse_capture_name(capture_index)?;
12101205
Ok(Either::Right(ast::Group {
12111206
span: open_span,
1212-
kind: kind(cap),
1207+
kind: ast::GroupKind::CaptureName { starts_with_p, name },
12131208
ast: Box::new(Ast::Empty(self.span())),
12141209
}))
12151210
} else if self.bump_if("?") {
@@ -2808,11 +2803,14 @@ bar
28082803
flag_set(pat, 0..4, ast::Flag::IgnoreWhitespace, false),
28092804
Ast::Group(ast::Group {
28102805
span: span_range(pat, 4..pat.len()),
2811-
kind: ast::GroupKind::CapturePName(ast::CaptureName {
2812-
span: span_range(pat, 9..12),
2813-
name: s("foo"),
2814-
index: 1,
2815-
}),
2806+
kind: ast::GroupKind::CaptureName {
2807+
starts_with_p: true,
2808+
name: ast::CaptureName {
2809+
span: span_range(pat, 9..12),
2810+
name: s("foo"),
2811+
index: 1,
2812+
}
2813+
},
28162814
ast: Box::new(lit_with('a', span_range(pat, 14..15))),
28172815
}),
28182816
]
@@ -3831,23 +3829,29 @@ bar
38313829
parser("(?P<a>z)").parse(),
38323830
Ok(Ast::Group(ast::Group {
38333831
span: span(0..8),
3834-
kind: ast::GroupKind::CapturePName(ast::CaptureName {
3835-
span: span(4..5),
3836-
name: s("a"),
3837-
index: 1,
3838-
}),
3832+
kind: ast::GroupKind::CaptureName {
3833+
starts_with_p: true,
3834+
name: ast::CaptureName {
3835+
span: span(4..5),
3836+
name: s("a"),
3837+
index: 1,
3838+
}
3839+
},
38393840
ast: Box::new(lit('z', 6)),
38403841
}))
38413842
);
38423843
assert_eq!(
38433844
parser("(?P<abc>z)").parse(),
38443845
Ok(Ast::Group(ast::Group {
38453846
span: span(0..10),
3846-
kind: ast::GroupKind::CapturePName(ast::CaptureName {
3847-
span: span(4..7),
3848-
name: s("abc"),
3849-
index: 1,
3850-
}),
3847+
kind: ast::GroupKind::CaptureName {
3848+
starts_with_p: true,
3849+
name: ast::CaptureName {
3850+
span: span(4..7),
3851+
name: s("abc"),
3852+
index: 1,
3853+
}
3854+
},
38513855
ast: Box::new(lit('z', 8)),
38523856
}))
38533857
);
@@ -3856,11 +3860,14 @@ bar
38563860
parser("(?P<a_1>z)").parse(),
38573861
Ok(Ast::Group(ast::Group {
38583862
span: span(0..10),
3859-
kind: ast::GroupKind::CapturePName(ast::CaptureName {
3860-
span: span(4..7),
3861-
name: s("a_1"),
3862-
index: 1,
3863-
}),
3863+
kind: ast::GroupKind::CaptureName {
3864+
starts_with_p: true,
3865+
name: ast::CaptureName {
3866+
span: span(4..7),
3867+
name: s("a_1"),
3868+
index: 1,
3869+
}
3870+
},
38643871
ast: Box::new(lit('z', 8)),
38653872
}))
38663873
);
@@ -3869,11 +3876,14 @@ bar
38693876
parser("(?P<a.1>z)").parse(),
38703877
Ok(Ast::Group(ast::Group {
38713878
span: span(0..10),
3872-
kind: ast::GroupKind::CapturePName(ast::CaptureName {
3873-
span: span(4..7),
3874-
name: s("a.1"),
3875-
index: 1,
3876-
}),
3879+
kind: ast::GroupKind::CaptureName {
3880+
starts_with_p: true,
3881+
name: ast::CaptureName {
3882+
span: span(4..7),
3883+
name: s("a.1"),
3884+
index: 1,
3885+
}
3886+
},
38773887
ast: Box::new(lit('z', 8)),
38783888
}))
38793889
);
@@ -3882,11 +3892,14 @@ bar
38823892
parser("(?P<a[1]>z)").parse(),
38833893
Ok(Ast::Group(ast::Group {
38843894
span: span(0..11),
3885-
kind: ast::GroupKind::CapturePName(ast::CaptureName {
3886-
span: span(4..8),
3887-
name: s("a[1]"),
3888-
index: 1,
3889-
}),
3895+
kind: ast::GroupKind::CaptureName {
3896+
starts_with_p: true,
3897+
name: ast::CaptureName {
3898+
span: span(4..8),
3899+
name: s("a[1]"),
3900+
index: 1,
3901+
}
3902+
},
38903903
ast: Box::new(lit('z', 9)),
38913904
}))
38923905
);

regex-syntax/src/ast/print.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,10 @@ impl<W: fmt::Write> Writer<W> {
157157
use crate::ast::GroupKind::*;
158158
match ast.kind {
159159
CaptureIndex(_) => self.wtr.write_str("("),
160-
CaptureName(ref x) => {
161-
self.wtr.write_str("(?<")?;
162-
self.wtr.write_str(&x.name)?;
163-
self.wtr.write_str(">")?;
164-
Ok(())
165-
}
166-
CapturePName(ref x) => {
167-
self.wtr.write_str("(?P<")?;
168-
self.wtr.write_str(&x.name)?;
160+
CaptureName { ref name, starts_with_p } => {
161+
let start = if starts_with_p { "(?P<" } else { "(?<" };
162+
self.wtr.write_str(start)?;
163+
self.wtr.write_str(&name.name)?;
169164
self.wtr.write_str(">")?;
170165
Ok(())
171166
}

regex-syntax/src/hir/translate.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -771,11 +771,10 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
771771
ast::GroupKind::CaptureIndex(idx) => {
772772
hir::GroupKind::CaptureIndex(idx)
773773
}
774-
ast::GroupKind::CaptureName(ref capname)
775-
| ast::GroupKind::CapturePName(ref capname) => {
774+
ast::GroupKind::CaptureName { ref name, .. } => {
776775
hir::GroupKind::CaptureName {
777-
name: capname.name.clone(),
778-
index: capname.index,
776+
name: name.name.clone(),
777+
index: name.index,
779778
}
780779
}
781780
ast::GroupKind::NonCapturing(_) => hir::GroupKind::NonCapturing,

0 commit comments

Comments
 (0)