bobzhang/peg/runtime does not have a README file

    ActionFn

    type ActionFn[T] = (ActionContext[T]) -> Value[T] raise

    Implementation of an action or semantic predicate code block. Predicates return a value whose JavaScript truthiness decides the match.

    InitFn

    type InitFn[T] = (ActionContext[T]) -> Map[String, (ActionContext[T]) -> Value[T] raise] raise

    Implementation of a grammar's initializer block. It runs once per parse (after the start rule is validated) and returns bindings that take precedence over the statically supplied ones, so actions can share per-parse state by closing over it.

    Truthy

    pub(open) trait Truthy {
    fn truthy(Self) -> Bool
    }

    Results of semantic predicates: Bool, or a Value judged by JavaScript truthiness (as in PEG.js).
    impl Truthy for Bool

    FeatureDisabled

    pub(all) suberror FeatureDisabled {
    FeatureDisabled(String)
    } derive(
    Debug
    )

    Raised when an action calls a helper disabled by the features option (in PEG.js the helper is simply undefined).

    InvalidStartRule

    pub(all) suberror InvalidStartRule {
    InvalidStartRule(String)
    } derive(
    Debug
    )

    Raised when parse is asked to start from a rule that is not in the grammar's allowed start rules.

    ReferenceError

    pub(all) suberror ReferenceError {
    ReferenceError(String)
    } derive(
    Debug
    )

    Raised when an action refers to a label that is not in scope (a ReferenceError in PEG.js-generated JavaScript).

    SyntaxError

    pub(all) suberror SyntaxError {
    SyntaxError(message~ : String, expected~ : Array[Expectation]?, found~ : String?, location~ : Location)
    } derive(
    Debug
    )

    Raised by a parser when the input does not match the grammar, or by the error() / expected() action helpers.

    expected and found are None for errors raised through error(), like the null fields of PEG.js' peg$SyntaxError.
    impl Show for SyntaxError

    SyntaxError::to_json

    fn SyntaxError::to_json(self : SyntaxError) -> Json

    ActionContext

    pub struct ActionContext[T] {
    state : State[T]
    params : ArrayView[String]
    args : ArrayView[Value[T]]
    }

    The helpers available to actions, predicates and initializers: labeled values plus the PEG.js text(), location(), error()... functions.

    ActionContext::error

    fn[T, R] ActionContext::error(self : ActionContext[T], message : String, location? : Location) -> R raise

    Aborts the parse with a syntax error carrying message.

    ActionContext::expected

    fn[T, R] ActionContext::expected(self : ActionContext[T], description : String, location? : Location) -> R raise

    Aborts the parse with a "expected " syntax error.

    ActionContext::input

    fn[T] ActionContext::input(self : ActionContext[T]) -> String

    ActionContext::label

    fn[T] ActionContext::label(self : ActionContext[T], label : String) -> Value[T] raise ReferenceError

    The value bound to label. Raises ReferenceError if the label is not in scope, like the generated JavaScript would.

    ActionContext::label_opt

    fn[T] ActionContext::label_opt(self : ActionContext[T], label : String) -> Value[T]?

    Like label, but None when the label is not in scope.

    ActionContext::location

    fn[T] ActionContext::location(self : ActionContext[T]) -> Location raise

    ActionContext::new

    fn[T] ActionContext::new(state : State[T], params : ArrayView[String], args : ArrayView[Value[T]]) -> ActionContext[T]

    ActionContext::offset

    fn[T] ActionContext::offset(self : ActionContext[T]) -> Int raise

    ActionContext::options

    fn[T] ActionContext::options(self : ActionContext[T]) -> Map[String, Value[T]]

    ActionContext::range

    fn[T] ActionContext::range(self : ActionContext[T]) -> (Int, Int) raise

    ActionContext::text

    fn[T] ActionContext::text(self : ActionContext[T]) -> String raise

    CacheEntry

    pub(all) struct CacheEntry[T] {
    next_pos : Int
    result : Slot[T]
    expectations : Array[Expectation]
    }

    A memoized rule result (the cache option).

    ClassMatcher

    pub struct ClassMatcher {
    // private fields
    }

    A compiled character class, equivalent to the /^[...]/i? regexps PEG.js generates. It tests a single UTF-16 code unit.

    ClassMatcher::matches

    fn ClassMatcher::matches(self : ClassMatcher, c : Int) -> Bool

    Tests the code unit c.

    ClassMatcher::matches_at

    fn ClassMatcher::matches_at(self : ClassMatcher, input : String, pos : Int) -> Bool

    Tests the code unit at pos of input; fails at end of input (like testing a regexp against input.charAt(pos), which is then "").

    ClassMatcher::new

    fn ClassMatcher::new(parts : ArrayView[ClassPart], inverted~ : Bool, ignore_case~ : Bool) -> ClassMatcher

    ClassPart

    pub(all) enum ClassPart {
    Char(String)
    Range(String, String)
    } derive(Eq,
    Debug
    )

    One part of a character class: a single code unit or an inclusive range.
    impl ToJson for ClassPart

    ClassPart::to_json

    fn ClassPart::to_json(self : ClassPart) -> Json

    Expectation

    pub(all) enum Expectation {
    Literal(text~ : String, ignore_case~ : Bool)
    Class(parts~ : Array[ClassPart], inverted~ : Bool, ignore_case~ : Bool)
    Any
    End
    Other(description~ : String)
    Not(Expectation)
    } derive(Eq,
    Debug
    )

    Something the parser expected at the failure position.

    Expectation::describe

    fn Expectation::describe(self : Expectation) -> String

    Human-readable description of an expectation, as in describeExpectation.

    Expectation::to_json

    fn Expectation::to_json(self : Expectation) -> Json

    Features

    pub(all) struct Features {
    text : Bool
    offset : Bool
    range : Bool
    location : Bool
    expected : Bool
    error : Bool
    filename : Bool
    default_tracer : Bool
    } derive(Eq,
    Debug
    )

    Which helpers the generated parser provides (the PEG.js features option). Disabled action helpers raise FeatureDisabled.

    Features::all

    fn Features::all() -> Features

    Location

    pub(all) struct Location {
    filename : String?
    start : Pos
    end : Pos
    } derive(Eq,
    Debug
    )

    A span of the input, optionally tagged with the source file name.
    impl ToJson for Location

    Location::to_json

    fn Location::to_json(self : Location) -> Json

    Location::to_value

    fn[T] Location::to_value(self : Location) -> Value[T]

    The location as a Value object, shaped like PEG.js' location objects.

    Pos

    pub(all) struct Pos {
    offset : Int
    line : Int
    column : Int
    } derive(Eq,
    Debug
    )

    A position in the input: a UTF-16 code-unit offset plus 1-based line and column (lines are only broken by \n, as in PEG.js).
    impl ToJson for Pos

    Pos::to_json

    fn Pos::to_json(self : Pos) -> Json

    PosCache

    pub struct PosCache {
    input : String
    details : Map[Int, (Int, Int)]
    filename : String?
    }

    Incremental offset -> line/column computation with the same caching strategy as the generated peg$computePosDetails.

    PosCache::location

    fn PosCache::location(self : PosCache, start : Int, end : Int) -> Location

    PosCache::new

    fn PosCache::new(input : String, filename? : String) -> PosCache

    PosCache::pos

    fn PosCache::pos(self : PosCache, offset : Int) -> Pos

    Slot

    pub(all) enum Slot[T] {
    Failed
    Pos(Int)
    Val(Value[T])
    } derive(Eq,
    Debug
    )

    A slot of the parsing machine's stack: a value, a saved input position or the peg$FAILED marker. Actions never observe Failed or Pos.

    State

    pub(all) struct State[T] {
    input : String
    curr_pos : Int
    saved_pos : Int
    silent_fails : Int
    tracer : Tracer[T]?
    options : Map[String, Value[T]]
    features : Features
    // private fields
    }

    State::begin

    fn[T] State::begin(self : State[T]) -> Unit

    peg$begin: opens a new expectation namespace at the current position.

    State::cache_lookup

    fn[T] State::cache_lookup(self : State[T], rule : Int) -> CacheEntry[T]?

    Looks up a memoized result for rule at the current position. On a hit, restores the position and replays the recorded expectations.

    State::cache_store

    fn[T] State::cache_store(self : State[T], rule : Int, start_pos : Int, result : Slot[T], expectations : Array[Expectation]) -> Unit

    State::compute_location

    fn[T] State::compute_location(self : State[T], start : Int, end : Int) -> Location

    State::end

    fn[T] State::end(self : State[T], invert : Bool) -> Unit

    peg$end: closes the innermost namespace, merging its expectations into the enclosing one (negated when invert) if both are at the same position.

    State::expect

    fn[T] State::expect(self : State[T], expected : Expectation) -> Unit

    peg$expect: records an expectation at the current position.

    State::finish

    fn[T] State::finish(self : State[T], result : Slot[T]) -> Value[T] raise

    peg$buildError + the final end-of-input check of peg$parse.

    State::new

    fn[T] State::new(input : String, options~ : Map[String, Value[T]], features~ : Features, tracer? : Tracer[T]) -> State[T]

    State::rule_expects

    fn[T] State::rule_expects(self : State[T], expected : Expectation) -> Unit

    rule$expects without caching: records unless failures are silenced.

    State::trace

    fn[T] State::trace(self : State[T], type_ : TraceType, rule : String, start_pos : Int, result? : Slot[T]) -> Unit raise

    Emits a trace event if tracing is enabled.

    TraceEvent

    pub(all) struct TraceEvent[T] {
    type_ : TraceType
    rule : String
    result : Value[T]?
    location : Location
    } derive(Eq,
    Debug
    )

    TraceType

    pub(all) enum TraceType {
    RuleEnter
    RuleMatch
    RuleFail
    } derive(Eq,
    Debug
    )

    TraceType::name

    fn TraceType::name(self : TraceType) -> String

    Tracer

    pub(all) struct Tracer[T] {
    trace : (TraceEvent[T]) -> Unit raise
    }

    Receives rule enter/match/fail events when tracing is enabled.

    Tracer::default

    fn[T] Tracer::default(log? : (String) -> Unit) -> Tracer[T]

    The PEG.js DefaultTracer: logs indented events through log (println by default).

    Value

    pub(all) enum Value[T] {
    Undefined
    Null
    Bool(Bool)
    Num(Double)
    Str(String)
    Arr(Array[Value[T]])
    Obj(Map[String, Value[T]])
    Custom(T)
    } derive(Eq,
    Debug
    )

    A dynamically typed value produced by a PEG.js-style parser.

    Mirrors the JavaScript values PEG.js parsers produce: literals, classes and . yield Str, sequences and repetitions yield Arr, ? yields Null on failure and predicates yield Undefined. Custom carries application payloads produced by actions.
    impl Truthy for Value[T]
    impl ToJson for Value[T]

    Value::as_arr

    fn[T] Value::as_arr(self : Value[T]) -> Array[Value[T]]?

    Returns the array payload, if any.

    Value::as_str

    fn[T] Value::as_str(self : Value[T]) -> String?

    Returns the string payload, if any.

    Value::js_string

    fn[T] Value::js_string(self : Value[T]) -> String

    JavaScript String(value). Objects and custom payloads render as [object Object].

    Value::to_json_with

    fn[T] Value::to_json_with(self : Value[T], custom : (T) -> Json) -> Json

    Converts to JSON. Undefined becomes {"$": "undefined"} and custom payloads are rendered with custom, matching the fixture encoding of tools/harvest.js.

    Value::truthy

    fn[T] Value::truthy(self : Value[T]) -> Bool

    JavaScript truthiness, used for semantic predicates.

    build_message

    fn build_message(expected : Array[Expectation], found : String?) -> String

    peg$SyntaxError.buildMessage(expected, found).

    code_unit_is

    fn code_unit_is(input : String, pos : Int, unit : Int) -> Bool

    Whether the code unit at pos is unit (input.charCodeAt(pos) === unit).

    drive

    fn[T] drive(input : String, options : Map[String, Value[T]], tracer : Tracer[T]?, allowed_start_rules~ : ArrayView[String], trace~ : Bool, features~ : Features, run~ : (State[T], String) -> Slot[T] raise) -> Value[T] raise

    Runs one parse the way PEG.js' peg$parse does, shared by the bytecode interpreter and generated parsers:

    1. selects the start rule from options["startRule"] (raising InvalidStartRule if it is not allowed);
    2. sets up tracing (tracer, else the default tracer or a no-op one);
    3. creates the parse state and calls run(state, start_rule), which must run the initializer, call state.begin() and parse the start rule;
    4. checks that all input was consumed, raising SyntaxError otherwise.

    js_canonicalize

    fn js_canonicalize(ch : Int) -> Int

    ES Canonicalize(ch) for non-unicode, ignore-case regexps.

    js_case_variants_any

    fn js_case_variants_any(ch : Int, f : (Int) -> Bool) -> Bool

    Calls f on every code unit whose canonical form equals that of ch (including ch itself), stopping early when f returns true.

    js_compare

    fn js_compare(a : String, b : String) -> Int

    Lexicographic comparison by UTF-16 code units, like JavaScript's default Array.prototype.sort and relational string operators.

    js_substr

    fn js_substr(input : String, pos : Int, n : Int) -> String

    JavaScript input.substr(pos, n) for non-negative pos and n.

    js_substring

    fn js_substring(s : String, start : Int, end : Int) -> String

    JavaScript String.prototype.substring: clamps and swaps its bounds.

    js_to_lower

    fn js_to_lower(s : StringView) -> String

    String.prototype.toLowerCase as implemented by V8.

    matches_ignore_case_at

    fn matches_ignore_case_at(input : String, pos : Int, lit : String) -> Bool

    Case-insensitive literal test: lower(input.substr(pos, lit.length)) ===lit, where lit is already lowercased.

    slot_append

    fn[T] slot_append(target : Slot[T], value : Slot[T]) -> Unit

    APPEND: pushes value onto the array held by target.

    slot_pos

    fn[T] slot_pos(slot : Slot[T]) -> Int

    The saved position in a stack slot; aborts otherwise (a compiler bug).

    slot_value

    fn[T] slot_value(slot : Slot[T]) -> Value[T]

    The value in a stack slot; aborts on Failed / Pos (a compiler bug).

    starts_with_at

    fn starts_with_at(input : String, pos : Int, lit : String) -> Bool

    Whether lit occurs in input at code-unit offset pos (input.substr(pos, lit.length) === lit).

    string_of_code_units

    fn string_of_code_units(units : ArrayView[Int]) -> String

    Builds a string from UTF-16 code units. Unlike string literals, this can produce lone surrogates, which JavaScript strings (and PEG.js grammars) may contain.

    truthy

    fn[R : Truthy] truthy(result : R) -> Bool

    The truthiness of a predicate result.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io