Skip to content

Commit 9a3acbb

Browse files
committed
Added support for . (any characer) token in grammar engine.
1 parent 4399f13 commit 9a3acbb

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

common/grammar-parser.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ namespace grammar_parser {
188188
throw std::runtime_error(std::string("expecting ')' at ") + pos);
189189
}
190190
pos = parse_space(pos + 1, is_nested);
191+
} else if (*pos == '.') { // any char
192+
last_sym_start = out_elements.size();
193+
out_elements.push_back({LLAMA_GRETYPE_CHAR_ANY, 0});
194+
pos = parse_space(pos + 1, is_nested);
191195
} else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
192196
if (last_sym_start == out_elements.size()) {
193197
throw std::runtime_error(std::string("expecting preceding item to */+/? at ") + pos);
@@ -316,6 +320,7 @@ namespace grammar_parser {
316320
case LLAMA_GRETYPE_CHAR_NOT: return true;
317321
case LLAMA_GRETYPE_CHAR_ALT: return true;
318322
case LLAMA_GRETYPE_CHAR_RNG_UPPER: return true;
323+
case LLAMA_GRETYPE_CHAR_ANY: return true;
319324
default: return false;
320325
}
321326
}
@@ -330,6 +335,7 @@ namespace grammar_parser {
330335
case LLAMA_GRETYPE_CHAR_NOT: fprintf(file, "CHAR_NOT"); break;
331336
case LLAMA_GRETYPE_CHAR_RNG_UPPER: fprintf(file, "CHAR_RNG_UPPER"); break;
332337
case LLAMA_GRETYPE_CHAR_ALT: fprintf(file, "CHAR_ALT"); break;
338+
case LLAMA_GRETYPE_CHAR_ANY: fprintf(file, "CHAR_ANY"); break;
333339
}
334340
switch (elem.type) {
335341
case LLAMA_GRETYPE_END:
@@ -341,6 +347,7 @@ namespace grammar_parser {
341347
case LLAMA_GRETYPE_CHAR_NOT:
342348
case LLAMA_GRETYPE_CHAR_RNG_UPPER:
343349
case LLAMA_GRETYPE_CHAR_ALT:
350+
case LLAMA_GRETYPE_CHAR_ANY:
344351
fprintf(file, "(\"");
345352
print_grammar_char(file, elem.value);
346353
fprintf(file, "\") ");
@@ -398,11 +405,15 @@ namespace grammar_parser {
398405
}
399406
print_grammar_char(file, elem.value);
400407
break;
408+
case LLAMA_GRETYPE_CHAR_ANY:
409+
fprintf(file, ".");
410+
break;
401411
}
402412
if (is_char_element(elem)) {
403413
switch (rule[i + 1].type) {
404414
case LLAMA_GRETYPE_CHAR_ALT:
405415
case LLAMA_GRETYPE_CHAR_RNG_UPPER:
416+
case LLAMA_GRETYPE_CHAR_ANY:
406417
break;
407418
default:
408419
fprintf(file, "] ");

llama.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -11716,7 +11716,7 @@ static std::pair<bool, const llama_grammar_element *> llama_grammar_match_char(
1171611716
const uint32_t chr) {
1171711717

1171811718
bool found = false;
11719-
bool is_positive_char = pos->type == LLAMA_GRETYPE_CHAR;
11719+
bool is_positive_char = pos->type == LLAMA_GRETYPE_CHAR || pos->type == LLAMA_GRETYPE_CHAR_ANY;
1172011720

1172111721
GGML_ASSERT(is_positive_char || pos->type == LLAMA_GRETYPE_CHAR_NOT); // NOLINT
1172211722

@@ -11725,6 +11725,10 @@ static std::pair<bool, const llama_grammar_element *> llama_grammar_match_char(
1172511725
// inclusive range, e.g. [a-z]
1172611726
found = found || (pos->value <= chr && chr <= pos[1].value);
1172711727
pos += 2;
11728+
} else if (pos->type == LLAMA_GRETYPE_CHAR_ANY) {
11729+
// Any character matches "."
11730+
found = true;
11731+
pos += 1;
1172811732
} else {
1172911733
// exact char match, e.g. [a] or "a"
1173011734
found = found || pos->value == chr;
@@ -11742,7 +11746,7 @@ static bool llama_grammar_match_partial_char(
1174211746
const llama_grammar_element * pos,
1174311747
const llama_partial_utf8 partial_utf8) {
1174411748

11745-
bool is_positive_char = pos->type == LLAMA_GRETYPE_CHAR;
11749+
bool is_positive_char = pos->type == LLAMA_GRETYPE_CHAR || pos->type == LLAMA_GRETYPE_CHAR_ANY;
1174611750
GGML_ASSERT(is_positive_char || pos->type == LLAMA_GRETYPE_CHAR_NOT);
1174711751

1174811752
uint32_t partial_value = partial_utf8.value;
@@ -11772,6 +11776,9 @@ static bool llama_grammar_match_partial_char(
1177211776
return is_positive_char;
1177311777
}
1177411778
pos += 2;
11779+
} else if (pos->type == LLAMA_GRETYPE_CHAR_ANY) {
11780+
// Any character matches "."
11781+
return true;
1177511782
} else {
1177611783
// exact char match, e.g. [a] or "a"
1177711784
if (low <= pos->value && pos->value <= high) {
@@ -11830,6 +11837,7 @@ static void llama_grammar_advance_stack(
1183011837
}
1183111838
case LLAMA_GRETYPE_CHAR:
1183211839
case LLAMA_GRETYPE_CHAR_NOT:
11840+
case LLAMA_GRETYPE_CHAR_ANY:
1183311841
new_stacks.emplace_back(stack);
1183411842
break;
1183511843
default:

llama.h

+3
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ extern "C" {
315315
// modifies a preceding LLAMA_GRETYPE_CHAR or
316316
// LLAMA_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
317317
LLAMA_GRETYPE_CHAR_ALT = 6,
318+
319+
// any character (.)
320+
LLAMA_GRETYPE_CHAR_ANY = 7,
318321
};
319322

320323
typedef struct llama_grammar_element {

0 commit comments

Comments
 (0)