MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / llama_grammar_validate

Function llama_grammar_validate

smallthinker/tests/test-gbnf-validator.cpp:11–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9#include <vector>
10
11static bool llama_grammar_validate(struct llama_grammar * grammar, const std::string & input_str, size_t & error_pos, std::string & error_msg) {
12 const auto cpts = unicode_cpts_from_utf8(input_str);
13
14 auto & stacks_cur = llama_grammar_get_stacks(grammar);
15
16 size_t pos = 0;
17 for (const auto & cpt : cpts) {
18 llama_grammar_accept(grammar, cpt);
19
20 if (stacks_cur.empty()) {
21 error_pos = pos;
22 error_msg = "Unexpected character '" + unicode_cpt_to_utf8(cpt) + "'";
23 return false;
24 }
25 ++pos;
26 }
27
28 for (const auto & stack : stacks_cur) {
29 if (stack.empty()) {
30 return true;
31 }
32 }
33
34 error_pos = pos;
35 error_msg = "Unexpected end of input";
36 return false;
37}
38
39static void print_error_message(const std::string & input_str, size_t error_pos, const std::string & error_msg) {
40 fprintf(stdout, "Input string is invalid according to the grammar.\n");

Callers 1

mainFunction · 0.85

Calls 4

unicode_cpts_from_utf8Function · 0.85
unicode_cpt_to_utf8Function · 0.85
llama_grammar_acceptFunction · 0.50
emptyMethod · 0.45

Tested by

no test coverage detected