pub struct Grammar { /* private fields */ }Expand description
A compiled grammar plus the live set of viable parse stacks.
Implementations§
Source§impl Grammar
impl Grammar
Sourcepub fn from_str_with_root(
src: &str,
root_name: &str,
) -> Result<Self, GrammarError>
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.
Sourcepub fn from_str_with_vocab(
src: &str,
root_name: &str,
vocab: Option<&dyn GrammarVocab>,
) -> Result<Self, GrammarError>
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.
Sourcepub fn from_parsed(
parsed: &ParsedGrammar,
root_name: &str,
) -> Result<Self, GrammarError>
pub fn from_parsed( parsed: &ParsedGrammar, root_name: &str, ) -> Result<Self, GrammarError>
Start a machine over an already-parsed grammar.
Sourcepub fn from_rules(
rules: Vec<GrammarRule>,
start_rule_index: u32,
name_of: impl Fn(u32) -> Option<String>,
) -> Result<Self, GrammarError>
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.
Sourcepub fn into_lazy(self, triggers: LazyTriggers) -> Result<Self, GrammarError>
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.
Sourcepub fn is_awaiting_trigger(&self) -> bool
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.
Sourcepub fn trigger_buffer(&self) -> &[u8] ⓘ
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.
Sourcepub fn rules(&self) -> &[GrammarRule] ⓘ
pub fn rules(&self) -> &[GrammarRule] ⓘ
The compiled rule table.
Sourcepub fn stacks(&self) -> &[GrammarStack] ⓘ
pub fn stacks(&self) -> &[GrammarStack] ⓘ
The stacks still viable after everything accepted so far.
Sourcepub fn partial_utf8(&self) -> PartialUtf8
pub fn partial_utf8(&self) -> PartialUtf8
The partial UTF-8 sequence carried over from the last piece.
Sourcepub fn allows_eog(&self) -> bool
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.
Sourcepub fn trigger_is_mandatory(&self) -> bool
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.
Sourcepub fn is_dead(&self) -> bool
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.
Sourcepub fn accept_codepoint(&mut self, chr: u32) -> Result<(), GrammarError>
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.
Sourcepub fn accept_str(&mut self, piece: &str) -> Result<(), GrammarError>
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.
Sourcepub fn accept_bytes(&mut self, piece: &[u8]) -> Result<(), GrammarError>
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.
Sourcepub fn accept_token(
&mut self,
token: u32,
piece: &[u8],
) -> Result<(), GrammarError>
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.
Sourcepub fn accept_eog(&mut self) -> Result<(), GrammarError>
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§
impl Eq for Grammar
impl StructuralPartialEq for Grammar
Auto Trait Implementations§
impl Freeze for Grammar
impl RefUnwindSafe for Grammar
impl Send for Grammar
impl Sync for Grammar
impl Unpin for Grammar
impl UnsafeUnpin for Grammar
impl UnwindSafe for Grammar
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,
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