parser: refactor token_accept()

Invert the test at the top of token_accept(), to return immediatly
if the next token doesn't match the requested token id.

Change the type of the function to Boolean, and add a comment to
explain why we're freeing the token string (if present).

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-33-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Alex Elder
2021-10-01 18:23:36 -05:00
committed by Bjorn Andersson
parent fae755df21
commit 624dcc7cc6

View File

@@ -337,19 +337,20 @@ static void token_init(void)
curr_token = yylex();
}
static int token_accept(enum token_id token_id, struct token *tok)
static bool token_accept(enum token_id token_id, struct token *tok)
{
if (curr_token.id == token_id) {
if (tok)
*tok = curr_token;
else if (curr_token.str)
free(curr_token.str);
if (curr_token.id != token_id)
return false;
curr_token = yylex();
return 1;
} else {
return 0;
}
/* Be sure to free the token string if caller won't be doing it */
if (tok)
*tok = curr_token;
else if (curr_token.str)
free(curr_token.str);
curr_token = yylex();
return true;
}
static void token_expect(enum token_id token_id, struct token *tok)