From c172d15f2b38916d2abe4443bdca81f3b7266b1c Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 1 Oct 2021 18:23:23 -0500 Subject: [PATCH] parser: use stronger typing in the symbol structure Refine the definition of the symbol structure with an anonymous union to make it a little clearer whether a token describes a message or a type. In symbol_add(), be more explicit about the type of additional arguments expected to be seen. Add a name to the enumerated type representing the defined "type" type values (U8, STRUCT, etc.). And change the type of the type field in the symbol structure to have that enumerated type. Specifically, if the symbol being added is a message, the argument that follows should be one of the message types (request, response, or indication). And if the symbol being added is a type, the next argument is expected to be one of the "type" types. This makes it a little easier to understand what the code is doing. Signed-off-by: Alex Elder Message-Id: <20211001232338.769309-20-elder@linaro.org> Signed-off-by: Bjorn Andersson --- parser.c | 35 ++++++++++++++++++++++++++--------- qmic.h | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/parser.c b/parser.c index c6a4d75..d5fe5e5 100644 --- a/parser.c +++ b/parser.c @@ -102,8 +102,14 @@ struct symbol { enum token_id token_id; const char *name; - int type; - struct qmi_struct *qmi_struct; + union { + enum message_type message_type; /* TOK_MESSAGE */ + struct { /* TOK_TYPE */ + enum symbol_type symbol_type; + /* TYPE_STRUCT also has a struct pointer */ + struct qmi_struct *qmi_struct; + }; + }; struct list_head node; }; @@ -123,11 +129,11 @@ static void symbol_add(const char *name, enum token_id token_id, ...) switch (token_id) { case TOK_MESSAGE: - sym->type = va_arg(ap, int); + sym->message_type = va_arg(ap, enum message_type); break; case TOK_TYPE: - sym->type = va_arg(ap, int); - if (sym->type == TYPE_STRUCT) + sym->symbol_type = va_arg(ap, enum symbol_type); + if (sym->symbol_type == TYPE_STRUCT) sym->qmi_struct = va_arg(ap, struct qmi_struct *); break; default: @@ -164,12 +170,23 @@ static struct token yylex() yyerror("strdup() failed in %s(), line %d\n", __func__, __LINE__); list_for_each_entry(sym, &symbols, node) { - if (strcmp(buf, sym->name) == 0) { - token.id = sym->token_id; - token.num = sym->type; + if (strcmp(buf, sym->name)) + continue; + + token.id = sym->token_id; + switch (token.id) { + case TOK_MESSAGE: + token.num = sym->message_type; + break; + case TOK_TYPE: + token.num = sym->symbol_type; token.qmi_struct = sym->qmi_struct; - return token; + break; + default: + break; } + + return token; } token.id = TOK_ID; diff --git a/qmic.h b/qmic.h index 482be4c..670adeb 100644 --- a/qmic.h +++ b/qmic.h @@ -7,7 +7,7 @@ #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) -enum { +enum symbol_type { TYPE_U8, TYPE_U16, TYPE_U32,