Skip to main content

Grammar

Struct Grammar 

Source
pub struct Grammar { /* private fields */ }
Expand description

A compiled grammar plus the live set of viable parse stacks.

Implementations§

Source§

impl Grammar

Source

pub fn from_str_with_root( src: &str, root_name: &str, ) -> Result<Self, GrammarError>

Parse GBNF text and start from root_name.

llama_grammar_init_impl(vocab, grammar_str, grammar_root, ...), minus the lazy-trigger machinery.

Source

pub fn from_str_with_vocab( src: &str, root_name: &str, vocab: Option<&dyn GrammarVocab>, ) -> Result<Self, GrammarError>

As Self::from_str_with_root, resolving <name> token elements through a vocabulary.

Source

pub fn from_parsed( parsed: &ParsedGrammar, root_name: &str, ) -> Result<Self, GrammarError>

Start a machine over an already-parsed grammar.

Source

pub fn from_rules( rules: Vec<GrammarRule>, start_rule_index: u32, name_of: impl Fn(u32) -> Option<String>, ) -> Result<Self, GrammarError>

Build from a raw rule table.

name_of supplies a symbol name for diagnostics; pass |_| None if there is no symbol table.

Source

pub fn into_lazy(self, triggers: LazyTriggers) -> Result<Self, GrammarError>

Make this grammar LAZY: it constrains nothing until one of triggers matches the output.

llama_grammar_init_impl’s lazy / trigger_patterns / trigger_tokens arguments. See super::lazy for what the triggers match against and what happens to the text before one.

Refuses an empty trigger set: upstream allows it, and the result is a grammar that can never switch on – an unconstrained generation that looks constrained from the outside.

Source

pub fn is_lazy(&self) -> bool

Whether this grammar waits for a trigger before it constrains.

Source

pub fn is_awaiting_trigger(&self) -> bool

Whether this grammar is lazy and has NOT yet been triggered, i.e. constrains nothing right now.

llama_grammar::awaiting_trigger, which is the first thing both llama_grammar_apply_impl and llama_grammar_accept_impl test.

Source

pub fn trigger_buffer(&self) -> &[u8]

The output accumulated while awaiting a trigger. Empty once one has fired, and for a grammar that is not lazy.

Source

pub fn rules(&self) -> &[GrammarRule]

The compiled rule table.

Source

pub fn stacks(&self) -> &[GrammarStack]

The stacks still viable after everything accepted so far.

Source

pub fn partial_utf8(&self) -> PartialUtf8

The partial UTF-8 sequence carried over from the last piece.

Source

pub fn allows_eog(&self) -> bool

True when at least one viable parse is complete, so an end-of-generation token is allowed.

llama_grammar_apply_impl’s allow_eog. An empty stack is a finished parse.

A lazy grammar that has not triggered allows it unconditionally: upstream’s awaiting_trigger early-return sits above both the allow_eog mask and the abort in llama_grammar_accept_impl, so an untriggered grammar has no opinion about ending. It has not been applied; a generation that never calls a tool must be able to stop.

Unless its trigger is MANDATORY, which is this repo’s own addition and the one place it departs from upstream here: see LazyTriggers::mandatory.

Source

pub fn trigger_is_mandatory(&self) -> bool

Whether this grammar’s trigger must fire before the generation may end. False for every grammar that is not lazy, and for every lazy grammar whose triggers were not marked mandatory.

Source

pub fn is_dead(&self) -> bool

True when no parse is viable at all. Reaching this means a token was accepted that should have been masked out.

Source

pub fn accept_codepoint(&mut self, chr: u32) -> Result<(), GrammarError>

Advance every stack over one code point.

llama_grammar_accept. Stacks that cannot take the character are dropped; a stack resting on a token element is dropped too, since a token element consumes a whole token, never a character.

Source

pub fn accept_str(&mut self, piece: &str) -> Result<(), GrammarError>

Accept a piece of generated text, carrying any partial UTF-8 sequence across the call.

llama_grammar_accept_str. Errors if nothing survives.

Source

pub fn accept_bytes(&mut self, piece: &[u8]) -> Result<(), GrammarError>

As Self::accept_str, for a piece that is not valid UTF-8 on its own.

This is the real signature: a BPE token piece is bytes, and a piece holding one byte of a multi-byte character is not a str at all. llama.cpp passes std::string, which has the same freedom.

Source

pub fn accept_token( &mut self, token: u32, piece: &[u8], ) -> Result<(), GrammarError>

Accept a sampled token, given its decoded piece.

llama_grammar_accept_token. This is not accept_str plus a token id: a stack resting on a Token / TokenNot element matches on the id and ignores the piece entirely, which is how a grammar can require a specific special token whose text is unreachable through its characters.

While a lazy grammar is awaiting its trigger this does NOT advance the parse: the token goes to the trigger buffer instead, and the grammar is fed only once a trigger fires, and only from where it says. That dispatch lives here, on the one accept path, rather than in a lazy-aware twin of it.

Source

pub fn accept_eog(&mut self) -> Result<(), GrammarError>

Accept an end-of-generation token.

The EOG branch of llama_grammar_accept_impl, which aborts if no stack is empty. Here it is a refusal: EOG at a point where the grammar is unsatisfied means the mask let it through.

Upstream’s EOG branch sits below the awaiting_trigger check, so an untriggered lazy grammar never reaches it: an EOG token is buffered like any other. A caller that has the token’s piece – crate::grammar_sampler::GrammarSampler does – must therefore send it to Self::accept_token while Self::is_awaiting_trigger, not here.

Trait Implementations§

Source§

impl Clone for Grammar

Source§

fn clone(&self) -> Grammar

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Grammar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Grammar

Source§

impl PartialEq for Grammar

Source§

fn eq(&self, other: &Grammar) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Grammar

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.