From 3c7264f3d101bdc710b5e8b4966ff3a06331f3f1 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Fri, 5 Jan 2024 21:15:46 +0800 Subject: [PATCH] feat(toml): add parser for analysis container --- .../src/main/antlr/TomlParser.g4 | 8 ++--- .../kotlin/chapi/parser/toml/TomlListener.kt | 13 +++++-- .../chapi/parser/toml/TomlAnalyserTest.kt | 34 ++++++++++++++++--- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/chapi-parser-toml/src/main/antlr/TomlParser.g4 b/chapi-parser-toml/src/main/antlr/TomlParser.g4 index bedc8168..c3c5e51f 100644 --- a/chapi-parser-toml/src/main/antlr/TomlParser.g4 +++ b/chapi-parser-toml/src/main/antlr/TomlParser.g4 @@ -130,15 +130,11 @@ standard_table ; inline_table - : L_BRACE inline_table_keyvals R_BRACE + : L_BRACE inline_table_keyvals (COMMA inline_table_keyvals)* R_BRACE ; inline_table_keyvals - : inline_table_keyvals_non_empty? - ; - -inline_table_keyvals_non_empty - : key EQUALS value (COMMA inline_table_keyvals_non_empty)? + : key EQUALS value ; array_table diff --git a/chapi-parser-toml/src/main/kotlin/chapi/parser/toml/TomlListener.kt b/chapi-parser-toml/src/main/kotlin/chapi/parser/toml/TomlListener.kt index 8ded0028..1d6a8c03 100644 --- a/chapi-parser-toml/src/main/kotlin/chapi/parser/toml/TomlListener.kt +++ b/chapi-parser-toml/src/main/kotlin/chapi/parser/toml/TomlListener.kt @@ -37,10 +37,8 @@ class TomlListener(val filePath: String) : TomlParserBaseListener() { private fun buildField(ctx: TomlParser.Key_valueContext?): CodeField { val key = ctx?.key()?.text ?: "" val valueContext = ctx?.value() - val value = valueContext?.text ?: "" val field = codeField(key, valueContext) - return field } @@ -67,6 +65,10 @@ class TomlListener(val filePath: String) : TomlParserBaseListener() { field.ArrayValue = buildArrayValue(valueContext) } + if (type == TomlType.InlineTable) { + field.ArrayValue = buildInlineTableValue(valueContext) + } + return field } @@ -76,6 +78,13 @@ class TomlListener(val filePath: String) : TomlParserBaseListener() { } ?: listOf() } + + private fun buildInlineTableValue(valueContext: TomlParser.ValueContext?): List { + return valueContext?.inline_table()?.inline_table_keyvals()?.map { + codeField(it.key().text, it.value()) + } ?: listOf() + } + override fun enterTable(ctx: TomlParser.TableContext?) { val tableName = ctx?.standard_table()?.key()?.text ?: "" currentTableName = tableName diff --git a/chapi-parser-toml/src/test/kotlin/chapi/parser/toml/TomlAnalyserTest.kt b/chapi-parser-toml/src/test/kotlin/chapi/parser/toml/TomlAnalyserTest.kt index 2f62fff9..3849d58f 100644 --- a/chapi-parser-toml/src/test/kotlin/chapi/parser/toml/TomlAnalyserTest.kt +++ b/chapi-parser-toml/src/test/kotlin/chapi/parser/toml/TomlAnalyserTest.kt @@ -91,10 +91,36 @@ class TomlAnalyserTest { val childFloat = secondArray.ArrayValue[1].ArrayValue[0] assertEquals("3.14", childFloat.TypeValue) assertEquals("Float", childFloat.TypeType) + } + + @Test + fun `analysis should return CodeContainer with correct field count 4`() { + // given + val tomlCode = """ + [database] + enabled = true + ports = [ 8000, 8001, 8002 ] + data = [ ["delta", "phi"], [3.14] ] + temp_targets = { cpu = 79.5, case = 72.0 } + """.trimIndent() + + val analyser = TomlAnalyser() + + val container = analyser.analysis(tomlCode, "path/to/file.toml") -// assertEquals("Array", firstChild.Fields[2].TypeType) -// assertEquals("{cpu=79.5,case=72.0}", firstChild.Fields[3].TypeValue) -// assertEquals("temp_targets", firstChild.Fields[3].TypeKey) -// assertEquals("InlineTable", firstChild.Fields[3].TypeType) + assertEquals(0, container.Fields.size) + val childContainer = container.Containers + assertEquals(1, childContainer.size) + + val firstChild = childContainer[0] + val thirdField = firstChild.Fields[3] + assertEquals("{cpu=79.5,case=72.0}", thirdField.TypeValue) + assertEquals("temp_targets", thirdField.TypeKey) + assertEquals("InlineTable", thirdField.TypeType) + + val childField = thirdField.ArrayValue[0] + assertEquals("cpu", childField.TypeKey) + assertEquals("79.5", childField.TypeValue) + assertEquals("Float", childField.TypeType) } }