include/entropic/inference/grammar_registry.h

← API

include/entropic/inference/grammar_registry.h

entropic::GrammarRegistry

Centralized grammar registry for named GBNF grammars.

size_t entropic::GrammarRegistry::load_bundled(const std::filesystem::path &grammar_dir)

Load all bundled grammars from a directory.

Load all bundled .gbnf grammars from a directory. Scans for *.gbnf files. Each file's stem becomes the key (e.g., "compactor.gbnf" -> key "compactor"). Files are read, validated, and registered. Invalid files are logged as WARNING and still registered with error metadata.

Parameters
  • grammar_dir — Path to data/grammars/ directory.
  • dir — Directory to scan.
Returns
  • Number of grammars loaded.
  • Number of grammars loaded; each file's stem becomes its key.

bool entropic::GrammarRegistry::register_grammar(const std::string &key, const std::string &gbnf_content, const std::string &source="runtime")

Register a grammar by key with GBNF content string.

The grammar is validated on registration. If validation fails, the grammar is still registered but entry.validated is false and entry.error is set.

Parameters
  • key — Unique grammar name.
  • gbnf_content — Raw GBNF grammar string.
  • source — Origin tag ("runtime", "dynamic", "file").
  • source — Origin tag.
Returns
  • true on success. false if key already exists.
  • true on success; false when the key is already registered. An invalid GBNF is still registered, flagged via GrammarEntry::validated/error rather than silently dropped.

bool entropic::GrammarRegistry::register_from_file(const std::string &key, const std::filesystem::path &path)

Register a grammar from a file path.

Parameters
  • key — Unique grammar name (if empty, uses filename stem).
  • path — Path to .gbnf file.
Returns
  • true on success. false if file unreadable or key exists.
  • true on success; false when the file is unreadable or the key is already registered. An empty key takes the file stem.

bool entropic::GrammarRegistry::deregister(const std::string &key)

Remove a grammar from the registry.

Bundled grammars can be deregistered (allows overriding defaults).

Parameters
  • key — Grammar name to remove.
Returns
true if removed. false if key not found.

std::string entropic::GrammarRegistry::get(const std::string &key) const

Get GBNF content string for a grammar key.

Returns content regardless of validation status.

Parameters
  • key — Grammar name.
Returns
GBNF content string, or empty string if not found.

bool entropic::GrammarRegistry::has(const std::string &key) const

Check if a grammar key exists in the registry.

Parameters
  • key — Grammar name.
Returns
true if registered.

GrammarEntry entropic::GrammarRegistry::entry(const std::string &key) const

Get full entry metadata for a grammar key.

Parameters
  • key — Grammar name.
Returns
GrammarEntry, or entry with empty key if not found.

std::vector< GrammarEntry > entropic::GrammarRegistry::list() const

List all registered grammar keys.

Returns
  • Vector of GrammarEntry metadata (content omitted for efficiency).
  • Vector of GrammarEntry metadata (content omitted).

std::string entropic::GrammarRegistry::validate(const std::string &gbnf_content)

Validate a GBNF grammar string.

Parses the grammar using llama_sampler_init_grammar() with a throwaway sampler. Does not require a loaded model. This is a static utility — can be called without registering the grammar.

Parameters
  • gbnf_content — Raw GBNF string to validate.
Returns
  • Empty string on success, error description on failure.
  • Empty string on success; an error description otherwise. Validation runs through a throwaway llama sampler, so a malformed grammar is caught at registration, not at decode.

size_t entropic::GrammarRegistry::size() const

Number of registered grammars.

Returns
Count of registered grammars.

void entropic::GrammarRegistry::clear()

Remove all registered grammars.