@@ -199,7 +199,8 @@ namespace grammar_parser {
199
199
sub_rule.push_back ({LLAMA_GRETYPE_RULE_REF, star_rule_id});
200
200
} else {
201
201
uint32_t last_rec_rule_id = 0 ;
202
- for (int i = 0 , n = max_times - min_times; i < n; i++) {
202
+ auto n_opt = max_times - min_times;
203
+ for (int i = 0 ; i < n_opt; i++) {
203
204
uint32_t rec_rule_id = generate_symbol_id (state, rule_name + " _" + std::to_string (i + 1 ));
204
205
if (i == 0 ) {
205
206
add_rule (state, rec_rule_id, {
@@ -217,7 +218,9 @@ namespace grammar_parser {
217
218
}
218
219
last_rec_rule_id = rec_rule_id;
219
220
}
220
- sub_rule.push_back ({LLAMA_GRETYPE_RULE_REF, last_rec_rule_id});
221
+ if (n_opt > 0 ) {
222
+ sub_rule.push_back ({LLAMA_GRETYPE_RULE_REF, last_rec_rule_id});
223
+ }
221
224
}
222
225
sub_rule.push_back ({LLAMA_GRETYPE_END, 0 });
223
226
add_rule (state, sub_rule_id, sub_rule);
@@ -291,24 +294,34 @@ namespace grammar_parser {
291
294
pos = parse_space (pos + 1 , is_nested);
292
295
size_t min_times = 0 ;
293
296
int max_times = -1 ;
297
+
294
298
if (is_digit_char (*pos)) {
295
299
const char * int_end = parse_int (pos);
296
300
min_times = std::stoul (std::string (pos, int_end - pos));
297
301
pos = parse_space (int_end, is_nested);
302
+ } else if (*pos != ' ,' ) {
303
+ throw std::runtime_error (std::string (" expecting an int or ',' at " ) + pos);
298
304
}
299
- if (*pos != ' ,' ) {
305
+
306
+ if (*pos == ' }' ) {
307
+ max_times = min_times;
308
+ pos = parse_space (pos + 1 , is_nested);
309
+ } else if (*pos == ' ,' ) {
310
+ pos = parse_space (pos + 1 , is_nested);
311
+
312
+ if (is_digit_char (*pos)) {
313
+ const char * int_end = parse_int (pos);
314
+ max_times = std::stoul (std::string (pos, int_end - pos));
315
+ pos = parse_space (int_end, is_nested);
316
+ }
317
+
318
+ if (*pos != ' }' ) {
319
+ throw std::runtime_error (std::string (" expecting '}' at " ) + pos);
320
+ }
321
+ pos = parse_space (pos + 1 , is_nested);
322
+ } else {
300
323
throw std::runtime_error (std::string (" expecting ',' at " ) + pos);
301
324
}
302
- pos = parse_space (pos + 1 , is_nested);
303
- if (is_digit_char (*pos)) {
304
- const char * int_end = parse_int (pos);
305
- max_times = std::stoul (std::string (pos, int_end - pos));
306
- pos = parse_space (int_end, is_nested);
307
- }
308
- if (*pos != ' }' ) {
309
- throw std::runtime_error (std::string (" expecting '}' at " ) + pos);
310
- }
311
- pos = parse_space (pos + 1 , is_nested);
312
325
handle_repetitions (min_times, max_times);
313
326
} else {
314
327
break ;
0 commit comments