83 lines
2.9 KiB
C
Raw Normal View History

2025-12-02 06:54:32 +01:00
#include "lexer.h"
#include <string.h>
typedef struct {
const char *keyword;
RavaTokenType_e type;
} RavaKeyword_t;
static const RavaKeyword_t KEYWORDS[] = {
{"abstract", RAVA_TOKEN_KEYWORD_ABSTRACT},
{"assert", RAVA_TOKEN_KEYWORD_ASSERT},
{"boolean", RAVA_TOKEN_KEYWORD_BOOLEAN},
{"break", RAVA_TOKEN_KEYWORD_BREAK},
{"byte", RAVA_TOKEN_KEYWORD_BYTE},
{"case", RAVA_TOKEN_KEYWORD_CASE},
{"catch", RAVA_TOKEN_KEYWORD_CATCH},
{"char", RAVA_TOKEN_KEYWORD_CHAR},
{"class", RAVA_TOKEN_KEYWORD_CLASS},
{"const", RAVA_TOKEN_KEYWORD_CONST},
{"continue", RAVA_TOKEN_KEYWORD_CONTINUE},
{"default", RAVA_TOKEN_KEYWORD_DEFAULT},
{"do", RAVA_TOKEN_KEYWORD_DO},
{"double", RAVA_TOKEN_KEYWORD_DOUBLE},
{"else", RAVA_TOKEN_KEYWORD_ELSE},
{"enum", RAVA_TOKEN_KEYWORD_ENUM},
{"extends", RAVA_TOKEN_KEYWORD_EXTENDS},
{"false", RAVA_TOKEN_LITERAL_FALSE},
{"final", RAVA_TOKEN_KEYWORD_FINAL},
{"finally", RAVA_TOKEN_KEYWORD_FINALLY},
{"float", RAVA_TOKEN_KEYWORD_FLOAT},
{"for", RAVA_TOKEN_KEYWORD_FOR},
{"goto", RAVA_TOKEN_KEYWORD_GOTO},
{"if", RAVA_TOKEN_KEYWORD_IF},
{"implements", RAVA_TOKEN_KEYWORD_IMPLEMENTS},
{"import", RAVA_TOKEN_KEYWORD_IMPORT},
{"instanceof", RAVA_TOKEN_KEYWORD_INSTANCEOF},
{"int", RAVA_TOKEN_KEYWORD_INT},
{"interface", RAVA_TOKEN_KEYWORD_INTERFACE},
{"long", RAVA_TOKEN_KEYWORD_LONG},
{"native", RAVA_TOKEN_KEYWORD_NATIVE},
{"new", RAVA_TOKEN_KEYWORD_NEW},
{"null", RAVA_TOKEN_LITERAL_NULL},
{"package", RAVA_TOKEN_KEYWORD_PACKAGE},
{"private", RAVA_TOKEN_KEYWORD_PRIVATE},
{"protected", RAVA_TOKEN_KEYWORD_PROTECTED},
{"public", RAVA_TOKEN_KEYWORD_PUBLIC},
{"return", RAVA_TOKEN_KEYWORD_RETURN},
{"short", RAVA_TOKEN_KEYWORD_SHORT},
{"static", RAVA_TOKEN_KEYWORD_STATIC},
{"strictfp", RAVA_TOKEN_KEYWORD_STRICTFP},
{"super", RAVA_TOKEN_KEYWORD_SUPER},
{"switch", RAVA_TOKEN_KEYWORD_SWITCH},
{"synchronized", RAVA_TOKEN_KEYWORD_SYNCHRONIZED},
{"this", RAVA_TOKEN_KEYWORD_THIS},
{"throw", RAVA_TOKEN_KEYWORD_THROW},
{"throws", RAVA_TOKEN_KEYWORD_THROWS},
{"transient", RAVA_TOKEN_KEYWORD_TRANSIENT},
{"true", RAVA_TOKEN_LITERAL_TRUE},
{"try", RAVA_TOKEN_KEYWORD_TRY},
{"void", RAVA_TOKEN_KEYWORD_VOID},
{"volatile", RAVA_TOKEN_KEYWORD_VOLATILE},
{"while", RAVA_TOKEN_KEYWORD_WHILE},
};
static const size_t KEYWORDS_COUNT = sizeof(KEYWORDS) / sizeof(KEYWORDS[0]);
RavaTokenType_e rava_lexer_lookup_keyword(const char *identifier) {
2025-12-03 15:01:02 +01:00
size_t low = 0;
size_t high = KEYWORDS_COUNT;
while (low < high) {
size_t mid = low + (high - low) / 2;
int cmp = strcmp(identifier, KEYWORDS[mid].keyword);
if (cmp == 0) {
return KEYWORDS[mid].type;
} else if (cmp < 0) {
high = mid;
} else {
low = mid + 1;
2025-12-02 06:54:32 +01:00
}
}
return RAVA_TOKEN_IDENTIFIER;
}