Skip to content

Commit

Permalink
Fix: Fix parser generation for nested repetition rules
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Dec 16, 2024
1 parent 7559dc5 commit fdb3c81
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/AbnfParsers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ private class AbnfParserListener : AbnfParser.Listener {
private var pendingIsIncrementalRule: Boolean? = null
private var pendingRuleElement: Element? = null

private var pendingRepetitionCount: IntRange? = null

private var pendingRepetitionCountStack: MutableList<IntRange?> = mutableListOf()
private val pendingElementsStack: MutableList<MutableList<Element>> = mutableListOf()

override fun exitRule(input: String, startIndex: Int, endIndex: Int) {
Expand Down Expand Up @@ -133,12 +132,13 @@ private class AbnfParserListener : AbnfParser.Listener {
}

override fun enterRepetition(input: String, startIndex: Int) {
pendingRepetitionCountStack += null
pendingElementsStack += mutableListOf<Element>()
}

override fun exitRepetition(input: String, startIndex: Int, endIndex: Int) {
val elements = pendingElementsStack.removeLast()
val count = pendingRepetitionCount.also { pendingRepetitionCount = null }
val count = pendingRepetitionCountStack.removeLast()
if (endIndex == -1) {
return
}
Expand All @@ -153,7 +153,7 @@ private class AbnfParserListener : AbnfParser.Listener {
}

val repeat = input.substring(startIndex, endIndex)
pendingRepetitionCount =
pendingRepetitionCountStack[pendingRepetitionCountStack.lastIndex] =
if ('*' in repeat) {
val counts =
repeat.split('*', limit = 2).map { if (it.isEmpty()) null else it.toInt() }
Expand Down

0 comments on commit fdb3c81

Please sign in to comment.