Parser¶
A parsing expression that can be applied to source text. Parsers are composed
using operator sugar: * for sequence, / for ordered choice, - for skip,
and not for negation.
Public Functions¶
parse¶
Parse source starting at byte offset. When tree is true, build an AST;
when false, only track the lexical position. The hidden parser defines
whitespace/comments to skip between tokens.
fun box parse(
source: Source val,
offset: USize val = 0,
tree: Bool val = true,
hidden: Parser box = reference)
: (USize val , (AST val | Token val | NotPresent val |
Skipped val | Lex val | Parser box))
Parameters¶
Returns¶
error_msg¶
A human-readable description of what this parser expected. Used to build error messages on parse failure.
Returns¶
- String val
skip_hidden¶
Return a new start location, skipping over hidden tokens.
Parameters¶
Returns¶
- USize val
result¶
Once a terminal parser has an offset and length, it should call result to
return either a token (if a tree is requested) or a new lexical position.
fun box result(
source: Source val,
offset: USize val,
from: USize val,
length: USize val,
tree: Bool val,
l: Label val = reference)
: (USize val , (AST val | Token val | NotPresent val |
Skipped val | Lex val | Parser box))
Parameters¶
- source: Source val
- offset: USize val
- from: USize val
- length: USize val
- tree: Bool val
- l: Label val = reference
Returns¶
mul¶
Sequence operator: a * b matches a then b.
Parameters¶
- that: Parser box
Returns¶
- Sequence ref
div¶
Ordered choice operator: a / b tries a first, then b.
Parameters¶
- that: Parser box
Returns¶
- Choice ref
neg¶
Skip operator: -a matches a but omits it from the tree.
Returns¶
- Skip ref
opt¶
Option: a.opt() matches a or succeeds with NotPresent.
Returns¶
- Option ref
many¶
Zero or more: a.many() or a.many(sep) for separated lists.
Parameters¶
- sep: Parser box = reference
Returns¶
- Many ref
many1¶
One or more: a.many1() or a.many1(sep) for separated lists.
Parameters¶
- sep: Parser box = reference
Returns¶
- Many ref
op_not¶
Not predicate: not a succeeds if a fails, consuming nothing.
Returns¶
- Not ref
op_and¶
And predicate: succeeds if a matches, consuming nothing.
Returns¶
- Not ref
hide¶
Set the hidden channel (whitespace/comments) for this parser.
Parameters¶
- that: Parser box
Returns¶
- Hidden ref
term¶
Wrap as a terminal that produces a single Token leaf.
Parameters¶
- l: Label val = reference
Returns¶
- Terminal ref
eof¶
Require the entire input to be consumed after this parser.
Returns¶
- EndOfFile ref