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

Show on the map →

Called by load_bundled

Calls read_file_contents, validate

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
lines 57–57std::string content = read_file_contents(path);lines 58–58◇ if (content.empty())lines 59–59logger->warn("Empty or unreadable grammar file: {}", path.string());lines 60–60return std::nullopt;lines 62–62std::string error = GrammarRegistry::validate(content);lines 63–63GrammarEntry ge;lines 64–64ge.key = path.stem().string();lines 65–65ge.gbnf_content = std::move(content);lines 66–66ge.source = "bundled";lines 67–67ge.validated = error.empty();lines 68–68ge.error = std::move(error);lines 69–69◇ if (!ge.validated)lines 70–71logger->warn("Bundled grammar '{}' has validation errors: {}", ge.key, ge.error);lines 73–73return ge;

Requirements

REQ-INFER-007

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

Show on the map →

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
lines 86–86◇ if (!std::filesystem::is_directory(grammar_dir))lines 87–88logger->warn("Grammar directory not found: {}", grammar_dir.string());lines 89–89return 0;lines 92–92size_t count = 0;lines 93–93◇ for (const auto& entry : std::filesystem::directory_iterator(grammar_dir))lines 94–94◇ if (entry.path().extension() != ".gbnf")lines 95–95continue;lines 97–97auto ge = build_grammar_entry(entry.path());lines 98–98◇ if (!ge)lines 99–99continue;lines 101–101std::string key = ge->key;lines 102–102std::lock_guard<std::mutex> lock(registry_mutex_);lines 103–103grammars_[key] = std::move(*ge);lines 104–104++count;lines 107–107logger->info("Loaded {} bundled grammar(s)", count);lines 108–108return count;

Requirements

REQ-INFER-007

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

Show on the map →

Called by SCENARIO

Calls read_file_contents, register_grammar

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
lines 160–160std::string content = read_file_contents(path);lines 161–161◇ if (content.empty())lines 162–162logger->error("Cannot read grammar file: {}", path.string());lines 163–163return false;lines 166–167std::string resolved_key = key.empty() ? path.stem().string() : key;lines 168–168return register_grammar(resolved_key, content, "file");

Requirements

REQ-INFER-007

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

Show on the map →

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
lines 127–127std::lock_guard<std::mutex> lock(registry_mutex_);lines 128–128◇ if (grammars_.count(key) > 0)lines 129–129logger->warn("Grammar key '{}' already registered", key);lines 130–130return false;lines 133–133std::string error = validate(gbnf_content);lines 134–134GrammarEntry ge;lines 135–135ge.key = key;lines 136–136ge.gbnf_content = gbnf_content;lines 137–137ge.source = source;lines 138–138ge.validated = error.empty();lines 139–139ge.error = std::move(error);lines 141–141grammars_[key] = std::move(ge);lines 142–143logger->info("Registered grammar '{}' (source={}, valid={})", key, source, grammars_[key].validated);lines 144–144return true;

Requirements

REQ-INFER-007

validate

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

Validate a GBNF grammar string.

src/inference/grammar_registry.cpp:263

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 "";
}
Activity · 7 steps
lines 264–264◇ if (gbnf_content.empty())lines 265–265return "empty grammar string";lines 271–272llama_sampler* sampler = llama_sampler_init_grammar( nullptr, gbnf_content.c_str(), "root");lines 274–274◇ if (sampler == nullptr)lines 275–275return "GBNF parse failed";lines 278–278llama_sampler_free(sampler);lines 279–279return "";

Requirements

REQ-INFER-007