diff --git a/common/grammar-parser.cpp b/common/grammar-parser.cpp index fde9d6981..a2710a217 100644 --- a/common/grammar-parser.cpp +++ b/common/grammar-parser.cpp @@ -187,7 +187,7 @@ namespace grammar_parser { uint32_t sub_rule_id = generate_symbol_id(state, rule_name); std::vector sub_rule; - for (size_t i = 0; i < min_times; i++) { + for (int i = 0; i < min_times; i++) { sub_rule.push_back({LLAMA_GRETYPE_RULE_REF, content_rule_id}); } if (max_times < 0) { @@ -294,16 +294,15 @@ namespace grammar_parser { handle_repetitions(0, 1); } else if (*pos == '{') { pos = parse_space(pos + 1, is_nested); - size_t min_times = 0; - int max_times = -1; - if (is_digit_char(*pos)) { - const char * int_end = parse_int(pos); - min_times = std::stoul(std::string(pos, int_end - pos)); - pos = parse_space(int_end, is_nested); - } else if (*pos != ',') { + if (!is_digit_char(*pos)) { throw std::runtime_error(std::string("expecting an int or ',' at ") + pos); } + const char * int_end = parse_int(pos); + int min_times = std::stoul(std::string(pos, int_end - pos)); + pos = parse_space(int_end, is_nested); + + int max_times = -1; if (*pos == '}') { max_times = min_times; diff --git a/grammars/README.md b/grammars/README.md index e9adc5a26..03cbff0bd 100644 --- a/grammars/README.md +++ b/grammars/README.md @@ -64,8 +64,8 @@ Parentheses `()` can be used to group sequences, which allows for embedding alte - `?` makes the preceding symbol or sequence optional (equivalent to `{0,1}`). - `{m}` repeats the precedent symbol or sequence exactly `m` times - `{m,}` repeats the precedent symbol or sequence at least `m` times -- `{m,n}` repeats the precedent symbol or sequence at betwen `m` and `n` times (included) -- `{,n}` repeats the precedent symbol or sequence at most `n` times (included) +- `{m,n}` repeats the precedent symbol or sequence at between `m` and `n` times (included) +- `{0,n}` repeats the precedent symbol or sequence at most `n` times (included) ## Comments and newlines diff --git a/tests/test-grammar-parser.cpp b/tests/test-grammar-parser.cpp index a8fbe64e7..545fa95ec 100644 --- a/tests/test-grammar-parser.cpp +++ b/tests/test-grammar-parser.cpp @@ -69,6 +69,12 @@ static void verify_parsing(const char *grammar_bytes, const std::vectorfirst; @@ -118,6 +124,12 @@ static void verify_parsing(const char *grammar_bytes, const std::vector