Repository: orchestr7/ktoml (kotlin)

Issue:
Parse error with multi-line array of arrays with trailing comma
The following results in an exception when parsing. However, `a = [[1], [2], ]` parses just fine.
```toml
a = [
    [1],
    [2],
]
```

```
Exception in thread "main" com.akuleshov7.ktoml.exceptions.ParseException: Line 1: Expected (]) in the end of ARRAY
	at com.akuleshov7.ktoml.parsers.TomlMultilineString.parseMultiline(TomlMultilineString.kt:76)
	at com.akuleshov7.ktoml.parsers.TomlMultilineString.<init>(TomlMultilineString.kt:35)
	at com.akuleshov7.ktoml.parsers.TomlParser.parseStringsToTomlTree-impl(TomlParser.kt:74)
	at com.akuleshov7.ktoml.parsers.TomlParser.parseLines-impl(TomlParser.kt:36)
```

Worth noting that the following parses fine, so the issue seems to only be with multi-line arrays of arrays with a trailing comma from what I can tell.
```toml
a = [
    1,
    2,
]
```

Pull request:
Fixes #324

Patch:
diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlMultilineString.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlMultilineString.kt
index fd94611c..2b20af79 100644
--- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlMultilineString.kt
+++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/parsers/TomlMultilineString.kt
@@ -119,9 +119,15 @@ internal class TomlMultilineString(
         }
 
         return if (isNested == true) {
-            lines.joinToString("")
-                .trim()
-                .endsWith(multilineType.closingSymbols + multilineType.closingSymbols)
+            val clearedString = lines.joinToString("")
+                .filter { !it.isWhitespace() }
+
+            if (multilineType == MultilineType.ARRAY) {
+                clearedString.endsWith(multilineType.closingSymbols + multilineType.closingSymbols) ||
+                        clearedString.endsWith(multilineType.closingSymbols + "," + multilineType.closingSymbols)
+            } else {
+                clearedString.endsWith(multilineType.closingSymbols + multilineType.closingSymbols)
+            }
         } else {
             lines.last()
                 .trim()


Test patch:
diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/multiline/MultilineArrayTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/multiline/MultilineArrayTest.kt
index bbca5f4e..cc95aed0 100644
--- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/multiline/MultilineArrayTest.kt
+++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/multiline/MultilineArrayTest.kt
@@ -326,4 +326,30 @@ class MultilineArrayTest {
 
         assertEquals(expected, Toml.decodeFromString(test))
     }
-}
\ No newline at end of file
+
+    @Test
+    fun trailingCommaInMultilineNestedArray() {
+        var toml = """
+            a = [
+                [1, 2],
+                [3, 4],
+            ]
+        """.trimIndent()
+        val expected = NestedArray(
+                listOf(
+                    listOf(1, 2),
+                    listOf(3, 4)
+                )
+            )
+
+        assertEquals(expected, Toml.decodeFromString(toml))
+
+        toml = """
+            a = [
+                [1, 2],
+                [3, 4] ,
+            ]
+        """.trimIndent()
+        assertEquals(expected, Toml.decodeFromString(toml))
+    }
+}