diff options
author | Alex Elder <elder@linaro.org> | 2021-10-01 18:23:34 -0500 |
---|---|---|
committer | Bjorn Andersson <bjorn.andersson@linaro.org> | 2021-10-04 14:17:06 -0500 |
commit | b793b576ffe27001ec2087dcffa0510da9d7734e (patch) | |
tree | 99d7358778b520bb0719b285b93fb38b93dff838 | |
parent | 734ecff5a5fd95bba1858f2da10578ba825a8fe2 (diff) |
parser: introduce symbol_find()
Create a new helper function symbol_find() that determines whether
there exists a defined symbol having the given name. It returns a
pointer to the symbol structure is returned if one exists, or a null
pointer otherwise.
Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-31-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
-rw-r--r-- | parser.c | 30 |
1 files changed, 17 insertions, 13 deletions
@@ -119,10 +119,19 @@ struct symbol { static struct list_head symbols = LIST_INIT(symbols); +static struct symbol *symbol_find(const char *name) +{ + struct symbol *sym; + + list_for_each_entry(sym, &symbols, node) + if (!strcmp(name, sym->name)) + return sym; + return NULL; +} + static bool symbol_valid(const char *name) { const char *p = name; - struct symbol *sym; char ch; /* Symbol name must start with an alphabetic character */ @@ -139,9 +148,8 @@ static bool symbol_valid(const char *name) return 0; /* Finally, symbol names must be unique */ - list_for_each_entry(sym, &symbols, node) - if (!strcmp(name, sym->name)) - return false; + if (symbol_find(name)) + return false; return true; } @@ -233,10 +241,8 @@ static struct token yylex() if (!token.str) yyerror("strdup() failed in %s(), line %d\n", __func__, __LINE__); - list_for_each_entry(sym, &symbols, node) { - if (strcmp(buf, sym->name)) - continue; - + sym = symbol_find(token.str); + if (sym) { token.id = sym->token_id; switch (token.id) { case TOK_MESSAGE: @@ -247,14 +253,12 @@ static struct token yylex() token.qmi_struct = sym->qmi_struct; break; default: - break; + break; /* Others just have id and string */ } - - return token; + } else { + token.id = TOK_ID; /* Just an identifier */ } - token.id = TOK_ID; - return token; } else if (isdigit(ch)) { /* Determine base and valid character set */ |