pub struct GrammarData {
pub allow_empty_rule_ids: Vec<i32>,
pub optimized: bool,
pub complete_fsm: CompactFsm,
pub per_rule_fsms: Vec<Option<CompactFsmWithStartEnd>>,
pub per_rule_fsm_hashes: Vec<Option<u64>>,
pub per_rule_fsm_new_state_ids: Vec<Vec<(i32, i32)>>,
/* private fields */
}Expand description
The BNF grammar AST. Equivalent to xgrammar’s Grammar::Impl.
The FSM acceleration fields (complete_fsm, per_rule_fsms,
per_rule_fsm_hashes, per_rule_fsm_new_state_ids) are populated
by the W3 grammar-functor passes (GrammarFSMBuilder /
GrammarFSMHasher); they are Default::default() until then.
Fields§
§allow_empty_rule_ids: Vec<i32>Rule ids permitted to match the empty string.
optimized: boolWhether grammar optimization passes have run.
complete_fsm: CompactFsmThe single FSM holding every rule’s states/edges, built by
GrammarFSMBuilder. Empty until the FSM builder runs.
per_rule_fsms: Vec<Option<CompactFsmWithStartEnd>>Per-rule FSM views into complete_fsm. None for a rule whose
body could not be expressed as an FSM. Empty until built.
per_rule_fsm_hashes: Vec<Option<u64>>Per-rule structural hash of the rule’s FSM, or None when the
rule could not be hashed. Set by GrammarFSMHasher.
per_rule_fsm_new_state_ids: Vec<Vec<(i32, i32)>>Per-rule (original_state_id, new_state_id) mapping produced
alongside the hash by GrammarFSMHasher.
Implementations§
Source§impl GrammarData
impl GrammarData
Sourcepub fn from_parts(
rules: Vec<Rule>,
expr_data: Vec<i32>,
expr_indptr: Vec<i32>,
root_rule_id: i32,
) -> Self
pub fn from_parts( rules: Vec<Rule>, expr_data: Vec<i32>, expr_indptr: Vec<i32>, root_rule_id: i32, ) -> Self
Construct directly from CSR parts — used by the grammar builder (W2) and the deserializer.
Sourcepub fn set_rule_name(&mut self, rule_id: i32, name: impl Into<String>)
pub fn set_rule_name(&mut self, rule_id: i32, name: impl Into<String>)
Mutable access to a rule’s name — used by RootRuleRenamer.
Sourcepub fn set_expr_data(&mut self, expr_id: i32, index: usize, value: i32)
pub fn set_expr_data(&mut self, expr_id: i32, index: usize, value: i32)
Borrowed view of expr expr_id as (type, &mut data[..]). Used
by RepetitionNormalizer to rewrite a Repeat payload in place.
Sourcepub fn rule(&self, rule_id: i32) -> &Rule
pub fn rule(&self, rule_id: i32) -> &Rule
The rule with the given id. Panics if out of bounds (matches
the C++ XGRAMMAR_DCHECK contract).
Sourcepub fn root_rule_id(&self) -> i32
pub fn root_rule_id(&self) -> i32
The root rule id (-1 if unset).
Sourcepub fn set_root_rule_id(&mut self, id: i32)
pub fn set_root_rule_id(&mut self, id: i32)
Set the root rule id.
Sourcepub fn append_expr(&mut self, kind: GrammarExprType, data: &[i32]) -> i32
pub fn append_expr(&mut self, kind: GrammarExprType, data: &[i32]) -> i32
Append a raw (type, data[]) expression to the CSR store and
return its new expr id. Used by LookaheadAssertionAnalyzer,
which adds Sequence exprs to an already-built grammar.
Sourcepub fn expr(&self, expr_id: i32) -> GrammarExpr<'_>
pub fn expr(&self, expr_id: i32) -> GrammarExpr<'_>
Borrowed view of expression expr_id. The CSR slot is
[type, data_len, data0, …]; this returns (type, &data[..]).
Sourcepub fn byte_string(&self, expr_id: i32) -> String
pub fn byte_string(&self, expr_id: i32) -> String
Decode a ByteString expression to its String.
Sourcepub fn tag_dispatch(&self, expr_id: i32) -> TagDispatch
pub fn tag_dispatch(&self, expr_id: i32) -> TagDispatch
Decode a TagDispatch expression. Panics if expr_id is not a
tag dispatch (matches the C++ XGRAMMAR_DCHECK).
Trait Implementations§
Source§impl Clone for GrammarData
impl Clone for GrammarData
Source§fn clone(&self) -> GrammarData
fn clone(&self) -> GrammarData
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GrammarData
impl Debug for GrammarData
Source§impl Default for GrammarData
impl Default for GrammarData
Source§fn default() -> GrammarData
fn default() -> GrammarData
Source§impl From<GrammarData> for Grammar
impl From<GrammarData> for Grammar
Source§fn from(data: GrammarData) -> Self
fn from(data: GrammarData) -> Self
Auto Trait Implementations§
impl Freeze for GrammarData
impl RefUnwindSafe for GrammarData
impl Send for GrammarData
impl Sync for GrammarData
impl Unpin for GrammarData
impl UnwindSafe for GrammarData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.