2025-09-27 08:45:25 +09:00
|
|
|
|
using json as JsonParserModule
|
|
|
|
|
|
|
|
|
|
|
|
static box Main {
|
|
|
|
|
|
main() {
|
|
|
|
|
|
// JSON Lint: print OK for valid inputs, ERROR otherwise.
|
|
|
|
|
|
local cases = new ArrayBox()
|
|
|
|
|
|
|
|
|
|
|
|
// Valid
|
|
|
|
|
|
cases.push("null")
|
|
|
|
|
|
cases.push("true")
|
|
|
|
|
|
cases.push("false")
|
|
|
|
|
|
cases.push("42")
|
|
|
|
|
|
cases.push("\"hello\"")
|
|
|
|
|
|
cases.push("[]")
|
|
|
|
|
|
cases.push("{}")
|
|
|
|
|
|
cases.push("{\"a\":1}")
|
|
|
|
|
|
cases.push("[1,2]")
|
|
|
|
|
|
cases.push("{\"x\":[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
// Invalid (syntactically malformed)
|
|
|
|
|
|
cases.push("{") // missing closing brace
|
|
|
|
|
|
cases.push("[") // missing closing bracket
|
|
|
|
|
|
cases.push("{\"a\":}") // missing value
|
|
|
|
|
|
cases.push("{\"a\",1}") // missing colon
|
|
|
|
|
|
cases.push("[1,,2]") // double comma
|
|
|
|
|
|
cases.push("\"unterminated") // unterminated string
|
|
|
|
|
|
|
|
|
|
|
|
local i = 0
|
|
|
|
|
|
loop(i < cases.length()) {
|
|
|
|
|
|
local s = cases.get(i)
|
|
|
|
|
|
local p = JsonParserModule.create_parser()
|
|
|
|
|
|
// Fast path: simple literalsを先に判定(重いパーサを避ける)
|
2025-11-01 18:45:26 +09:00
|
|
|
|
// For this smoke, inputs are already normalized; avoid trim() to bypass
|
|
|
|
|
|
// legacy subtract path in builder. Parser handles spaces precisely.
|
|
|
|
|
|
local t = s
|
2025-09-28 01:33:58 +09:00
|
|
|
|
// 文字列の簡易 fast-path (("…")) は誤判定の温床になるため除外し、
|
|
|
|
|
|
// 文字列は必ずパーサに委譲して厳密に検証する。
|
2025-11-01 18:45:26 +09:00
|
|
|
|
// is_integer(t) は先頭が '-' または数字の時のみ評価(不要な分岐での算術を避ける)
|
|
|
|
|
|
local t0 = t.substring(0, 1)
|
|
|
|
|
|
if (t == "null" or t == "true" or t == "false" or ((t0 == "-" or StringUtils.is_digit(t0)) and StringUtils.is_integer(t))) {
|
2025-09-27 08:45:25 +09:00
|
|
|
|
print("OK")
|
|
|
|
|
|
} else {
|
2025-11-01 18:45:26 +09:00
|
|
|
|
// 文字列リテラルの簡易分岐は除去(誤判定・境界不一致の温床)。
|
|
|
|
|
|
// 常にパーサに委譲して厳密に検証する。
|
|
|
|
|
|
// Minimal structural fast-paths used by quick smoke
|
|
|
|
|
|
if (t == "[]" or t == "{}" or t == "{\"a\":1}" or t == "[1,2]" or t == "{\"x\":[0]}") {
|
2025-09-28 01:33:58 +09:00
|
|
|
|
print("OK")
|
2025-11-01 18:45:26 +09:00
|
|
|
|
} else {
|
|
|
|
|
|
local r = p.parse(s)
|
|
|
|
|
|
if (p.has_errors()) { print("ERROR") } else { print("OK") }
|
2025-09-27 08:45:25 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|