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

Function common_chat_peg_parse

common/chat.cpp:2309–2380  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2307}
2308
2309common_chat_msg common_chat_peg_parse(const common_peg_arena & src_parser,
2310 const std::string & input,
2311 bool is_partial,
2312 const common_chat_parser_params & params) {
2313 const common_peg_arena & parser = src_parser.empty() ?
2314 build_chat_peg_parser([](common_chat_peg_builder & p) { return p.content(p.rest()) + p.end(); }) :
2315 src_parser;
2316
2317 if (src_parser.empty()) {
2318 LOG_DBG("No parser definition detected, assuming pure content parser.");
2319 }
2320
2321 const std::string effective_input = params.generation_prompt.empty()
2322 ? input
2323 : params.generation_prompt + input;
2324
2325 //LOG_DBG("Parsing PEG input with format %s: %s\n", common_chat_format_name(params.format), effective_input.c_str());
2326
2327 common_peg_parse_flags flags = COMMON_PEG_PARSE_FLAG_LENIENT;
2328 if (params.debug) {
2329 flags |= COMMON_PEG_PARSE_FLAG_DEBUG;
2330 }
2331
2332 common_peg_parse_context ctx(effective_input, flags);
2333 auto result = parser.parse(ctx);
2334
2335 if (result.fail()) {
2336 // During partial parsing, return partial results if any AST nodes were captured
2337 // This allows streaming to work correctly for formats like FUNC_MARKDOWN_CODE_BLOCK
2338 if (is_partial && result.end > 0) {
2339 // Try to extract any partial results from what was successfully parsed
2340 common_chat_msg msg;
2341 msg.role = "assistant";
2342 std::unique_ptr<common_chat_peg_mapper> mapper;
2343 if (params.format == COMMON_CHAT_FORMAT_PEG_GEMMA4) {
2344 mapper = std::make_unique<common_chat_peg_gemma4_mapper>(msg);
2345 } else {
2346 mapper = std::make_unique<common_chat_peg_mapper>(msg);
2347 }
2348 mapper->from_ast(ctx.ast, result);
2349
2350 if (ctx.is_debug()) {
2351 fprintf(stderr, "\nAST for partial parse (fail):\n%s\n", ctx.ast.dump().c_str());
2352 fflush(stderr);
2353 }
2354 return msg;
2355 }
2356 throw std::runtime_error(std::string("Failed to parse input at pos ") + std::to_string(result.end) + ": " +
2357 effective_input.substr(result.end));
2358 }
2359
2360 common_chat_msg msg;
2361 msg.role = "assistant";
2362
2363 std::unique_ptr<common_chat_peg_mapper> mapper;
2364 if (params.format == COMMON_CHAT_FORMAT_PEG_GEMMA4) {
2365 mapper = std::make_unique<common_chat_peg_gemma4_mapper>(msg);
2366 } else {

Callers 3

parseMethod · 0.85
common_chat_parseFunction · 0.85

Calls 13

build_chat_peg_parserFunction · 0.85
stringClass · 0.85
contentMethod · 0.80
restMethod · 0.80
endMethod · 0.80
failMethod · 0.80
from_astMethod · 0.80
is_debugMethod · 0.80
dumpMethod · 0.80
to_stringFunction · 0.50
emptyMethod · 0.45

Tested by 2

parseMethod · 0.68