py/lexer: Add static assert that token enum values all fit in a byte.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2024-07-18 12:44:44 +10:00
parent e00d80d9e2
commit 96007e7de5
2 changed files with 5 additions and 1 deletions

View File

@ -228,7 +228,6 @@ static const char *const tok_enc =
"=e=" // = == "=e=" // = ==
"!."; // start of special cases: != . ... "!."; // start of special cases: != . ...
// TODO static assert that number of tokens is less than 256 so we can safely make this table with byte sized entries
static const uint8_t tok_enc_kind[] = { static const uint8_t tok_enc_kind[] = {
MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE, MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE,
MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE, MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE,
@ -774,6 +773,9 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
} else { } else {
// search for encoded delimiter or operator // search for encoded delimiter or operator
// assert that the token enum value fits in a byte, so they all fit in tok_enc_kind
MP_STATIC_ASSERT(MP_TOKEN_NUMBER_OF <= 256);
const char *t = tok_enc; const char *t = tok_enc;
size_t tok_enc_index = 0; size_t tok_enc_index = 0;
for (; *t != 0 && !is_char(lex, *t); t += 1) { for (; *t != 0 && !is_char(lex, *t); t += 1) {

View File

@ -152,6 +152,8 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_SEMICOLON, MP_TOKEN_DEL_SEMICOLON,
MP_TOKEN_DEL_EQUAL, MP_TOKEN_DEL_EQUAL,
MP_TOKEN_DEL_MINUS_MORE, MP_TOKEN_DEL_MINUS_MORE,
MP_TOKEN_NUMBER_OF,
} mp_token_kind_t; } mp_token_kind_t;
// this data structure is exposed for efficiency // this data structure is exposed for efficiency