Archived
chat: a call written as its name and one object; reasoning that opens again later
The reply format described one call syntax, the tagged one: each argument in its own tags,
typed by the tool's schema. A call written as its name followed by ONE object holding the
arguments could not be described -- Gemma 4's `<|tool_call>call:NAME{key:<|"|>text<|"|>}<tool_call|>`
is one, and its tool requests were refused; JSON behind a name tag (`[TOOL_CALLS]NAME[ARGS]{...}`)
or inside a function tag is the same shape.
RadChatFormat grows at its end (no ABI bump; an older plugin reads the new fields as zero):
- args_form RAD_CHAT_ARGS_OBJECT: the arguments are one object after the name. name_suffix may
be empty, and the name then ends at the object's `{`; call_end may be empty, and the object's
own `}` ends the call. The arg_* fields are unused and must be empty.
- object_string_open / object_string_close: a string delimiter of the object's dialect besides
JSON's `"`, between which the text is verbatim (Gemma 4: <|"|> twice).
- object_bare_keys: the dialect writes keys bare, `key:value`.
- reasoning_anywhere: a reasoning block may open again later in the reply (after the answer,
between calls) and is read as reasoning there too; a close with no block open is dropped.
The streaming parser reads the object as it arrives and writes it as JSON as it reads: brackets
and separators as they come, a key once its `:` shows it is one, a string's bytes as they arrive
(the dialect's verbatim, JSON's with its escapes checked: an unknown escape is text, a lone
surrogate is U+FFFD), and a bare value as the tagged family writes a typed one -- held while it
could be a number or literal, else a string. Trailing and missing separators, a bracket of the
wrong kind, a key without value and an object cut off by the call's close are repaired locally
and reported as leaving the grammar; a closed call's arguments are JSON whatever was written.
The streaming contract holds: deltas are never revised, holds stay bounded.
The lazy grammar writes the object in its dialect (strings between the delimiters, bare keys,
JSON numbers and literals) and leaves the values to the model, as llama.cpp's own Gemma 4
grammar does: the schema converter speaks JSON, and the dialect is not JSON.
chat_format_check refuses what the parser could not read unambiguously, by name: tagged fields in
an object format, a dialect string opener that begins like a bare word, a call close that holds
object punctuation or cannot be told apart from the string opener, a reasoning marker that
overlaps a call opener in content.
chatparse_test: JSON behind a name tag and behind a function tag (strict, lenient, streamed every
way, grammar admits and refuses), reasoning_anywhere on and off, and the refusals.
This commit is contained in:
1 parent
b9229091a9
commit
dbd4e164b3
6 files changed
+1007
-53
No files matched your search
+551
-15
@@ -744,6 +744,25 @@ struct ReplyFormat::Impl {
|
||||
std::string cc_core;
|
||||
Dfa think, think_breaks, text, arg_name, value, junk, arg_name_loose, value_loose;
|
||||
size_t longest = 0; /* the longest delimiter */
|
||||
|
||||
/* THE OBJECT FAMILY. `object`: a call's arguments are one object; `ns_object`: the object's
|
||||
* own `{` is what ends the name (name_suffix empty), and ns_core is "{". */
|
||||
bool object = false, ns_object = false;
|
||||
/* Where a key or a value may begin, besides the object's punctuation: the dialect string's
|
||||
* opener, and the call's close and end -- a reply that leaves its object unclosed writes them
|
||||
* there. Read anchored, by their first bytes (chat_format_check keeps them apart). */
|
||||
enum : uint8_t { LitString, LitClose, LitEnd };
|
||||
std::vector<std::string> obj_lits;
|
||||
std::vector<uint8_t> obj_lit_kind;
|
||||
/* A dialect string runs to its close. A bare value ends at `,` `}` `]` and a bare key at `:`
|
||||
* `,` `}` -- patterns 0..2 -- or at the call's close or end, which follow at the indices
|
||||
* below (-1: none). */
|
||||
Dfa obj_str, obj_bare, obj_key;
|
||||
int bare_cc = -1, bare_ce = -1, key_cc = -1, key_ce = -1;
|
||||
|
||||
/* REASONING ANYWHERE: content is scanned for a later block's opener and a stray close too,
|
||||
* after the call openers (text_think) or alone when the request has no calls (think_only). */
|
||||
Dfa text_think, think_only;
|
||||
};
|
||||
|
||||
ReplyFormat::ReplyFormat() : impl_(std::make_unique<Impl>()) {}
|
||||
@@ -773,6 +792,12 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
if (f.call_open.empty()) m.openers = { f.name_prefix };
|
||||
else m.openers = { m.co_core, f.name_prefix };
|
||||
m.name_end = f.arg_name_suffix.empty() ? f.arg_value_prefix : f.arg_name_suffix;
|
||||
m.object = f.has_tools() && f.object_args();
|
||||
if (m.object && f.name_suffix.empty()) {
|
||||
m.ns_object = true;
|
||||
m.ns_core = "{";
|
||||
m.ns_trail.clear();
|
||||
}
|
||||
|
||||
if (f.has_reasoning()) {
|
||||
m.think.build({ m.close_core });
|
||||
@@ -780,7 +805,39 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
for (const std::string& s : f.reasoning_breaks) b.push_back(s);
|
||||
m.think_breaks.build(b);
|
||||
}
|
||||
if (f.has_tools()) {
|
||||
if (f.reasoning_anywhere && f.has_reasoning()) {
|
||||
std::vector<std::string> t;
|
||||
if (f.has_tools()) t = m.openers;
|
||||
t.push_back(m.open_core);
|
||||
t.push_back(m.close_core);
|
||||
m.text_think.build(t);
|
||||
m.think_only.build({ m.open_core, m.close_core });
|
||||
}
|
||||
if (m.object) {
|
||||
m.text.build(m.openers);
|
||||
std::string trail;
|
||||
split_ws(f.call_close, &m.cc_core, &trail);
|
||||
std::vector<std::string> bare{ ",", "}", "]" }, key{ ":", ",", "}" };
|
||||
if (!f.object_string_open.empty()) {
|
||||
m.obj_lits.push_back(f.object_string_open);
|
||||
m.obj_lit_kind.push_back(Impl::LitString);
|
||||
m.obj_str.build({ f.object_string_close });
|
||||
}
|
||||
if (!m.cc_core.empty()) {
|
||||
m.obj_lits.push_back(m.cc_core);
|
||||
m.obj_lit_kind.push_back(Impl::LitClose);
|
||||
m.bare_cc = (int)bare.size(); bare.push_back(m.cc_core);
|
||||
m.key_cc = (int)key.size(); key.push_back(m.cc_core);
|
||||
}
|
||||
if (!m.end_core.empty() && m.end_core != m.cc_core) {
|
||||
m.obj_lits.push_back(m.end_core);
|
||||
m.obj_lit_kind.push_back(Impl::LitEnd);
|
||||
m.bare_ce = (int)bare.size(); bare.push_back(m.end_core);
|
||||
m.key_ce = (int)key.size(); key.push_back(m.end_core);
|
||||
}
|
||||
m.obj_bare.build(bare);
|
||||
m.obj_key.build(key);
|
||||
} else if (f.has_tools()) {
|
||||
m.text.build(m.openers);
|
||||
m.arg_name.build({ m.name_end });
|
||||
m.value.build({ f.arg_value_suffix });
|
||||
@@ -800,7 +857,8 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
const std::string* const delims[] = { &f.reasoning_open, &f.reasoning_close, &f.call_open,
|
||||
&f.call_close, &f.name_prefix, &f.name_suffix,
|
||||
&f.call_end, &f.arg_prefix, &m.name_end,
|
||||
&f.arg_value_suffix };
|
||||
&f.arg_value_suffix, &f.object_string_open,
|
||||
&f.object_string_close };
|
||||
for (const std::string* s : delims) m.longest = std::max(m.longest, s->size());
|
||||
for (const std::string& s : f.reasoning_breaks) m.longest = std::max(m.longest, s.size());
|
||||
*out = std::move(r);
|
||||
@@ -831,6 +889,9 @@ struct ReplyParser::Impl {
|
||||
bool calls = false; /* the request sent tools and the format has calls */
|
||||
bool breaks = false; /* an unclosed reasoning block ends at a call */
|
||||
bool pure = false; /* no reasoning, calls or document: every byte is content */
|
||||
/* A reasoning block may open later in the reply too (ChatFormat::reasoning_anywhere), and
|
||||
* this request extracts reasoning. */
|
||||
bool anywhere = false;
|
||||
std::vector<Tool> tools;
|
||||
std::unordered_map<std::string, size_t> by_name;
|
||||
std::vector<std::string> terminators;
|
||||
@@ -897,9 +958,10 @@ static int bind_tools(const ReplyFormat::Impl& F, const ojson& tools, ReplyParse
|
||||
optional.push_back(pname);
|
||||
}
|
||||
/* A name the argument's own delimiter could end early would be read as a
|
||||
* different name. */
|
||||
* different name. An object's keys are delimited by its own punctuation, and
|
||||
* one that holds it is written as a string. */
|
||||
const std::string probe = pname + F.name_end;
|
||||
if (probe.find(F.name_end) != pname.size()) {
|
||||
if (!F.object && probe.find(F.name_end) != pname.size()) {
|
||||
*why = "parameter '" + pname + "' of tool '" + t.name + "' contains '" +
|
||||
F.name_end + "', which this model's tool-call format uses to end an "
|
||||
"argument name";
|
||||
@@ -909,7 +971,8 @@ static int bind_tools(const ReplyFormat::Impl& F, const ojson& tools, ReplyParse
|
||||
}
|
||||
t.arms.insert(t.arms.end(), optional.begin(), optional.end());
|
||||
if (t.name.find(F.ns_core) != std::string::npos ||
|
||||
(t.name + F.f.name_suffix).find(F.f.name_suffix) != t.name.size()) {
|
||||
(!F.ns_object &&
|
||||
(t.name + F.f.name_suffix).find(F.f.name_suffix) != t.name.size())) {
|
||||
*why = "tool '" + t.name + "' has a name containing '" + F.ns_core +
|
||||
"', which this model's tool-call format uses to end a function name";
|
||||
return RAD_E_INVAL;
|
||||
@@ -949,6 +1012,8 @@ int ReplyParser::create(std::shared_ptr<const ReplyFormat> fmt, const ReplyOptio
|
||||
P.calls = want_calls && !opt.response_format;
|
||||
P.breaks = want_calls && F.f.has_reasoning() && !F.f.reasoning_breaks.empty();
|
||||
P.pure = !F.f.has_reasoning() && !want_calls && !opt.response_format;
|
||||
P.anywhere = F.f.reasoning_anywhere && F.f.has_reasoning() && opt.extract_reasoning &&
|
||||
!opt.response_format;
|
||||
|
||||
if (F.f.has_reasoning()) {
|
||||
switch (F.f.reasoning_in_prompt) {
|
||||
@@ -1004,6 +1069,17 @@ enum class St : uint8_t {
|
||||
RfClose, /* "```" */
|
||||
RfEnd, /* after the closing fence */
|
||||
RfRaw, /* a reply that is not the document it was asked for: content */
|
||||
ObjStart, /* after a name_suffix: whitespace, then the argument object's `{` */
|
||||
ObjKey, /* where a key may begin, or the object's `}` */
|
||||
ObjKeyBare, /* a bare key, to its `:` */
|
||||
ObjColon, /* after a quoted key: whitespace, then `:` */
|
||||
ObjValue, /* where a value may begin, or an empty array's `]` */
|
||||
ObjLit, /* the dialect's string opener, or the call's close or end, being matched */
|
||||
ObjStr, /* a dialect string, to its close */
|
||||
ObjJStr, /* a JSON string, to its quote */
|
||||
ObjBare, /* a bare value: a number, a literal, or a word read as a string */
|
||||
ObjAfter, /* after a value: whitespace, then `,` or a closing bracket */
|
||||
ObjEnd, /* after the object: whitespace, then call_end */
|
||||
};
|
||||
|
||||
/* A function name the grammar does not know is still read as a call, up to this long. */
|
||||
@@ -1018,8 +1094,20 @@ struct ReplyStream::Impl {
|
||||
explicit Impl(const ReplyParser::Impl& p) : P(p), F(p.fmt->impl()) {
|
||||
content.init(P.terminators.empty() ? nullptr : &P.terms, &P.terminators);
|
||||
st = P.pure ? St::Text : St::LeadWs;
|
||||
if (P.anywhere) {
|
||||
text_dfa = P.calls ? &F.text_think : &F.think_only;
|
||||
n_open = P.calls ? F.openers.size() : 0;
|
||||
} else if (P.calls) {
|
||||
text_dfa = &F.text;
|
||||
n_open = F.openers.size();
|
||||
}
|
||||
}
|
||||
|
||||
/* What content is scanned with: the call openers when calls are on, then -- reasoning
|
||||
* anywhere -- a later block's opener and a stray close. Null: content is every byte. */
|
||||
const Dfa* text_dfa = nullptr;
|
||||
size_t n_open = 0;
|
||||
|
||||
ReplyMessage msg;
|
||||
ContentOut content;
|
||||
bool think_text = false; /* the reasoning has had a non-blank byte */
|
||||
@@ -1062,6 +1150,19 @@ struct ReplyStream::Impl {
|
||||
size_t vs_pos = 0;
|
||||
bool fenced = false;
|
||||
|
||||
/* THE ARGUMENT OBJECT being read: its open brackets, whether a key (rather than a value) is
|
||||
* being read, a separator read but not yet written -- it is written only once the next
|
||||
* member shows the object goes on, so a trailing one never reaches the JSON -- whether the
|
||||
* container was just opened, and whether a key's `:` went out with no value yet. */
|
||||
std::vector<char> ostack;
|
||||
bool okey = false, ocomma = false, ofirst = false, vpending = false;
|
||||
/* A bare value's first bytes, to tell at its end whether it was a JSON number or literal --
|
||||
* which the grammar admits -- or a word read as a string, which it does not. */
|
||||
std::string bare_txt;
|
||||
/* A JSON string's escape being read -- the backslash, and a "u" escape's four hex digits,
|
||||
* in vheld -- and a high surrogate's escape, held until the low one shows it is a pair. */
|
||||
std::string jhi;
|
||||
|
||||
/* published offsets, for the deltas */
|
||||
size_t sent_r = 0, sent_c = 0, sent_a = 0;
|
||||
int cur = -1; /* the channel last published: 0 r, 1 c, 2 args */
|
||||
@@ -1202,7 +1303,7 @@ struct ReplyStream::Impl {
|
||||
|
||||
bool declared_prefix(std::string_view n) const {
|
||||
for (const Tool& t : P.tools) {
|
||||
const std::string full = t.name + F.f.name_suffix;
|
||||
const std::string full = t.name + (F.ns_object ? F.ns_core : F.f.name_suffix);
|
||||
if (full.size() >= n.size() && full.compare(0, n.size(), n) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
@@ -1283,14 +1384,180 @@ struct ReplyStream::Impl {
|
||||
cur = -1;
|
||||
event(ReplyEvent::CallBegin, (uint32_t)msg.calls.size() - 1, msg.calls.back().name);
|
||||
depth = 0; in_str = false; esc = false;
|
||||
args_out("{");
|
||||
argc = 0;
|
||||
++calls;
|
||||
att.clear();
|
||||
post_call = false;
|
||||
if (F.object) {
|
||||
/* The arguments are the object's, and its `{` is written now: a reply that ends
|
||||
* here is a call with no arguments yet, as in the tagged family. */
|
||||
obj_out("{");
|
||||
ostack.assign(1, '{');
|
||||
okey = ocomma = vpending = false;
|
||||
ofirst = true;
|
||||
jhi.clear();
|
||||
st = F.ns_object ? St::ObjKey : St::ObjStart;
|
||||
return;
|
||||
}
|
||||
args_out("{");
|
||||
st = St::Body;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ the argument object
|
||||
*
|
||||
* The object of the object family, read as it streams and written as JSON as it is read:
|
||||
* brackets and separators as they come, a key as soon as its `:` shows it is one, a string
|
||||
* as its bytes arrive, and a bare value as the tagged family writes a typed one (ValueOut,
|
||||
* Raw): held while it could still be a literal or a number, else a string. What the dialect
|
||||
* spells otherwise -- its string delimiters, its bare keys -- is respelled; what leaves it
|
||||
* -- a missing separator, a trailing one, a bracket of the wrong kind, an object the call's
|
||||
* close cuts off -- is repaired locally and said so (strict). The arguments a closed call
|
||||
* reports are JSON whatever was written. */
|
||||
|
||||
void obj_out(std::string_view s) {
|
||||
if (s.empty()) return;
|
||||
touch(2);
|
||||
msg.calls.back().arguments.append(s.data(), s.size());
|
||||
}
|
||||
void obj_flush() { if (!scratch.empty()) { obj_out(scratch); scratch.clear(); } }
|
||||
|
||||
/* A member or an element begins: the separator before it, if one was read, goes out now. */
|
||||
void obj_sep() {
|
||||
if (ocomma) { obj_out(","); ocomma = false; }
|
||||
ofirst = false;
|
||||
}
|
||||
|
||||
/* The outermost bracket closed: what follows the object. */
|
||||
void obj_done() {
|
||||
if (!F.end_core.empty()) { lpos = 0; lhold.clear(); st = St::ObjEnd; }
|
||||
else end_call();
|
||||
}
|
||||
|
||||
/* A closing bracket where a key, a value or a separator may stand. A separator before it is
|
||||
* dropped, and a bracket of the other kind closes the open container all the same. */
|
||||
void obj_close_bracket(char c) {
|
||||
if (ocomma) { lenient(); ocomma = false; }
|
||||
const char top = ostack.back();
|
||||
if ((top == '{') != (c == '}')) lenient();
|
||||
if (vpending) { lenient(); obj_out("null"); vpending = false; }
|
||||
ostack.pop_back();
|
||||
ofirst = false;
|
||||
obj_out(top == '{' ? "}" : "]");
|
||||
if (ostack.empty()) obj_done();
|
||||
else st = St::ObjAfter;
|
||||
}
|
||||
|
||||
/* The punctuation after a value. */
|
||||
void obj_struct(char c) {
|
||||
if (c != ',') { obj_close_bracket(c); return; }
|
||||
if (ocomma) lenient(); /* `,,`: one separator */
|
||||
ocomma = true;
|
||||
ofirst = false;
|
||||
okey = ostack.back() == '{';
|
||||
st = okey ? St::ObjKey : St::ObjValue;
|
||||
}
|
||||
|
||||
bool obj_lit_start(unsigned char c) const {
|
||||
for (const std::string& l : F.obj_lits)
|
||||
if ((unsigned char)l[0] == c) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Bytes that open no string and close no call: a bare key or a bare value. */
|
||||
void obj_bare_begin() {
|
||||
ds = 0;
|
||||
hold.clear();
|
||||
if (okey) {
|
||||
if (!F.f.object_bare_keys) lenient();
|
||||
name.clear();
|
||||
st = St::ObjKeyBare;
|
||||
return;
|
||||
}
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
bare_txt.clear();
|
||||
val.begin(ValueOut::Raw, /*cdata=*/false, /*lead=*/false, /*trail=*/false, scratch);
|
||||
obj_flush();
|
||||
st = St::ObjBare;
|
||||
}
|
||||
|
||||
static constexpr size_t kBareKeep = 64; /* longer than any number a model writes */
|
||||
|
||||
/* A bare value ended: one that is no JSON number or literal was read by the recovery rule. */
|
||||
void bare_done() {
|
||||
std::string_view v(bare_txt);
|
||||
while (!v.empty() && is_ws((unsigned char)v.back())) v.remove_suffix(1);
|
||||
const bool json = bare_txt.size() < kBareKeep && !v.empty() &&
|
||||
(v == "true" || v == "false" || v == "null" ||
|
||||
((v[0] == '-' || (v[0] >= '0' && v[0] <= '9')) &&
|
||||
nlohmann::json::accept(v)));
|
||||
if (!json) lenient();
|
||||
}
|
||||
|
||||
/* A JSON string, which is the dialect's own only when it declares no string delimiters. */
|
||||
void json_string_begin() {
|
||||
if (!F.f.object_string_open.empty()) lenient();
|
||||
obj_out("\"");
|
||||
vheld.clear();
|
||||
st = St::ObjJStr;
|
||||
}
|
||||
|
||||
/* One of the anchored literals matched where a key or a value may begin. */
|
||||
void obj_lit_done(uint8_t kind) {
|
||||
if (kind == ReplyFormat::Impl::LitString) {
|
||||
obj_sep();
|
||||
if (!okey) vpending = false;
|
||||
obj_out("\"");
|
||||
ds = 0;
|
||||
hold.clear();
|
||||
st = St::ObjStr;
|
||||
return;
|
||||
}
|
||||
/* The call's close or end where the object wanted more: the reply left the object
|
||||
* unclosed, and the call ends here with it closed. */
|
||||
lenient();
|
||||
ocomma = false;
|
||||
if (kind == ReplyFormat::Impl::LitClose) end_call_at_close();
|
||||
else end_core_done();
|
||||
}
|
||||
|
||||
/* A JSON string's text goes out as it is, its escapes checked: one JSON has not is the
|
||||
* backslash and the letter as text, a raw control character is escaped, and a surrogate
|
||||
* escape that is not half of a pair is U+FFFD -- a client's JSON parser refuses all three. */
|
||||
void jstr_char(const char* q, size_t n) {
|
||||
if (n == 0) return;
|
||||
if (!jhi.empty()) { lenient(); jhi.clear(); obj_out("\\ufffd"); }
|
||||
touch(2);
|
||||
json_escape_into(msg.calls.back().arguments, q, n);
|
||||
}
|
||||
|
||||
void jstr_escape_done(const std::string& e) {
|
||||
/* e: a complete two-character escape, or a "u" escape with its four digits. */
|
||||
if (e.size() == 6) {
|
||||
const unsigned long cp = std::strtoul(e.c_str() + 2, nullptr, 16);
|
||||
if (cp >= 0xD800 && cp <= 0xDBFF) {
|
||||
if (!jhi.empty()) { lenient(); obj_out("\\ufffd"); }
|
||||
jhi = e;
|
||||
return;
|
||||
}
|
||||
if (cp >= 0xDC00 && cp <= 0xDFFF) {
|
||||
if (jhi.empty()) { lenient(); obj_out("\\ufffd"); return; }
|
||||
obj_out(jhi + e);
|
||||
jhi.clear();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!jhi.empty()) { lenient(); jhi.clear(); obj_out("\\ufffd"); }
|
||||
obj_out(e);
|
||||
}
|
||||
|
||||
/* A string or a key closed. */
|
||||
void obj_string_done() {
|
||||
if (!jhi.empty()) { lenient(); jhi.clear(); obj_out("\\ufffd"); }
|
||||
obj_out("\"");
|
||||
st = okey ? St::ObjColon : St::ObjAfter;
|
||||
}
|
||||
|
||||
/* The core of call_end has matched. The whitespace the format writes after it is part of the
|
||||
* tag, so the call closes when that has arrived too -- a reply that ends in between leaves the
|
||||
* call open, as the grammar does -- and a reply that goes straight on without it has closed
|
||||
@@ -1306,8 +1573,18 @@ struct ReplyStream::Impl {
|
||||
* match, so no quote is ever pending here; a raw value that left a string open is passed
|
||||
* through as the model wrote it. */
|
||||
void end_call() {
|
||||
std::string close((size_t)std::max(depth, 0), '}');
|
||||
args_out(close);
|
||||
if (F.object) {
|
||||
/* What the object left open is closed, so the arguments are JSON: a key whose value
|
||||
* never came is null, and each bracket gets its partner. */
|
||||
std::string close = vpending ? "null" : "";
|
||||
vpending = false;
|
||||
for (auto it = ostack.rbegin(); it != ostack.rend(); ++it) close += *it == '{' ? '}' : ']';
|
||||
ostack.clear();
|
||||
obj_out(close);
|
||||
} else {
|
||||
std::string close((size_t)std::max(depth, 0), '}');
|
||||
args_out(close);
|
||||
}
|
||||
publish(2, true);
|
||||
msg.calls.back().closed = true;
|
||||
event(ReplyEvent::CallEnd, (uint32_t)msg.calls.size() - 1, {});
|
||||
@@ -1499,14 +1776,22 @@ struct ReplyStream::Impl {
|
||||
return p;
|
||||
|
||||
case St::Text:
|
||||
if (!P.calls) {
|
||||
if (!text_dfa) {
|
||||
text_out(p, (size_t)(e - p));
|
||||
return e;
|
||||
}
|
||||
p = scan(F.text, p, e, &hit, [this](const char* q, size_t n) { text_out(q, n); });
|
||||
if (hit >= 0) {
|
||||
p = scan(*text_dfa, p, e, &hit, [this](const char* q, size_t n) { text_out(q, n); });
|
||||
if (hit < 0) return p;
|
||||
if ((size_t)hit < n_open) {
|
||||
att_at = here(p) - F.openers[(size_t)hit].size();
|
||||
begin_text_attempt(hit);
|
||||
} else if ((size_t)hit == n_open) {
|
||||
/* A reasoning block opens again, later in the reply. */
|
||||
trail = 0;
|
||||
st = St::ThinkOpenWs;
|
||||
} else {
|
||||
/* Its close with no block open: not part of the answer. */
|
||||
lenient();
|
||||
}
|
||||
return p;
|
||||
|
||||
@@ -1558,12 +1843,18 @@ struct ReplyStream::Impl {
|
||||
lpos = 0;
|
||||
lhold.clear();
|
||||
tag_end = att.size();
|
||||
if (loose) st = St::OpenPeek;
|
||||
if (loose && F.object) {
|
||||
/* A call written without its wrapper, in the object family: a declared
|
||||
* tool's name and the object's opening are what make it one. */
|
||||
if (tool < 0) { fail_attempt(); return p + 1; }
|
||||
begin_call(false);
|
||||
} else if (loose) st = St::OpenPeek;
|
||||
else if (F.ns_trail.empty()) begin_call(true);
|
||||
else st = St::OpenTail;
|
||||
return p + 1;
|
||||
}
|
||||
if (name.size() > kMaxLooseName || (c == '<' && !declared_prefix(name))) {
|
||||
if (name.size() > kMaxLooseName || (c == '<' && !declared_prefix(name)) ||
|
||||
(loose && F.object && !declared_prefix(name))) {
|
||||
fail_attempt();
|
||||
return p + 1;
|
||||
}
|
||||
@@ -1775,6 +2066,245 @@ struct ReplyStream::Impl {
|
||||
if (p < e) { post_call = true; ds = 0; hold.clear(); st = St::Text; }
|
||||
return p;
|
||||
|
||||
case St::ObjStart:
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
if (*p == '{') { ofirst = true; okey = true; st = St::ObjKey; return p + 1; }
|
||||
/* No object after the name: the call takes none, and what stands there instead is
|
||||
* read as what follows a call. */
|
||||
lenient();
|
||||
obj_done();
|
||||
return p;
|
||||
|
||||
case St::ObjKey: {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
const char c = *p;
|
||||
okey = true;
|
||||
if (c == '}' || c == ']') { obj_close_bracket(c); return p + 1; }
|
||||
if (c == ',') { lenient(); return p + 1; } /* an empty member */
|
||||
if (c == '"') {
|
||||
obj_sep();
|
||||
json_string_begin();
|
||||
return p + 1;
|
||||
}
|
||||
if (obj_lit_start((unsigned char)c)) { lhold.clear(); st = St::ObjLit; return p; }
|
||||
obj_bare_begin();
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjKeyBare:
|
||||
p = scan(F.obj_key, p, e, &hit, [this](const char* q, size_t n) { name.append(q, n); });
|
||||
if (hit < 0) return p;
|
||||
if (hit <= 2) {
|
||||
std::string_view k(name);
|
||||
while (!k.empty() && is_ws((unsigned char)k.back())) k.remove_suffix(1);
|
||||
if (k.empty()) lenient();
|
||||
obj_sep();
|
||||
std::string out = json_quote(k);
|
||||
name.clear();
|
||||
if (hit == 0) {
|
||||
obj_out(out + ":");
|
||||
vpending = true;
|
||||
okey = false;
|
||||
st = St::ObjValue;
|
||||
return p;
|
||||
}
|
||||
/* A key with no value: it is null. */
|
||||
lenient();
|
||||
obj_out(out + ":null");
|
||||
obj_struct(hit == 1 ? ',' : '}');
|
||||
return p;
|
||||
}
|
||||
/* The call's close or end inside a key: the key had no value, and goes. */
|
||||
name.clear();
|
||||
lenient();
|
||||
ocomma = false;
|
||||
if (hit == F.key_cc) end_call_at_close();
|
||||
else end_core_done();
|
||||
return p;
|
||||
|
||||
case St::ObjColon:
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
vpending = true;
|
||||
okey = false;
|
||||
st = St::ObjValue;
|
||||
if (*p == ':') { obj_out(":"); return p + 1; }
|
||||
lenient();
|
||||
obj_out(":");
|
||||
return p;
|
||||
|
||||
case St::ObjValue: {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
const char c = *p;
|
||||
const bool in_array = ostack.back() == '[';
|
||||
okey = false;
|
||||
if (c == '{' || c == '[') {
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
obj_out(c == '{' ? "{" : "[");
|
||||
ostack.push_back(c);
|
||||
ofirst = true;
|
||||
okey = c == '{';
|
||||
st = c == '{' ? St::ObjKey : St::ObjValue;
|
||||
return p + 1;
|
||||
}
|
||||
if (c == ']' && in_array && ofirst) { obj_close_bracket(c); return p + 1; }
|
||||
if (c == '}' || c == ']' || c == ',') {
|
||||
/* No value where one was due: a member's is null, and an array's gap is no
|
||||
* element. */
|
||||
lenient();
|
||||
if (!in_array) {
|
||||
obj_out("null");
|
||||
vpending = false;
|
||||
st = St::ObjAfter;
|
||||
return p;
|
||||
}
|
||||
if (c == ',') return p + 1;
|
||||
obj_close_bracket(c);
|
||||
return p + 1;
|
||||
}
|
||||
if (c == '"') {
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
json_string_begin();
|
||||
return p + 1;
|
||||
}
|
||||
if (obj_lit_start((unsigned char)c)) { lhold.clear(); st = St::ObjLit; return p; }
|
||||
obj_bare_begin();
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjLit: {
|
||||
const char c = *p;
|
||||
lhold.push_back(c);
|
||||
bool part = false;
|
||||
for (size_t i = 0; i < F.obj_lits.size(); ++i) {
|
||||
const std::string& l = F.obj_lits[i];
|
||||
if (lhold.size() > l.size() || l.compare(0, lhold.size(), lhold) != 0) continue;
|
||||
if (lhold.size() < l.size()) { part = true; continue; }
|
||||
lhold.clear();
|
||||
obj_lit_done(F.obj_lit_kind[i]);
|
||||
return p + 1;
|
||||
}
|
||||
if (part) return p + 1;
|
||||
/* None of them: the bytes begin a bare key or value. */
|
||||
std::string again;
|
||||
again.swap(lhold);
|
||||
obj_bare_begin();
|
||||
read_again(again);
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
case St::ObjStr:
|
||||
p = scan(F.obj_str, p, e, &hit, [this](const char* q, size_t n) {
|
||||
touch(2);
|
||||
json_escape_into(msg.calls.back().arguments, q, n);
|
||||
});
|
||||
if (hit >= 0) obj_string_done();
|
||||
return p;
|
||||
|
||||
case St::ObjJStr: {
|
||||
const char* run = p;
|
||||
while (p < e) {
|
||||
const unsigned char c = (unsigned char)*p;
|
||||
if (vheld.size() == 1) { /* after the backslash */
|
||||
if (c && std::strchr("\"\\/bfnrt", (int)c)) {
|
||||
vheld.push_back((char)c);
|
||||
jstr_escape_done(vheld);
|
||||
vheld.clear();
|
||||
run = ++p;
|
||||
continue;
|
||||
}
|
||||
if (c == 'u') { vheld.push_back('u'); run = ++p; continue; }
|
||||
/* No JSON escape: the backslash is text, and what follows it is read as
|
||||
* the string's next byte. */
|
||||
lenient();
|
||||
vheld.clear();
|
||||
jstr_char("\\", 1);
|
||||
run = p;
|
||||
continue;
|
||||
}
|
||||
if (!vheld.empty()) { /* a "u" escape's digits */
|
||||
if (std::isxdigit(c)) {
|
||||
vheld.push_back((char)c);
|
||||
if (vheld.size() == 6) { jstr_escape_done(vheld); vheld.clear(); }
|
||||
run = ++p;
|
||||
continue;
|
||||
}
|
||||
lenient();
|
||||
const std::string text = vheld;
|
||||
vheld.clear();
|
||||
jstr_char(text.data(), text.size());
|
||||
run = p;
|
||||
continue;
|
||||
}
|
||||
if (c != '"' && c != '\\') { ++p; continue; }
|
||||
jstr_char(run, (size_t)(p - run));
|
||||
++p;
|
||||
if (c == '"') { obj_string_done(); return p; }
|
||||
vheld.assign(1, '\\');
|
||||
run = p;
|
||||
}
|
||||
jstr_char(run, (size_t)(p - run));
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjBare:
|
||||
p = scan(F.obj_bare, p, e, &hit, [this](const char* q, size_t n) {
|
||||
if (bare_txt.size() < kBareKeep)
|
||||
bare_txt.append(q, std::min(n, kBareKeep - bare_txt.size()));
|
||||
val.add(q, n, scratch);
|
||||
obj_flush();
|
||||
});
|
||||
if (hit < 0) return p;
|
||||
val.finish(false, scratch);
|
||||
obj_flush();
|
||||
bare_done();
|
||||
if (hit <= 2) { obj_struct(",}]"[hit]); return p; }
|
||||
lenient();
|
||||
if (hit == F.bare_cc) end_call_at_close();
|
||||
else end_core_done();
|
||||
return p;
|
||||
|
||||
case St::ObjAfter: {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
const char c = *p;
|
||||
if (c == ',' || c == '}' || c == ']') { obj_struct(c); return p + 1; }
|
||||
/* A key or a value with no separator before it: one is supplied. */
|
||||
lenient();
|
||||
ocomma = true;
|
||||
ofirst = false;
|
||||
okey = ostack.back() == '{';
|
||||
st = okey ? St::ObjKey : St::ObjValue;
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjEnd: {
|
||||
if (lpos == 0) {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
}
|
||||
const char c = *p;
|
||||
if (c == F.end_core[lpos]) {
|
||||
lhold.push_back(c);
|
||||
if (++lpos == F.end_core.size()) { lhold.clear(); lpos = 0; end_core_done(); }
|
||||
return p + 1;
|
||||
}
|
||||
/* The object is not followed by the call's end: the call ends with it all the same,
|
||||
* and what stands there is read as what follows a call. */
|
||||
lenient();
|
||||
std::string again;
|
||||
again.swap(lhold);
|
||||
lpos = 0;
|
||||
end_call();
|
||||
read_again(again);
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::RfWs:
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p < e) {
|
||||
@@ -1998,6 +2528,11 @@ struct ReplyStream::Impl {
|
||||
case St::ValJsonEnd:
|
||||
json_value_done(std::string_view(vheld).substr(0, vheld.size() - vs_pos));
|
||||
break;
|
||||
case St::ObjBare:
|
||||
/* A bare value the reply was cut off inside: as it arrived. The call is open. */
|
||||
val.finish(true, scratch);
|
||||
obj_flush();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -2066,7 +2601,8 @@ ReplyHeld ReplyStream::held() const {
|
||||
(m.msg.reasoning.size() - m.sent_r) + (m.msg.content.size() - m.sent_c) +
|
||||
(m.msg.calls.empty() ? 0 : m.msg.calls.back().arguments.size() - m.sent_a);
|
||||
h.space = m.content.held_space() + m.think_pend.size();
|
||||
h.structure = m.att.size() + m.name.size() + m.vheld.size() + m.val.held_container();
|
||||
h.structure = m.att.size() + m.name.size() + m.vheld.size() + m.val.held_container() +
|
||||
m.jhi.size();
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user