Skip to content

Support (?< syntax for named capture groups #956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions regex-syntax/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ impl Group {
/// Returns true if and only if this group is capturing.
pub fn is_capturing(&self) -> bool {
match self.kind {
GroupKind::CaptureIndex(_) | GroupKind::CaptureName(_) => true,
GroupKind::CaptureIndex(_) | GroupKind::CaptureName { .. } => true,
GroupKind::NonCapturing(_) => false,
}
}
Expand All @@ -1214,7 +1214,7 @@ impl Group {
pub fn capture_index(&self) -> Option<u32> {
match self.kind {
GroupKind::CaptureIndex(i) => Some(i),
GroupKind::CaptureName(ref x) => Some(x.index),
GroupKind::CaptureName { ref name, .. } => Some(name.index),
GroupKind::NonCapturing(_) => None,
}
}
Expand All @@ -1225,8 +1225,13 @@ impl Group {
pub enum GroupKind {
/// `(a)`
CaptureIndex(u32),
/// `(?P<name>a)`
CaptureName(CaptureName),
/// `(?<name>a)` or `(?P<name>a)`
CaptureName {
/// True if the `?P<` syntax is used and false if the `?<` syntax is used.
starts_with_p: bool,
/// The capture name.
name: CaptureName,
},
/// `(?:a)` and `(?i:a)`
NonCapturing(Flags),
}
Expand Down
103 changes: 70 additions & 33 deletions regex-syntax/src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,16 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
));
}
let inner_span = self.span();
if self.bump_if("?P<") {
let mut starts_with_p = true;
if self.bump_if("?P<") || {
starts_with_p = false;
self.bump_if("?<")
} {
let capture_index = self.next_capture_index(open_span)?;
let cap = self.parse_capture_name(capture_index)?;
let name = self.parse_capture_name(capture_index)?;
Ok(Either::Right(ast::Group {
span: open_span,
kind: ast::GroupKind::CaptureName(cap),
kind: ast::GroupKind::CaptureName { starts_with_p, name },
ast: Box::new(Ast::Empty(self.span())),
}))
} else if self.bump_if("?") {
Expand Down Expand Up @@ -2799,11 +2803,14 @@ bar
flag_set(pat, 0..4, ast::Flag::IgnoreWhitespace, false),
Ast::Group(ast::Group {
span: span_range(pat, 4..pat.len()),
kind: ast::GroupKind::CaptureName(ast::CaptureName {
span: span_range(pat, 9..12),
name: s("foo"),
index: 1,
}),
kind: ast::GroupKind::CaptureName {
starts_with_p: true,
name: ast::CaptureName {
span: span_range(pat, 9..12),
name: s("foo"),
index: 1,
}
},
ast: Box::new(lit_with('a', span_range(pat, 14..15))),
}),
]
Expand Down Expand Up @@ -3818,27 +3825,48 @@ bar

#[test]
fn parse_capture_name() {
assert_eq!(
parser("(?<a>z)").parse(),
Ok(Ast::Group(ast::Group {
span: span(0..7),
kind: ast::GroupKind::CaptureName {
starts_with_p: false,
name: ast::CaptureName {
span: span(3..4),
name: s("a"),
index: 1,
}
},
ast: Box::new(lit('z', 5)),
}))
);
assert_eq!(
parser("(?P<a>z)").parse(),
Ok(Ast::Group(ast::Group {
span: span(0..8),
kind: ast::GroupKind::CaptureName(ast::CaptureName {
span: span(4..5),
name: s("a"),
index: 1,
}),
kind: ast::GroupKind::CaptureName {
starts_with_p: true,
name: ast::CaptureName {
span: span(4..5),
name: s("a"),
index: 1,
}
},
ast: Box::new(lit('z', 6)),
}))
);
assert_eq!(
parser("(?P<abc>z)").parse(),
Ok(Ast::Group(ast::Group {
span: span(0..10),
kind: ast::GroupKind::CaptureName(ast::CaptureName {
span: span(4..7),
name: s("abc"),
index: 1,
}),
kind: ast::GroupKind::CaptureName {
starts_with_p: true,
name: ast::CaptureName {
span: span(4..7),
name: s("abc"),
index: 1,
}
},
ast: Box::new(lit('z', 8)),
}))
);
Expand All @@ -3847,11 +3875,14 @@ bar
parser("(?P<a_1>z)").parse(),
Ok(Ast::Group(ast::Group {
span: span(0..10),
kind: ast::GroupKind::CaptureName(ast::CaptureName {
span: span(4..7),
name: s("a_1"),
index: 1,
}),
kind: ast::GroupKind::CaptureName {
starts_with_p: true,
name: ast::CaptureName {
span: span(4..7),
name: s("a_1"),
index: 1,
}
},
ast: Box::new(lit('z', 8)),
}))
);
Expand All @@ -3860,11 +3891,14 @@ bar
parser("(?P<a.1>z)").parse(),
Ok(Ast::Group(ast::Group {
span: span(0..10),
kind: ast::GroupKind::CaptureName(ast::CaptureName {
span: span(4..7),
name: s("a.1"),
index: 1,
}),
kind: ast::GroupKind::CaptureName {
starts_with_p: true,
name: ast::CaptureName {
span: span(4..7),
name: s("a.1"),
index: 1,
}
},
ast: Box::new(lit('z', 8)),
}))
);
Expand All @@ -3873,11 +3907,14 @@ bar
parser("(?P<a[1]>z)").parse(),
Ok(Ast::Group(ast::Group {
span: span(0..11),
kind: ast::GroupKind::CaptureName(ast::CaptureName {
span: span(4..8),
name: s("a[1]"),
index: 1,
}),
kind: ast::GroupKind::CaptureName {
starts_with_p: true,
name: ast::CaptureName {
span: span(4..8),
name: s("a[1]"),
index: 1,
}
},
ast: Box::new(lit('z', 9)),
}))
);
Expand Down
8 changes: 5 additions & 3 deletions regex-syntax/src/ast/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ impl<W: fmt::Write> Writer<W> {
use crate::ast::GroupKind::*;
match ast.kind {
CaptureIndex(_) => self.wtr.write_str("("),
CaptureName(ref x) => {
self.wtr.write_str("(?P<")?;
self.wtr.write_str(&x.name)?;
CaptureName { ref name, starts_with_p } => {
let start = if starts_with_p { "(?P<" } else { "(?<" };
self.wtr.write_str(start)?;
self.wtr.write_str(&name.name)?;
self.wtr.write_str(">")?;
Ok(())
}
Expand Down Expand Up @@ -499,6 +500,7 @@ mod tests {
fn print_group() {
roundtrip("(?i:a)");
roundtrip("(?P<foo>a)");
roundtrip("(?<foo>a)");
roundtrip("(a)");
}

Expand Down
6 changes: 3 additions & 3 deletions regex-syntax/src/hir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,10 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
ast::GroupKind::CaptureIndex(idx) => {
hir::GroupKind::CaptureIndex(idx)
}
ast::GroupKind::CaptureName(ref capname) => {
ast::GroupKind::CaptureName { ref name, .. } => {
hir::GroupKind::CaptureName {
name: capname.name.clone(),
index: capname.index,
name: name.name.clone(),
index: name.index,
}
}
ast::GroupKind::NonCapturing(_) => hir::GroupKind::NonCapturing,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ regex matches `abc` at positions `0`, `1`, `2` and `3`.
<pre class="rust">
(exp) numbered capture group (indexed by opening parenthesis)
(?P&lt;name&gt;exp) named (also numbered) capture group (allowed chars: [_0-9a-zA-Z.\[\]])
(?&lt;name&gt;exp) named (also numbered) capture group (allowed chars: [_0-9a-zA-Z.\[\]])
(?:exp) non-capturing group
(?flags) set flags within current group
(?flags:exp) set flags for exp (non-capturing)
Expand Down