| 33 | } |
| 34 | |
| 35 | static bool match_string(const std::string & input, llama_grammar * grammar) { |
| 36 | const auto cpts = unicode_cpts_from_utf8(input); |
| 37 | |
| 38 | auto & stacks_cur = llama_grammar_get_stacks(grammar); |
| 39 | |
| 40 | for (const auto & cpt : cpts) { |
| 41 | llama_grammar_accept(grammar, cpt); |
| 42 | |
| 43 | if (stacks_cur.empty()) { |
| 44 | // no stacks means that the grammar failed to match at this point |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | for (const auto & stack : stacks_cur) { |
| 50 | if (stack.empty()) { |
| 51 | // An empty stack means that the grammar has been completed |
| 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | static void test(const std::string & test_desc, const std::string & grammar_str, const std::vector<std::string> & passing_strings, const std::vector<std::string> & failing_strings) { |
| 60 | fprintf(stderr, "⚫ Testing %s\n%s\n", test_desc.c_str(), grammar_str.c_str()); |
no test coverage detected