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