Expand description
2026-09-26: Earley parser for grammar matching. EarleyParser holds the Earley
items, advances them one byte at a time (advance), expands rule references
(predict, predict_fsm), completes finished rules (complete), reports the
acceptable next bytes (accept), and pushes or rolls back steps for the matcher.
Owner: grammar.
Invariants: completable, is_completed and scanable_history hold one entry per
step. new, an accepted advance, push_state_and_expand and
push_one_state_to_check add one to each; pop_last_states(count) removes count
from each and panics rather than remove the first step; reset clears all three.
Structs§
- Earley
Parser - The Earley parser. Drives grammar matching: it keeps the set of Earley items, advances them byte-by-byte, and supports push/rollback of parser state for the matcher’s token rollback.
- Parser
State - An Earley item: a position inside a rule’s body, paired with the input position the rule was predicted at.
- Process
Queue - Below this many distinct states the C++
RepeatDetectorscans a small vector; above it, it switches to a hash set. We always use a hash set —AHashSetmakes the small-N case cheap enough that the dual representation is not worth the complexity. Behavior (which states are reported visited) is identical.
Constants§
- NO_
PREV_ INPUT_ POS - A
rule_start_posof this value marks an item as a root of the parsing stack (no preceding input position). - UNEXPANDED_
RULE_ START_ SEQUENCE_ ID - A
sequence_idof this value means the rule has not been expanded yet (used for the initial state pushed into the parser).
Functions§
- cache_
key - The C++ uses two equality notions; we model the parsing one as
PartialEq(all fields). The cache notion ignoresrule_start_posandrepeat_count— seecache_key.
Type Aliases§
- Completable
Entry - One
(referenced_rule_id, parent_state)entry of the completable-states table. The C++ storesCompact2DArray<pair<int32_t, ParserState>>; the Rust port now uses the portedCompact2DArray(CSR) layout, matching it exactly.