grammars: handle x{n}
and fix x{n,n}
This commit is contained in:
parent
01604690c1
commit
f2030e3210
1 changed files with 26 additions and 13 deletions
|
@ -199,7 +199,8 @@ namespace grammar_parser {
|
||||||
sub_rule.push_back({LLAMA_GRETYPE_RULE_REF, star_rule_id});
|
sub_rule.push_back({LLAMA_GRETYPE_RULE_REF, star_rule_id});
|
||||||
} else {
|
} else {
|
||||||
uint32_t last_rec_rule_id = 0;
|
uint32_t last_rec_rule_id = 0;
|
||||||
for (int i = 0, n = max_times - min_times; i < n; i++) {
|
auto n_opt = max_times - min_times;
|
||||||
|
for (int i = 0; i < n_opt; i++) {
|
||||||
uint32_t rec_rule_id = generate_symbol_id(state, rule_name + "_" + std::to_string(i + 1));
|
uint32_t rec_rule_id = generate_symbol_id(state, rule_name + "_" + std::to_string(i + 1));
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
add_rule(state, rec_rule_id, {
|
add_rule(state, rec_rule_id, {
|
||||||
|
@ -217,8 +218,10 @@ namespace grammar_parser {
|
||||||
}
|
}
|
||||||
last_rec_rule_id = rec_rule_id;
|
last_rec_rule_id = rec_rule_id;
|
||||||
}
|
}
|
||||||
|
if (n_opt > 0) {
|
||||||
sub_rule.push_back({LLAMA_GRETYPE_RULE_REF, last_rec_rule_id});
|
sub_rule.push_back({LLAMA_GRETYPE_RULE_REF, last_rec_rule_id});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
sub_rule.push_back({LLAMA_GRETYPE_END, 0});
|
sub_rule.push_back({LLAMA_GRETYPE_END, 0});
|
||||||
add_rule(state, sub_rule_id, sub_rule);
|
add_rule(state, sub_rule_id, sub_rule);
|
||||||
|
|
||||||
|
@ -291,24 +294,34 @@ namespace grammar_parser {
|
||||||
pos = parse_space(pos + 1, is_nested);
|
pos = parse_space(pos + 1, is_nested);
|
||||||
size_t min_times = 0;
|
size_t min_times = 0;
|
||||||
int max_times = -1;
|
int max_times = -1;
|
||||||
|
|
||||||
if (is_digit_char(*pos)) {
|
if (is_digit_char(*pos)) {
|
||||||
const char * int_end = parse_int(pos);
|
const char * int_end = parse_int(pos);
|
||||||
min_times = std::stoul(std::string(pos, int_end - pos));
|
min_times = std::stoul(std::string(pos, int_end - pos));
|
||||||
pos = parse_space(int_end, is_nested);
|
pos = parse_space(int_end, is_nested);
|
||||||
|
} else if (*pos != ',') {
|
||||||
|
throw std::runtime_error(std::string("expecting an int or ',' at ") + pos);
|
||||||
}
|
}
|
||||||
if (*pos != ',') {
|
|
||||||
throw std::runtime_error(std::string("expecting ',' at ") + pos);
|
if (*pos == '}') {
|
||||||
}
|
max_times = min_times;
|
||||||
pos = parse_space(pos + 1, is_nested);
|
pos = parse_space(pos + 1, is_nested);
|
||||||
|
} else if (*pos == ',') {
|
||||||
|
pos = parse_space(pos + 1, is_nested);
|
||||||
|
|
||||||
if (is_digit_char(*pos)) {
|
if (is_digit_char(*pos)) {
|
||||||
const char * int_end = parse_int(pos);
|
const char * int_end = parse_int(pos);
|
||||||
max_times = std::stoul(std::string(pos, int_end - pos));
|
max_times = std::stoul(std::string(pos, int_end - pos));
|
||||||
pos = parse_space(int_end, is_nested);
|
pos = parse_space(int_end, is_nested);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*pos != '}') {
|
if (*pos != '}') {
|
||||||
throw std::runtime_error(std::string("expecting '}' at ") + pos);
|
throw std::runtime_error(std::string("expecting '}' at ") + pos);
|
||||||
}
|
}
|
||||||
pos = parse_space(pos + 1, is_nested);
|
pos = parse_space(pos + 1, is_nested);
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error(std::string("expecting ',' at ") + pos);
|
||||||
|
}
|
||||||
handle_repetitions(min_times, max_times);
|
handle_repetitions(min_times, max_times);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue