diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index 77a6f299b..e89e6005b 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -31,13 +31,96 @@ static inline KDColor TokenColor(mp_token_kind_t tokenKind) { if (tokenKind == MP_TOKEN_INTEGER || tokenKind == MP_TOKEN_FLOAT_OR_IMAG) { return NumberColor; } + static_assert(MP_TOKEN_ELLIPSIS + 1 == MP_TOKEN_KW_FALSE + && MP_TOKEN_KW_FALSE + 1 == MP_TOKEN_KW_NONE + && MP_TOKEN_KW_NONE + 1 == MP_TOKEN_KW_TRUE + && MP_TOKEN_KW_TRUE + 1 == MP_TOKEN_KW___DEBUG__ + && MP_TOKEN_KW___DEBUG__ + 1 == MP_TOKEN_KW_AND + && MP_TOKEN_KW_AND + 1 == MP_TOKEN_KW_AS + && MP_TOKEN_KW_AS + 1 == MP_TOKEN_KW_ASSERT + /* Here there are keywords that depend on MICROPY_PY_ASYNC_AWAIT, we do + * not test them */ + && MP_TOKEN_KW_BREAK + 1 == MP_TOKEN_KW_CLASS + && MP_TOKEN_KW_CLASS + 1 == MP_TOKEN_KW_CONTINUE + && MP_TOKEN_KW_CONTINUE + 1 == MP_TOKEN_KW_DEF + && MP_TOKEN_KW_DEF + 1 == MP_TOKEN_KW_DEL + && MP_TOKEN_KW_DEL + 1 == MP_TOKEN_KW_ELIF + && MP_TOKEN_KW_ELIF + 1 == MP_TOKEN_KW_ELSE + && MP_TOKEN_KW_ELSE + 1 == MP_TOKEN_KW_EXCEPT + && MP_TOKEN_KW_EXCEPT + 1 == MP_TOKEN_KW_FINALLY + && MP_TOKEN_KW_FINALLY + 1 == MP_TOKEN_KW_FOR + && MP_TOKEN_KW_FOR + 1 == MP_TOKEN_KW_FROM + && MP_TOKEN_KW_FROM + 1 == MP_TOKEN_KW_GLOBAL + && MP_TOKEN_KW_GLOBAL + 1 == MP_TOKEN_KW_IF + && MP_TOKEN_KW_IF + 1 == MP_TOKEN_KW_IMPORT + && MP_TOKEN_KW_IMPORT + 1 == MP_TOKEN_KW_IN + && MP_TOKEN_KW_IN + 1 == MP_TOKEN_KW_IS + && MP_TOKEN_KW_IS + 1 == MP_TOKEN_KW_LAMBDA + && MP_TOKEN_KW_LAMBDA + 1 == MP_TOKEN_KW_NONLOCAL + && MP_TOKEN_KW_NONLOCAL + 1 == MP_TOKEN_KW_NOT + && MP_TOKEN_KW_NOT + 1 == MP_TOKEN_KW_OR + && MP_TOKEN_KW_OR + 1 == MP_TOKEN_KW_PASS + && MP_TOKEN_KW_PASS + 1 == MP_TOKEN_KW_RAISE + && MP_TOKEN_KW_RAISE + 1 == MP_TOKEN_KW_RETURN + && MP_TOKEN_KW_RETURN + 1 == MP_TOKEN_KW_TRY + && MP_TOKEN_KW_TRY + 1 == MP_TOKEN_KW_WHILE + && MP_TOKEN_KW_WHILE + 1 == MP_TOKEN_KW_WITH + && MP_TOKEN_KW_WITH + 1 == MP_TOKEN_KW_YIELD + && MP_TOKEN_KW_YIELD + 1 == MP_TOKEN_OP_TILDE, + "MP_TOKEN order changed, so Code::PythonTextArea::TokenColor might need to change too."); if (tokenKind >= MP_TOKEN_KW_FALSE && tokenKind <= MP_TOKEN_KW_YIELD) { return KeywordColor; } - if (tokenKind >= MP_TOKEN_OP_PLUS && tokenKind <= MP_TOKEN_OP_NOT_EQUAL) { - return OperatorColor; - } - if (tokenKind >= MP_TOKEN_DEL_EQUAL && tokenKind <= MP_TOKEN_DEL_MINUS_MORE) { + static_assert(MP_TOKEN_OP_TILDE + 1 == MP_TOKEN_OP_LESS + && MP_TOKEN_OP_LESS + 1 == MP_TOKEN_OP_MORE + && MP_TOKEN_OP_MORE + 1 == MP_TOKEN_OP_DBL_EQUAL + && MP_TOKEN_OP_DBL_EQUAL + 1 == MP_TOKEN_OP_LESS_EQUAL + && MP_TOKEN_OP_LESS_EQUAL + 1 == MP_TOKEN_OP_MORE_EQUAL + && MP_TOKEN_OP_MORE_EQUAL + 1 == MP_TOKEN_OP_NOT_EQUAL + && MP_TOKEN_OP_NOT_EQUAL + 1 == MP_TOKEN_OP_PIPE + && MP_TOKEN_OP_PIPE + 1 == MP_TOKEN_OP_CARET + && MP_TOKEN_OP_CARET + 1 == MP_TOKEN_OP_AMPERSAND + && MP_TOKEN_OP_AMPERSAND + 1 == MP_TOKEN_OP_DBL_LESS + && MP_TOKEN_OP_DBL_LESS + 1 == MP_TOKEN_OP_DBL_MORE + && MP_TOKEN_OP_DBL_MORE + 1 == MP_TOKEN_OP_PLUS + && MP_TOKEN_OP_PLUS + 1 == MP_TOKEN_OP_MINUS + && MP_TOKEN_OP_MINUS + 1 == MP_TOKEN_OP_STAR + && MP_TOKEN_OP_STAR + 1 == MP_TOKEN_OP_AT + && MP_TOKEN_OP_AT + 1 == MP_TOKEN_OP_DBL_SLASH + && MP_TOKEN_OP_DBL_SLASH + 1 == MP_TOKEN_OP_SLASH + && MP_TOKEN_OP_SLASH + 1 == MP_TOKEN_OP_PERCENT + && MP_TOKEN_OP_PERCENT + 1 == MP_TOKEN_OP_DBL_STAR + && MP_TOKEN_OP_DBL_STAR + 1 == MP_TOKEN_DEL_PIPE_EQUAL + && MP_TOKEN_DEL_PIPE_EQUAL + 1 == MP_TOKEN_DEL_CARET_EQUAL + && MP_TOKEN_DEL_CARET_EQUAL + 1 == MP_TOKEN_DEL_AMPERSAND_EQUAL + && MP_TOKEN_DEL_AMPERSAND_EQUAL + 1 == MP_TOKEN_DEL_DBL_LESS_EQUAL + && MP_TOKEN_DEL_DBL_LESS_EQUAL + 1 == MP_TOKEN_DEL_DBL_MORE_EQUAL + && MP_TOKEN_DEL_DBL_MORE_EQUAL + 1 == MP_TOKEN_DEL_PLUS_EQUAL + && MP_TOKEN_DEL_PLUS_EQUAL + 1 == MP_TOKEN_DEL_MINUS_EQUAL + && MP_TOKEN_DEL_MINUS_EQUAL + 1 == MP_TOKEN_DEL_STAR_EQUAL + && MP_TOKEN_DEL_STAR_EQUAL + 1 == MP_TOKEN_DEL_AT_EQUAL + && MP_TOKEN_DEL_AT_EQUAL + 1 == MP_TOKEN_DEL_DBL_SLASH_EQUAL + && MP_TOKEN_DEL_DBL_SLASH_EQUAL + 1 == MP_TOKEN_DEL_SLASH_EQUAL + && MP_TOKEN_DEL_SLASH_EQUAL + 1 == MP_TOKEN_DEL_PERCENT_EQUAL + && MP_TOKEN_DEL_PERCENT_EQUAL + 1 == MP_TOKEN_DEL_DBL_STAR_EQUAL + && MP_TOKEN_DEL_DBL_STAR_EQUAL + 1 == MP_TOKEN_DEL_PAREN_OPEN + && MP_TOKEN_DEL_PAREN_OPEN + 1 == MP_TOKEN_DEL_PAREN_CLOSE + && MP_TOKEN_DEL_PAREN_CLOSE + 1 == MP_TOKEN_DEL_BRACKET_OPEN + && MP_TOKEN_DEL_BRACKET_OPEN + 1 == MP_TOKEN_DEL_BRACKET_CLOSE + && MP_TOKEN_DEL_BRACKET_CLOSE + 1 == MP_TOKEN_DEL_BRACE_OPEN + && MP_TOKEN_DEL_BRACE_OPEN + 1 == MP_TOKEN_DEL_BRACE_CLOSE + && MP_TOKEN_DEL_BRACE_CLOSE + 1 == MP_TOKEN_DEL_COMMA + && MP_TOKEN_DEL_COMMA + 1 == MP_TOKEN_DEL_COLON + && MP_TOKEN_DEL_COLON + 1 == MP_TOKEN_DEL_PERIOD + && MP_TOKEN_DEL_PERIOD + 1 == MP_TOKEN_DEL_SEMICOLON + && MP_TOKEN_DEL_SEMICOLON + 1 == MP_TOKEN_DEL_EQUAL + && MP_TOKEN_DEL_EQUAL + 1 == MP_TOKEN_DEL_MINUS_MORE, + "MP_TOKEN order changed, so Code::PythonTextArea::TokenColor might need to change too."); + + if ((tokenKind >= MP_TOKEN_OP_TILDE && tokenKind <= MP_TOKEN_DEL_DBL_STAR_EQUAL) + || tokenKind == MP_TOKEN_DEL_EQUAL + || tokenKind == MP_TOKEN_DEL_MINUS_MORE) + { return OperatorColor; } return DefaultColor;