From: Cloudwalk Date: Mon, 10 May 2021 17:58:48 +0000 (-0400) Subject: Don't initialize pos when creating the parser context. Remove CurrentToken and make... X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=3f1984b7ff5c4bed57ba385d03e5bb37a7679b84;p=xonotic%2Fdarkplaces.git Don't initialize pos when creating the parser context. Remove CurrentToken and make NextToken initialize pos instead. Avoids having to call SkipToToken twice. --- diff --git a/json.c b/json.c index 7e7373cb..2f060345 100644 --- a/json.c +++ b/json.c @@ -235,7 +235,7 @@ static inline void Json_Parse_Object(struct qjson_state_s *json) */ Json_Parse_Pairs(json); - if(Parse_CurrentToken(json->state) != '}') + if(*json->state->pos != '}') Parse_Error(json->state, PARSE_ERR_INVAL, ", or }"); Parse_Dedent(json->state); @@ -255,7 +255,7 @@ static inline void Json_Parse_Array(struct qjson_state_s *json) break; } while (Parse_NextToken(json->state) == ','); - if(Parse_CurrentToken(json->state) != ']') + if(*json->state->pos != ']') Parse_Error(json->state, PARSE_ERR_INVAL, ", or ]"); Parse_Dedent(json->state); @@ -279,7 +279,7 @@ static qjson_token_t *Json_Parse_Main(qjson_state_t *json) return NULL; } - switch(Parse_CurrentToken(json->state)) + switch(Parse_NextToken(json->state)) { case '{': Json_Parse_Object(json); diff --git a/parser.c b/parser.c index b6ecc083..0b86be70 100644 --- a/parser.c +++ b/parser.c @@ -138,21 +138,13 @@ static inline qbool Parse_SkipToToken(struct qparser_state_s *state) // Skip to the next token. Advance the pointer at least 1 if we're not sitting on whitespace. char Parse_NextToken(struct qparser_state_s *state) { - // Check if we will skip first. - if(!Parse_SkipToToken(state)) - { - // If not, advance the pointer. + if(!state->pos) + state->pos = state->buf; + else Parse_Next(state, 1); - // Ensure we didn't land on whitespace and skip that too. - Parse_SkipToToken(state); - } - return *state->pos; -} -// Return the current token but skip comments. -char Parse_CurrentToken(struct qparser_state_s *state) -{ Parse_SkipToToken(state); + return *state->pos; } @@ -169,7 +161,7 @@ qparser_state_t *Parse_New(const unsigned char *in) out = (qparser_state_t *)Z_Malloc(sizeof(qparser_state_t)); out->buf = in; - out->pos = in; + out->pos = NULL; out->line = 1; out->col = 1; out->depth = 0; diff --git a/parser.h b/parser.h index 05cf6e3a..eee99bb9 100644 --- a/parser.h +++ b/parser.h @@ -52,7 +52,6 @@ extern jmp_buf parse_error; void Parse_Error(struct qparser_state_s *state, qparser_err_t error, const char *expected); void Parse_Next(struct qparser_state_s *state, size_t count); char Parse_NextToken(struct qparser_state_s *state); -char Parse_CurrentToken(struct qparser_state_s *state); qparser_state_t *Parse_New(const unsigned char *in); qparser_state_t *Parse_LoadFile(const char *file);