WIP
This commit is contained in:
@@ -4,13 +4,11 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
scanner_t make_scanner(const char *name, const char *buffer, allocator_t allocator) {
|
||||
scanner_t make_scanner(char const *name, char const *buffer, allocator_t allocator) {
|
||||
scanner_t s = {
|
||||
.name = name,
|
||||
.start = buffer,
|
||||
.cur = buffer,
|
||||
.value = {0},
|
||||
.errors = NULL,
|
||||
.allocator = allocator
|
||||
};
|
||||
return s;
|
||||
@@ -24,37 +22,28 @@ void scanner_recover(scanner_t *s) {
|
||||
s->cur++; // move *past* the newline
|
||||
}
|
||||
|
||||
void scanner_error(scanner_t *s, const char *message) {
|
||||
const char *error_pos = s->cur; // use current scanner position
|
||||
scan_result_t scanner_error(scanner_t *s, char const * message) {
|
||||
char const *error_pos = s->cur; // use current scanner position
|
||||
// Compute line + column (1-based)
|
||||
int line = 1, col = 1;
|
||||
for (const char *p = s->start; p < error_pos; p++) {
|
||||
if (*p == '\n') { line++; col = 1; }
|
||||
else col++;
|
||||
}
|
||||
// need 40 bytes for pair of largest possible 64 bit values, then some for
|
||||
// ':' and ' ' characters. Just rounded up to 64 because reasons.
|
||||
size_t length = strlen(s->name) + 1 + strlen(message) + 1 + 64;
|
||||
char *buf = allocator_alloc(s->allocator, length);
|
||||
snprintf(
|
||||
buf,
|
||||
length,
|
||||
"%s:%d:%d: %s",
|
||||
s->name, line, col, message
|
||||
);
|
||||
error_node_t *node = allocator_alloc(s->allocator, sizeof(error_node_t));
|
||||
node->msg = buf;
|
||||
node->next = NULL;
|
||||
if (!s->errors) {
|
||||
s->errors = node;
|
||||
} else {
|
||||
error_node_t *p = s->errors;
|
||||
while (p->next) p = p->next;
|
||||
p->next = node;
|
||||
int line = 1, column = 1;
|
||||
for (char const *p = s->start; p < error_pos; p++) {
|
||||
if (*p == '\n') { line++; column = 1; }
|
||||
else column++;
|
||||
}
|
||||
|
||||
return (scan_result_t) {
|
||||
.type=scan_result_type_error,
|
||||
.error=(scan_error_t) {
|
||||
.message=message,
|
||||
.buffer_name=s->name,
|
||||
.line=line,
|
||||
.column=column
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void scanner_error_and_recover(scanner_t *s, const char *message) {
|
||||
void scanner_error_and_recover(scanner_t *s, char const *message) {
|
||||
scanner_error(s, message);
|
||||
scanner_recover(s);
|
||||
}
|
||||
@@ -63,8 +52,8 @@ bool scan_eof(scanner_t *s) {
|
||||
return *s->cur == '\0';
|
||||
}
|
||||
|
||||
bool scan_literal(scanner_t *s, const char *lit) {
|
||||
const char *save = s->cur;
|
||||
bool scan_literal(scanner_t *s, char const *lit) {
|
||||
char const *save = s->cur;
|
||||
while (*lit && *lit == *s->cur) {
|
||||
lit++;
|
||||
s->cur++;
|
||||
@@ -76,7 +65,7 @@ bool scan_literal(scanner_t *s, const char *lit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int scan_repeat_literal(scanner_t *s, const char *lit) {
|
||||
int scan_repeat_literal(scanner_t *s, char const *lit) {
|
||||
int n_repeats = 0;
|
||||
while (scan_literal(s, lit)) {
|
||||
n_repeats++;
|
||||
@@ -85,26 +74,36 @@ int scan_repeat_literal(scanner_t *s, const char *lit) {
|
||||
}
|
||||
|
||||
bool scan_whitespace(scanner_t *s) {
|
||||
const char *save = s->cur;
|
||||
char const *save = s->cur;
|
||||
while (isspace((unsigned char)*s->cur)) s->cur++;
|
||||
return save != s->cur;
|
||||
}
|
||||
|
||||
bool scan_digit(scanner_t *s) {
|
||||
scan_result_t scan_digit(scanner_t *s) {
|
||||
if (!isdigit((unsigned char)*s->cur)) {
|
||||
return false;
|
||||
return (scan_result_t){
|
||||
.type=scan_result_type_none,
|
||||
};
|
||||
}
|
||||
s->value.digit = (*s->cur) - '0';
|
||||
scan_result_t ret = (scan_result_t){
|
||||
.type=scan_result_type_value,
|
||||
.value=(scan_value_t){
|
||||
.digit=(*s->cur) - '0'
|
||||
}
|
||||
};
|
||||
s->cur++;
|
||||
return true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool scan_i64(scanner_t *s) {
|
||||
const char *save = s->cur;
|
||||
scan_result_t scan_i64(scanner_t *s) {
|
||||
char const *save = s->cur;
|
||||
if (*s->cur == '-' || *s->cur == '+') s->cur++;
|
||||
if (!isdigit((unsigned char)*s->cur)) {
|
||||
s->cur = save;
|
||||
return false;
|
||||
return (scan_result_t) {
|
||||
.type=scan_result_type_none,
|
||||
};
|
||||
// TODO: make macros for each of the scan_result_t types.
|
||||
}
|
||||
s->cur = save;
|
||||
char *end;
|
||||
@@ -112,11 +111,12 @@ bool scan_i64(scanner_t *s) {
|
||||
int64_t val = strtoll(s->cur, &end, 10);
|
||||
if (end == s->cur) {
|
||||
s->cur = save;
|
||||
return false;
|
||||
return (scan_result_t){
|
||||
.type=scan_result_type_none,
|
||||
};
|
||||
}
|
||||
if (errno == ERANGE) {
|
||||
scanner_error(s, "integer does not fit in i64 value");
|
||||
return false;
|
||||
return scanner_error(s, "integer does not fit in i64 value");
|
||||
}
|
||||
s->cur = end;
|
||||
s->value.i64 = val;
|
||||
@@ -166,7 +166,7 @@ bool scan_i8(scanner_t *s) {
|
||||
}
|
||||
|
||||
bool scan_u64(scanner_t *s) {
|
||||
const char *save = s->cur;
|
||||
char const *save = s->cur;
|
||||
if (*s->cur == '-') {
|
||||
scanner_error(s, "- is not allowed for unsigned integers");
|
||||
return false;
|
||||
@@ -236,7 +236,7 @@ bool scan_u8(scanner_t *s) {
|
||||
}
|
||||
|
||||
bool scan_f64(scanner_t *s) {
|
||||
const char *save = s->cur;
|
||||
char const *save = s->cur;
|
||||
char *end;
|
||||
errno = 0;
|
||||
double val = strtod(s->cur, &end);
|
||||
@@ -271,7 +271,7 @@ bool scan_identifier(scanner_t *s) {
|
||||
if (!isalpha((unsigned char)*s->cur) && *s->cur != '_') {
|
||||
return false;
|
||||
}
|
||||
const char *start = s->cur++;
|
||||
char const *start = s->cur++;
|
||||
while (isalnum((unsigned char)*s->cur) || *s->cur == '_') {
|
||||
s->cur++;
|
||||
}
|
||||
@@ -285,7 +285,7 @@ bool scan_identifier(scanner_t *s) {
|
||||
|
||||
|
||||
bool scan_string_literal(scanner_t *s) {
|
||||
const char *save = s->cur;
|
||||
char const *save = s->cur;
|
||||
if (*s->cur != '"')
|
||||
return false;
|
||||
s->cur++; // skip opening quote
|
||||
@@ -334,7 +334,7 @@ bool scanner_print_errors(scanner_t *s, FILE *fp) {
|
||||
}
|
||||
|
||||
bool looks_like_float(scanner_t *s) {
|
||||
const char *cur = s->cur;
|
||||
char const *cur = s->cur;
|
||||
if (
|
||||
((*cur) == '+') ||
|
||||
((*cur) == '-')
|
||||
@@ -358,7 +358,7 @@ bool looks_like_float(scanner_t *s) {
|
||||
}
|
||||
|
||||
bool looks_like_int(scanner_t *s) {
|
||||
const char *cur = s->cur;
|
||||
char const *cur = s->cur;
|
||||
if (*cur == '+' || *cur == '-') {
|
||||
cur++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user