| 9 | #include <vector> |
| 10 | |
| 11 | static 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 | |
| 39 | static 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"); |
no test coverage detected