This commit is contained in:
blueloveTH 2024-06-05 21:59:19 +08:00
parent d1e6fdc948
commit c0a855ab7a
2 changed files with 12 additions and 12 deletions

View File

@ -25,9 +25,11 @@ constexpr const char* kTokens[] = {
"**", "=", ">", "<", "..", "...", "->", "@", "==", "!=", ">=", "<=", "**", "=", ">", "<", "..", "...", "->", "@", "==", "!=", ">=", "<=",
"++", "--", "~", "++", "--", "~",
/** KW_BEGIN **/ /** KW_BEGIN **/
"class", "import", "as", "def", "lambda", "pass", "del", "from", "with", "yield", // NOTE: These keywords should be sorted in ascending order!!
"None", "in", "is", "and", "or", "not", "True", "False", "global", "try", "except", "finally", "False", "None", "True", "and", "as", "assert", "break", "class", "continue",
"while", "for", "if", "elif", "else", "break", "continue", "return", "assert", "raise" "def", "del", "elif", "else", "except", "finally", "for", "from", "global",
"if", "import", "in", "is", "lambda", "not", "or", "pass", "raise", "return",
"try", "while", "with", "yield",
}; };
// clang-format on // clang-format on
@ -50,12 +52,6 @@ constexpr TokenIndex TK(const char token[]) {
constexpr inline bool is_raw_string_used(TokenIndex t) { return t == TK("@id") || t == TK("@long"); } constexpr inline bool is_raw_string_used(TokenIndex t) { return t == TK("@id") || t == TK("@long"); }
#define TK_STR(t) kTokens[t] #define TK_STR(t) kTokens[t]
const small_map<std::string_view, TokenIndex> kTokenKwMap = []() {
small_map<std::string_view, TokenIndex> map;
for(int k = TK("class"); k < kTokenCount; k++)
map.insert(kTokens[k], k);
return map;
}();
struct Token { struct Token {
TokenIndex type; TokenIndex type;

View File

@ -153,9 +153,13 @@ int Lexer::eat_name() {
return 0; return 0;
} }
auto it = kTokenKwMap.try_get(name); const auto KW_BEGIN = kTokens + TK("False");
if(it != nullptr) { const auto KW_END = kTokens + kTokenCount;
add_token(*it);
auto it = std::lower_bound(KW_BEGIN, KW_END, name);
if(it != KW_END) {
assert(*it == name);
add_token(it - kTokens);
} else { } else {
add_token(TK("@id")); add_token(TK("@id"));
} }