From 624dcc7cc652de82d75ddd6ea9919e83340093d3 Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 1 Oct 2021 18:23:36 -0500 Subject: [PATCH] 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 Message-Id: <20211001232338.769309-33-elder@linaro.org> Signed-off-by: Bjorn Andersson --- parser.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/parser.c b/parser.c index ffd8249..7c47cf5 100644 --- a/parser.c +++ b/parser.c @@ -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)