Squash merge feature/scanner_v2 into main
C Tests / Build and Test (push) Successful in 10m52s

This commit is contained in:
2026-10-01 22:19:09 +02:00
parent 27c5c4a55b
commit af356a0d71
10 changed files with 260 additions and 247 deletions
-23
View File
@@ -1,23 +0,0 @@
snippet testfile
#include <criterion/criterion.h>
#include "allocator.h"
Test($1, $2) {
$0
}
snippet allocator
#include "allocator.h"
static void *${1}_alloc(void *this, size_t bytes, const char *file, int line) {
}
static void *${1}_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) {
}
static void ${1}_free(void *this, void *ptr, const char *file, int line) {
}
static const allocator_vtbl_t ${1}_vtbl = {
.alloc = ${1}_alloc,
.resize = ${1}_resize,
.free = ${1}_free,
};
+2 -2
View File
@@ -101,7 +101,7 @@ int main(void) {
#define CIG_IMPL #define CIG_IMPL
#include "cig/cig.h" #include "cig/cig.h"
int main(int argc, const char **argv) { int main(int argc, char const **argv) {
args_t args = cli_make_args(argc, argv); args_t args = cli_make_args(argc, argv);
if (cli_command(&args, "say-foo")) { if (cli_command(&args, "say-foo")) {
@@ -112,7 +112,7 @@ int main(int argc, const char **argv) {
} else } else
if (cli_command(&args), "greet") { if (cli_command(&args), "greet") {
bool do_nothing = cli_bool(args, "--do-nothing"); bool do_nothing = cli_bool(args, "--do-nothing");
const char *name = cli_req_str(args, "--name"); char const *name = cli_req_str(args, "--name");
if (args.help) exit(0); if (args.help) exit(0);
if (do_nothing) { if (do_nothing) {
+67 -52
View File
@@ -31,8 +31,8 @@ typedef struct allocator {
const allocator_vtbl_t *vtbl; const allocator_vtbl_t *vtbl;
} allocator_t; } allocator_t;
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line); void *allocator_alloc_func(allocator_t this, size_t bytes, char const *file, int line);
void *allocator_resize_func(allocator_t this, void *old_ptr, size_t bytes, const char *file, int line); void *allocator_resize_func(allocator_t this, void *old_ptr, size_t bytes, char const *file, int line);
void allocator_reset(allocator_t this); void allocator_reset(allocator_t this);
#define allocator_alloc(this, bytes) allocator_alloc_func(this, bytes, __FILE__, __LINE__) #define allocator_alloc(this, bytes) allocator_alloc_func(this, bytes, __FILE__, __LINE__)
#define allocator_resize(this, old_ptr, bytes) allocator_resize_func(this, old_ptr, bytes, __FILE__, __LINE__) #define allocator_resize(this, old_ptr, bytes) allocator_resize_func(this, old_ptr, bytes, __FILE__, __LINE__)
@@ -150,7 +150,7 @@ typedef struct dyn_array_create_func_args {
allocator_t allocator; allocator_t allocator;
size_t itemsize; size_t itemsize;
size_t initial_capacity; size_t initial_capacity;
const char *file; char const *file;
int line; int line;
} dyn_array_create_func_args_t; } dyn_array_create_func_args_t;
void *dyn_array_create_func(dyn_array_create_func_args_t args); void *dyn_array_create_func(dyn_array_create_func_args_t args);
@@ -164,8 +164,8 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args);
// Always reassign the array. if multiple variables reference the same growing // Always reassign the array. if multiple variables reference the same growing
// array, then you should be using pointer pointers. // array, then you should be using pointer pointers.
void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line); void *dyn_array_grow_func(void *this, size_t n_new_items, char const *file, int line);
void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line); void dyn_array_shrink_func(void *this, size_t n_items_to_remove, char const *file, int line);
#define arr_grow(THIS, N_NEW_ITEMS) \ #define arr_grow(THIS, N_NEW_ITEMS) \
dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__) dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__)
@@ -190,7 +190,7 @@ size_t arr_cap(void *this);
#define arr_pop(THIS) (arr_shrink(THIS, 1), THIS[arr_len(THIS)]) #define arr_pop(THIS) (arr_shrink(THIS, 1), THIS[arr_len(THIS)])
void dyn_array_bounds_check_func(void *this, size_t index, const char *file, int line, bool print_error); void dyn_array_bounds_check_func(void *this, size_t index, char const *file, int line, bool print_error);
#define arr_get(THIS, INDEX) \ #define arr_get(THIS, INDEX) \
(dyn_array_bounds_check_func(THIS, INDEX, __FILE__, __LINE__, true), (THIS)[INDEX]) (dyn_array_bounds_check_func(THIS, INDEX, __FILE__, __LINE__, true), (THIS)[INDEX])
@@ -230,18 +230,18 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn);
typedef struct args { typedef struct args {
int count; int count;
const char **values; char const **values;
bool help; bool help;
int indentation; int indentation;
} args_t; } args_t;
args_t cli_make_args(int argc, const char **argv); args_t cli_make_args(int argc, char const **argv);
bool cli_command(args_t *args, const char *command_name); bool cli_command(args_t *args, char const *command_name);
bool cli_bool(args_t args, const char *flag_name); bool cli_bool(args_t args, char const *flag_name);
bool cli_opt_str_func(args_t args, const char *flag_name, const char **output, const char *file, int line); bool cli_opt_str_func(args_t args, char const *flag_name, char const **output, char const *file, int line);
const char *cli_req_str_func(args_t args, const char *flag_name, const char *file, int line); char const *cli_req_str_func(args_t args, char const *flag_name, char const *file, int line);
bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const char *file, int line); bool cli_opt_int_func(args_t args, char const *flag_name, int *output, char const *file, int line);
int cli_req_int_func(args_t args, const char *flag_name, const char *file, int line); int cli_req_int_func(args_t args, char const *flag_name, char const *file, int line);
#define cli_opt_str(ARGS, FLAG_NAME, OUTPUT) cli_opt_str_func(ARGS, FLAG_NAME, OUTPUT, __FILE__, __LINE__) #define cli_opt_str(ARGS, FLAG_NAME, OUTPUT) cli_opt_str_func(ARGS, FLAG_NAME, OUTPUT, __FILE__, __LINE__)
#define cli_req_str(ARGS, FLAG_NAME) cli_req_str_func(ARGS, FLAG_NAME, __FILE__, __LINE__) #define cli_req_str(ARGS, FLAG_NAME) cli_req_str_func(ARGS, FLAG_NAME, __FILE__, __LINE__)
@@ -249,12 +249,12 @@ int cli_req_int_func(args_t args, const char *flag_name, const char *file, int l
#define cli_req_int(ARGS, FLAG_NAME) cli_req_int_func(ARGS, FLAG_NAME, __FILE__, __LINE__) #define cli_req_int(ARGS, FLAG_NAME) cli_req_int_func(ARGS, FLAG_NAME, __FILE__, __LINE__)
#define with_opt_str(ARGS, FLAG_NAME, VAR) \ #define with_opt_str(ARGS, FLAG_NAME, VAR) \
for (const char *VAR = NULL; VAR == NULL;) \ for (char const *VAR = NULL; VAR == NULL;) \
for (bool CLI_UNIQUE1 = true; CLI_UNIQUE1;) \ for (bool CLI_UNIQUE1 = true; CLI_UNIQUE1;) \
for ( \ for ( \
bool CLI_UNIQUE2 = (CLI_UNIQUE1 = cli_opt_str(ARGS, FLAG_NAME, &VAR), true); \ bool CLI_UNIQUE2 = (CLI_UNIQUE1 = cli_opt_str(ARGS, FLAG_NAME, &VAR), true); \
CLI_UNIQUE2; \ CLI_UNIQUE2; \
CLI_UNIQUE2 = (VAR = ((const char *) 1), CLI_UNIQUE1 = false, false) \ CLI_UNIQUE2 = (VAR = ((char const *) 1), CLI_UNIQUE1 = false, false) \
) \ ) \
if (CLI_UNIQUE1) if (CLI_UNIQUE1)
@@ -270,10 +270,12 @@ int cli_req_int_func(args_t args, const char *flag_name, const char *file, int l
// scanner ///////////////////////////////////////////////////////////////////// // scanner /////////////////////////////////////////////////////////////////////
typedef struct error_node { typedef struct scan_error {
char *msg; char const * message;
struct error_node *next; char const * buffer_name;
} error_node_t; int line;
int column;
} scan_error_t;
typedef union scan_value { typedef union scan_value {
int digit; int digit;
@@ -291,39 +293,52 @@ typedef union scan_value {
char *string_literal; char *string_literal;
} scan_value_t; } scan_value_t;
typedef struct scan_result {
bool ok;
union {
scan_value_t value;
scan_error_t error;
};
} scan_result_t;
typedef struct scanner { typedef struct scanner {
const char *name; // name of the buffer char const *name; // name of the buffer
const char *start; // pointer to the full buffer char const *start; // pointer to the full buffer
const char *cur; // current pointer char const *cur; // current pointer
scan_value_t value;
error_node_t *errors; // singly linked list of error strings
allocator_t allocator; allocator_t allocator;
} scanner_t; } scanner_t;
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);
void scanner_recover(scanner_t *s); void scanner_recover(scanner_t *s);
void scanner_error(scanner_t *s, const char *message); scan_result_t scan_error(scanner_t *s, char const *message);
void scanner_error_and_recover(scanner_t *s, const char *message);
scan_result_t scan_value_func(scan_value_t value);
#define scan_value(arg) scan_value_func((scan_value_t){arg})
scan_result_t scan_error_and_recover(scanner_t *s, char const *message);
bool scan_eof(scanner_t *s); bool scan_eof(scanner_t *s);
bool scan_literal(scanner_t *s, const char *lit); bool scan_literal(scanner_t *s, char const *lit);
int scan_repeat_literal(scanner_t *s, const char *lit); int scan_repeat_literal(scanner_t *s, char const *lit);
bool scan_whitespace(scanner_t *s); bool scan_whitespace(scanner_t *s);
bool scan_digit(scanner_t *s); scan_result_t scan_digit(scanner_t *s);
bool scan_i64(scanner_t *s); scan_result_t scan_i64(scanner_t *s);
bool scan_i32(scanner_t *s); // TODO: make a lot of these references constant. they no longer write back to
bool scan_i16(scanner_t *s); // the scanner struct.
bool scan_i8(scanner_t *s); scan_result_t scan_i32(scanner_t *s);
bool scan_u64(scanner_t *s); scan_result_t scan_i16(scanner_t *s);
bool scan_u32(scanner_t *s); scan_result_t scan_i8(scanner_t *s);
bool scan_u16(scanner_t *s); scan_result_t scan_u64(scanner_t *s);
bool scan_u8(scanner_t *s); scan_result_t scan_u32(scanner_t *s);
bool scan_f64(scanner_t *s); scan_result_t scan_u16(scanner_t *s);
bool scan_f32(scanner_t *s); scan_result_t scan_u8(scanner_t *s);
scan_result_t scan_f64(scanner_t *s);
scan_result_t scan_f32(scanner_t *s);
// Scan as much of an identifier as possible, you are responsible to scan for // Scan as much of an identifier as possible, you are responsible to scan for
// valid characters after the identifier is scanned. // valid characters after the identifier is scanned.
bool scan_identifier(scanner_t *s); scan_result_t scan_identifier(scanner_t *s);
bool scan_string_literal(scanner_t *s); scan_result_t scan_string_literal(scanner_t *s);
bool scanner_print_errors(scanner_t *s, FILE *fp); bool scanner_print_errors_func(FILE *fp, scan_result_t const *results, int results_len);
#define scanner_print_errors(FP, ...) scanner_print_errors_func(FP, (scan_result_t const []){__VA_ARGS__}, sizeof((scan_result_t[]){__VA_ARGS__}) / sizeof(((scan_result_t[]){__VA_ARGS__})[0]))
bool looks_like_float(scanner_t *s); bool looks_like_float(scanner_t *s);
bool looks_like_int(scanner_t *s); bool looks_like_int(scanner_t *s);
@@ -331,7 +346,7 @@ bool looks_like_int(scanner_t *s);
typedef struct string_builder_node { typedef struct string_builder_node {
struct string_builder_node *next; struct string_builder_node *next;
size_t length; size_t length;
const char *string; char const *string;
} string_builder_node_t; } string_builder_node_t;
typedef struct string_builder { typedef struct string_builder {
@@ -342,7 +357,7 @@ typedef struct string_builder {
string_builder_t sb_create(allocator_t builder_allocator); string_builder_t sb_create(allocator_t builder_allocator);
// assumes the provided string will live until sb_build_and_clear is called. // assumes the provided string will live until sb_build_and_clear is called.
void sb_add_string(string_builder_t *this, const char *string); void sb_add_string(string_builder_t *this, char const *string);
void sb_add_i64(string_builder_t *this, int64_t i64); void sb_add_i64(string_builder_t *this, int64_t i64);
void sb_add_i32(string_builder_t *this, int32_t i32); void sb_add_i32(string_builder_t *this, int32_t i32);
void sb_add_i16(string_builder_t *this, int16_t i16); void sb_add_i16(string_builder_t *this, int16_t i16);
@@ -351,17 +366,17 @@ void sb_add_u64(string_builder_t *this, uint64_t u64);
void sb_add_u32(string_builder_t *this, uint32_t u32); void sb_add_u32(string_builder_t *this, uint32_t u32);
void sb_add_u16(string_builder_t *this, uint16_t u16); void sb_add_u16(string_builder_t *this, uint16_t u16);
void sb_add_u8(string_builder_t *this, uint8_t u8); void sb_add_u8(string_builder_t *this, uint8_t u8);
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length); void sb_add_substring(string_builder_t *this, char const *string, size_t substring_length);
const char *sb_build_and_clear(string_builder_t *this, allocator_t output_allocator); char const *sb_build_and_clear(string_builder_t *this, allocator_t output_allocator);
void sb_fprint_and_clear(string_builder_t *this, FILE *dest); void sb_fprint_and_clear(string_builder_t *this, FILE *dest);
// file_io ///////////////////////////////////////////////////////////////////// // file_io /////////////////////////////////////////////////////////////////////
char *read_entire_file(const char *path, allocator_t allocator); char *read_entire_file(char const *path, allocator_t allocator);
char **read_all_file_lines(const char *path, allocator_t allocator); char **read_all_file_lines(char const *path, allocator_t allocator);
#ifdef CIG_IMPL #ifdef CIG_IMPL
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line) { void *allocator_alloc_func(allocator_t this, size_t bytes, char const *file, int line) {
void *ptr = this.vtbl->alloc(this.this, bytes); void *ptr = this.vtbl->alloc(this.this, bytes);
if (ptr == NULL) { if (ptr == NULL) {
fprintf(stderr, "%s:%d: alloc returned NULL\n", file, line); fprintf(stderr, "%s:%d: alloc returned NULL\n", file, line);
@@ -369,7 +384,7 @@ void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int
} }
return ptr; return ptr;
} }
void *allocator_resize_func(allocator_t this, void *old_ptr, size_t bytes, const char *file, int line) { void *allocator_resize_func(allocator_t this, void *old_ptr, size_t bytes, char const *file, int line) {
void *ptr = this.vtbl->resize(this.this, old_ptr, bytes); void *ptr = this.vtbl->resize(this.this, old_ptr, bytes);
if (ptr == NULL) { if (ptr == NULL) {
fprintf(stderr, "%s:%d: alloc returned NULL\n", file, line); fprintf(stderr, "%s:%d: alloc returned NULL\n", file, line);
+11 -11
View File
@@ -9,7 +9,7 @@ static void cli_print_indentation(args_t args) {
} }
} }
bool cli_command(args_t *args, const char *command_name) { bool cli_command(args_t *args, char const *command_name) {
if (args->help) { if (args->help) {
cli_print_indentation(*args); cli_print_indentation(*args);
printf("[ cmd ] %s\n", command_name); printf("[ cmd ] %s\n", command_name);
@@ -25,7 +25,7 @@ bool cli_command(args_t *args, const char *command_name) {
return false; return false;
} }
bool cli_bool(args_t args, const char *flag_name) { bool cli_bool(args_t args, char const *flag_name) {
if (args.help) { if (args.help) {
cli_print_indentation(args); cli_print_indentation(args);
printf("[ flg ] %s\n", flag_name); printf("[ flg ] %s\n", flag_name);
@@ -39,7 +39,7 @@ bool cli_bool(args_t args, const char *flag_name) {
return false; return false;
} }
bool cli_opt_str_func(args_t args, const char *flag_name, const char **output, const char *file, int line) { bool cli_opt_str_func(args_t args, char const *flag_name, char const **output, char const *file, int line) {
if (args.help) { if (args.help) {
cli_print_indentation(args); cli_print_indentation(args);
printf("[ opt ] %s\n", flag_name); printf("[ opt ] %s\n", flag_name);
@@ -56,7 +56,7 @@ bool cli_opt_str_func(args_t args, const char *flag_name, const char **output, c
break; break;
} }
} }
static const char *dummy = ""; static char const *dummy = "";
if (!is_flag_name_found) { if (!is_flag_name_found) {
*output = dummy; *output = dummy;
return false; return false;
@@ -70,14 +70,14 @@ bool cli_opt_str_func(args_t args, const char *flag_name, const char **output, c
return true; return true;
} }
const char *cli_req_str_func(args_t args, const char *flag_name, const char *file, int line) { char const *cli_req_str_func(args_t args, char const *flag_name, char const *file, int line) {
if (args.help) { if (args.help) {
cli_print_indentation(args); cli_print_indentation(args);
printf("[ req ] %s\n", flag_name); printf("[ req ] %s\n", flag_name);
return ""; return "";
} }
const char *value; char const *value;
if (cli_opt_str_func(args, flag_name, &value, file, line)) { if (cli_opt_str_func(args, flag_name, &value, file, line)) {
return value; return value;
} else { } else {
@@ -87,7 +87,7 @@ const char *cli_req_str_func(args_t args, const char *flag_name, const char *fil
return ""; return "";
} }
bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const char *file, int line) { bool cli_opt_int_func(args_t args, char const *flag_name, int *output, char const *file, int line) {
if (args.help) { if (args.help) {
cli_print_indentation(args); cli_print_indentation(args);
printf("[ opt ] %s\n", flag_name); printf("[ opt ] %s\n", flag_name);
@@ -95,7 +95,7 @@ bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const cha
return false; return false;
} }
const char *text = ""; char const *text = "";
if (cli_opt_str_func(args, flag_name, &text, file, line)) { if (cli_opt_str_func(args, flag_name, &text, file, line)) {
if ( if (
text[0] != '-' && text[0] != '-' &&
@@ -112,7 +112,7 @@ bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const cha
fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text); fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text);
exit(1); exit(1);
} }
for ( const char *c = &text[1]; (*c) != '\0'; c++ ) { for ( char const *c = &text[1]; (*c) != '\0'; c++ ) {
if ((*c) < '0' || '9' < (*c)) { if ((*c) < '0' || '9' < (*c)) {
fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text); fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text);
exit(1); exit(1);
@@ -125,7 +125,7 @@ bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const cha
return false; return false;
} }
int cli_req_int_func(args_t args, const char *flag_name, const char *file, int line) { int cli_req_int_func(args_t args, char const *flag_name, char const *file, int line) {
if (args.help) { if (args.help) {
cli_print_indentation(args); cli_print_indentation(args);
printf("[ req ] %s\n", flag_name); printf("[ req ] %s\n", flag_name);
@@ -142,7 +142,7 @@ int cli_req_int_func(args_t args, const char *flag_name, const char *file, int l
return 0; return 0;
} }
args_t cli_make_args(int argc, const char **argv) { args_t cli_make_args(int argc, char const **argv) {
args_t args = (args_t) { args_t args = (args_t) {
.count = argc-1, .count = argc-1,
.values = argv+1, .values = argv+1,
+3 -3
View File
@@ -16,7 +16,7 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args) {
return &header->bytes; return &header->bytes;
} }
void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line) { void *dyn_array_grow_func(void *this, size_t n_new_items, char const *file, int line) {
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
size_t new_size = header->n_items + n_new_items; size_t new_size = header->n_items + n_new_items;
@@ -40,7 +40,7 @@ void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int
} }
void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line) { void dyn_array_shrink_func(void *this, size_t n_items_to_remove, char const *file, int line) {
if (arr_len(this) < n_items_to_remove) { if (arr_len(this) < n_items_to_remove) {
fprintf( fprintf(
stderr, stderr,
@@ -97,7 +97,7 @@ bool dyn_array_contains_eq_func(void *this, uint8_t *value, dyn_array_eq_fn eq)
return false; return false;
} }
void dyn_array_bounds_check_func(void *this, size_t index, const char *file, int line, bool print_error) { void dyn_array_bounds_check_func(void *this, size_t index, char const *file, int line, bool print_error) {
size_t len = arr_len(this); size_t len = arr_len(this);
if ((index >= len)) { if ((index >= len)) {
if (print_error) { if (print_error) {
+2 -2
View File
@@ -1,7 +1,7 @@
#include "cig.h" #include "cig.h"
#include <assert.h> #include <assert.h>
char *read_entire_file(const char *path, allocator_t allocator) { char *read_entire_file(char const *path, allocator_t allocator) {
FILE *fp = fopen(path, "rb"); FILE *fp = fopen(path, "rb");
assert(fp != NULL); assert(fp != NULL);
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
@@ -17,7 +17,7 @@ char *read_entire_file(const char *path, allocator_t allocator) {
} }
// Returns dynamic array, of fixed char strings. // Returns dynamic array, of fixed char strings.
char **read_all_file_lines(const char *path, allocator_t allocator) { char **read_all_file_lines(char const *path, allocator_t allocator) {
char *contents = read_entire_file(path, allocator); char *contents = read_entire_file(path, allocator);
char **lines = make_arr(char*, allocator); char **lines = make_arr(char*, allocator);
arr_append(lines, contents); arr_append(lines, contents);
+136 -147
View File
@@ -4,13 +4,11 @@
#include <string.h> #include <string.h>
#include <errno.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 = { scanner_t s = {
.name = name, .name = name,
.start = buffer, .start = buffer,
.cur = buffer, .cur = buffer,
.value = {0},
.errors = NULL,
.allocator = allocator .allocator = allocator
}; };
return s; return s;
@@ -24,47 +22,46 @@ void scanner_recover(scanner_t *s) {
s->cur++; // move *past* the newline s->cur++; // move *past* the newline
} }
void scanner_error(scanner_t *s, const char *message) { scan_result_t scan_error(scanner_t *s, char const * message) {
const char *error_pos = s->cur; // use current scanner position char const *error_pos = s->cur; // use current scanner position
// Compute line + column (1-based) // Compute line + column (1-based)
int line = 1, col = 1; int line = 1, column = 1;
for (const char *p = s->start; p < error_pos; p++) { for (char const *p = s->start; p < error_pos; p++) {
if (*p == '\n') { line++; col = 1; } if (*p == '\n') { line++; column = 1; }
else col++; else column++;
}
// 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;
} }
return (scan_result_t) {
.ok=false,
.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) { scan_result_t scan_value_func(scan_value_t value) {
scanner_error(s, message); return (scan_result_t) {
.ok=true,
.value=value,
};
}
scan_result_t scan_error_and_recover(scanner_t *s, char const *message) {
scan_result_t result = scan_error(s, message);
scanner_recover(s); scanner_recover(s);
return result;
} }
bool scan_eof(scanner_t *s) { bool scan_eof(scanner_t *s) {
return *s->cur == '\0'; return *s->cur == '\0';
} }
bool scan_literal(scanner_t *s, const char *lit) { bool scan_literal(scanner_t *s, char const *lit) {
const char *save = s->cur; char const *save = s->cur;
while (*lit && *lit == *s->cur) { while (*lit && *lit == *s->cur) {
lit++; lit++;
s->cur++; s->cur++;
@@ -76,7 +73,7 @@ bool scan_literal(scanner_t *s, const char *lit) {
return false; 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; int n_repeats = 0;
while (scan_literal(s, lit)) { while (scan_literal(s, lit)) {
n_repeats++; n_repeats++;
@@ -85,26 +82,26 @@ int scan_repeat_literal(scanner_t *s, const char *lit) {
} }
bool scan_whitespace(scanner_t *s) { bool scan_whitespace(scanner_t *s) {
const char *save = s->cur; char const *save = s->cur;
while (isspace((unsigned char)*s->cur)) s->cur++; while (isspace((unsigned char)*s->cur)) s->cur++;
return save != 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)) { if (!isdigit((unsigned char)*s->cur)) {
return false; return scan_error(s, "not a digit");
} }
s->value.digit = (*s->cur) - '0'; scan_result_t ret = scan_value(.digit=(*s->cur) - '0');
s->cur++; s->cur++;
return true; return ret;
} }
bool scan_i64(scanner_t *s) { scan_result_t scan_i64(scanner_t *s) {
const char *save = s->cur; char const *save = s->cur;
if (*s->cur == '-' || *s->cur == '+') s->cur++; if (*s->cur == '-' || *s->cur == '+') s->cur++;
if (!isdigit((unsigned char)*s->cur)) { if (!isdigit((unsigned char)*s->cur)) {
s->cur = save; s->cur = save;
return false; return scan_error(s, "not an i64");
} }
s->cur = save; s->cur = save;
char *end; char *end;
@@ -112,69 +109,63 @@ bool scan_i64(scanner_t *s) {
int64_t val = strtoll(s->cur, &end, 10); int64_t val = strtoll(s->cur, &end, 10);
if (end == s->cur) { if (end == s->cur) {
s->cur = save; s->cur = save;
return false; return scan_error(s, "not an i64");
} }
if (errno == ERANGE) { if (errno == ERANGE) {
scanner_error(s, "integer does not fit in i64 value"); return scan_error(s, "integer does not fit in i64 value");
return false;
} }
s->cur = end; s->cur = end;
s->value.i64 = val; return scan_value(.i64=val);
return true;
} }
bool scan_i32(scanner_t *s) { scan_result_t scan_i32(scanner_t *s) {
if (!scan_i64(s)) { scan_result_t result = scan_i64(s);
return false; if (!result.ok) {
return result;
} }
int32_t val = (int32_t)s->value.i64; int32_t val = (int32_t)result.value.i64;
int64_t back = (int64_t)val; int64_t back = (int64_t)val;
if (back != s->value.i64) { if (back != result.value.i64) {
scanner_error(s, "int does not fit in i32 value"); return scan_error(s, "int does not fit in i32 value");
return false;
} }
s->value.i32 = val; return scan_value(val);
return true;
} }
bool scan_i16(scanner_t *s) { scan_result_t scan_i16(scanner_t *s) {
if (!scan_i64(s)) { scan_result_t result = scan_i64(s);
return false; if (!result.ok) {
return result;
} }
int16_t val = (int16_t)s->value.i64; int16_t val = (int16_t)result.value.i64;
int64_t back = (int64_t)val; int64_t back = (int64_t)val;
if (back != s->value.i64) { if (back != result.value.i64) {
scanner_error(s, "int does not fit in i16 value"); return scan_error(s, "int does not fit in i16 value");
return false;
} }
s->value.i16 = val; return scan_value(val);
return true;
} }
bool scan_i8(scanner_t *s) { scan_result_t scan_i8(scanner_t *s) {
if (!scan_i64(s)) { scan_result_t result = scan_i64(s);
return false; if (!result.ok) {
return result;
} }
int8_t val = (int8_t)s->value.i64; int8_t val = (int8_t)result.value.i64;
int64_t back = (int64_t)val; int64_t back = (int64_t)val;
if (back != s->value.i64) { if (back != result.value.i64) {
scanner_error(s, "int does not fit in i8 value"); return scan_error(s, "int does not fit in i8 value");
return false;
} }
s->value.i8 = val; return scan_value(val);
return true;
} }
bool scan_u64(scanner_t *s) { scan_result_t scan_u64(scanner_t *s) {
const char *save = s->cur; char const *save = s->cur;
if (*s->cur == '-') { if (*s->cur == '-') {
scanner_error(s, "- is not allowed for unsigned integers"); return scan_error(s, "- is not allowed for unsigned integers");
return false;
} }
if (*s->cur == '+') s->cur++; if (*s->cur == '+') s->cur++;
if (!isdigit((unsigned char)*s->cur)) { if (!isdigit((unsigned char)*s->cur)) {
s->cur = save; s->cur = save;
return false; return scan_error(s, "is not number");
} }
s->cur = save; s->cur = save;
char *end; char *end;
@@ -182,96 +173,89 @@ bool scan_u64(scanner_t *s) {
uint64_t val = strtoull(s->cur, &end, 10); uint64_t val = strtoull(s->cur, &end, 10);
if (end == s->cur) { if (end == s->cur) {
s->cur = save; s->cur = save;
return false; return scan_error(s, "is not number");
// TODO: standardize error messages
} }
if (errno == ERANGE) { if (errno == ERANGE) {
scanner_error(s, "integer does not fit in u64 value"); return scan_error(s, "integer does not fit in u64 value");
return false;
} }
s->cur = end; s->cur = end;
s->value.u64 = val; return scan_value(.u64=val);
return true;
} }
bool scan_u32(scanner_t *s) { scan_result_t scan_u32(scanner_t *s) {
if (!scan_u64(s)) { scan_result_t result = scan_u64(s);
return false; if (!result.ok) {
return result;
} }
uint32_t val = (uint32_t)s->value.u64; uint32_t val = (uint32_t)result.value.u64;
uint64_t back = (uint64_t)val; uint64_t back = (uint64_t)val;
if (back != s->value.u64) { if (back != result.value.u64) {
scanner_error(s, "int does not fit in u32 value"); return scan_error(s, "int does not fit in u32 value");
return false;
} }
s->value.u32 = val; return scan_value(val);
return true;
} }
bool scan_u16(scanner_t *s) { scan_result_t scan_u16(scanner_t *s) {
if (!scan_u64(s)) { scan_result_t result = scan_u64(s);
return false; if (!result.ok) {
return result;
} }
uint16_t val = (uint16_t)s->value.u64; uint16_t val = (uint16_t)result.value.u64;
uint64_t back = (uint64_t)val; uint64_t back = (uint64_t)val;
if (back != s->value.u64) { if (back != result.value.u64) {
scanner_error(s, "int does not fit in u16 value"); return scan_error(s, "int does not fit in u16 value");
return false;
} }
s->value.u16 = val; return scan_value(val);
return true;
} }
bool scan_u8(scanner_t *s) { scan_result_t scan_u8(scanner_t *s) {
if (!scan_u64(s)) { scan_result_t result = scan_u64(s);
return false; if (!result.ok) {
return result;
} }
uint8_t val = (uint8_t)s->value.u64; uint8_t val = (uint8_t)result.value.u64;
uint64_t back = (uint64_t)val; uint64_t back = (uint64_t)val;
if (back != s->value.u64) { if (back != result.value.u64) {
scanner_error(s, "int does not fit in u8 value"); return scan_error(s, "int does not fit in u8 value");
return false;
} }
s->value.u8 = val; return scan_value(val);
return true;
} }
bool scan_f64(scanner_t *s) { scan_result_t scan_f64(scanner_t *s) {
const char *save = s->cur; char const *save = s->cur;
char *end; char *end;
errno = 0; errno = 0;
double val = strtod(s->cur, &end); double val = strtod(s->cur, &end);
if (end == s->cur) { if (end == s->cur) {
s->cur = save; s->cur = save;
return false; return scan_error(s, "not a f64");
} }
if (errno == ERANGE) { if (errno == ERANGE) {
scanner_error(s, "float does not fit in f64 value"); return scan_error(s, "float does not fit in f64 value");
return false;
} }
s->cur = end; s->cur = end;
s->value.f64 = val; return scan_value(.f64=val);
return true;
} }
bool scan_f32(scanner_t *s) { scan_result_t scan_f32(scanner_t *s) {
if (!scan_f64(s)) { scan_result_t result = scan_f64(s);
return false; if (!result.ok) {
return result;
} }
float val = (float)s->value.f64; float val = (float)result.value.f64;
double back = (double)val; double back = (double)val;
if (back != s->value.f64) { if (back != result.value.f64) {
scanner_error(s, "float does not fit in f32 value"); return scan_error(s, "float does not fit in f32 value");
return false;
} }
s->value.f32 = val; return scan_value(.f32=val);
return true;
} }
bool scan_identifier(scanner_t *s) { scan_result_t scan_identifier(scanner_t *s) {
if (!isalpha((unsigned char)*s->cur) && *s->cur != '_') { if (!isalpha((unsigned char)*s->cur) && *s->cur != '_') {
return false; return scan_error(s, "not an identifier");
} }
const char *start = s->cur++; char const *start = s->cur++;
while (isalnum((unsigned char)*s->cur) || *s->cur == '_') { while (isalnum((unsigned char)*s->cur) || *s->cur == '_') {
s->cur++; s->cur++;
} }
@@ -279,21 +263,21 @@ bool scan_identifier(scanner_t *s) {
char *buf = allocator_alloc(s->allocator, len + 1); char *buf = allocator_alloc(s->allocator, len + 1);
memcpy(buf, start, len); memcpy(buf, start, len);
buf[len] = '\0'; buf[len] = '\0';
s->value.identifier = buf; return scan_value(.identifier=buf);
return true;
} }
bool scan_string_literal(scanner_t *s) { scan_result_t scan_string_literal(scanner_t *s) {
const char *save = s->cur; char const *save = s->cur;
if (*s->cur != '"') if (*s->cur != '"')
return false; return scan_error(s, "not a string");
s->cur++; // skip opening quote s->cur++; // skip opening quote
char *buf = allocator_alloc(s->allocator, 256); char *buf = allocator_alloc(s->allocator, 256);
size_t cap = 256; size_t cap = 256;
size_t len = 0; size_t len = 0;
while (*s->cur && *s->cur != '"') { while (*s->cur && *s->cur != '"') {
char c = *s->cur++; char c = *s->cur++;
scan_result_t ret;
if (c == '\\') { if (c == '\\') {
char esc = *s->cur++; char esc = *s->cur++;
switch (esc) { switch (esc) {
@@ -304,9 +288,9 @@ bool scan_string_literal(scanner_t *s) {
case '"': c = '"'; break; case '"': c = '"'; break;
case '0': c = '\0'; break; case '0': c = '\0'; break;
default: default:
scanner_error(s, "invalid escape sequence"); ret = scan_error(s, "invalid escape sequence");
s->cur = save; s->cur = save;
return false; return ret;
} }
} }
if (len + 2 >= cap) { if (len + 2 >= cap) {
@@ -316,25 +300,30 @@ bool scan_string_literal(scanner_t *s) {
buf[len++] = c; buf[len++] = c;
} }
if (*s->cur != '"') { if (*s->cur != '"') {
scanner_error(s, "unterminated string literal"); scan_result_t ret = scan_error(s, "unterminated string literal");
s->cur = save; s->cur = save;
return false; return ret;
} }
s->cur++; // skip closing quote s->cur++; // skip closing quote
buf[len] = '\0'; buf[len] = '\0';
s->value.string_literal = buf; return scan_value(.string_literal=buf);
return true;
} }
bool scanner_print_errors(scanner_t *s, FILE *fp) { bool scanner_print_errors_func(FILE *fp, scan_result_t const *results, int results_len) {
for (error_node_t *node = s->errors; node != NULL; node = node->next) { bool did_print_errors = false;
fprintf(fp, "%s\n", node->msg); 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) { bool looks_like_float(scanner_t *s) {
const char *cur = s->cur; char const *cur = s->cur;
if ( if (
((*cur) == '+') || ((*cur) == '+') ||
((*cur) == '-') ((*cur) == '-')
@@ -358,7 +347,7 @@ bool looks_like_float(scanner_t *s) {
} }
bool looks_like_int(scanner_t *s) { bool looks_like_int(scanner_t *s) {
const char *cur = s->cur; char const *cur = s->cur;
if (*cur == '+' || *cur == '-') { if (*cur == '+' || *cur == '-') {
cur++; cur++;
} }
+4 -4
View File
@@ -12,7 +12,7 @@ string_builder_t sb_create(allocator_t builder_allocator) {
}; };
} }
void sb_add_string(string_builder_t *this, const char *string) { void sb_add_string(string_builder_t *this, char const *string) {
string_builder_node_t *node = allocator_alloc(this->allocator, sizeof(string_builder_node_t)); string_builder_node_t *node = allocator_alloc(this->allocator, sizeof(string_builder_node_t));
node->length = strlen(string); node->length = strlen(string);
node->string = string; node->string = string;
@@ -78,7 +78,7 @@ void sb_add_u8(string_builder_t *this, uint8_t u8) {
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length) { void sb_add_substring(string_builder_t *this, char const *string, size_t substring_length) {
size_t string_length = strlen(string); size_t string_length = strlen(string);
size_t min_length = string_length < substring_length size_t min_length = string_length < substring_length
? string_length ? string_length
@@ -91,7 +91,7 @@ void sb_add_substring(string_builder_t *this, const char *string, size_t substri
sb_add_string(this, buffer); sb_add_string(this, buffer);
} }
const char *sb_build_and_clear( char const *sb_build_and_clear(
string_builder_t *this, string_builder_t *this,
allocator_t output_allocator allocator_t output_allocator
) { ) {
@@ -111,7 +111,7 @@ const char *sb_build_and_clear(
node != NULL; node != NULL;
node = node->next node = node->next
) { ) {
for (const char *c = node->string; (*c) != '\0'; c++) { for (char const *c = node->string; (*c) != '\0'; c++) {
buffer[i] = *c; buffer[i] = *c;
i++; i++;
} }
+1 -1
View File
@@ -145,7 +145,7 @@ Test(dynamic_arrays, contains_cmp_empty) {
// Test using arr_contains_cmp for a simple key-value map // Test using arr_contains_cmp for a simple key-value map
typedef struct { typedef struct {
int key; int key;
const char *value; char const *value;
} kv_pair_t; } kv_pair_t;
static bool kv_eq_by_key(const void *a, const void *b) { static bool kv_eq_by_key(const void *a, const void *b) {
+34 -2
View File
@@ -1,9 +1,10 @@
#include <assert.h> #include <assert.h>
#include <criterion/criterion.h> #include <criterion/criterion.h>
#include <stdio.h>
#include "cig.h" #include "cig.h"
Test(scanner, looks_like_float) { Test(scanner, looks_like_float) {
const char *buffer = char const *buffer =
"1.0 true\n" "1.0 true\n"
"12.34 true\n" "12.34 true\n"
"0.5 true\n" "0.5 true\n"
@@ -22,7 +23,7 @@ Test(scanner, looks_like_float) {
scanner_t scanner = make_scanner("test", buffer, allocator); scanner_t scanner = make_scanner("test", buffer, allocator);
while (!scan_eof(&scanner)) { while (!scan_eof(&scanner)) {
bool does_look_like_float = looks_like_float(&scanner); bool does_look_like_float = looks_like_float(&scanner);
bool does_look_like_int = looks_like_float(&scanner); bool does_look_like_int = looks_like_int(&scanner);
while (*scanner.cur++ != ' '); while (*scanner.cur++ != ' ');
bool expect; bool expect;
if (scan_literal(&scanner, "true")) { if (scan_literal(&scanner, "true")) {
@@ -41,3 +42,34 @@ Test(scanner, looks_like_float) {
} }
} }
Test(scanner, print_errors) {
char const *buffer =
"1.0 true\n"
"12.34 true\n"
"0.5 true\n"
"9.99 true\n"
"+1.2 true\n"
"-3.14 true\n"
"1 false\n"
"42 false\n"
"+7 false\n"
"1. false\n"
".5 false\n"
"+. false\n"
"abc false\n"
"+x false\n";
FILE *f = tmpfile();
with_borrow(allocator) {
scanner_t scanner = make_scanner(__FILE__, buffer, allocator);
scan_result_t res1 = scan_error(&scanner, "ignore this\n");
scan_result_t res2 = scan_error(&scanner, "and this\n");
scan_result_t res3 = scan_value(.i64=10);
scan_result_t res4 = scan_value(.string_literal="foobar");
cr_assert(scanner_print_errors(f, res1, res2));
cr_assert(scanner_print_errors(f, res1, res2, res3, res4));
cr_assert(!scanner_print_errors(f, res3, res4));
}
}