| 85 | } |
| 86 | |
| 87 | static bool match_string(const std::string & input, llama_grammar * grammar) { |
| 88 | const auto parsed = parse_tokens(input); |
| 89 | |
| 90 | auto & stacks_cur = llama_grammar_get_stacks(grammar); |
| 91 | |
| 92 | for (const auto & in : parsed) { |
| 93 | try { |
| 94 | llama_grammar_accept_token(*grammar, in.token, in.piece); |
| 95 | } catch (const std::runtime_error & /*e*/) { |
| 96 | // normally this shouldn't get hit because of llama_grammar_apply |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (stacks_cur.empty()) { |
| 101 | // no stacks means that the grammar failed to match at this point |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | for (const auto & stack : stacks_cur) { |
| 107 | if (stack.empty()) { |
| 108 | // An empty stack means that the grammar has been completed |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | 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) { |
| 117 | fprintf(stderr, "⚫ Testing %s\n%s\n", test_desc.c_str(), grammar_str.c_str()); |
no test coverage detected