From 7960182674068851d2ab61ad504143db0bb3f15e Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 2 Dec 2020 16:13:48 +0300 Subject: [PATCH] [CMI] Fix CodeMetaInfoParser to properly handle nested meta infos There was a problem with cases like that: some text ^ ^ 1 2 (1) is a closing tag for and (2) is for , but before the fix they were matched contrariwise --- .../kotlin/codeMetaInfo/CodeMetaInfoParser.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoParser.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoParser.kt index 07f969bbfdd..343498384ec 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoParser.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoParser.kt @@ -8,7 +8,7 @@ package org.jetbrains.kotlin.codeMetaInfo import org.jetbrains.kotlin.codeMetaInfo.model.ParsedCodeMetaInfo object CodeMetaInfoParser { - private val openingRegex = """()""".toRegex() + private val openingRegex = """()""".toRegex() private val closingRegex = """()""".toRegex() /* @@ -22,7 +22,8 @@ object CodeMetaInfoParser { fun getCodeMetaInfoFromText(renderedText: String): List { var text = renderedText val openingMatchResults = ArrayDeque() - val closingMatchResults = ArrayDeque() + val stackOfOpeningMatchResults = ArrayDeque() + val closingMatchResults = mutableMapOf() val result = mutableListOf() while (true) { @@ -40,10 +41,11 @@ object CodeMetaInfoParser { text = if (openingStartOffset < closingStartOffset) { requireNotNull(opening) openingMatchResults.addLast(opening) + stackOfOpeningMatchResults.addLast(opening) text.removeRange(openingStartOffset, opening.range.last + 1) } else { requireNotNull(closing) - closingMatchResults.addLast(closing) + closingMatchResults[stackOfOpeningMatchResults.removeLast()] = closing text.removeRange(closingStartOffset, closing.range.last + 1) } } @@ -52,7 +54,7 @@ object CodeMetaInfoParser { } while (!openingMatchResults.isEmpty()) { val openingMatchResult = openingMatchResults.removeLast() - val closingMatchResult = closingMatchResults.removeLast() + val closingMatchResult = closingMatchResults.getValue(openingMatchResult) val allMetaInfos = openingMatchResult.groups[2]!!.value tagRegex.findAll(allMetaInfos).map { it.groups }.forEach { val tag = it[1]!!.value