cusel/compiler/tokenizer.c
Juhani Krekelä eb0e732831 Adopt clang-format
This seems to be as close as I can get it to my style. I'd've wanted to
also set Cpp11BracedListStyle[1] to Block, but that ended up
misformatting some compound literals that included expressions with
binary operators.

I'm also unsure what is going on with the formatting of _Generic().

[1] https://clang.llvm.org/docs/ClangFormatStyleOptions.html#cpp11bracedliststyle
2026-11-26 15:17:13 +02:00

201 lines
3.9 KiB
C

#include "tokenizer.h"
#include "bytestr.h"
#include "vector.h"
static bool identifier_start(char);
static bool identifier(char);
static bool digit(char);
struct token_mapping {
SPAN(char) string;
enum token_id id;
};
struct token_mapping keywords[] = {
{STR("fn"), TOK_FN},
{STR("type"), TOK_TYPE},
{STR("break"), TOK_BREAK},
{STR("continue"), TOK_CONTINUE},
{STR("do"), TOK_DO},
{STR("elif"), TOK_ELIF},
{STR("else"), TOK_ELSE},
{STR("end"), TOK_END},
{STR("for"), TOK_FOR},
{STR("if"), TOK_IF},
{STR("in"), TOK_IN},
{STR("match"), TOK_MATCH},
{STR("of"), TOK_OF},
{STR("return"), TOK_RETURN},
{STR("then"), TOK_THEN},
{STR("while"), TOK_WHILE},
{STR("and"), TOK_AND},
{STR("or"), TOK_OR},
{STR("not"), TOK_NOT},
};
// The code relies on these being sorted such that no token is a prefix
// of the following ones
struct token_mapping multi_char_tokens[] = {
{STR("<<<"), TOK_ROL},
{STR(">>>"), TOK_ROR},
{STR("<<"), TOK_SHL},
{STR(">>"), TOK_SHR},
{STR("<="), TOK_LTE},
{STR(">="), TOK_GTE},
{STR("=="), TOK_EQ},
{STR("!="), TOK_NEQ},
};
char const single_char_tokens[] = "@$.:=,()[]{}<>+-*/%&|^~";
VEC(token) tokenize(SPAN(char) source) {
VEC(token) tokens = {};
size_t offset = 0;
size_t line = 1;
size_t line_start_offset = 0;
for (;;) {
struct source_location at = {
.line = line,
.column = offset - line_start_offset + 1,
.offset = offset,
};
if (LENGTH(source) <= offset) {
VEC_PUSH(
tokens,
((token) {
.at = at,
.length = 0,
.id = TOK_EOF,
})
);
break;
}
auto ch = AT(source, offset);
auto rest = SLICE(source, offset, LENGTH(source));
if (ch == '\n') {
// Update line and column tracking on newline
line++;
offset++;
line_start_offset = offset;
} else if (ch == ' ' || ch == '\t') {
// Ignore other whitespace
offset++;
} else if (starts_with(rest, STR("--"))) {
// Comment until end of the line
offset += 2;
while (offset < LENGTH(source) && AT(source, offset) != '\n') {
offset++;
}
} else if (identifier_start(ch)) {
// Either an identifier or a keyword
auto start = offset;
while (offset < LENGTH(source) && identifier(AT(source, offset))) {
offset++;
}
auto identifier = SLICE(source, start, offset);
enum token_id id = TOK_IDENTIFIER;
for (size_t i = 0; i < sizeof keywords / sizeof keywords[0]; i++) {
auto tok = keywords[i];
if (str_eq(identifier, tok.string)) {
id = tok.id;
break;
}
}
VEC_PUSH(
tokens,
((token) {
.at = at,
.length = offset - start,
.id = id,
})
);
} else if (digit(ch)) {
// Numeric literal
auto start = offset;
while (offset < LENGTH(source) && digit(AT(source, offset))) {
offset++;
}
VEC_PUSH(
tokens,
((token) {
.at = at,
.length = offset - start,
.id = TOK_NATURAL,
})
);
} else {
size_t length = 0;
enum token_id id = 0;
// Does any of the multi character tokens follow?
for (
size_t i = 0;
i < sizeof multi_char_tokens / sizeof multi_char_tokens[0]; i++
) {
auto tok = multi_char_tokens[i];
if (starts_with(rest, tok.string)) {
length = LENGTH(tok.string);
id = tok.id;
}
}
if (!length) {
// No. What about single character tokens?
for (size_t i = 0; single_char_tokens[i]; i++) {
if (ch == single_char_tokens[i]) {
length = 1;
id = single_char_tokens[i];
}
}
}
if (!length) {
// Still no. Add an error token but continue through
// the rest of the file.
length = 1;
id = TOK_ERROR;
}
VEC_PUSH(
tokens,
((token) {
.at = at,
.length = length,
.id = id,
})
);
offset += length;
}
}
return tokens;
}
static bool identifier_start(char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || c == '_';
}
static bool identifier(char c) {
return identifier_start(c) || digit(c);
}
static bool digit(char c) {
return '0' <= c && c <= '9';
}