Skip to content

Commit 45d2252

Browse files
authored
Backported . (any chat) from llama.cpp
1 parent 4bf3b43 commit 45d2252

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

llama_cpp/llama_grammar.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,12 @@ def end(self) -> "std.map[T, U].iterator[T, U]":
432432
# // be an inclusive range ([a-z])
433433
# LLAMA_GRETYPE_CHAR_RNG_UPPER = 5,
434434

435-
436435
# // modifies a preceding LLAMA_GRETYPE_CHAR or
437436
# // LLAMA_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
438437
# LLAMA_GRETYPE_CHAR_ALT = 6,
438+
439+
# // any character (.)
440+
# LLAMA_GRETYPE_CHAR_ANY = 7,
439441
# };
440442
class llama_gretype(Enum):
441443
"""grammar element type"""
@@ -447,6 +449,7 @@ class llama_gretype(Enum):
447449
LLAMA_GRETYPE_CHAR_NOT = 4 # inverse char(s) ([^a], [^a-b] [^abc])
448450
LLAMA_GRETYPE_CHAR_RNG_UPPER = 5 # modifies a preceding LLAMA_GRETYPE_CHAR or LLAMA_GRETYPE_CHAR_ALT to be an inclusive range ([a-z])
449451
LLAMA_GRETYPE_CHAR_ALT = 6 # modifies a preceding LLAMA_GRETYPE_CHAR or LLAMA_GRETYPE_CHAR_RNG_UPPER to add an alternate char to match ([ab], [a-zA])
452+
LLAMA_GRETYPE_CHAR_ANY = 7 # any character (.)
450453

451454

452455
# struct parse_state {
@@ -830,6 +833,10 @@ def parse_sequence(
830833
# if (last_sym_start == out_elements.size()) {
831834
# throw std::runtime_error(std::string("expecting preceeding item to */+/? at ") + pos);
832835
# }
836+
elif pos[0] == '.':
837+
last_sym_start = out_elements.size()
838+
out_elements.push_back(LlamaGrammarElement(llama_gretype.LLAMA_GRETYPE_CHAR_ANY, 0))
839+
pos = parse_space(pos + 1, is_nested)
833840
elif pos[0] in ("*", "+", "?"): # repetition operator
834841
if last_sym_start == out_elements.size():
835842
raise RuntimeError("expecting preceding item to */+/? at " + str(pos))
@@ -1039,6 +1046,7 @@ def is_char_element(elem: LlamaGrammarElement) -> bool:
10391046
llama_gretype.LLAMA_GRETYPE_CHAR_NOT,
10401047
llama_gretype.LLAMA_GRETYPE_CHAR_ALT,
10411048
llama_gretype.LLAMA_GRETYPE_CHAR_RNG_UPPER,
1049+
llama_gretype.LLAMA_GRETYPE_CHAR_ANY,
10421050
)
10431051

10441052

@@ -1135,6 +1143,8 @@ def print_rule(
11351143
+ str(i)
11361144
)
11371145
print_grammar_char(file, elem.value)
1146+
elif case is llama_gretype.LLAMA_GRETYPE_CHAR_ANY:
1147+
print(".", file=file, end="")
11381148
# if (is_char_element(elem)) {
11391149
# switch (rule[i + 1].type) {
11401150
# case LLAMA_GRETYPE_CHAR_ALT:

0 commit comments

Comments
 (0)