some progress. test printing of errors later
This commit is contained in:
@@ -251,9 +251,9 @@ scan_result_t scan_f32(scanner_t *s) {
|
||||
return scan_value(.f32=val);
|
||||
}
|
||||
|
||||
bool scan_identifier(scanner_t *s) {
|
||||
scan_result_t scan_identifier(scanner_t *s) {
|
||||
if (!isalpha((unsigned char)*s->cur) && *s->cur != '_') {
|
||||
return false;
|
||||
return scan_error(s, "not an identifier");
|
||||
}
|
||||
char const *start = s->cur++;
|
||||
while (isalnum((unsigned char)*s->cur) || *s->cur == '_') {
|
||||
@@ -263,21 +263,21 @@ bool scan_identifier(scanner_t *s) {
|
||||
char *buf = allocator_alloc(s->allocator, len + 1);
|
||||
memcpy(buf, start, len);
|
||||
buf[len] = '\0';
|
||||
s->value.identifier = buf;
|
||||
return true;
|
||||
return scan_value(.identifier=buf);
|
||||
}
|
||||
|
||||
|
||||
bool scan_string_literal(scanner_t *s) {
|
||||
scan_result_t scan_string_literal(scanner_t *s) {
|
||||
char const *save = s->cur;
|
||||
if (*s->cur != '"')
|
||||
return false;
|
||||
return scan_error(s, "not a string");
|
||||
s->cur++; // skip opening quote
|
||||
char *buf = allocator_alloc(s->allocator, 256);
|
||||
size_t cap = 256;
|
||||
size_t len = 0;
|
||||
while (*s->cur && *s->cur != '"') {
|
||||
char c = *s->cur++;
|
||||
scan_result_t ret;
|
||||
if (c == '\\') {
|
||||
char esc = *s->cur++;
|
||||
switch (esc) {
|
||||
@@ -288,9 +288,9 @@ bool scan_string_literal(scanner_t *s) {
|
||||
case '"': c = '"'; break;
|
||||
case '0': c = '\0'; break;
|
||||
default:
|
||||
scanner_error(s, "invalid escape sequence");
|
||||
ret = scan_error(s, "invalid escape sequence");
|
||||
s->cur = save;
|
||||
return false;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (len + 2 >= cap) {
|
||||
@@ -300,21 +300,26 @@ bool scan_string_literal(scanner_t *s) {
|
||||
buf[len++] = c;
|
||||
}
|
||||
if (*s->cur != '"') {
|
||||
scanner_error(s, "unterminated string literal");
|
||||
scan_result_t ret = scan_error(s, "unterminated string literal");
|
||||
s->cur = save;
|
||||
return false;
|
||||
return ret;
|
||||
}
|
||||
s->cur++; // skip closing quote
|
||||
buf[len] = '\0';
|
||||
s->value.string_literal = buf;
|
||||
return true;
|
||||
return scan_value(.string_literal=buf);
|
||||
}
|
||||
|
||||
bool scanner_print_errors(scanner_t *s, FILE *fp) {
|
||||
for (error_node_t *node = s->errors; node != NULL; node = node->next) {
|
||||
fprintf(fp, "%s\n", node->msg);
|
||||
bool scanner_print_errors_func(FILE *fp, scan_result_t const *results, int results_len) {
|
||||
bool did_print_errors = false;
|
||||
for (int i = 0; i < results_len; i++) {
|
||||
scan_result_t res = results[i];
|
||||
if (!res.ok) {
|
||||
scan_error_t err = res.error;
|
||||
fprintf(fp, "%s:%d:%d: %s", err.buffer_name, err.line, err.column, err.message);
|
||||
did_print_errors = true;
|
||||
}
|
||||
}
|
||||
return s->errors != NULL;
|
||||
return did_print_errors;
|
||||
}
|
||||
|
||||
bool looks_like_float(scanner_t *s) {
|
||||
|
||||
Reference in New Issue
Block a user