Constructs an interpreter for the given grammar, ready to parse source.
The AST is indexed into a Map so rule lookups during parsing are O(1).
Constructing a new GrammarInterpreter for each input is cheap; the
expensive work is Generator.EBNFParser.parse / Generator.ABNFParser.parse that produces the AST.
A GrammarAST produced by EBNFParser.parse or ABNFParser.parse.
The input to parse, wrapped in a DataView.
Protected ReadonlysourceThe raw input buffer. Accessible to subclasses so they can decode slices.
ProtectedadvanceMoves the current position forward by one byte. Has no effect when already at end of input.
ProtectedatReturns true when the parser has reached the end of input.
Implemented by subclasses based on their input representation.
ProtectedcaptureReturns a zero-copy DataView slice of the source buffer.
Start byte offset (inclusive), relative to the start of source.
End byte offset (exclusive), relative to the start of source.
ProtectedconsumeProtecteddescribeReturns 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.
The position index to describe.
ProtectederrorProtectederrorThrows 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.
ProtectedexpectProtectedgetReturns the furthest position at which a terminal match was attempted and failed. Useful for producing informative error messages when the overall parse fails.
ProtectedgetReturns the current position within the input.
ProtectedmatchIf the current byte equals expectedByte, consumes it and returns true.
Otherwise leaves the position unchanged and returns false.
The byte value to match.
ProtectednotifyCall at the entry of each production method to notify the observer.
Name of the production rule being entered.
ProtectednotifyCall before each return in a production method to notify the observer.
Name of the production rule being exited.
Whether the production successfully matched input.
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.
true on a full match, false otherwise.
ProtectedpeekReturns the byte at the current position without consuming it,
or null if the parser has reached the end of input.
ProtectedreadIf 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.
The byte value to match.
ProtectedreadIf 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.
Lower bound (inclusive) of the byte range.
Upper bound (inclusive) of the byte range.
ProtectedrestoreRestores the current position to a previously saved value.
Used with getPosition to implement backtracking in parser alternatives.
The position to restore to.
ProtectedupdateRecords the current position as a fail point if it is further than any previously recorded fail. Subclasses call this when a terminal match fails.
Attaches an observer that will receive parse events during parsing.
Returns this for chaining: new MyParser(source).withObserver(obs).parse().
The observer to attach.
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.
Example