/* * cat/lexer.c -- tokenizer implementation. * * Naming rule: symbols here are cat_lexer_* / CAT_LEXER_*. */ #include "cat/lexer.h" #include #include #include /* Keyword table: name -> token kind. */ struct kw { const char *name; int kind; }; static const struct kw g_keywords[] = { { "and", CAT_LEXER_TOK_AND }, { "break", CAT_LEXER_TOK_BREAK }, { "do", CAT_LEXER_TOK_DO }, { "else", CAT_LEXER_TOK_ELSE }, { "elseif", CAT_LEXER_TOK_ELSEIF }, { "end", CAT_LEXER_TOK_END }, { "false", CAT_LEXER_TOK_FALSE }, { "for", CAT_LEXER_TOK_FOR }, { "function", CAT_LEXER_TOK_FUNCTION }, { "if", CAT_LEXER_TOK_IF }, { "in", CAT_LEXER_TOK_IN }, { "local", CAT_LEXER_TOK_LOCAL }, { "nil", CAT_LEXER_TOK_NIL }, { "not", CAT_LEXER_TOK_NOT }, { "or", CAT_LEXER_TOK_OR }, { "repeat", CAT_LEXER_TOK_REPEAT }, { "return", CAT_LEXER_TOK_RETURN }, { "then", CAT_LEXER_TOK_THEN }, { "true", CAT_LEXER_TOK_TRUE }, { "until", CAT_LEXER_TOK_UNTIL }, { "while", CAT_LEXER_TOK_WHILE }, }; void cat_lexer_init(CAT_LEXER_Lexer *lx, const char *src) { lx->src = src ? src : ""; lx->pos = 0; lx->line = 1; } static int peek(CAT_LEXER_Lexer *lx) { return (unsigned char)lx->src[lx->pos]; } static int peek2(CAT_LEXER_Lexer *lx) { return (unsigned char)lx->src[lx->pos + 1]; } static int advance(CAT_LEXER_Lexer *lx) { int c = lx->src[lx->pos]; if (c == '\0') return -1; lx->pos++; if (c == '\n') lx->line++; return c; } /* Skip whitespace and `--` line comments. */ static void skip_ws(CAT_LEXER_Lexer *lx) { for (;;) { int c = peek(lx); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { advance(lx); } else if (c == '-' && peek2(lx) == '-') { advance(lx); advance(lx); while (peek(lx) != '\n' && peek(lx) != '\0') advance(lx); } else { break; } } } static int lookup_keyword(const char *s) { size_t i; for (i = 0; i < sizeof(g_keywords) / sizeof(g_keywords[0]); i++) if (strcmp(g_keywords[i].name, s) == 0) return g_keywords[i].kind; return CAT_LEXER_TOK_NAME; } int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out) { int c; memset(out, 0, sizeof(*out)); skip_ws(lx); out->line = lx->line; c = peek(lx); if (c == '\0') { out->kind = CAT_LEXER_TOK_EOF; return 0; } /* identifiers / keywords */ if (isalpha(c) || c == '_') { int n = 0; while (isalnum(peek(lx)) || peek(lx) == '_') { if (n < (int)sizeof(out->text) - 1) out->text[n++] = (char)advance(lx); else advance(lx); } out->text[n] = '\0'; out->kind = lookup_keyword(out->text); return 0; } /* numbers: int or float */ if (isdigit(c)) { int isfloat = 0; int n = 0; while (isdigit(peek(lx))) { if (n < (int)sizeof(out->text) - 1) out->text[n++] = (char)advance(lx); else advance(lx); } if (peek(lx) == '.' && isdigit(peek2(lx))) { isfloat = 1; if (n < (int)sizeof(out->text) - 1) out->text[n++] = (char)advance(lx); while (isdigit(peek(lx))) { if (n < (int)sizeof(out->text) - 1) out->text[n++] = (char)advance(lx); else advance(lx); } } out->text[n] = '\0'; if (isfloat) { out->kind = CAT_LEXER_TOK_FLOAT; out->fval = strtod(out->text, NULL); } else { out->kind = CAT_LEXER_TOK_INT; out->ival = strtoll(out->text, NULL, 10); } return 0; } /* strings: '...' or "..." with \n \t \\ \" escapes */ if (c == '"' || c == '\'') { int quote = advance(lx); int n = 0; while (peek(lx) != quote && peek(lx) != '\0') { int ch = advance(lx); if (ch == '\\') { int e = advance(lx); switch (e) { case 'n': ch = '\n'; break; case 't': ch = '\t'; break; case '\\': ch = '\\'; break; case '"': ch = '"'; break; case '\'': ch = '\''; break; default: ch = e; break; } } if (n < (int)sizeof(out->text) - 1) out->text[n++] = (char)ch; } if (peek(lx) == quote) advance(lx); out->text[n] = '\0'; out->kind = CAT_LEXER_TOK_STRING; return 0; } /* symbols */ switch (c) { case '+': advance(lx); out->kind = CAT_LEXER_TOK_PLUS; return 0; case '-': advance(lx); if (peek(lx) == '>') { advance(lx); out->kind = CAT_LEXER_TOK_ARROW; return 0; } out->kind = CAT_LEXER_TOK_MINUS; return 0; case '*': advance(lx); out->kind = CAT_LEXER_TOK_STAR; return 0; case '/': advance(lx); out->kind = CAT_LEXER_TOK_SLASH; return 0; case '%': advance(lx); out->kind = CAT_LEXER_TOK_PERCENT; return 0; case '^': advance(lx); out->kind = CAT_LEXER_TOK_CARET; return 0; case '#': advance(lx); out->kind = CAT_LEXER_TOK_HASH; return 0; case ';': advance(lx); out->kind = CAT_LEXER_TOK_SEMI; return 0; case ',': advance(lx); out->kind = CAT_LEXER_TOK_COMMA; return 0; case '(': advance(lx); out->kind = CAT_LEXER_TOK_LPAREN; return 0; case ')': advance(lx); out->kind = CAT_LEXER_TOK_RPAREN; return 0; case '{': advance(lx); out->kind = CAT_LEXER_TOK_LBRACE; return 0; case '}': advance(lx); out->kind = CAT_LEXER_TOK_RBRACE; return 0; case '[': advance(lx); out->kind = CAT_LEXER_TOK_LBRACKET; return 0; case ']': advance(lx); out->kind = CAT_LEXER_TOK_RBRACKET; return 0; case '=': advance(lx); if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_EQ; } else out->kind = CAT_LEXER_TOK_ASSIGN; return 0; case '~': advance(lx); if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_NE; return 0; } out->kind = CAT_LEXER_TOK_EOF; return 0; case '<': advance(lx); if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_LE; } else out->kind = CAT_LEXER_TOK_LT; return 0; case '>': advance(lx); if (peek(lx) == '=') { advance(lx); out->kind = CAT_LEXER_TOK_GE; } else out->kind = CAT_LEXER_TOK_GT; return 0; case '.': advance(lx); if (peek(lx) == '.') { advance(lx); out->kind = CAT_LEXER_TOK_CONCAT; } else { out->kind = CAT_LEXER_TOK_DOT; } return 0; case ':': advance(lx); if (peek(lx) == ':') { advance(lx); out->kind = CAT_LEXER_TOK_DCOLON; } else out->kind = CAT_LEXER_TOK_COLON; return 0; default: advance(lx); out->kind = CAT_LEXER_TOK_EOF; return 0; } } const char *cat_lexer_kind_name(int kind) { static const char *names[] = { "EOF", "NAME", "INT", "FLOAT", "STRING", "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<", ">", "=", "(", ")", "{", "}", "[", "]", ";", ",", ".", ":", "::", "..", "->" }; if (kind >= 0 && kind < (int)(sizeof(names) / sizeof(names[0]))) return names[kind]; return "?"; }