@@ -129,34 +129,30 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
129129 }
130130
131131 var result = ParserStructure <T >(emptyList(), emptyList())
132- val flatParsers = mutableListOf<List <ParserOperation <T >>>()
132+ val accumulatedOperations = mutableListOf<List <ParserOperation <T >>>()
133133
134- for (parser in this .asReversed()) {
135- if (parser.followedBy.isEmpty()) {
136- flatParsers.add(parser.operations)
137- } else {
138- if (flatParsers.isNotEmpty()) {
139- val operations = buildList() {
140- for (i in flatParsers.lastIndex downTo 0 ) {
141- addAll(flatParsers[i])
142- }
134+ fun flushAccumulatedOperations () {
135+ if (accumulatedOperations.isNotEmpty()) {
136+ val operations = buildList {
137+ for (parserOperations in accumulatedOperations.asReversed()) {
138+ addAll(parserOperations)
143139 }
144- result = ParserStructure (operations, emptyList()).simplifyAndAppend(result)
145- flatParsers.clear()
146140 }
147- result = parser.simplifyAndAppend(result)
141+ result = ParserStructure (operations, emptyList()).simplifyAndAppend(result)
142+ accumulatedOperations.clear()
148143 }
149144 }
150145
151- if (flatParsers.isNotEmpty()) {
152- val operations = buildList {
153- for (i in flatParsers.lastIndex downTo 0 ) {
154- addAll(flatParsers[i])
155- }
146+ for (parser in this .asReversed()) {
147+ if (parser.followedBy.isEmpty()) {
148+ accumulatedOperations.add(parser.operations)
149+ } else {
150+ flushAccumulatedOperations()
151+ result = parser.simplifyAndAppend(result)
156152 }
157- result = ParserStructure (operations, emptyList()).simplifyAndAppend(result)
158153 }
159154
155+ flushAccumulatedOperations()
160156 return result
161157}
162158
0 commit comments