aboutsummaryrefslogtreecommitdiff
path: root/src/lex.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:13:29 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:13:29 +0000
commit8bbefe95d25fa2981add4df35bdd9903c91a8e62 (patch)
treef45cabee854e1ef7e4b4ce82d4ceced0a47ae2a5 /src/lex.c
downloadlab-01-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/lex.c')
-rw-r--r--src/lex.c220
1 files changed, 220 insertions, 0 deletions
diff --git a/src/lex.c b/src/lex.c
new file mode 100644
index 0000000..3b10266
--- /dev/null
+++ b/src/lex.c
@@ -0,0 +1,220 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <stdint.h>
4#include <stdarg.h>
5#include <string.h>
6#include <assert.h>
7#include <regex.h>
8#include <errno.h>
9#include <ctype.h>
10#include <fcntl.h>
11#include <unistd.h>
12#include <sys/stat.h>
13#include <sys/mman.h>
14#include "utils.h"
15#include "string-builder.h"
16#include "lexer.h"
17
18const char *file;
19
20static void
21print_lexing_error (const struct lexer *lexer,
22 const struct lexeme *lexeme,
23 const char *fmt, ...)
24{
25 const char *beginning_of_line = lexer->sv.beg;
26 size_t line = 1;
27
28 for (const char *p = lexer->sv.beg; p < lexeme->sv.beg; ++p) {
29 if (*p == '\n') {
30 beginning_of_line = p + 1;
31 line += 1;
32 }
33 }
34
35 fprintf (stderr, "%s:%d:%d: error: ", file, (int) line,
36 (int) (lexeme->sv.beg - beginning_of_line));
37 va_list ap;
38 va_start (ap, fmt);
39 vfprintf (stderr, fmt, ap);
40 va_end (ap);
41 putc ('\n', stderr);
42
43 const char *p = beginning_of_line;
44 fprintf (stderr, "%5lu | ", line);
45 while (*p != '\n')
46 putc (*p++, stderr);
47 putc ('\n', stderr);
48
49 p = beginning_of_line;
50 fprintf (stderr, " | ");
51 while (p++ < lexeme->sv.beg)
52 putc (' ', stderr);
53 putc ('^', stderr);
54 while (p++ < lexeme->sv.end)
55 putc ('~', stderr);
56 putc ('\n', stderr);
57}
58
59static int
60display_to_byte_len (const char *str, int len)
61{
62 while (*str) {
63 if (*str++ & 0xD0) {
64 len += 1;
65 str += 1;
66 }
67 }
68
69 return len;
70}
71
72int
73main (int argc, char *argv[])
74{
75 const char *progname = *argv++;
76 if (argc < 2) DIE ("Usage: %s file", progname);
77
78 file = *argv++;
79 int fd = open (file, O_RDONLY);
80 if (fd == -1) DIE ("error: %s: %s", strerror (errno), file);
81
82 struct stat st;
83 fstat (fd, &st);
84 char *string = mmap (NULL, st.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0);
85 close (fd);
86
87 puts ("Исходный текст:");
88 fwrite (string, 1, st.st_size, stdout);
89
90 FILE *dump = fopen ("./build/dump.txt", "w");
91
92 struct {
93 struct lexeme *items;
94 uint32_t count;
95 uint32_t capacity;
96 } invalid_lexemes = {0};
97
98 struct string_builder sb = {0};
99 struct lexer lexer = lexer_create (string, st.st_size);
100 putchar ('\n');
101 printf ("| *Лексема* | *Тип лексемы* | *Значение* |\n");
102 printf ("|------------------+-----------------------------+------------|\n");
103 for (struct lexeme lexeme = lexer_next_lexeme (&lexer);
104 lexeme.kind != LEXEME_KIND_END_OF_FILE;
105 lexeme = lexer_next_lexeme (&lexer)) {
106 switch (lexeme.kind) {
107 case LEXEME_KIND_INVALID:
108 case LEXEME_KIND_IDENTIFIER_TOO_LONG:
109 case LEXEME_KIND_CONSTANT_STRING_TOO_LONG:
110 case LEXEME_KIND_CONSTANT_NUMBER_INVALID:
111 case LEXEME_KIND_COMMENT_UNTERMINATED:
112 DYNAMIC_ARRAY_APPEND (invalid_lexemes, lexeme);
113 continue;
114 default: break;
115 }
116
117 if (lexeme.kind == LEXEME_KIND_COMMENT)
118 continue;
119
120 fwrite (lexeme.sv.beg, STRING_VIEW_LEN (lexeme.sv), 1, dump);
121 putc (' ', dump);
122
123 switch (lexeme.kind) {
124 case LEXEME_KIND_KEYWORD:
125 string_builder_printf (&sb, "K[%d]", lexeme.value.table_index);
126 break;
127 case LEXEME_KIND_SYMBOL_COMPARISON:
128 string_builder_printf (&sb, "C[%d]", lexeme.value.table_index);
129 break;
130 case LEXEME_KIND_CONSTANT_INTEGER:
131 string_builder_printf (&sb, "%d", lexeme.value.integer);
132 break;
133 case LEXEME_KIND_CONSTANT_REAL:
134 string_builder_printf (&sb, "%g", lexeme.value.real);
135 break;
136 case LEXEME_KIND_CONSTANT_STRING:
137 string_builder_printf (&sb, "S[%d]", lexeme.value.table_index);
138 break;
139 case LEXEME_KIND_IDENTIFIER:
140 string_builder_printf (&sb, "I[%d]", lexeme.value.table_index);
141 break;
142 default:
143 STRING_BUILDER_APPEND_CSTR (sb, "");
144 }
145 string_builder_append_ntimes (&sb, ' ', 10 - sb.count);
146 STRING_BUILDER_APPEND_NULL (sb);
147 printf ("| %-16.*s | %-*s | %s |\n",
148 (int) STRING_VIEW_LEN (lexeme.sv), lexeme.sv.beg,
149 display_to_byte_len (lexeme_kind_names[lexeme.kind - 1], 27),
150 lexeme_kind_names[lexeme.kind - 1],
151 sb.items);
152 STRING_BUILDER_RESET (sb);
153 }
154
155 fclose (dump);
156
157 putchar ('\n');
158 printf ("| *№* | *K* |\n");
159 printf ("|-----+-----|\n");
160 for (int32_t i = 0; i < KEYWORD_COUNT; ++i)
161 printf ("| %3d | %-3s |\n", i, keyword_names[i]);
162
163 putchar ('\n');
164 printf ("| *№* | *C* |\n");
165 printf ("|-----+-----|\n");
166 for (int32_t i = 0; i < COMPARISON_COUNT; ++i)
167 printf ("| %3d | %-3s |\n", i, comparison_names[i]);
168
169 putchar ('\n');
170 printf ("| *№* | *S* |\n");
171 printf ("|-----+----------------|\n");
172 DYNAMIC_ARRAY_FOREACH (e, lexer.constant_string_table) {
173 printf ("| %3ld | %-14.*s |\n", e - lexer.constant_string_table.items,
174 (int) STRING_VIEW_LEN (*e), e->beg);
175 }
176
177 putchar ('\n');
178 printf ("| *№* | *I* |\n");
179 printf ("|-----+------------------|\n");
180 DYNAMIC_ARRAY_FOREACH (e, lexer.identifier_table) {
181 printf ("| %3ld | %-16.*s |\n", e - lexer.identifier_table.items,
182 (int) STRING_VIEW_LEN (*e), e->beg);
183 }
184
185 DYNAMIC_ARRAY_FOREACH (invalid_lexeme, invalid_lexemes) {
186 switch (invalid_lexeme->kind) {
187 case LEXEME_KIND_INVALID:
188 print_lexing_error (&lexer, invalid_lexeme, "invalid lexeme `%.*s'",
189 (int) STRING_VIEW_LEN (invalid_lexeme->sv),
190 invalid_lexeme->sv.beg);
191 break;
192 case LEXEME_KIND_CONSTANT_STRING_TOO_LONG:
193 print_lexing_error (&lexer, invalid_lexeme,
194 "string constant longer than 16 symbols (%d)",
195 (int) STRING_VIEW_LEN (invalid_lexeme->sv));
196 break;
197 case LEXEME_KIND_IDENTIFIER_TOO_LONG:
198 print_lexing_error (&lexer, invalid_lexeme,
199 "identifier longer than 16 symbols (%d)",
200 (int) STRING_VIEW_LEN (invalid_lexeme->sv));
201 break;
202 case LEXEME_KIND_CONSTANT_NUMBER_INVALID:
203 print_lexing_error (&lexer, invalid_lexeme, "invalid number constant `%.*s'",
204 (int) STRING_VIEW_LEN (invalid_lexeme->sv),
205 invalid_lexeme->sv.beg);
206 break;
207 case LEXEME_KIND_COMMENT_UNTERMINATED:
208 invalid_lexeme->sv.end = invalid_lexeme->sv.beg + 2;
209 print_lexing_error (&lexer, invalid_lexeme, "unterminated comment",
210 (int) STRING_VIEW_LEN (invalid_lexeme->sv),
211 invalid_lexeme->sv.beg);
212 break;
213 default: break;
214 }
215 }
216
217 munmap (string, st.st_size);
218
219 return EXIT_SUCCESS;
220}