Skip to content

Commit 40b8dd3

Browse files
Refactor Parser to streamline handling of accumulated operations.
1 parent 07f8e77 commit 40b8dd3

File tree

1 file changed

+15
-19
lines changed
  • core/common/src/internal/format/parser

1 file changed

+15
-19
lines changed

core/common/src/internal/format/parser/Parser.kt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)