A pure MoonBit TOML parser supporting strings, numbers, booleans, arrays, tables, and comments
moon add tonyfettes/toml_parser///|
test "basic usage" {
let toml_text = "name = \"Alice\"\nage = 30\nenabled = true"
let value = @toml_parser.parse(toml_text) catch {
err => {
println(err.to_string())
fail("parse failed")
}
}
// Access values from the parsed TOML
let name = value.get("name").unwrap().as_string().unwrap()
let age = value.get("age").unwrap().as_int().unwrap()
let enabled = value.get("enabled").unwrap().as_bool().unwrap()
inspect(name, content="Alice")
inspect(age, content="30")
inspect(enabled, content="true")
}///|
test "table parsing" {
let toml_text =
#|[server]
#|host = "localhost"
#|port = 8080
#|
let result = @toml_parser.parse(toml_text) catch { _ => fail("parse failed") }
let server = result.get("server").unwrap().as_table().unwrap()
inspect(server.get("host").unwrap().as_string().unwrap(), content="localhost")
inspect(server.get("port").unwrap().as_int().unwrap(), content="8080")
}///|
test "array parsing" {
let toml_text = "numbers = [1, 2, 3, 4, 5]"
let result = @toml_parser.parse(toml_text) catch { _ => fail("parse failed") }
let numbers = result.get("numbers").unwrap().as_array().unwrap()
inspect(numbers.length(), content="5")
inspect(numbers[0].as_int().unwrap(), content="1")
inspect(numbers[4].as_int().unwrap(), content="5")
}///|
test "inline table" {
let toml_text = "person = { name = \"Bob\", age = 25 }"
let result = @toml_parser.parse(toml_text) catch { _ => fail("parse failed") }
let person = result.get("person").unwrap().as_table().unwrap()
inspect(person.get("name").unwrap().as_string().unwrap(), content="Bob")
inspect(person.get("age").unwrap().as_int().unwrap(), content="25")
}///|
test "complex document" {
let config =
#|# Application configuration
#|title = "My App"
#|
#|[database]
#|server = "192.168.1.1"
#|ports = [8001, 8002, 8003]
#|enabled = true
#|
#|[owner]
#|name = "Tom"
#|email = "tom@example.com"
#|
let result = @toml_parser.parse(config) catch { _ => fail("parse failed") }
inspect(result.get("title").unwrap().as_string().unwrap(), content="My App")
let db = result.get("database").unwrap().as_table().unwrap()
inspect(db.get("server").unwrap().as_string().unwrap(), content="192.168.1.1")
inspect(db.get("enabled").unwrap().as_bool().unwrap(), content="true")
let ports = db.get("ports").unwrap().as_array().unwrap()
inspect(ports.length(), content="3")
}///|
pub enum TomlValue {
String(String)
Integer(Int64)
Float(Double)
Boolean(Bool)
DateTime(String)
Array(Array[TomlValue])
Table(Map[String, TomlValue])
}///|
pub suberror ParseError {
UnexpectedChar(Int, Char) // position, character
UnexpectedEof
InvalidEscape(Int, Char)
InvalidNumber(Int, String)
InvalidDateTime(Int, String)
DuplicateKey(String)
InvalidKey(Int, String)
}///|
test "parse function" {
let result = @toml_parser.parse("key = \"value\"") catch {
_ => fail("parse failed")
}
inspect(result.get("key").unwrap().as_string().unwrap(), content="value")
}cd toml_parser
moon run cmd/mainmoon testpub(all) suberror ParseError {
UnexpectedChar(Int, Char)
UnexpectedEof
InvalidEscape(Int, Char)
InvalidNumber(Int, String)
InvalidDateTime(Int, String)
DuplicateKey(String)
InvalidKey(Int, String)
}impl Eq for ParseErrorimpl Show for ParseErrorA pure MoonBit TOML parser supporting strings, numbers, booleans, arrays, tables, and comments