Expand description
2026-09-26: Grammar compiler: turns a grammar, a JSON schema or a structural tag,
together with a tokenizer, into a CompiledGrammar, which holds the grammar and
computes, per parser state, the token masks the matcher uses to fill the logit bitmask.
GrammarCompiler caches compiled grammars in GrammarCache, and, when caching is
enabled, shares one RuleLevelCache of per-rule masks across every grammar it builds.
Owner: grammar. Invariants: none beyond the types.
Re-exports§
pub use mask_snapshot::SnapshotError;pub use mask_snapshot::SnapshotIdentity;
Modules§
Structs§
- Adaptive
Token Mask - Preprocessed accept/reject/uncertain partition for one parser state.
- Compiled
Grammar - A grammar compiled against a tokenizer — the result of
preprocessing performed by
super::GrammarCompiler. - Compiled
Grammar Impl - The inner, shared state of a
CompiledGrammar. Port ofCompiledGrammar::Impl. - Grammar
Compiler - A tokenizer-bound grammar compiler with an internal cache.
- Grammar
Decomposition - The result of the WGRAMMAR static/dynamic decomposition pass over a
whole grammar — one
RuleDecompositionper rule, indexed byrule_id. - Rule
Decomposition - The decomposition of one rule body into alternating static / dynamic segments, in body order.
- Rule
Level Cache - Cross-grammar adaptive-token-mask cache. Cheap to clone — the inner
state is shared via
Arc, matching the C++ pimplshared_ptr. - Rule
Mask Key - The structural cache key — port of
RuleLevelCache::Impl::NodeKey.
Enums§
- Compile
Error - An error produced while compiling a grammar, schema or tag.
- Forced
- The result of a forced-token analysis over a next-token bitmask.
- Segment
- One decomposed span of a rule body under WGRAMMAR.
- Store
Type - How an
AdaptiveTokenMaskstores its accepted/rejected partition.
Constants§
- UNLIMITED_
SIZE - Sentinel
max_cache_memory_sizemeaning “no memory bound” — port ofRuleLevelCache::kUnlimitedSize(static_cast<size_t>(-1)). - USE_
BITSET_ THRESHOLD - Above this many accepted (and rejected) indices, the partition is
stored as a vocab-sized bitset instead of an index vector. Faithful
to the C++
AdaptiveTokenMask::USE_BITSET_THRESHOLD.
Functions§
- analyze_
bitmask - Analyze a fully-computed next-token bitmask for the forced-token condition.
- decompose_
static_ regions - Run the WGRAMMAR static/dynamic decomposition over an optimized grammar.