diff options
author | Clint Herron <hanclinto@gmail.com> | 2024-03-10 11:17:43 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-10 17:17:43 +0200 |
commit | 2960eae847f8dbde23be6d170a61bcf44ebf32de (patch) | |
tree | 02457a27a273f795b549adb6f1c739dfa7e12bd1 | |
parent | c78541479cf835dd9eb568ccd9a2083198a7203d (diff) |
grammar : verify parsed state (#5950)
-rw-r--r-- | common/grammar-parser.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/common/grammar-parser.cpp b/common/grammar-parser.cpp index bf89a96f..2a130156 100644 --- a/common/grammar-parser.cpp +++ b/common/grammar-parser.cpp @@ -278,6 +278,22 @@ namespace grammar_parser { while (*pos) { pos = parse_rule(state, pos); } + // Validate the state to ensure that all rules are defined + for (const auto & rule : state.rules) { + for (const auto & elem : rule) { + if (elem.type == LLAMA_GRETYPE_RULE_REF) { + // Ensure that the rule at that location exists + if (elem.value >= state.rules.size() || state.rules[elem.value].empty()) { + // Get the name of the rule that is missing + for (const auto & kv : state.symbol_ids) { + if (kv.second == elem.value) { + throw std::runtime_error("Undefined rule identifier '" + kv.first + "'"); + } + } + } + } + } + } return state; } catch (const std::exception & err) { fprintf(stderr, "%s: error parsing grammar: %s\n", __func__, err.what()); |