1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include "lexer.h"
#include <regex.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#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;
}
|