grammar_registry.cpp¶
src/inference/grammar_registry.cpp
@file grammar_registry.cpp @brief GrammarRegistry implementation — named grammar management.
Owners: @tvanfossen
build_grammar_entry
static std::optional< GrammarEntry > entropic::build_grammar_entry(const std::filesystem::path &path)
Load all bundled grammars from a directory.
src/inference/grammar_registry.cpp:55
Source · 20 lines
static std::optional<GrammarEntry> build_grammar_entry(
const std::filesystem::path& path) {
std::string content = read_file_contents(path);
if (content.empty()) {
logger->warn("Empty or unreadable grammar file: {}", path.string());
return std::nullopt;
}
std::string error = GrammarRegistry::validate(content);
GrammarEntry ge;
ge.key = path.stem().string();
ge.gbnf_content = std::move(content);
ge.source = "bundled";
ge.validated = error.empty();
ge.error = std::move(error);
if (!ge.validated) {
logger->warn("Bundled grammar '{}' has validation errors: {}",
ge.key, ge.error);
}
return ge;
}Activity · 14 steps
Requirements
load_bundled
size_t entropic::GrammarRegistry::load_bundled(const std::filesystem::path &grammar_dir)
Load all bundled grammars from a directory.
src/inference/grammar_registry.cpp:83
Called by SCENARIO, load_bundled_grammars, load_grammars_from
Calls build_grammar_entry, entry
Source · 27 lines
size_t GrammarRegistry::load_bundled(
const std::filesystem::path& grammar_dir)
{
if (!std::filesystem::is_directory(grammar_dir)) {
logger->warn("Grammar directory not found: {}",
grammar_dir.string());
return 0;
}
size_t count = 0;
for (const auto& entry : std::filesystem::directory_iterator(grammar_dir)) {
if (entry.path().extension() != ".gbnf") {
continue;
}
auto ge = build_grammar_entry(entry.path());
if (!ge) {
continue;
}
std::string key = ge->key;
std::lock_guard<std::mutex> lock(registry_mutex_);
grammars_[key] = std::move(*ge);
++count;
}
logger->info("Loaded {} bundled grammar(s)", count);
return count;
}Activity · 16 steps
Requirements
register_from_file
bool entropic::GrammarRegistry::register_from_file(const std::string &key, const std::filesystem::path &path)
Register a grammar from a file path.
src/inference/grammar_registry.cpp:156
Source · 14 lines
bool GrammarRegistry::register_from_file(
const std::string& key,
const std::filesystem::path& path)
{
std::string content = read_file_contents(path);
if (content.empty()) {
logger->error("Cannot read grammar file: {}", path.string());
return false;
}
std::string resolved_key = key.empty()
? path.stem().string() : key;
return register_grammar(resolved_key, content, "file");
}Activity · 6 steps
Requirements
register_grammar
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.
src/inference/grammar_registry.cpp:122
Called by SCENARIO, SCENARIO, SCENARIO, TEST_CASE, register_from_file
Calls validate
Source · 24 lines
bool GrammarRegistry::register_grammar(
const std::string& key,
const std::string& gbnf_content,
const std::string& source)
{
std::lock_guard<std::mutex> lock(registry_mutex_);
if (grammars_.count(key) > 0) {
logger->warn("Grammar key '{}' already registered", key);
return false;
}
std::string error = validate(gbnf_content);
GrammarEntry ge;
ge.key = key;
ge.gbnf_content = gbnf_content;
ge.source = source;
ge.validated = error.empty();
ge.error = std::move(error);
grammars_[key] = std::move(ge);
logger->info("Registered grammar '{}' (source={}, valid={})",
key, source, grammars_[key].validated);
return true;
}Activity · 14 steps
Requirements
validate
std::string entropic::GrammarRegistry::validate(const std::string &gbnf_content)
Validate a GBNF grammar string.
src/inference/grammar_registry.cpp:263
Called by SCENARIO, build_grammar_entry, entropic_grammar_validate, register_grammar
Calls none
Source · 18 lines
std::string GrammarRegistry::validate(const std::string& gbnf_content) {
if (gbnf_content.empty()) {
return "empty grammar string";
}
// Use llama_sampler_init_grammar with nullptr vocab for validation.
// Grammar parsing is independent of vocabulary — the GBNF parser
// only needs the grammar string. Returns nullptr on parse failure.
llama_sampler* sampler = llama_sampler_init_grammar(
nullptr, gbnf_content.c_str(), "root");
if (sampler == nullptr) {
return "GBNF parse failed";
}
llama_sampler_free(sampler);
return "";
}