[K/N and WASM] Throw on duplicate group name
This commit is contained in:
committed by
Space
parent
187faa121e
commit
9593069cb3
@@ -130,6 +130,9 @@ internal class Pattern(val pattern: String, flags: Int = 0) {
|
|||||||
|
|
||||||
if (ch == Lexer.CHAR_NAMED_GROUP) {
|
if (ch == Lexer.CHAR_NAMED_GROUP) {
|
||||||
val name = (lexemes.curSpecialToken as NamedGroup).name
|
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
|
groupNameToIndex[name] = fSet.groupIndex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,6 +174,17 @@ class RegexTest {
|
|||||||
assertEquals("123", namedGroups["areaCode"]?.value)
|
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() {
|
@Test fun matchOptionalNamedGroup() {
|
||||||
if (!supportsNamedCapturingGroup) return
|
if (!supportsNamedCapturingGroup) return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user