Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ build/
# Generated runtime artifacts
src/main/cod/src/bin/
src/main/cod/src/idx/
src/main/cod/demo/src/bin/
src/main/cod/demo/src/idx/

This file was deleted.

106 changes: 87 additions & 19 deletions src/main/cod/std/json/Json.cod
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
unit json

use {unicode}

// JSON standard library for Coderive.
// Supports parsing, mutation, and compact/pretty serialization of JSON values.
share JsonValue {
Expand Down Expand Up @@ -303,12 +301,82 @@ share Json {
if start + 5 >= raw.length { ~> (false) }
if raw[start] != "\\" { ~> (false) }
if raw[start + 1] != "u" { ~> (false) }
if !Unicode.isHexDigit(raw[start + 2]) { ~> (false) }
if !Unicode.isHexDigit(raw[start + 3]) { ~> (false) }
if !Unicode.isHexDigit(raw[start + 4]) { ~> (false) }
if !Unicode.isHexDigit(raw[start + 5]) { ~> (false) }
if !Json.isHexDigit(raw[start + 2]) { ~> (false) }
if !Json.isHexDigit(raw[start + 3]) { ~> (false) }
if !Json.isHexDigit(raw[start + 4]) { ~> (false) }
if !Json.isHexDigit(raw[start + 5]) { ~> (false) }
~> (true)
}

share isDigit(ch: text) :: bool {
~> (any[ch == "0", ch == "1", ch == "2", ch == "3", ch == "4", ch == "5", ch == "6", ch == "7", ch == "8", ch == "9"])
}

share isHexDigit(ch: text) :: bool {
~> (any[
Json.isDigit(ch),
any[ch == "a", ch == "b", ch == "c", ch == "d", ch == "e", ch == "f"],
any[ch == "A", ch == "B", ch == "C", ch == "D", ch == "E", ch == "F"]
])
}

share hexValue(ch: text) :: int {
if ch == "0" { ~> (0) }
if ch == "1" { ~> (1) }
if ch == "2" { ~> (2) }
if ch == "3" { ~> (3) }
if ch == "4" { ~> (4) }
if ch == "5" { ~> (5) }
if ch == "6" { ~> (6) }
if ch == "7" { ~> (7) }
if ch == "8" { ~> (8) }
if ch == "9" { ~> (9) }
if any[ch == "a", ch == "A"] { ~> (10) }
if any[ch == "b", ch == "B"] { ~> (11) }
if any[ch == "c", ch == "C"] { ~> (12) }
if any[ch == "d", ch == "D"] { ~> (13) }
if any[ch == "e", ch == "E"] { ~> (14) }
if any[ch == "f", ch == "F"] { ~> (15) }
~> (-1)
}

share hexDigit(value: int) :: text {
if value == 0 { ~> ("0") }
if value == 1 { ~> ("1") }
if value == 2 { ~> ("2") }
if value == 3 { ~> ("3") }
if value == 4 { ~> ("4") }
if value == 5 { ~> ("5") }
if value == 6 { ~> ("6") }
if value == 7 { ~> ("7") }
if value == 8 { ~> ("8") }
if value == 9 { ~> ("9") }
if value == 10 { ~> ("A") }
if value == 11 { ~> ("B") }
if value == 12 { ~> ("C") }
if value == 13 { ~> ("D") }
if value == 14 { ~> ("E") }
~> ("F")
}

share hex4(value: int) :: text {
x: int = value
if x < 0 { x = 0 }
if x > 65535 { x = x % 65536 }
d0 := (x / 4096) % 16
d1 := (x / 256) % 16
d2 := (x / 16) % 16
d3 := x % 16
~> (Json.hexDigit(d0) + Json.hexDigit(d1) + Json.hexDigit(d2) + Json.hexDigit(d3))
}

share isHighSurrogate(unit: int) :: bool {
~> (all[unit >= 55296, unit <= 56319])
}

share isLowSurrogate(unit: int) :: bool {
~> (all[unit >= 56320, unit <= 57343])
}

share indent(depth: int) :: text {
textValue := ""
Expand Down Expand Up @@ -359,7 +427,7 @@ share JsonParser {
if ch == "\"" { ~> (this.parseText()) }
if ch == "[" { ~> (this.parseArray()) }
if ch == "{" { ~> (this.parseObject()) }
if any[ch == "-", Unicode.isDigit(ch)] { ~> (this.parseNumber()) }
if any[ch == "-", Json.isDigit(ch)] { ~> (this.parseNumber()) }

~> (JsonValue.makeError("invalid json value at index " + this.index))
}
Expand Down Expand Up @@ -431,9 +499,9 @@ share JsonParser {
~> (JsonValue.makeError("invalid unicode escape at index " + this.index))
}
this.index = this.index + 4
firstText := "\\u" + Unicode.hex4(firstUnit)
firstText := "\\u" + Json.hex4(firstUnit)

if Unicode.isHighSurrogate(firstUnit) {
if Json.isHighSurrogate(firstUnit) {
if this.index + 5 >= this.source.length {
~> (JsonValue.makeError("missing low surrogate at index " + this.index))
}
Expand All @@ -442,15 +510,15 @@ share JsonParser {
}
this.index = this.index + 2
secondUnit := this.parseHex4At(this.index)
if any[secondUnit < 0, !Unicode.isLowSurrogate(secondUnit)] {
if any[secondUnit < 0, !Json.isLowSurrogate(secondUnit)] {
~> (JsonValue.makeError("invalid low surrogate at index " + this.index))
}
this.index = this.index + 4
outText = outText + firstText + "\\u" + Unicode.hex4(secondUnit)
outText = outText + firstText + "\\u" + Json.hex4(secondUnit)
continue
}

if Unicode.isLowSurrogate(firstUnit) {
if Json.isLowSurrogate(firstUnit) {
~> (JsonValue.makeError("unexpected low surrogate at index " + (this.index - 4)))
}

Expand Down Expand Up @@ -480,24 +548,24 @@ share JsonParser {
if this.source[this.index] == "0" {
this.index = this.index + 1
} else {
if !Unicode.isDigit(this.source[this.index]) {
if !Json.isDigit(this.source[this.index]) {
~> (JsonValue.makeError("invalid number at index " + start))
}
for i of this.index to this.source.length {
if this.index >= this.source.length { break }
if !Unicode.isDigit(this.source[this.index]) { break }
if !Json.isDigit(this.source[this.index]) { break }
this.index = this.index + 1
}
}

if all[this.index < this.source.length, this.source[this.index] == "."] {
this.index = this.index + 1
if any[this.index >= this.source.length, !Unicode.isDigit(this.source[this.index])] {
if any[this.index >= this.source.length, !Json.isDigit(this.source[this.index])] {
~> (JsonValue.makeError("invalid number fraction at index " + start))
}
for i of this.index to this.source.length {
if this.index >= this.source.length { break }
if !Unicode.isDigit(this.source[this.index]) { break }
if !Json.isDigit(this.source[this.index]) { break }
this.index = this.index + 1
}
}
Expand All @@ -507,12 +575,12 @@ share JsonParser {
if all[this.index < this.source.length, any[this.source[this.index] == "+", this.source[this.index] == "-"]] {
this.index = this.index + 1
}
if any[this.index >= this.source.length, !Unicode.isDigit(this.source[this.index])] {
if any[this.index >= this.source.length, !Json.isDigit(this.source[this.index])] {
~> (JsonValue.makeError("invalid number exponent at index " + start))
}
for i of this.index to this.source.length {
if this.index >= this.source.length { break }
if !Unicode.isDigit(this.source[this.index]) { break }
if !Json.isDigit(this.source[this.index]) { break }
this.index = this.index + 1
}
}
Expand Down Expand Up @@ -648,7 +716,7 @@ share JsonParser {
if start + 3 >= this.source.length { ~> (-1) }
value: int = 0
for j of 0 to 3 {
digit := Unicode.hexValue(this.source[start + j])
digit := Json.hexValue(this.source[start + j])
if digit < 0 { ~> (-1) }
value = value * 16 + digit
}
Expand Down
132 changes: 0 additions & 132 deletions src/main/cod/std/unicode/Unicode.cod

This file was deleted.

Loading