moonpsecparser combinators
github
a parser combinator library moonbit · apache-2.0 · no dependencies

moon parser

moonpsec reads text with a grammar written in MoonBit. Give it a document, and it answers with the value — or with the place it stopped, and everything it would have taken there.

install
moon add moonbitstack/moonpsec
four documents read on this page by the library, compiled to javascript
documentwhat the reader said
[1, 2, 3]—
{"name": "moonpsec"}—
{"name": "moonpsec",}—
{name: "moonpsec"}—

A refusal is not a failure to say yes; it is the position, the character found there, and the set of things that would have done instead.

01position

A position is three numbers.

An offset into the text, a line, and a column. Every step carries them forward. A tab moves to the next stop — with the default width of eight, a tab at the start of a line puts the next character in column 9 — and a character above the basic plane counts once, the way a reader counts it, not the way a machine stores it.

The position is not a side effect of reading; it is an answer. position is a parser like any other, so a grammar can return where it got to.

let walked = @moonpsec.take(1).then(@moonpsec.position)
let measured = @moonpsec.run_position(walked, "\ta").unwrap()
// measured.1 is the place after the tab
after a tab 1:9
02expecting

One place, every alternative.

When a choice fails, the last branch tried is not the one that speaks. Every branch that failed at the same place contributes its expectation, and the message names them together: one position, one sentence, all the alternatives.

label does not hide them. It replaces the character classes of one branch with the reader's word for it, which is what a person reading the message needs.

let either = @moonpsec.char('a').or(@moonpsec.char('b'))
let refusal = @moonpsec.run(either, "c").unwrap_err()
both branches named at the same offset

1:1: unexpected 'c'; expecting 'a' or 'b'

the refusal position, what was found, what would have been taken

03commit

A branch that has read something has committed.

Once a parser has consumed a character, a later or will not undo it: the text has been read, and the failure is about what comes next. That is what keeps a grammar from trying every alternative at every offset.

attempt is how a grammar says the opposite — give the character back, and let the other branch have it.

let ab = @moonpsec.string("ab")
let ac = @moonpsec.string("ac")
// the 'a' was read, so the second branch is never tried
let refused = @moonpsec.run(ab.or(ac), "ac")
// the 'a' is given back before the second branch runs
let accepted = @moonpsec.run(ab.attempt().or(ac), "ac")
two readings 1:2: expecting "ab" · Ok("ac")

Every parser stops somewhere. The only question is what it can tell you when it does.

04pieces

The text does not have to arrive all at once.

A feeder takes the text in chunks and answers Needs while the end of what it has is still in the middle of something. Feed the rest and the same grammar finishes the job — no second parser, and nothing that was settled is read twice.

A tree that arrives over a socket is the same parser with a different source of characters: the grammar does not know how its text was delivered.

let hello = @moonpsec.string("hello").eof()
let feeder = @moonpsec.feeder(hello)
let first = feeder.feed("he")
// Needs(1): the word is not finished
let rest = feeder.feed("llo")
let done = feeder.finish()
two chunks Needs(1) → Ok("hello")
05start

Read something.

One package, no dependencies, four backends. The guide takes a grammar from a single character to a whole document; the reference is generated from the interface file; the play page runs the library on a document of your own.

moon add moonbitstack/moonpsec