A TOML v1.0.0 parser and serializer for MoonBit with full toml-test compliance.
Dependencies
moon add sa2360/toml// moon.mod: import { "sa2360/toml" }let doc = @toml.parse("title = \"Example\"\n[owner]\nname = \"Tom\"")
doc.get_string("title") // Some("Example")
doc.get_path("owner.name") // Some(Str("Tom")) — dotted path lookup
doc.get_int("port") // Option[Int64]
doc.get_double / get_bool / get_array / get_table / get_datetimelet text = @toml.encode(doc) // full document ([table] sections, [[array of tables]])
let inline = @toml.encode_value(v) // a single value in inline formlet json = value.to_json() // -> moonbitlang/core Json
let text = value.to_json_string() // compact JSON string
let back = @toml.Value::from_json(json) // JSON -> TOML (raises on null)pub(all) enum Value {
Str(String); Int(Int64); Float(Double); Bool(Bool)
Datetime(Datetime); Array(Array[Value]); Table(Table)
}moon build --target native cmd/toml2json
_build/native/debug/build/cmd/toml2json/toml2json.exe config.tomlmoon test # 316 tests: unit + embedded toml-test suite + doc examples
moon test --target native # same suite on the native backend
moon bench # parse / encode / to_json benchmarks (+ core JSON reference)
moon build --target native cmd/toml2json
python scripts/run_toml_test.py # official protocol runner over the CLI (281 cases)value.mbt Value / Table / Datetime model, accessors, equality
error.mbt Position, ParseErrorData, ParseError
scanner.mbt cursor, whitespace/comment/newline handling, char classes
strings.mbt the four string kinds and escape validation
numbers.mbt integers, floats, datetimes (with range checks)
parser.mbt document/table/keyval/array/inline-table + definition rules
encode.mbt serializer (documents and inline values)
json.mbt JSON interop (to_json, ToJson impl)
cmd/toml2json/ native CLI emitting toml-test tagged JSON
cmd/example/ runnable demo: parse, read, re-encode, convert to JSON
scripts/ conformance generator + CLI runner
toml-test-tests/ vendored official suite (v1.0.0)let doc = @toml.parse("title = \"Example\"\n[owner]\nname = \"Tom\"")
doc.get_string("title") // Some("Example")
doc.get_path("owner.name") // Some(Str("Tom"))
let text = @toml.encode(doc) // 序列化回 TOML 文档
let json = @toml.Value::Table(doc).to_json_string() // 转 JSON(整数精度无损)let doc = @toml.parse(input) catch {
err => println(err.message()) // 例如:line 3, column 5: duplicate key 'apple'
}moon test # 316 个测试:单元测试 + 内嵌 toml-test 全量套件 + 文档示例
moon test --target native # 同一套件在 native 后端
moon bench # parse / encode / to_json 基准(含 core JSON 参照)
moon build --target native cmd/toml2json
python scripts/run_toml_test.py # 官方协议 runner(281 个用例,含字节级 UTF-8 用例)impl Show for ParseErrorpub(all) enum ParseErrorData {
UnexpectedChar(Position, Char)
UnexpectedEof(Position)
UnterminatedString(Position)
InvalidEscape(Position, Char)
InvalidUnicodeEscape(Position, String)
InvalidNumber(Position, String)
InvalidDatetime(Position, String)
InvalidValue(Position, Char)
DuplicateKey(Position, String)
TableConflict(Position, String)
InvalidSyntax(Position, String)
} derive(Eq, Debug)pub struct Table {
// private fields
}test {
let v = @toml.Value::from_json(@json.parse("{\"a\": [1, true]}")) catch {
_ => abort("unreachable")
}
@test.assert_eq(@toml.encode_value(v), "{a = [1, true]}")
}test {
let doc = @toml.parse("port = 8080\nactive = true")
@test.assert_eq(
@toml.Value::Table(doc).to_json_string(),
"{\"port\":8080,\"active\":true}",
)
}let MAX_NESTING_DEPTH : Inttest {
let doc = @toml.parse("[server]\nhost = \"localhost\"\nport = 80")
@test.assert_eq(
@toml.encode(doc),
"[server]\nhost = \"localhost\"\nport = 80\n",
)
}test {
let doc = @toml.parse(
"title = \"demo\"\n[server]\nhost = \"127.0.0.1\"\nport = 8080",
)
@test.assert_eq(doc.get_string("title"), Some("demo"))
@test.assert_eq(doc.get_path("server.port"), Some(@toml.Value::Int(8080L)))
}test {
@test.assert_eq(@toml.parse_value("1_000").as_int(), Some(1000L))
@test.assert_eq(@toml.parse_value("1979-05-27").type_name(), "date-local")
}Install
Download zipA TOML v1.0.0 parser and serializer for MoonBit with full toml-test compliance.
Dependencies