From 9a5b94e25d50746010cf283ab9a4c83bd81ae0a6 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Mon, 19 May 2025 17:02:45 -0400 Subject: [PATCH] Remove redundant nonterminals --- src/parser3.h | 49 +++++++------------------------------------------ 1 file changed, 7 insertions(+), 42 deletions(-) diff --git a/src/parser3.h b/src/parser3.h index 41a5ac2..939d402 100644 --- a/src/parser3.h +++ b/src/parser3.h @@ -32,7 +32,6 @@ typedef Status (*Continuation)(struct Parser3 *); // These appear in the stack of the pushdown // automata enum Symbol : uint8_t { - N_JSON, N_VALUE, N_OBJECT, N_OBJECT2, @@ -40,11 +39,9 @@ enum Symbol : uint8_t { N_ARRAY, N_ARRAY2, N_ARRAY3, - N_ELEMENT, N_STRING, N_STRING2, N_STRING_FOLLOWING_ESCAPE, - N_NUMBER, N_INTEGER, N_INTEGER2, N_DIGITS, @@ -79,7 +76,7 @@ enum Symbol : uint8_t { struct Parser3 { Parser3(const Callbacks *callbacks, void *data) : callbacks(callbacks), data(data) { - std::ignore = push({N_JSON, T_EOF}); + std::ignore = push({N_WHITESPACE, N_VALUE, N_WHITESPACE, T_EOF}); } [[nodiscard]] Status parse(char *buf, int len) { @@ -151,14 +148,6 @@ struct Parser3 { uint32_t minCodepoint; }; -inline Status n_json(Parser3 *self) { - self->pop(); - if (auto s = self->push({N_ELEMENT})) { - return s; - } - MUSTTAIL return Parser3::keepGoing(self); -} - inline Status n_value(Parser3 *self) { switch (*self->buf) { case '{': @@ -191,7 +180,8 @@ inline Status n_value(Parser3 *self) { case '9': case '-': self->pop(); - if (auto s = self->push({N_NUMBER})) { + if (auto s = + self->push({N_INTEGER, N_FRACTION, N_EXPONENT, T_END_NUMBER})) { return s; } break; @@ -244,8 +234,8 @@ inline Status n_object2(Parser3 *self) { MUSTTAIL return Parser3::keepGoing(self); case '"': self->pop(); - if (auto s = self->push( - {N_STRING, N_WHITESPACE, T_COLON, N_ELEMENT, N_OBJECT3})) { + if (auto s = self->push({N_STRING, N_WHITESPACE, T_COLON, N_WHITESPACE, + N_VALUE, N_WHITESPACE, N_OBJECT3})) { return s; } MUSTTAIL return Parser3::keepGoing(self); @@ -265,7 +255,7 @@ inline Status n_object3(Parser3 *self) { ++self->buf; self->pop(); if (auto s = self->push({N_WHITESPACE, N_STRING, N_WHITESPACE, T_COLON, - N_ELEMENT, N_OBJECT3})) { + N_WHITESPACE, N_VALUE, N_WHITESPACE, N_OBJECT3})) { return s; } MUSTTAIL return Parser3::keepGoing(self); @@ -313,7 +303,7 @@ inline Status n_array3(Parser3 *self) { case ',': ++self->buf; self->pop(); - if (auto s = self->push({N_ELEMENT, N_ARRAY3})) { + if (auto s = self->push({N_WHITESPACE, N_VALUE, N_WHITESPACE, N_ARRAY3})) { return s; } MUSTTAIL return Parser3::keepGoing(self); @@ -322,14 +312,6 @@ inline Status n_array3(Parser3 *self) { } } -inline Status n_element(Parser3 *self) { - self->pop(); - if (auto s = self->push({N_WHITESPACE, N_VALUE, N_WHITESPACE})) { - return s; - } - MUSTTAIL return Parser3::keepGoing(self); -} - inline Status n_string(Parser3 *self) { if (*self->buf != '"') { return S_REJECT; @@ -619,14 +601,6 @@ inline Status t_hex3(Parser3 *self) { MUSTTAIL return Parser3::keepGoing(self); } -inline Status n_number(Parser3 *self) { - self->pop(); - if (auto s = self->push({N_INTEGER, N_FRACTION, N_EXPONENT, T_END_NUMBER})) { - return s; - } - MUSTTAIL return Parser3::keepGoing(self); -} - inline Status n_integer(Parser3 *self) { self->callbacks->on_begin_number(self->data); self->dataBegin = self->buf; @@ -868,7 +842,6 @@ constexpr inline struct ContinuationTable { return S_REJECT; }; } - continuations[N_JSON] = n_json; continuations[N_VALUE] = n_value; continuations[N_OBJECT] = n_object; continuations[N_OBJECT2] = n_object2; @@ -876,11 +849,9 @@ constexpr inline struct ContinuationTable { continuations[N_ARRAY] = n_array; continuations[N_ARRAY2] = n_array2; continuations[N_ARRAY3] = n_array3; - continuations[N_ELEMENT] = n_element; continuations[N_STRING] = n_string; continuations[N_STRING2] = n_string2; continuations[N_STRING_FOLLOWING_ESCAPE] = n_string_following_escape; - continuations[N_NUMBER] = n_number; continuations[N_INTEGER] = n_integer; continuations[N_INTEGER2] = n_integer2; continuations[N_DIGITS] = n_digits; @@ -911,7 +882,6 @@ constexpr inline struct ContinuationTable { continuations[T_END_NUMBER] = t_end_number; continuations[T_BACKSLASH] = singleChar<'\\'>; - symbolNames[N_JSON] = "n_json"; symbolNames[N_VALUE] = "n_value"; symbolNames[N_OBJECT] = "n_object"; symbolNames[N_OBJECT2] = "n_object2"; @@ -919,11 +889,9 @@ constexpr inline struct ContinuationTable { symbolNames[N_ARRAY] = "n_array"; symbolNames[N_ARRAY2] = "n_array2"; symbolNames[N_ARRAY3] = "n_array3"; - symbolNames[N_ELEMENT] = "n_element"; symbolNames[N_STRING] = "n_string"; symbolNames[N_STRING2] = "n_string2"; symbolNames[N_STRING_FOLLOWING_ESCAPE] = "n_string_following_escape"; - symbolNames[N_NUMBER] = "n_number"; symbolNames[N_INTEGER] = "n_integer"; symbolNames[N_INTEGER2] = "n_integer2"; symbolNames[N_DIGITS] = "n_digits"; @@ -971,7 +939,6 @@ inline Status Parser3::keepGoing(Parser3 *self) { if (self->len() == 0) { if (!self->complete) { switch (self->top()) { - case N_NUMBER: case N_INTEGER: case N_INTEGER2: case N_DIGITS: @@ -996,7 +963,6 @@ inline Status Parser3::keepGoing(Parser3 *self) { case T_U2: self->flushString(); break; - case N_JSON: case N_VALUE: case N_OBJECT: case N_OBJECT2: @@ -1004,7 +970,6 @@ inline Status Parser3::keepGoing(Parser3 *self) { case N_ARRAY: case N_ARRAY2: case N_ARRAY3: - case N_ELEMENT: case N_WHITESPACE: case N_TRUE: case N_FALSE: