From 469d5fcb8c40f437567489391b86884175eb9efd Mon Sep 17 00:00:00 2001 From: "alexander.nutz" Date: Wed, 3 Apr 2024 07:47:48 +0200 Subject: [PATCH] fix json; v0.13 --- README.md | 2 +- build.gradle.kts | 2 +- src/main/kotlin/blitz/parse/JSON.kt | 11 +++++-- src/main/kotlin/blitz/parse/comb/Special.kt | 36 +++++++++++++++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/blitz/parse/comb/Special.kt diff --git a/README.md b/README.md index 711b184..eb5617e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ repositories { } dependencies { - implementation("me.alex_s168:blitz:0.12") + implementation("me.alex_s168:blitz:0.13") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 4b2cdce..6c25c2c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "me.alex_s168" -version = "0.12" +version = "0.13" repositories { mavenCentral() diff --git a/src/main/kotlin/blitz/parse/JSON.kt b/src/main/kotlin/blitz/parse/JSON.kt index 169caef..e486d79 100644 --- a/src/main/kotlin/blitz/parse/JSON.kt +++ b/src/main/kotlin/blitz/parse/JSON.kt @@ -12,8 +12,8 @@ object JSON { } val jsonString = parser { - it.require("\"") - ?.untilRequire("\"") { str -> Str(str) } + it.stringWithEscape() + ?.mapSecond { Str(it) } } val jsonArray = parser { @@ -30,6 +30,8 @@ object JSON { val jsonBool = parser { it.require("true")?.to(Bool(true)) } or parser { it.require("false")?.to(Bool(false)) } + val jsonNull = parser { it.require("null")?.to(Nul()) } + val jsonObj = parser { it.require("{") ?.array(",") { elem -> @@ -47,7 +49,7 @@ object JSON { } init { - jsonElement = (jsonArray or jsonNum or jsonString or jsonObj or jsonBool).trim() + jsonElement = (jsonArray or jsonNum or jsonString or jsonObj or jsonBool or jsonNull).trim() } interface Element { @@ -62,6 +64,7 @@ object JSON { fun isStr() = this is Str fun isObj() = this is Obj fun isBool() = this is Bool + fun isNul() = this is Nul } data class Array(val value: List): Element { @@ -90,6 +93,8 @@ object JSON { value.toString() } + class Nul: Element + fun parse(string: String): Element? = jsonElement(Parsable(string))?.second } \ No newline at end of file diff --git a/src/main/kotlin/blitz/parse/comb/Special.kt b/src/main/kotlin/blitz/parse/comb/Special.kt new file mode 100644 index 0000000..2cc067a --- /dev/null +++ b/src/main/kotlin/blitz/parse/comb/Special.kt @@ -0,0 +1,36 @@ +package blitz.parse.comb + +fun Parsable.stringWithEscape(): Pair? { + var escaped = false + var index = 0 + val out = StringBuilder() + for (c in str) { + if (index == 0) { + if (c != '"') + return null + } else { + if (escaped) { + escaped = false + when (c) { + '"' -> out.append('"') + '\\' -> out.append('\\') + 'n' -> out.append('\n') + 'r' -> out.append('\r') + 'b' -> out.append('\b') + 't' -> out.append('\t') + else -> return null + } + } else if (c == '"') + break + else if (c == '\\') + escaped = true + else { + out.append(c) + } + } + index ++ + } + if (escaped) + return null + return Parsable(str.substring(index + 1), loc?.plus(index + 1)) to out.toString() +} \ No newline at end of file