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 <elder@linaro.org>
Message-Id: <20211001232338.769309-20-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Alex Elder 2021-10-01 18:23:23 -05:00 committed by Bjorn Andersson
parent 34a471e5e5
commit c172d15f2b
2 changed files with 27 additions and 10 deletions

View File

@ -102,8 +102,14 @@ struct symbol {
enum token_id token_id; enum token_id token_id;
const char *name; const char *name;
int type; union {
struct qmi_struct *qmi_struct; 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; struct list_head node;
}; };
@ -123,11 +129,11 @@ static void symbol_add(const char *name, enum token_id token_id, ...)
switch (token_id) { switch (token_id) {
case TOK_MESSAGE: case TOK_MESSAGE:
sym->type = va_arg(ap, int); sym->message_type = va_arg(ap, enum message_type);
break; break;
case TOK_TYPE: case TOK_TYPE:
sym->type = va_arg(ap, int); sym->symbol_type = va_arg(ap, enum symbol_type);
if (sym->type == TYPE_STRUCT) if (sym->symbol_type == TYPE_STRUCT)
sym->qmi_struct = va_arg(ap, struct qmi_struct *); sym->qmi_struct = va_arg(ap, struct qmi_struct *);
break; break;
default: default:
@ -164,12 +170,23 @@ static struct token yylex()
yyerror("strdup() failed in %s(), line %d\n", yyerror("strdup() failed in %s(), line %d\n",
__func__, __LINE__); __func__, __LINE__);
list_for_each_entry(sym, &symbols, node) { list_for_each_entry(sym, &symbols, node) {
if (strcmp(buf, sym->name) == 0) { if (strcmp(buf, sym->name))
token.id = sym->token_id; continue;
token.num = sym->type;
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; token.qmi_struct = sym->qmi_struct;
return token; break;
default:
break;
} }
return token;
} }
token.id = TOK_ID; token.id = TOK_ID;

2
qmic.h
View File

@ -7,7 +7,7 @@
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
enum { enum symbol_type {
TYPE_U8, TYPE_U8,
TYPE_U16, TYPE_U16,
TYPE_U32, TYPE_U32,