From 8bbefe95d25fa2981add4df35bdd9903c91a8e62 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 08:13:29 +0300 Subject: publish --- src/lexer.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/lexer.c (limited to 'src/lexer.c') diff --git a/src/lexer.c b/src/lexer.c new file mode 100644 index 0000000..a219c91 --- /dev/null +++ b/src/lexer.c @@ -0,0 +1,172 @@ +#include "lexer.h" +#include +#include +#include +#include +#include +#include "dynamic-array.h" + +#define X(SYM, STR, REG) [LEXEME_KIND_##SYM - 1] = "^(" REG ")", +static const char *lexeme_regex_strings[LEXEME_KIND_COUNT - 1] = { LEXEME_KINDS () }; +#undef X + +static regex_t lexeme_regexes[LEXEME_KIND_COUNT - 1]; + +static void +print_regex_error (const regex_t *comp, const char *regex, int errcode) +{ + size_t length = regerror (errcode, comp, NULL, 0); + char *buffer = malloc (length); + regerror (errcode, comp, buffer, length); + fprintf (stderr, "error: regcomp (\"%s\"): %s\n", + regex, buffer); + free (buffer); +} + +static void __attribute__((constructor)) +compile_regexes (void) +{ + for (enum lexeme_kind kind = 1; kind < LEXEME_KIND_COUNT; ++kind) { + int errcode = regcomp (&lexeme_regexes[kind - 1], + lexeme_regex_strings[kind - 1], + REG_EXTENDED); + if (errcode) { + print_regex_error (&lexeme_regexes[kind - 1], + lexeme_regex_strings[kind - 1], + errcode); + exit (1); + } + } +} + +struct lexer +lexer_create (const char *string, size_t len) +{ + return (struct lexer) { + .sv = { + .beg = string, + .end = string + len, + }, + .cursor = string, + .beginning_of_line = string, + .line = 0, + }; +} + +static void +lexer_skip_whitespace (struct lexer *lexer) +{ + while (lexer->cursor < lexer->sv.end && isspace (*lexer->cursor)) { + if (*lexer->cursor == '\n') { + lexer->line += 1; + lexer->beginning_of_line = lexer->cursor + 1; + } + lexer->cursor += 1; + } +} + +static void +lexer_progress_cursor_by (struct lexer *lexer, int count) +{ + const char *end = lexer->cursor + count; + ASSERT (end <= lexer->sv.end); + + while (lexer->cursor < end) { + if (*lexer->cursor == '\n') { + lexer->line += 1; + lexer->beginning_of_line = lexer->cursor + 1; + } + lexer->cursor += 1; + } +} + +struct lexeme +lexer_next_lexeme (struct lexer *lexer) +{ + lexer_skip_whitespace (lexer); + struct lexeme lexeme = { .sv.beg = lexer->cursor }; + + if (lexer->cursor >= lexer->sv.end) { + lexeme.kind = LEXEME_KIND_END_OF_FILE; + lexeme.sv.end = lexer->cursor; + return lexeme; + } + + regmatch_t regmatch; + enum lexeme_kind longest_matching_lexeme_kind = LEXEME_KIND_INVALID; + int longest_matching_lexeme_kind_length = 0; + for (enum lexeme_kind lexeme_kind = 1; lexeme_kind < LEXEME_KIND_COUNT; ++lexeme_kind) { + if (regexec (&lexeme_regexes[lexeme_kind - 1], lexer->cursor, 1, ®match, 0)) continue; + if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue; + longest_matching_lexeme_kind_length = regmatch.rm_eo; + longest_matching_lexeme_kind = lexeme_kind; + } + + if (longest_matching_lexeme_kind) { + lexeme.kind = longest_matching_lexeme_kind; + lexer_progress_cursor_by (lexer, longest_matching_lexeme_kind_length); + lexeme.sv.end = lexer->cursor; + + switch (lexeme.kind) { + case LEXEME_KIND_KEYWORD: { + for (enum keyword keyword = 0; keyword < KEYWORD_COUNT; ++keyword) { + struct string_view keyword_sv = { + .beg = keyword_names[keyword], + .end = keyword_names[keyword] + strlen (keyword_names[keyword]), + }; if (!STRING_VIEW_EQUALS (lexeme.sv, keyword_sv)) continue; + + lexeme.value.table_index = keyword; + } + } break; + case LEXEME_KIND_SYMBOL_COMPARISON: { + for (enum comparison comparison = 0; comparison < COMPARISON_COUNT; ++comparison) { + struct string_view comparison_sv = { + .beg = comparison_names[comparison], + .end = comparison_names[comparison] + strlen (comparison_names[comparison]), + }; if (!STRING_VIEW_EQUALS (lexeme.sv, comparison_sv)) continue; + + lexeme.value.table_index = comparison; + } + } break; + case LEXEME_KIND_CONSTANT_INTEGER: { + TODO ("fix overflow"); + lexeme.value.integer = strtol (lexeme.sv.beg, NULL, 10); + } break; + case LEXEME_KIND_CONSTANT_REAL: + TODO ("fix overflow"); + lexeme.value.real = strtof (lexeme.sv.beg, NULL); + break; + case LEXEME_KIND_CONSTANT_STRING: { + struct string_view sv = { + .beg = lexeme.sv.beg + 1, + .end = lexeme.sv.end - 1, + }; + DYNAMIC_ARRAY_FOREACH (e, lexer->constant_string_table) { + if (STRING_VIEW_EQUALS (sv, *e)) { + lexeme.value.table_index = e - lexer->constant_string_table.items; + return lexeme; + } + } + lexeme.value.table_index = lexer->constant_string_table.count; + DYNAMIC_ARRAY_APPEND (lexer->constant_string_table, sv); + } break; + case LEXEME_KIND_IDENTIFIER: { + DYNAMIC_ARRAY_FOREACH (e, lexer->identifier_table) { + if (STRING_VIEW_EQUALS (lexeme.sv, *e)) { + lexeme.value.table_index = e - lexer->identifier_table.items; + return lexeme; + } + } + lexeme.value.table_index = lexer->identifier_table.count; + DYNAMIC_ARRAY_APPEND (lexer->identifier_table, lexeme.sv); + } break; + default: + break; + } + + return lexeme; + } + + lexeme.sv.end = ++lexer->cursor; + return lexeme; +} -- cgit v1.3.1