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.
moon add moonbitstack/moonpsec
| document | what 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.
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
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()
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")
Every parser stops somewhere. The only question is what it can tell you when it does.
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()
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