diff options
author | Alex Elder <elder@linaro.org> | 2021-10-01 18:23:38 -0500 |
---|---|---|
committer | Bjorn Andersson <bjorn.andersson@linaro.org> | 2021-10-04 14:19:51 -0500 |
commit | ed896c97dc2b3b7edcba103e02fd0f3368b56ddd (patch) | |
tree | 0e3d5d35b5be366475446d9968a5419d604522ec | |
parent | 2bd95bd13bbf56cd674fa890e1d91e9bf3fd3c93 (diff) |
parser: add support for constant value substitution
Register symbolic constants as defined symbols when they are
defined. When a constant symbol reference occurs after it's
been defined, the parsed token is modified to be a number type,
whose value is the value constant symbol.
One difference between a "normal" number token and a "constant"
number token is that the the string in a constant token contains
a copy of the symbolic name, whereas its a null pointer for a
"normal" number.
Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-35-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
-rw-r--r-- | parser.c | 12 | ||||
-rw-r--r-- | tests/symbolic_values.qmi | 26 |
2 files changed, 38 insertions, 0 deletions
@@ -42,6 +42,7 @@ enum token_id { TOK_ID, TOK_MESSAGE, TOK_NUM, + TOK_VALUE, TOK_PACKAGE, TOK_STRUCT, TOK_TYPE, @@ -112,6 +113,7 @@ struct symbol { /* TYPE_STRUCT also has a struct pointer */ struct qmi_struct *qmi_struct; }; + unsigned long long value; /* TOK_VALUE */ }; struct list_head node; @@ -200,6 +202,9 @@ static void symbol_add(const char *name, enum token_id token_id, ...) if (sym->symbol_type == TYPE_STRUCT) sym->qmi_struct = va_arg(ap, struct qmi_struct *); break; + case TOK_VALUE: + sym->value = va_arg(ap, unsigned long long); + break; default: break; /* Most tokens are standalone */ } @@ -324,6 +329,11 @@ static struct token yylex() token.num = sym->symbol_type; token.qmi_struct = sym->qmi_struct; break; + case TOK_VALUE: + /* Override token id; use numeric value */ + token.id = TOK_NUM; + token.num = sym->value; + break; default: break; /* Others just have id and string */ } @@ -424,6 +434,8 @@ static void qmi_const_parse() qc->value = num_tok.num; list_add(&qmi_consts, &qc->node); + + symbol_add(qc->name, TOK_VALUE, qc->value); } static void qmi_message_parse(enum message_type message_type) diff --git a/tests/symbolic_values.qmi b/tests/symbolic_values.qmi new file mode 100644 index 0000000..79821b9 --- /dev/null +++ b/tests/symbolic_values.qmi @@ -0,0 +1,26 @@ +package test; + +# Request identifiers +const TEST_REQUEST_RESPONSE = 35; +const TEST_INDICATION = 37; + +# Message field identifiers +const QMI_RESULT = 2; + +struct qmi_result { + u16 result; + u16 error; +}; + +request test_request { + required u8 test_number = 0x12; +} = TEST_REQUEST_RESPONSE; # Value 35 gets substitued + +response test_response { + # Value 2 gets substitued for QMI_RESULT + required qmi_result r = QMI_RESULT; +} = TEST_REQUEST_RESPONSE; + +indication test_indication { + optional u64 value = 0x99; +} = TEST_INDICATION; # Value 37 gets substitued |