in src/commonMain/kotlin/org/intellij/markdown/parser/TreeBuilder.kt [18:60]
fun buildTree(production: List<SequentialParser.Node>): ASTNode {
val events = constructEvents(production)
val markersStack = Stack<Pair<MyEvent, MutableList<MyASTNodeWrapper>>>()
assert(events.isNotEmpty()) { "nonsense" }
assert(events.first().info == events.last().info) {
"more than one root?\nfirst: ${events.first().info}\nlast: ${events.last().info}"
}
for (i in events.indices) {
cancellationToken.checkCancelled()
val event = events[i]
flushEverythingBeforeEvent(event, if (markersStack.isEmpty()) null else markersStack.peek().second)
if (event.isStart()) {
markersStack.push(Pair(event, ArrayList()))
} else {
val currentNodeChildren = if (event.isEmpty()) {
ArrayList()
} else {
val eventAndChildren = markersStack.pop()
assert(eventAndChildren.first.info == event.info) {
"Intersecting parsed nodes detected: ${eventAndChildren.first.info} vs ${event.info}"
}
eventAndChildren.second
}
val isTopmostNode = markersStack.isEmpty()
val newNode = createASTNodeOnClosingEvent(event, currentNodeChildren, isTopmostNode)
if (isTopmostNode) {
assert(i + 1 == events.size)
return newNode.astNode
} else {
markersStack.peek().second.add(newNode)
}
}
}
throw AssertionError("markers stack should close some time thus would not be here!")
}