Skip to content

Commit

Permalink
feat(toml): add parser for analysis container
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 5, 2024
1 parent 2eb203a commit 3c7264f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
8 changes: 2 additions & 6 deletions chapi-parser-toml/src/main/antlr/TomlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -67,6 +65,10 @@ class TomlListener(val filePath: String) : TomlParserBaseListener() {
field.ArrayValue = buildArrayValue(valueContext)
}

if (type == TomlType.InlineTable) {
field.ArrayValue = buildInlineTableValue(valueContext)
}

return field
}

Expand All @@ -76,6 +78,13 @@ class TomlListener(val filePath: String) : TomlParserBaseListener() {
} ?: listOf()
}


private fun buildInlineTableValue(valueContext: TomlParser.ValueContext?): List<CodeField> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 3c7264f

Please sign in to comment.