MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / format_expected_element

Function format_expected_element

tests/test-chat.cpp:145–216  ·  view source on GitHub ↗

Helper to format expected element from grammar stack

Source from the content-addressed store, hash-verified

143
144// Helper to format expected element from grammar stack
145static std::string format_expected_element(const llama_grammar_rules & /* rules*/, const llama_grammar_element * elem) {
146 if (!elem) {
147 return "<end>";
148 }
149
150 switch (elem->type) {
151 case LLAMA_GRETYPE_END:
152 return "<end of rule>";
153 case LLAMA_GRETYPE_ALT:
154 return "<alternative>";
155 case LLAMA_GRETYPE_RULE_REF:
156 {
157 // Find rule name - just show rule ID for now
158 return "<rule-" + std::to_string(elem->value) + ">";
159 }
160 case LLAMA_GRETYPE_CHAR:
161 {
162 std::string result;
163 const llama_grammar_element * pos = elem;
164 bool first = true;
165
166 do {
167 if (!first) {
168 result += " | ";
169 }
170 first = false;
171
172 if (pos[1].type == LLAMA_GRETYPE_CHAR_RNG_UPPER) {
173 // Range like [a-z]
174 result += "[" + format_codepoint(pos->value) + "-" + format_codepoint(pos[1].value) + "]";
175 pos += 2;
176 } else {
177 result += format_codepoint(pos->value);
178 pos += 1;
179 }
180 } while (pos->type == LLAMA_GRETYPE_CHAR_ALT);
181
182 return result;
183 }
184 case LLAMA_GRETYPE_CHAR_NOT:
185 {
186 std::string result = "[^";
187 const llama_grammar_element * pos = elem;
188 bool first = true;
189
190 do {
191 if (!first) {
192 result += " ";
193 }
194 first = false;
195
196 if (pos[1].type == LLAMA_GRETYPE_CHAR_RNG_UPPER) {
197 result += format_codepoint(pos->value) + "-" + format_codepoint(pos[1].value);
198 pos += 2;
199 } else {
200 result += format_codepoint(pos->value);
201 pos += 1;
202 }

Callers 1

get_expected_descriptionFunction · 0.85

Calls 2

format_codepointFunction · 0.85
to_stringFunction · 0.50

Tested by

no test coverage detected