Fix some callback calls

This commit is contained in:
2025-05-15 19:53:22 -04:00
parent a7b8cc91ea
commit d8a8bcbd19

View File

@@ -161,23 +161,23 @@ enum Symbol : int8_t {
}; };
static const char *symbolNames[N_PAST_END] = { static const char *symbolNames[N_PAST_END] = {
"COLON", "T_COLON",
"TRUE", "T_TRUE",
"FALSE", "T_FALSE",
"NULL", "T_NULL",
"R", "T_R",
"U", "T_U",
"A", "T_A",
"L", "T_L",
"S", "T_S",
"DUBQUOTE", "T_DUBQUOTE",
"STRING", "N_STRING",
"NUMBER", "N_NUMBER",
"VALUE", "N_VALUE",
"ARRAY_VALUE_OR_END", "N_ARRAY_VALUE_OR_END",
"OBJECT_VALUE_OR_END", "N_OBJECT_VALUE_OR_END",
"ARRAY_MAYBE_CONTINUE", "N_ARRAY_MAYBE_CONTINUE",
"OBJECT_MAYBE_CONTINUE", "N_OBJECT_MAYBE_CONTINUE",
}; };
constexpr static struct Tables { constexpr static struct Tables {
@@ -474,12 +474,7 @@ private:
} }
Status parse_number() { Status parse_number() {
char *const bufBefore = buf; char *const bufBefore = buf;
if (len() == 0 || !('0' <= *buf && *buf <= '9' || (*buf == '.'))) { while (len() > 0) {
return S_REJECT;
}
callbacks->on_begin_number(data);
++buf;
for (;;) {
if ('0' <= *buf && *buf <= '9' || (*buf == '.')) { if ('0' <= *buf && *buf <= '9' || (*buf == '.')) {
++buf; ++buf;
} else { } else {
@@ -487,17 +482,23 @@ private:
} }
} }
callbacks->on_number_data(data, bufBefore, buf - bufBefore); callbacks->on_number_data(data, bufBefore, buf - bufBefore);
if (len() == 0) {
return S_AGAIN;
}
callbacks->on_end_number(data); callbacks->on_end_number(data);
return S_OK; return S_OK;
} }
Status parse_string() { Status parse_string() {
callbacks->on_begin_string(data);
auto *result = (char *)memchr(buf, '"', len()); auto *result = (char *)memchr(buf, '"', len());
if (result == nullptr) { if (result == nullptr) {
return S_REJECT; callbacks->on_string_data(data, buf, len());
buf += len();
return S_AGAIN;
} }
int stringLen = result - buf; int stringLen = result - buf;
callbacks->on_string_data(data, buf, stringLen); if (stringLen > 0) {
callbacks->on_string_data(data, buf, stringLen);
}
buf += stringLen + 1; buf += stringLen + 1;
callbacks->on_end_string(data); callbacks->on_end_string(data);
return S_OK; return S_OK;
@@ -513,16 +514,15 @@ private:
} }
static Status keepGoing(Parser2 *self) { static Status keepGoing(Parser2 *self) {
// self->debugPrint();
if (self->empty()) { if (self->empty()) {
return S_OK; return S_OK;
} }
auto top = *(self->stackPtr - 1); self->maybeSkipWs();
if (self->len() == 0) { if (self->len() == 0) {
return S_AGAIN; return S_AGAIN;
} }
self->maybeSkipWs(); // self->debugPrint();
MUSTTAIL return table[top](self); MUSTTAIL return table[*(self->stackPtr - 1)](self);
} }
static Status string(Parser2 *self) { static Status string(Parser2 *self) {
@@ -560,6 +560,7 @@ private:
case '"': case '"':
++self->buf; ++self->buf;
self->pop(); self->pop();
self->callbacks->on_begin_string(self->data);
if (Status s = self->push({N_STRING})) { if (Status s = self->push({N_STRING})) {
return s; return s;
} }
@@ -587,6 +588,7 @@ private:
break; break;
default: default:
self->pop(); self->pop();
self->callbacks->on_begin_number(self->data);
if (Status s = self->push({N_NUMBER})) { if (Status s = self->push({N_NUMBER})) {
return s; return s;
} }
@@ -803,7 +805,12 @@ TEST_CASE("parser2") {
{ {
auto copy = json; auto copy = json;
Parser2 parser(&c, nullptr); Parser2 parser(&c, nullptr);
parser.prime(copy.data(), copy.length()); int i = 0;
for (; i < copy.length() - 1; ++i) {
parser.prime(copy.data() + i, 1);
REQUIRE(parser.parse() == Parser2::S_AGAIN);
}
parser.prime(copy.data() + i, 1);
CHECK(parser.parse() == Parser2::S_OK); CHECK(parser.parse() == Parser2::S_OK);
} }
{ {