C API

On this page

glrmask_compile_options

Options for compiling a grammar or module. Set struct_size to sizeof(glrmask_compile_options); fields past struct_size count as zero.

struct glrmask_compile_options
size_t struct_size;
uint32_t mask_strategy;

A GLRMASK_MASK_STRATEGY_* value; 0 is Auto.

uint32_t reserved[3];

Reserved; must be zero.

glrmask_expr_operand

One part of an expression; kind is a GLRMASK_OPERAND_* value.

struct glrmask_expr_operand
uint32_t kind;
const void* handle;

glrmask_token_range

A range of token IDs. start is included and end is not.

struct glrmask_token_range
uint32_t start;
uint32_t end;

glrmask_vocab_entry

One vocabulary entry: a token ID and its exact bytes.

struct glrmask_vocab_entry
uint32_t id;
const uint8_t* bytes;
size_t bytes_len;

Functions and constants

#define GLRMASK_VERSION_STRING "1.0.0-rc.1"
enum {
    GLRMASK_OK =0,
    GLRMASK_SYNTAX =1,
    GLRMASK_INVALID_INPUT =2,
    GLRMASK_COMPILATION =3,
    GLRMASK_RESOURCE_LIMIT =4,
    GLRMASK_STRATEGY_UNSUPPORTED =5,
    GLRMASK_SERIALIZATION =6,
    GLRMASK_INTERNAL =7,
    GLRMASK_BUFFER_TOO_SMALL =8,
    GLRMASK_VOCAB_MISMATCH =10,
    GLRMASK_CLOSED =9,
};

Status values. On failure an output handle is NULL. Treat an unknown non-zero code as a failure.

GLRMASK_INVALID_INPUT: A handle or argument is invalid.

GLRMASK_STRATEGY_UNSUPPORTED: The chosen strategy does not support the grammar.

GLRMASK_SERIALIZATION: Saved bytes could not be loaded.

GLRMASK_BUFFER_TOO_SMALL: The buffer is too small; nothing was written.

GLRMASK_VOCAB_MISMATCH: Artifact or precompiled module is for another vocabulary.

GLRMASK_CLOSED: Closed handle (reported by managed wrappers; raw dangling pointers are invalid).

enum {
    GLRMASK_MASK_STRATEGY_AUTO =0,
    GLRMASK_MASK_STRATEGY_FAST_COMPILE =3,
    GLRMASK_MASK_STRATEGY_FAST_MASKS =4,
};

Controls how the constraint is compiled.

GLRMASK_MASK_STRATEGY_AUTO: The default; currently GLRMASK_MASK_STRATEGY_FAST_MASKS.

GLRMASK_MASK_STRATEGY_FAST_COMPILE: Faster compilation, slower masks.

GLRMASK_MASK_STRATEGY_FAST_MASKS: Faster masks, slower compilation.

enum {
    GLRMASK_EXTERN_TOKEN =0,
    GLRMASK_EXTERN_GRAMMAR =1,
};

What an extern can be bound to.

GLRMASK_EXTERN_TOKEN: A token set.

GLRMASK_EXTERN_GRAMMAR: A grammar, module, expression or token set.

enum {
    GLRMASK_OPERAND_GRAMMAR =0,
    GLRMASK_OPERAND_MODULE =1,
    GLRMASK_OPERAND_EXPR =2,
    GLRMASK_OPERAND_TOKEN_SET =4,
    GLRMASK_OPERAND_CONSTRAINT =5,
};

The kinds of expression part: grammar, module, constraint, expression, token set.

typedef int32_t glrmask_status;

Result of a fallible call. GLRMASK_OK means success; otherwise output handles are set to NULL and glrmask_error_message() describes the failure.

typedef struct glrmask_vocab glrmask_vocab;

Each token ID and its bytes.

typedef struct glrmask_token_set glrmask_token_set;

A set of token IDs.

typedef struct glrmask_grammar glrmask_grammar;
typedef struct glrmask_module glrmask_module;

A partially compiled grammar.

typedef struct glrmask_expr glrmask_expr;

A grammar expression.

typedef struct glrmask_constraint glrmask_constraint;
typedef struct glrmask_state glrmask_state;
typedef struct glrmask_bytes glrmask_bytes;
typedef struct glrmask_externs glrmask_externs;

A read-only collection of extern declarations.

uint32_t glrmask_abi_version(void);
const char * glrmask_version(void);

The GLRMask version, as a static string.

const char * glrmask_error_message(void);

The last error on this thread (empty string if none). Valid until the next GLRMask call on this thread, except this error-message accessor.

glrmask_status glrmask_compile_options_init(glrmask_compile_options *opts, size_t struct_size);

Initializes the caller-owned options buffer with defaults and its size in bytes.

glrmask_status glrmask_vocab_new(const glrmask_vocab_entry *entries, size_t n, glrmask_vocab **out);

Each token ID and its bytes. Hugging Face tokenizers don’t expose token bytes (#2719); converting them is outside GLRMask’s scope.

glrmask_status glrmask_vocab_clone(const glrmask_vocab *vocab, glrmask_vocab **out);
void glrmask_vocab_free(glrmask_vocab *vocab);
glrmask_status glrmask_token_set_new(const uint32_t *ids, size_t n, glrmask_token_set **out);
glrmask_status glrmask_token_set_from_ranges(const glrmask_token_range *ranges, size_t n, glrmask_token_set **out);
glrmask_status glrmask_token_set_clone(const glrmask_token_set *token_set, glrmask_token_set **out);
glrmask_status glrmask_token_set_equal(const glrmask_token_set *a, const glrmask_token_set *b, bool *out);
void glrmask_token_set_free(glrmask_token_set *token_set);
glrmask_status glrmask_grammar_from_glrm(const char *source, size_t n, glrmask_grammar **out);

Parses GLRM grammar source.

glrmask_status glrmask_grammar_from_lark(const char *source, size_t n, glrmask_grammar **out);

Parses Lark grammar source.

glrmask_status glrmask_grammar_from_json_schema(const char *source, size_t n, glrmask_grammar **out);

Parses JSON Schema source.

glrmask_status glrmask_grammar_from_expr(const glrmask_expr *expr, glrmask_grammar **out);

Builds a grammar from an Expr.

glrmask_status glrmask_grammar_clone(const glrmask_grammar *grammar, glrmask_grammar **out);
void glrmask_grammar_free(glrmask_grammar *grammar);
glrmask_status glrmask_grammar_externs(const glrmask_grammar *grammar, glrmask_externs **out);

All extern declarations.

glrmask_status glrmask_grammar_unbound_externs(const glrmask_grammar *grammar, glrmask_externs **out);

Only externs that still need bindings.

glrmask_status glrmask_grammar_compile(const glrmask_grammar *grammar, const glrmask_vocab *vocab, const glrmask_compile_options *options, glrmask_constraint **out);

Compiles the grammar into a constraint.

glrmask_status glrmask_module_new(const glrmask_grammar *grammar, glrmask_module **out);

Starts with the grammar and its unbound externs.

glrmask_status glrmask_module_clone(const glrmask_module *module, glrmask_module **out);
void glrmask_module_free(glrmask_module *module);
glrmask_status glrmask_module_bind_grammar(glrmask_module *module, const char *name, size_t name_len, const glrmask_grammar *value);

Binds an extern by name.

glrmask_status glrmask_module_bind_module(glrmask_module *module, const char *name, size_t name_len, const glrmask_module *value);

Binds an extern by name.

glrmask_status glrmask_module_bind_constraint(glrmask_module *module, const char *name, size_t name_len, const glrmask_constraint *value);

Binds an extern by name.

glrmask_status glrmask_module_bind_expr(glrmask_module *module, const char *name, size_t name_len, const glrmask_expr *value);

Binds an extern by name.

glrmask_status glrmask_module_bind_tokens(glrmask_module *module, const char *name, size_t name_len, const glrmask_token_set *value);

Binds an extern by name.

glrmask_status glrmask_module_precompile(glrmask_module *module, const glrmask_vocab *vocab, const glrmask_compile_options *options);

Compiles what it can now, even with externs unbound. Further compilation must use the same vocab.

glrmask_status glrmask_module_compile(const glrmask_module *module, const glrmask_vocab *vocab, const glrmask_compile_options *options, glrmask_constraint **out);

Finishes compiling the module into a constraint.

glrmask_status glrmask_module_externs(const glrmask_module *module, glrmask_externs **out);

All extern declarations, including bound externs.

glrmask_status glrmask_module_unbound_externs(const glrmask_module *module, glrmask_externs **out);

Only externs that still need bindings.

glrmask_status glrmask_module_save(const glrmask_module *module, glrmask_bytes **out);

Serializes the module, including any precompiled work.

glrmask_status glrmask_module_load(const uint8_t *bytes, size_t n, glrmask_module **out);

May fail if saved by a different GLRMask version.

glrmask_status glrmask_expr_choice(const glrmask_expr_operand *operands, size_t n, glrmask_expr **out);

Matches any one of operands.

glrmask_status glrmask_expr_sequence(const glrmask_expr_operand *operands, size_t n, glrmask_expr **out);

Matches operands in order.

glrmask_status glrmask_expr_literal(const uint8_t *bytes, size_t n, glrmask_expr **out);

Matches bytes exactly.

glrmask_status glrmask_expr_clone(const glrmask_expr *expr, glrmask_expr **out);
void glrmask_expr_free(glrmask_expr *expr);
glrmask_status glrmask_constraint_clone(const glrmask_constraint *constraint, glrmask_constraint **out);
void glrmask_constraint_free(glrmask_constraint *constraint);
glrmask_status glrmask_constraint_start(const glrmask_constraint *constraint, glrmask_state **out);

Starts a new sequence.

glrmask_status glrmask_constraint_mask_bit_len(const glrmask_constraint *constraint, size_t *out);

The mask’s length in bits (max token ID plus one).

glrmask_status glrmask_constraint_save(const glrmask_constraint *constraint, glrmask_bytes **out);

Serializes the compiled constraint for later loading.

glrmask_status glrmask_constraint_load(const uint8_t *bytes, size_t n, const glrmask_vocab *vocab, glrmask_constraint **out);

May fail if saved by a different GLRMask version. Fails if vocab differs from the one it was compiled with.

glrmask_status glrmask_state_clone(const glrmask_state *state, glrmask_state **out);
void glrmask_state_free(glrmask_state *state);
glrmask_status glrmask_state_mask_bit_len(const glrmask_state *state, size_t *out);

The mask’s length in bits (max token ID plus one).

glrmask_status glrmask_state_is_accepting(const glrmask_state *state, bool *out);

Whether the output so far could end here. Writes a boolean to out.

glrmask_status glrmask_state_fill_mask(const glrmask_state *state, uint32_t *out, size_t word_count);

Packed uint32 words: token i is bit i % 32 of word i / 32. Writes the words into out. Returns GLRMASK_BUFFER_TOO_SMALL, writing nothing, if word_count is below (N + 31) / 32, where N is the state mask bit length.

glrmask_status glrmask_state_is_terminated(const glrmask_state *state, bool *out);

Whether nothing more can be committed. Writes a boolean to out.

glrmask_status glrmask_state_commit_token(glrmask_state *state, uint32_t id, bool *allowed);

Advances the state by the token. If it isn’t allowed, writes false to allowed and terminates the state. Returns GLRMASK_OK on ordinary rejection; a non-OK status reports a failure.

glrmask_status glrmask_state_commit_bytes(glrmask_state *state, const uint8_t *bytes, size_t n, bool *allowed);

Advances the state by the bytes. If they aren’t allowed, writes false to allowed and terminates the state. Returns GLRMASK_OK on ordinary rejection; a non-OK status reports a failure.

glrmask_status glrmask_state_forced_bytes(const glrmask_state *state, glrmask_bytes **out);

The bytes that must come next.

glrmask_status glrmask_bytes_data(const glrmask_bytes *bytes, const uint8_t **out_data, size_t *out_len);

Points *out_data at the contents, valid until the buffer is freed.

void glrmask_bytes_free(glrmask_bytes *bytes);
glrmask_status glrmask_externs_len(const glrmask_externs *externs, size_t *out);
glrmask_status glrmask_externs_name(const glrmask_externs *externs, size_t index, const char **out, size_t *n);

Returns the name of the extern at index; the name stays valid until the view is freed.

glrmask_status glrmask_externs_kind(const glrmask_externs *externs, size_t index, uint32_t *out);
void glrmask_externs_free(glrmask_externs *externs);

Documentation source