@@ -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>
0 commit comments