-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
242 lines (220 loc) · 5.92 KB
/
file.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "file.h"
#include "dlb_memory.h"
static const char *file_mode_str(file_mode mode) {
const char *str = "";
switch(mode) {
case FILE_READ:
str = "rb";
break;
case FILE_WRITE:
str = "wb";
break;
default:
DLB_ASSERT(0);
}
return str;
}
file *file_open(const char *filename, file_mode mode) {
FILE *hnd = fopen(filename, file_mode_str(mode));
if (!hnd) {
perror("fopen error");
PANIC("Failed to open file: %s\n", filename);
}
file *file = dlb_calloc(1, sizeof(*file));
file->filename = filename;
file->mode = mode;
file->hnd = hnd;
file->pos.line = 1;
file->pos.column = 1;
return file;
}
void file_close(file *f) {
fclose(f->hnd);
free(f);
}
void file_debug_context(file *f)
{
fprintf(stderr, "File: %s:%d:%d\n\n", f->filename, f->pos.line, f->pos.column);
fprintf(stderr, "%04d:%.*s", f->pos.line, f->context_len, f->context_buf);
size_t col = f->pos.column;
char buf[1024] = { 0 };
f->replay = false;
file_read(f, buf, sizeof(buf) - 1, 0, "\r\n", 0);
fprintf(stderr, "%s\n", buf);
fprintf(stderr, " ");
for (u32 i = 0; i < col - 1; i++) {
fprintf(stderr, "-");
}
fprintf(stderr, "^\n\n");
}
char file_char(file *f) {
if (f->replay) {
f->replay = false;
return f->prev;
}
if (f->prev == '\n') {
f->context_len = 0;
f->pos.line++;
f->pos.column = 0;
}
f->pos.column++;
int c = fgetc(f->hnd);
if (c == EOF) {
f->eof = true;
}
if (f->context_len < sizeof(f->context_buf)) {
f->context_buf[f->context_len] = c;
}
f->context_len++;
f->prev = (char)c;
return f->prev;
}
char file_char_escaped(file *f)
{
file_expect_char(f, "\\", 1);
char c = file_char(f);
switch (c) {
case '\"':
c = '\"';
break;
case '\\':
c = '\\';
break;
case 't':
c = '\t';
break;
case 'r':
c = '\r';
break;
case 'n':
c = '\n';
break;
case '0':
c = '\0';
break;
case 'x':
// TODO: Handle hex byte codes
PANIC_FILE(f,
"[PARSE_ERROR] Hex byte codes not yet supported in char "
"literals.\n"
);
break;
case 'u':
// TODO: Handle short unicode code points
PANIC_FILE(f,
"[PARSE_ERROR] Short Unicode hex code points not yet supported "
"in char literals.\n"
);
break;
case 'U':
// TODO: Handle long unicode code points
PANIC_FILE(f,
"[PARSE_ERROR] Long unicode hex code points not yet supported "
"in char literals.\n"
);
break;
case EOF:
PANIC_FILE(f,
"[PARSE_ERROR] Unexpected EOF while reading character.\n"
);
default:
PANIC_FILE(f,
"[PARSE_ERROR] Invalid escape sequence in char literal '%s'."
"\n", char_printable(&c)
);
}
return c;
}
char file_peek(file *f) {
char c = file_char(f);
f->replay = true;
return c;
}
char file_read(file *f, char *buf, size_t count, const char *valid_chars,
const char *delims, int *len)
{
DLB_ASSERT(!buf || count);
DLB_ASSERT(valid_chars || delims || count);
file_pos pos_start = f->pos;
pos_start.column += 1;
char delim = 0;
u32 i;
for (i = 0; !count || i < count; i++) {
char c = file_char(f);
if (f->eof) {
break;
}
delim = str_contains_chr(delims, c);
if (delim) {
f->replay = true;
break;
}
char valid = str_contains_chr(valid_chars, c);
if (valid_chars && !valid) {
if (delims) {
PANIC_FILE(f,
"[PARSE_ERROR] Unexpected character '%s' in expression "
"starting at %d:%d. Expected [%s] or delimeter [%s].\n",
char_printable(&c), (int)pos_start.line,
(int)pos_start.column, valid_chars, delims
);
} else {
f->replay = true;
break;
}
}
if (buf) buf[i] = c;
}
if (delims && !delim) {
PANIC_FILE(f,
"[PARSE_ERROR] Expected delim [%s] to end expression starting at "
"%d:%d\n", delims, (int)pos_start.line, (int)pos_start.column
);
}
if (len) *len = i;
return delim;
}
int file_expect_char(file *f, const char *chars, int times) {
int count;
file_read(f, 0, times, chars, 0, &count);
if (count != times) {
char next = file_peek(f);
PANIC_FILE(f,
"[PARSE_ERROR] Missing expected character [%s]. Found '%s' instead."
"\n", chars, char_printable(&next)
);
}
return count;
}
int file_allow_char(file *f, const char *chars, int times) {
int count;
file_read(f, 0, times, chars, 0, &count);
return count;
}
#if 0
static void file_expect_string(file *f, const char *str) {
const char *s = str;
while (*s) {
char c = file_char(f);
if (c != *s) {
PANIC_FILE(f,
"[PARSE_ERROR] Missing expected character '%s' of string %s. "
"Found '%s' instead.\n", char_printable(s), str,
char_printable(&c)
);
}
s++;
}
}
char *file_read_all(file *f) {
DLB_ASSERT(!fseek(f->hnd, 0, SEEK_END));
long pos = ftell(f->hnd);
DLB_ASSERT(pos <= SIZE_MAX);
DLB_ASSERT(!fseek(f->hnd, 0, SEEK_SET));
size_t end = (size_t)pos;
char *buf = dlb_malloc(end + 1);
fread(buf, end, 1, f->hnd);
buf[end] = 0;
return buf;
}
#endif