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>
This commit is contained in:
Alex Elder 2021-10-01 18:23:38 -05:00 committed by Bjorn Andersson
parent 2bd95bd13b
commit ed896c97dc
2 changed files with 38 additions and 0 deletions

View File

@ -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)

26
tests/symbolic_values.qmi Normal file
View File

@ -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