Commit bf14e87a authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

State definition using macro

parent b6e6624a
...@@ -50,20 +50,22 @@ static const char * strstate[] = { ...@@ -50,20 +50,22 @@ static const char * strstate[] = {
}; };
#undef _ENUM_GEN #undef _ENUM_GEN
#define STATE(st) \
case s_##st: \
l_##st: \
state = s_##st; \
if (pos >= end) { /* No more data */ \
goto finalize; \
} \
ch = *pos;
#define TRANSITION(st) do { \ #define TRANSITION(st) do { \
pos++; nread++; \ pos++; nread++; \
TRANSITION_WITHOUT_POS_INCR(st); \ TRANSITION_WITHOUT_POS_INCR(st); \
} while(0) } while(0)
#define TRANSITION_WITHOUT_POS_INCR(st) do { \ #define TRANSITION_WITHOUT_POS_INCR(st) do { \
state = s_##st; \ goto l_##st; \
if (pos < end) { \
ch = *pos; \
goto l_##st; \
} \
\
/* No more data */ \
goto finalize; \
} while(0) } while(0)
#ifdef DEBUG #ifdef DEBUG
...@@ -153,12 +155,9 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -153,12 +155,9 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
while (pos < end && stackidx >= 0) { while (pos < end && stackidx >= 0) {
cur = &stack[stackidx]; cur = &stack[stackidx];
cur->parent = stackidx > 0 ? &stack[stackidx-1] : NULL; cur->parent = stackidx > 0 ? &stack[stackidx-1] : NULL;
ch = *pos;
switch (state) { switch (state) {
case s_type_char: STATE(type_char) {
l_type_char:
{
cur->poff = nread; cur->poff = nread;
switch (ch) { switch (ch) {
...@@ -182,9 +181,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -182,9 +181,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto error; goto error;
} }
case s_integer_start: STATE(integer_start) {
l_integer_start:
{
i64.neg = 0; i64.neg = 0;
i64.ui64 = 0; i64.ui64 = 0;
...@@ -214,9 +211,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -214,9 +211,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto error; goto error;
} }
case s_integer_19: STATE(integer_19) {
l_integer_19:
{
if (ch >= '1' && ch <= '9') { if (ch >= '1' && ch <= '9') {
i64.ui64 = ch - '0'; i64.ui64 = ch - '0';
TRANSITION(integer_09); TRANSITION(integer_09);
...@@ -225,9 +220,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -225,9 +220,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto error; goto error;
} }
case s_integer_09: STATE(integer_09) {
l_integer_09:
{
if (ch >= '0' && ch <= '9') { if (ch >= '0' && ch <= '9') {
if (i64.ui64 > (UINT64_MAX / 10)) /* Overflow */ if (i64.ui64 > (UINT64_MAX / 10)) /* Overflow */
goto error; goto error;
...@@ -243,9 +236,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -243,9 +236,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto error; goto error;
} }
case s_integer_cr: STATE(integer_cr) {
l_integer_cr:
{
if (ch == '\r') { if (ch == '\r') {
TRANSITION(integer_lf); TRANSITION(integer_lf);
} }
...@@ -253,9 +244,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -253,9 +244,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto error; goto error;
} }
case s_integer_lf: STATE(integer_lf) {
l_integer_lf:
{
if (ch != '\n') { if (ch != '\n') {
goto error; goto error;
} }
...@@ -313,9 +302,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -313,9 +302,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
assert(NULL && "unexpected object type in s_integer_lf"); assert(NULL && "unexpected object type in s_integer_lf");
} }
case s_bulk: STATE(bulk) {
l_bulk:
{
size_t remaining = cur->remaining; size_t remaining = cur->remaining;
size_t available = (end-pos); size_t available = (end-pos);
...@@ -334,9 +321,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -334,9 +321,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto finalize; goto finalize;
} }
case s_bulk_cr: STATE(bulk_cr) {
l_bulk_cr:
{
if (ch == '\r') { if (ch == '\r') {
TRANSITION(bulk_lf); TRANSITION(bulk_lf);
} }
...@@ -344,9 +329,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons ...@@ -344,9 +329,7 @@ size_t redis_parser_execute(redis_parser_t *parser, redis_protocol_t **dst, cons
goto error; goto error;
} }
case s_bulk_lf: STATE(bulk_lf) {
l_bulk_lf:
{
if (ch == '\n') { if (ch == '\n') {
goto done; goto done;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment