10000 py/lexer: Add static assert that token enum values all fit in a byte. · scy/micropython@96007e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96007e7

Browse files
committed
py/lexer: Add static assert that token enum values all fit in a byte.
Signed-off-by: Damien George <damien@micropython.org>
1 parent e00d80d commit 96007e7

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

py/lexer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ static const char *const tok_enc =
228228
"=e=" // = ==
229229
"!."; // start of special cases: != . ...
230230

231-
// TODO static assert that number of tokens is less than 256 so we can safely make this table with byte sized entries
232231
static const uint8_t tok_enc_kind[] = {
233232
MP_TOKEN_DEL_PAREN_OPEN, MP_TOKEN_DEL_PAREN_CLOSE,
234233
MP_TOKEN_DEL_BRACKET_OPEN, MP_TOKEN_DEL_BRACKET_CLOSE,
@@ -774,6 +773,9 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
774773
} else {
775774
// search for encoded delimiter or operator
776775

776+
// assert that the token enum value fits in a byte, so they all fit in tok_enc_kind
777+
MP_STATIC_ASSERT(MP_TOKEN_NUMBER_OF <= 256);
778+
777779
const char *t = tok_enc;
778780
size_t tok_enc_index = 0;
779781
for (; *t != 0 && !is_char(lex, *t); t += 1) {

py/lexer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ typedef enum _mp_token_kind_t {
152152
MP_TOKEN_DEL_SEMICOLON,
153153
MP_TOKEN_DEL_EQUAL,
154154
MP_TOKEN_DEL_MINUS_MORE,
155+
156+
MP_TOKEN_NUMBER_OF,
155157
} mp_token_kind_t;
156158

157159
// this data structure is exposed for efficiency

0 commit comments

Comments
 (0)
0