fix: 同名捕获组回引与 groups 字段改末次已定义语义

This commit is contained in:
2026-10-14 08:05:13 +00:00
parent 3d3bdaa5cf
commit fe430d4bcb
12 changed files with 287 additions and 42 deletions
+3 -9
View File
@@ -1689,15 +1689,9 @@ where
ir::Node::BackRef(group_indices[0] + 1)
}
_ => {
// Unusual case of multiple groups sharing a name: the backref should try each in turn.
// Lower to alternations of backreferences. Reverse to keep it right-associative: a | (b | (c | d))...
let backrefs = group_indices
.iter()
.rev()
.map(|group_index| ir::Node::BackRef(*group_index + 1));
backrefs
.reduce(|right, left| ir::Node::Alt(Box::new(left), Box::new(right)))
.unwrap()
// Multiple groups sharing a name: the backref reads the
// last occurrence that is defined at match time.
ir::Node::BackRefLastDefined(group_indices.iter().map(|i| i + 1).collect())
}
};
Ok(node)