A nom-like parser combinator framework for MoonBit
src/
nom/ — the parkit parser combinator library (cogna-dev/parkit/nom)
examples/
json/ — JSON parser sample built with parkit (not published to mooncakes)
benchmark/ — MoonBit benchmarks (not published to mooncakes)
reference/
nom-json/ — Rust nom reference JSON parser (cross-language validation + benchmarks)// Tag: match an exact string
let p = @nom.tag("hello")
assert_eq!(p.parse("hello world"), Ok((" world", "hello")))
// many0: repeat zero or more times
let nums = @nom.many0(@nom.digit1())
assert_eq!(nums.parse("123 456"), Ok((" 456", ["123"])))
// JSON parsing
let json = @json.parse("{\"key\": 42}")
// => Ok(Object([("key", Number(42.0))]))moon test --manifest-path moon.work.json# MoonBit benchmarks (native target)
moon bench --manifest-path moon.work.json --target native
# Rust criterion benchmarks
cargo bench --manifest-path reference/nom-json/Cargo.toml| Benchmark | MoonBit (native) | Rust nom (native) |
|---|---|---|
| json_parse (3-object array) | 1.17 ms | 5.53 µs |
Note: The performance difference reflects the overhead of MoonBit's runtime and memory model compared to Rust's zero-cost abstractions, not algorithmic differences — both parsers implement the same combinator logic and produce identical results.
| Benchmark | Mean | σ | Range |
|---|---|---|---|
| json_parse (3-object array) | 1.17 ms | ±6.29 µs | 1.16 ms … 1.19 ms |
| tag | 0.21 µs | ±0.00 µs | 0.21 µs … 0.21 µs |
| take_while1_digits | 0.43 µs | ±0.00 µs | 0.43 µs … 0.43 µs |
| separated_list0 | 2.49 µs | ±0.02 µs | 2.47 µs … 2.54 µs |
cargo test --manifest-path reference/nom-json/Cargo.toml| Combinator | Description |
|---|---|
| tag(s) | Match exact literal string |
| satisfy(pred) | Match single char satisfying predicate |
| char_(c) | Match exact character |
| take_while(pred) | Take chars while predicate holds (0+) |
| take_while1(pred) | Take chars while predicate holds (1+) |
| take(n) | Take exactly n characters |
| take_while_m_n(m,n,pred) | Take m..n chars while predicate holds |
| many0(p) | Run parser 0 or more times |
| many1(p) | Run parser 1 or more times |
| alt(parsers) | Try each parser; return first success |
| pair(a, b) | Run two parsers, return tuple |
| tuple3(a, b, c) | Run three parsers, return tuple |
| preceded(skip, keep) | Skip prefix, keep result |
| terminated(keep, skip) | Keep result, skip suffix |
| delimited(open, p, close) | Keep middle between delimiters |
| separated_list0(sep, p) | 0+ items separated by sep |
| separated_list1(sep, p) | 1+ items separated by sep |
| opt(p) | Optional: Some(v) or None |
| map(p, f) | Transform output |
| map_res(p, f) | Fallibly transform output |
| verify(p, pred) | Check output satisfies predicate |
| ws(p) | Skip leading whitespace, then run p |
| context(msg, p) | Add context to error messages |
| eof() | Match end of input |
| success(v) | Always succeed with value |
| fail(msg) | Always fail |
| double() | Parse floating-point number |
| int() | Parse signed integer |
| uint() | Parse unsigned integer |
| digit0/1() | Parse decimal digits |