summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Elder <elder@linaro.org>2021-10-01 18:23:36 -0500
committerBjorn Andersson <bjorn.andersson@linaro.org>2021-10-04 14:18:49 -0500
commit624dcc7cc652de82d75ddd6ea9919e83340093d3 (patch)
tree60785f48c76d16f05f67130e9e5e2bfc86ba09d4
parentfae755df216617dd132bb72645e1287167726c72 (diff)
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>
-rw-r--r--parser.c25
1 files changed, 13 insertions, 12 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);
-
- curr_token = yylex();
- return 1;
- } else {
- return 0;
- }
+ if (curr_token.id != token_id)
+ return false;
+
+ /* 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)