Skip to content

Parser

[Source]

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.

trait box Parser

Public Functions

parse

[Source]

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

[Source]

A human-readable description of what this parser expected. Used to build error messages on parse failure.

fun box error_msg()
: String val

Returns


skip_hidden

[Source]

Return a new start location, skipping over hidden tokens.

fun box skip_hidden(
  source: Source val,
  offset: USize val,
  hidden: Parser box)
: USize val

Parameters

Returns


result

[Source]

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

Returns


mul

[Source]

Sequence operator: a * b matches a then b.

fun box mul(
  that: Parser box)
: Sequence ref

Parameters

Returns


div

[Source]

Ordered choice operator: a / b tries a first, then b.

fun box div(
  that: Parser box)
: Choice ref

Parameters

Returns


neg

[Source]

Skip operator: -a matches a but omits it from the tree.

fun box neg()
: Skip ref

Returns


opt

[Source]

Option: a.opt() matches a or succeeds with NotPresent.

fun box opt()
: Option ref

Returns


many

[Source]

Zero or more: a.many() or a.many(sep) for separated lists.

fun box many(
  sep: Parser box = reference)
: Many ref

Parameters

Returns


many1

[Source]

One or more: a.many1() or a.many1(sep) for separated lists.

fun box many1(
  sep: Parser box = reference)
: Many ref

Parameters

Returns


op_not

[Source]

Not predicate: not a succeeds if a fails, consuming nothing.

fun box op_not()
: Not ref

Returns


op_and

[Source]

And predicate: succeeds if a matches, consuming nothing.

fun box op_and()
: Not ref

Returns


hide

[Source]

Set the hidden channel (whitespace/comments) for this parser.

fun box hide(
  that: Parser box)
: Hidden ref

Parameters

Returns


term

[Source]

Wrap as a terminal that produces a single Token leaf.

fun box term(
  l: Label val = reference)
: Terminal ref

Parameters

  • l: Label val = reference

Returns


eof

[Source]

Require the entire input to be consumed after this parser.

fun box eof()
: EndOfFile ref

Returns