C++ API
On this page
Vocab
Each token ID and its bytes.
class glrmask::Vocab
glrmask::Vocab::Vocab(const std::vector< VocabEntry > &entries);
Hugging Face tokenizers don’t expose token bytes (#2719); converting them is outside GLRMask’s scope.
Grammar
class glrmask::Grammar
static Grammar glrmask::Grammar::from_glrm(std::string_view text);
Parses GLRM source.
static Grammar glrmask::Grammar::from_lark(std::string_view text);
Parses Lark source.
static Grammar glrmask::Grammar::from_json_schema(std::string_view text);
Parses a JSON Schema, given as JSON text.
Grammar glrmask::Grammar::from_expr(const Expr &value);
Builds a grammar from an Expr.
Externs glrmask::Grammar::externs() const;
All extern declarations.
Constraint glrmask::Grammar::compile(const Vocab &vocab, const CompileOptions &options={}) const;
Compiles the grammar into a constraint.
Constraint
class glrmask::Constraint
ConstraintState glrmask::Constraint::start() const;
Starts a new sequence.
std::size_t glrmask::Constraint::mask_bit_len() const;
The mask’s length in bits (max token ID plus one).
Bytes glrmask::Constraint::save() const;
Serializes the compiled constraint for later loading.
static Constraint glrmask::Constraint::load(const Bytes &bytes, const Vocab &vocab);
May fail if saved by a different GLRMask version. Fails if vocab differs from the one it was compiled with.
static Constraint glrmask::Constraint::load(const std::uint8_t *bytes, std::size_t size, const Vocab &vocab);
ConstraintState
class glrmask::ConstraintState
std::vector< std::uint32_t > glrmask::ConstraintState::mask() const;
Packed uint32 words: token i is bit i % 32 of word i / 32.
void glrmask::ConstraintState::fill_mask(std::uint32_t *output, std::size_t word_count) const;
Like mask(), but writes the words into output.
Throws Error if word_count is less than (mask_bit_len() + 31) / 32.
void glrmask::ConstraintState::fill_mask(std::vector< std::uint32_t > &output) const;
Like mask(), but writes the words into output. Throws Error if the buffer is too small.
std::size_t glrmask::ConstraintState::mask_bit_len() const;
The mask’s length in bits (max token ID plus one).
bool glrmask::ConstraintState::commit_token(TokenId id);
Advances the state by the token. If it isn’t allowed, returns false and terminates the state.
bool glrmask::ConstraintState::commit_bytes(const std::uint8_t *bytes, std::size_t size);
Advances the state by the bytes. If they aren’t allowed, returns false and terminates the state.
bool glrmask::ConstraintState::commit_bytes(const Bytes &bytes);
Advances the state by the bytes. If they aren’t allowed, returns false and terminates the state.
bool glrmask::ConstraintState::commit_bytes(std::string_view bytes);
Advances the state by the bytes. If they aren’t allowed, returns false and terminates the state.
Bytes glrmask::ConstraintState::forced_bytes() const;
The bytes that must come next.
bool glrmask::ConstraintState::is_accepting() const;
Whether the output so far could end here.
bool glrmask::ConstraintState::is_terminated() const;
Whether nothing more can be committed.
Expr
A grammar expression.
class glrmask::Expr
static Expr glrmask::Expr::choice(const std::vector< ExprOperand > &values);
Matches any one of values.
static Expr glrmask::Expr::sequence(const std::vector< ExprOperand > &values);
Matches values in order.
static Expr glrmask::Expr::literal(const std::uint8_t *data, std::size_t len);
Matches data exactly.
static Expr glrmask::Expr::literal(std::string_view bytes);
Matches bytes exactly.
static Expr glrmask::Expr::literal(const Bytes &bytes);
Matches bytes exactly.
TokenSet
A set of token IDs.
class glrmask::TokenSet
glrmask::TokenSet::TokenSet(const std::vector< TokenId > &ids);
static TokenSet glrmask::TokenSet::from_ranges(const std::vector< TokenRange > &ranges);
bool operator==(const TokenSet &a, const TokenSet &b);
bool operator!=(const TokenSet &a, const TokenSet &b);
Extern
A named placeholder in a grammar.
class glrmask::Extern
glrmask::Extern::Extern(std::string name, ExternKind kind);
const std::string & glrmask::Extern::name() const noexcept;
ExternKind glrmask::Extern::kind() const noexcept;
bool glrmask::Extern::operator==(const Extern &other) const;
bool glrmask::Extern::operator!=(const Extern &other) const;
Externs
A read-only mapping of extern names to Extern.
class glrmask::Externs
using glrmask::Externs::const_iterator = std::vector<Extern>::const_iterator;
const_iterator glrmask::Externs::begin() const noexcept;
const_iterator glrmask::Externs::end() const noexcept;
const_iterator glrmask::Externs::cbegin() const noexcept;
const_iterator glrmask::Externs::cend() const noexcept;
std::size_t glrmask::Externs::size() const noexcept;
bool glrmask::Externs::empty() const noexcept;
std::optional< Extern > glrmask::Externs::get(std::string_view name) const;
bool glrmask::Externs::has(std::string_view name) const;
Externs glrmask::Externs::unbound() const;
Only externs that still need bindings.
bool glrmask::Externs::operator==(const Externs &other) const;
bool glrmask::Externs::operator!=(const Externs &other) const;
MaskStrategy
Controls how the constraint is compiled.
class glrmask::MaskStrategy
MaskStrategy glrmask::MaskStrategy::Auto;
The default; currently FastMasks.
MaskStrategy glrmask::MaskStrategy::FastCompile;
Faster compilation, slower masks.
MaskStrategy glrmask::MaskStrategy::FastMasks;
Faster masks, slower compilation.
bool glrmask::MaskStrategy::operator==(const MaskStrategy &other) const noexcept;
bool glrmask::MaskStrategy::operator!=(const MaskStrategy &other) const noexcept;
CompileOptions
Options for compiling a grammar or module.
struct glrmask::CompileOptions
MaskStrategy glrmask::CompileOptions::mask_strategy = MaskStrategy::Auto;
Defaults to MaskStrategy::Auto.
glrmask_compile_options glrmask::CompileOptions::native() const noexcept;
Error
class glrmask::Error
glrmask::Error::Error(glrmask_status status, const std::string &message);
glrmask_status glrmask::Error::status() const noexcept;
The status code for this error.
Module
A partially compiled grammar.
class glrmask::Module
glrmask::Module::Module(const Grammar &grammar);
Starts with the grammar and its unbound externs.
Externs glrmask::Module::externs() const;
All extern declarations, including bound externs.
void glrmask::Module::bind(std::string_view name, const Module &value);
Binds an extern by name.
void glrmask::Module::bind(std::string_view name, const Grammar &value);
Binds an extern by name.
void glrmask::Module::bind(std::string_view name, const Constraint &value);
Binds an extern by name.
void glrmask::Module::bind(std::string_view name, const Expr &value);
Binds an extern by name.
void glrmask::Module::bind(std::string_view name, const TokenSet &value);
Binds an extern by name.
void glrmask::Module::precompile(const Vocab &vocab, const CompileOptions &options={});
Compiles what it can now, even with externs unbound. Further compilation must use the same vocab.
Constraint glrmask::Module::compile(const Vocab &vocab, const CompileOptions &options={}) const;
Finishes compiling the module into a constraint.
Bytes glrmask::Module::save() const;
Serializes the module, including any precompiled work.
static Module glrmask::Module::load(const Bytes &bytes);
May fail if saved by a different GLRMask version.
static Module glrmask::Module::load(const std::uint8_t *bytes, std::size_t size);
May fail if saved by a different GLRMask version.
ExprOperand
One part of an expression: a grammar, module, constraint, expression or token set.
class glrmask::ExprOperand
glrmask::ExprOperand::ExprOperand(const Grammar &);
glrmask::ExprOperand::ExprOperand(const Module &);
glrmask::ExprOperand::ExprOperand(const Constraint &);
glrmask::ExprOperand::ExprOperand(const Expr &);
glrmask::ExprOperand::ExprOperand(const TokenSet &);
VocabEntry
One vocabulary entry: a token ID and its exact bytes.
struct glrmask::VocabEntry
TokenId glrmask::VocabEntry::id;
Bytes glrmask::VocabEntry::bytes;
glrmask::VocabEntry::VocabEntry(TokenId id_, Bytes bytes_);
glrmask::VocabEntry::VocabEntry(TokenId id_, std::string_view bytes_);
glrmask
enum class ExternKind : std::uint32_t {
Token = GLRMASK_EXTERN_TOKEN,
Grammar = GLRMASK_EXTERN_GRAMMAR,
};
What an extern can be bound to.
Token: A token set.
Grammar: A grammar, module, expression or token set.
using glrmask::TokenId = std::uint32_t;
using glrmask::TokenRange = glrmask_token_range;
A range of token IDs. start is included and end is not.
using glrmask::Bytes = std::vector<std::uint8_t>;
const char * glrmask::version() noexcept;