Skip to content

Commit a843106

Browse files
committed
Corrected nested group ordering for regex back references.
1 parent cd7c2c0 commit a843106

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

core/src/main/kotlin/org/evomaster/core/parser/GeneRegexJavaVisitor.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
4040
* Populated as the tree is walked. A backreference is only valid if it
4141
* appears after the group it references, which Java regex requires anyway.
4242
*/
43-
private val captureGroups = mutableListOf<DisjunctionListRxGene>()
43+
private val captureGroups = mutableListOf<DisjunctionListRxGene?>()
4444

4545
/**
4646
* Same as [captureGroups] but for named backreferences, which can be accessed
@@ -312,6 +312,10 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
312312

313313
if(ctx.disjunction() != null){
314314

315+
// to correctly handle group nesting order, we must record group index before visiting
316+
val groupIndex = captureGroups.size
317+
captureGroups.add(null) // add placeholder for the gene
318+
315319
val res = ctx.disjunction().accept(this)
316320

317321
val disjList = DisjunctionListRxGene(res.genes.map { it as DisjunctionRxGene })
@@ -328,7 +332,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
328332
val isNamedCaptureGroup = ctx.NAMED_CAPTURE_GROUP_OPEN() != null
329333

330334
if (isCapturingGroup) {
331-
captureGroups.add(disjList)
335+
captureGroups[groupIndex] = disjList
332336
}
333337
if (isNamedCaptureGroup) {
334338
val name = ctx.NAMED_CAPTURE_GROUP_OPEN().text.drop(3).dropLast(1) // strip "(?<" and ")"
@@ -524,7 +528,7 @@ class GeneRegexJavaVisitor : RegexJavaBaseVisitor<VisitResult>(){
524528
"capture group(s) have been defined so far"
525529
)
526530
}
527-
return VisitResult(BackReferenceRxGene(n, captureGroups[n - 1]))
531+
return VisitResult(BackReferenceRxGene(n, captureGroups[n - 1]!!))
528532
}
529533

530534
// named backreference \k<name>

core/src/test/kotlin/org/evomaster/core/parser/GeneRegexJavaVisitorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class GeneRegexJavaVisitorTest : GeneRegexEcma262VisitorTest() {
243243
checkSameAsJava("""(?<randomName>a|b|c)\1\k<randomName>""")
244244
checkSameAsJava("""<>[(?<notAName>abc)]""")
245245
checkCanSample("""[(?<notAName>abc)]""", "N", 100)
246+
checkSameAsJava("""((A)(B(C)))\1\2\3\4""")
246247
}
247248

248249
@Test

0 commit comments

Comments
 (0)