Skip to content

Commit ae3aed5

Browse files
Improve comments in Parser for better clarity on reverse order processing and batching operations
1 parent dbffa0a commit ae3aed5

File tree

1 file changed

+5
-0
lines changed
  • core/common/src/internal/format/parser

1 file changed

+5
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
178178
}
179179
}
180180

181+
// Combine parsers in reverse order, batching operations from parsers without followedBy.
181182
var result = ParserStructure<T>(emptyList(), emptyList())
182183
val accumulatedOperations = mutableListOf<List<ParserOperation<T>>>()
183184

184185
fun flushAccumulatedOperations() {
185186
if (accumulatedOperations.isNotEmpty()) {
187+
// Reverse to restore the original order (since parsers are processed in reverse).
186188
val operations = buildList {
187189
for (parserOperations in accumulatedOperations.asReversed()) {
188190
addAll(parserOperations)
@@ -195,13 +197,16 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
195197

196198
for (parser in this.asReversed()) {
197199
if (parser.followedBy.isEmpty()) {
200+
// No followedBy: accumulate for batch processing.
198201
accumulatedOperations.add(parser.operations)
199202
} else {
203+
// Has followedBy: flush accumulated operations, then process individually.
200204
flushAccumulatedOperations()
201205
result = parser.simplifyAndAppend(result)
202206
}
203207
}
204208

209+
// Flush remaining accumulated operations.
205210
flushAccumulatedOperations()
206211
return result
207212
}

0 commit comments

Comments
 (0)