@configuredthings/rdp.js - v0.7.0
    Preparing search index...

    Class GrammarInterpreter

    A runtime interpreter for grammars expressed as a GrammarAST.

    Extends ObservableRDParser so every rule entry and exit is visible to an attached ObservableRDParser.ParseObserver, making it suitable as a drop-in replacement for a generated observable parser in debugging and testing contexts.

    import { GrammarInterpreter } from '@configuredthings/rdp.js/interpreter'
    import { EBNFParser } from '@configuredthings/rdp.js/generator'
    import { TraceObserver } from '@configuredthings/rdp.js/observable'

    // Parse an EBNF grammar into an AST once, then reuse the AST for many inputs
    const ast = EBNFParser.parse(`
    Number = Digit, {Digit};
    Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
    `)

    function tryParse(input: string): boolean {
    const bytes = new TextEncoder().encode(input)
    return new GrammarInterpreter(ast, new DataView(bytes.buffer)).parse()
    }

    tryParse('42') // → true
    tryParse('abc') // → false

    // Attach a trace observer for step-by-step debugging
    const obs = new TraceObserver()
    const bytes = new TextEncoder().encode('7')
    new GrammarInterpreter(ast, new DataView(bytes.buffer))
    .withObserver(obs)
    .parse()
    console.log(obs.events) // enter/exit events for Number and Digit

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    source: DataView

    The raw input buffer. Accessible to subclasses so they can decode slices.

    Methods

    • Returns true when the parser has reached the end of input. Implemented by subclasses based on their input representation.

      Returns boolean

    • Returns a zero-copy DataView slice of the source buffer.

      Parameters

      • from: number

        Start byte offset (inclusive), relative to the start of source.

      • to: number

        End byte offset (exclusive), relative to the start of source.

      Returns DataView

    • Returns a human-readable description of the input element at pos for use in error messages. In scannerless parsers this describes a byte; in token parsers it describes a token.

      Parameters

      • pos: number

        The position index to describe.

      Returns string

    • Notifies the observer of the error before re-throwing via the base class.

      Parameters

      • message: string

        Description of the parse failure.

      Returns never

      Always.

    • Throws a RDParserException naming the unexpected input element at the furthest position reached during parsing.

      Call this from the top-level parse() method when the first rule returns null, to give callers a precise error location rather than a generic "Failed to parse input (at position 0)" message.

      Returns never

      Always.

    • Asserts that the current byte equals expectedByte and consumes it.

      Parameters

      • expectedByte: number

        The byte value expected.

      • OptionalexpectedCharDescription: string

        Optional human-readable name for the expected character.

      Returns void

      If the current byte does not match expectedByte.

    • If the current byte equals expectedByte, consumes it and returns true. Otherwise leaves the position unchanged and returns false.

      Parameters

      • expectedByte: number

        The byte value to match.

      Returns boolean

    • Call at the entry of each production method to notify the observer.

      Parameters

      • production: string

        Name of the production rule being entered.

      Returns void

    • Call before each return in a production method to notify the observer.

      Parameters

      • production: string

        Name of the production rule being exited.

      • matched: boolean

        Whether the production successfully matched input.

      Returns void

    • Attempts to match the entire source against the grammar's first production rule.

      Returns true if and only if the first rule matches and all input has been consumed. Returns false if the grammar is empty, the rule fails, or input remains after a successful rule match.

      Returns boolean

      true on a full match, false otherwise.

    • Returns the byte at the current position without consuming it, or null if the parser has reached the end of input.

      Returns number | null

    • If the current byte equals expectedByte, consumes it and returns it as a one-character string. Otherwise leaves the position unchanged and returns null.

      This is the value-returning counterpart of matchChar, used by generated parsers to capture terminal characters directly into the parse tree.

      Parameters

      • expectedByte: number

        The byte value to match.

      Returns string | null

    • If the current byte falls within [lowerBound, upperBound] (inclusive), consumes it and returns it as a one-character string. Otherwise leaves the position unchanged and returns null.

      Used by generated parsers to capture character-range terminals into the parse tree.

      Parameters

      • lowerBound: number

        Lower bound (inclusive) of the byte range.

      • upperBound: number

        Upper bound (inclusive) of the byte range.

      Returns string | null