[CMI] Parse description of meta info and save it to ParsedCodeMetaInfo

This commit is contained in:
Dmitriy Novozhilov
2020-11-05 13:41:07 +03:00
parent 2bbab3170f
commit 87a6a66953
2 changed files with 22 additions and 11 deletions
@@ -8,11 +8,16 @@ package org.jetbrains.kotlin.codeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.model.ParsedCodeMetaInfo
object CodeMetaInfoParser {
private val openingRegex = "(<!([^>]+?)!>)".toRegex()
private val closingRegex = "(<!>)".toRegex()
private val openingRegex = """(<!([^>]+?)!>)""".toRegex()
private val closingRegex = """(<!>)""".toRegex()
private val descriptionRegex = "\\(\".*?\"\\)".toRegex()
private val platformRegex = "\\{(.+)}".toRegex()
/*
* ([\S&&[^,(){}]]+) -- tag, allowing all non-space characters except bracers and curly bracers
* ([{](.*?)[}])? -- list of platforms
* (\("(.*?)"\))? -- arguments of meta info
* (, )? -- possible separator between different infos
*/
private val tagRegex = """([\S&&[^,(){}]]+)([{](.*?)[}])?(\("(.*?)"\))?(, )?""".toRegex()
fun getCodeMetaInfoFromText(renderedText: String): List<ParsedCodeMetaInfo> {
var text = renderedText
@@ -48,14 +53,19 @@ object CodeMetaInfoParser {
while (!openingMatchResults.isEmpty()) {
val openingMatchResult = openingMatchResults.removeLast()
val closingMatchResult = closingMatchResults.removeLast()
val metaInfoWithoutParams = openingMatchResult.groups[2]!!.value.replace(descriptionRegex, "")
metaInfoWithoutParams.split(",").forEach {
val tag = platformRegex.replace(it, "").trim()
val platforms =
if (platformRegex.containsMatchIn(it)) platformRegex.find(it)!!.destructured.component1().split(";") else listOf()
val allMetaInfos = openingMatchResult.groups[2]!!.value
tagRegex.findAll(allMetaInfos).map { it.groups }.forEach {
val tag = it[1]!!.value
val platforms = it[3]?.value?.split(";") ?: emptyList()
val description = it[5]?.value
result.add(
ParsedCodeMetaInfo(
openingMatchResult.range.first, closingMatchResult.range.first, platforms.toMutableList(), tag,
openingMatchResult.range.first,
closingMatchResult.range.first,
platforms.toMutableList(),
tag,
description
)
)
}
@@ -11,7 +11,8 @@ class ParsedCodeMetaInfo(
override val start: Int,
override val end: Int,
override val platforms: MutableList<String>,
private val tag: String
private val tag: String,
val description: String?
) : CodeMetaInfo {
override val renderConfiguration = object : AbstractCodeMetaInfoRenderConfiguration(false) {}