| 8 | #include <cassert> |
| 9 | |
| 10 | int main() |
| 11 | { |
| 12 | grammar_parser::parse_state parsed_grammar; |
| 13 | |
| 14 | const char *grammar_bytes = R"""(root ::= (expr "=" term "\n")+ |
| 15 | expr ::= term ([-+*/] term)* |
| 16 | term ::= [0-9]+)"""; |
| 17 | |
| 18 | parsed_grammar = grammar_parser::parse(grammar_bytes); |
| 19 | |
| 20 | std::vector<std::pair<std::string, uint32_t>> expected = { |
| 21 | {"expr", 2}, |
| 22 | {"expr_5", 5}, |
| 23 | {"expr_6", 6}, |
| 24 | {"root", 0}, |
| 25 | {"root_1", 1}, |
| 26 | {"root_4", 4}, |
| 27 | {"term", 3}, |
| 28 | {"term_7", 7}, |
| 29 | }; |
| 30 | |
| 31 | uint32_t index = 0; |
| 32 | for (auto it = parsed_grammar.symbol_ids.begin(); it != parsed_grammar.symbol_ids.end(); ++it) |
| 33 | { |
| 34 | std::string key = it->first; |
| 35 | uint32_t value = it->second; |
| 36 | std::pair<std::string, uint32_t> expected_pair = expected[index]; |
| 37 | |
| 38 | // pretty print error message before asserting |
| 39 | if (expected_pair.first != key || expected_pair.second != value) |
| 40 | { |
| 41 | fprintf(stderr, "expected_pair: %s, %d\n", expected_pair.first.c_str(), expected_pair.second); |
| 42 | fprintf(stderr, "actual_pair: %s, %d\n", key.c_str(), value); |
| 43 | fprintf(stderr, "expected_pair != actual_pair\n"); |
| 44 | } |
| 45 | |
| 46 | assert(expected_pair.first == key && expected_pair.second == value); |
| 47 | |
| 48 | index++; |
| 49 | } |
| 50 | std::vector<llama_grammar_element> expected_rules = { |
| 51 | {LLAMA_GRETYPE_RULE_REF, 4}, |
| 52 | {LLAMA_GRETYPE_END, 0}, |
| 53 | {LLAMA_GRETYPE_RULE_REF, 2}, |
| 54 | {LLAMA_GRETYPE_CHAR, 61}, |
| 55 | {LLAMA_GRETYPE_RULE_REF, 3}, |
| 56 | {LLAMA_GRETYPE_CHAR, 10}, |
| 57 | {LLAMA_GRETYPE_END, 0}, |
| 58 | {LLAMA_GRETYPE_RULE_REF, 3}, |
| 59 | {LLAMA_GRETYPE_RULE_REF, 6}, |
| 60 | {LLAMA_GRETYPE_END, 0}, |
| 61 | {LLAMA_GRETYPE_RULE_REF, 7}, |
| 62 | {LLAMA_GRETYPE_END, 0}, |
| 63 | {LLAMA_GRETYPE_RULE_REF, 1}, |
| 64 | {LLAMA_GRETYPE_RULE_REF, 4}, |
| 65 | {LLAMA_GRETYPE_ALT, 0}, |
| 66 | {LLAMA_GRETYPE_RULE_REF, 1}, |
| 67 | {LLAMA_GRETYPE_END, 0}, |