[K/N and WASM] Throw on duplicate group name

This commit is contained in:
Abduqodiri Qurbonzoda
2022-04-04 16:14:06 +03:00
committed by Space
parent 187faa121e
commit 9593069cb3
2 changed files with 14 additions and 0 deletions
@@ -130,6 +130,9 @@ internal class Pattern(val pattern: String, flags: Int = 0) {
if (ch == Lexer.CHAR_NAMED_GROUP) {
val name = (lexemes.curSpecialToken as NamedGroup).name
if (groupNameToIndex.containsKey(name)) {
throw PatternSyntaxException("Named capturing group <$name> is already defined", pattern, lexemes.curTokenIndex)
}
groupNameToIndex[name] = fSet.groupIndex
}
}
+11
View File
@@ -174,6 +174,17 @@ class RegexTest {
assertEquals("123", namedGroups["areaCode"]?.value)
}
@Test fun matchDuplicateGroupName() {
if (!supportsNamedCapturingGroup) return
assertFailsWith<IllegalArgumentException> {
"(?<hi>hi)|(?<hi>bye)".toRegex()
}
assertFailsWith<IllegalArgumentException> {
Regex("(?<first>\\d+)-(?<first>\\d+)")
}
}
@Test fun matchOptionalNamedGroup() {
if (!supportsNamedCapturingGroup) return