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
+67 -52
View File
@@ -31,8 +31,8 @@ typedef struct allocator {
const allocator_vtbl_t *vtbl;
} allocator_t;
void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line);
void *allocator_resize_func(allocator_t this, void *old_ptr, 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, char const *file, int line);
void allocator_reset(allocator_t this);
#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__)
@@ -150,7 +150,7 @@ typedef struct dyn_array_create_func_args {
allocator_t allocator;
size_t itemsize;
size_t initial_capacity;
const char *file;
char const *file;
int line;
} dyn_array_create_func_args_t;
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
// 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_shrink_func(void *this, size_t n_items_to_remove, 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, char const *file, int line);
#define arr_grow(THIS, N_NEW_ITEMS) \
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)])
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) \
(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 {
int count;
const char **values;
char const **values;
bool help;
int indentation;
} args_t;
args_t cli_make_args(int argc, const char **argv);
bool cli_command(args_t *args, const char *command_name);
bool cli_bool(args_t args, const char *flag_name);
bool cli_opt_str_func(args_t args, const char *flag_name, const char **output, const char *file, int line);
const char *cli_req_str_func(args_t args, const char *flag_name, const char *file, int line);
bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const char *file, int line);
int cli_req_int_func(args_t args, const char *flag_name, const char *file, int line);
args_t cli_make_args(int argc, char const **argv);
bool cli_command(args_t *args, char const *command_name);
bool cli_bool(args_t args, char const *flag_name);
bool cli_opt_str_func(args_t args, char const *flag_name, char const **output, char const *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, char const *flag_name, int *output, char const *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_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 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_UNIQUE2 = (CLI_UNIQUE1 = cli_opt_str(ARGS, FLAG_NAME, &VAR), true); \
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)
@@ -270,10 +270,12 @@ int cli_req_int_func(args_t args, const char *flag_name, const char *file, int l
// scanner /////////////////////////////////////////////////////////////////////
typedef struct error_node {
char *msg;
struct error_node *next;
} error_node_t;
typedef struct scan_error {
char const * message;
char const * buffer_name;
int line;
int column;
} scan_error_t;
typedef union scan_value {
int digit;
@@ -291,39 +293,52 @@ typedef union scan_value {
char *string_literal;
} scan_value_t;
typedef struct scan_result {
bool ok;
union {
scan_value_t value;
scan_error_t error;
};
} scan_result_t;
typedef struct scanner {
const char *name; // name of the buffer
const char *start; // pointer to the full buffer
const char *cur; // current pointer
scan_value_t value;
error_node_t *errors; // singly linked list of error strings
char const *name; // name of the buffer
char const *start; // pointer to the full buffer
char const *cur; // current pointer
allocator_t allocator;
} 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_error(scanner_t *s, const char *message);
void scanner_error_and_recover(scanner_t *s, const char *message);
scan_result_t scan_error(scanner_t *s, char const *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_literal(scanner_t *s, const char *lit);
int scan_repeat_literal(scanner_t *s, const char *lit);
bool scan_literal(scanner_t *s, char const *lit);
int scan_repeat_literal(scanner_t *s, char const *lit);
bool scan_whitespace(scanner_t *s);
bool scan_digit(scanner_t *s);
bool scan_i64(scanner_t *s);
bool scan_i32(scanner_t *s);
bool scan_i16(scanner_t *s);
bool scan_i8(scanner_t *s);
bool scan_u64(scanner_t *s);
bool scan_u32(scanner_t *s);
bool scan_u16(scanner_t *s);
bool scan_u8(scanner_t *s);
bool scan_f64(scanner_t *s);
bool scan_f32(scanner_t *s);
scan_result_t scan_digit(scanner_t *s);
scan_result_t scan_i64(scanner_t *s);
// TODO: make a lot of these references constant. they no longer write back to
// the scanner struct.
scan_result_t scan_i32(scanner_t *s);
scan_result_t scan_i16(scanner_t *s);
scan_result_t scan_i8(scanner_t *s);
scan_result_t scan_u64(scanner_t *s);
scan_result_t scan_u32(scanner_t *s);
scan_result_t scan_u16(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
// valid characters after the identifier is scanned.
bool scan_identifier(scanner_t *s);
bool scan_string_literal(scanner_t *s);
bool scanner_print_errors(scanner_t *s, FILE *fp);
scan_result_t scan_identifier(scanner_t *s);
scan_result_t scan_string_literal(scanner_t *s);
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_int(scanner_t *s);
@@ -331,7 +346,7 @@ bool looks_like_int(scanner_t *s);
typedef struct string_builder_node {
struct string_builder_node *next;
size_t length;
const char *string;
char const *string;
} string_builder_node_t;
typedef struct string_builder {
@@ -342,7 +357,7 @@ typedef struct string_builder {
string_builder_t sb_create(allocator_t builder_allocator);
// 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_i32(string_builder_t *this, int32_t i32);
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_u16(string_builder_t *this, uint16_t u16);
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);
const char *sb_build_and_clear(string_builder_t *this, allocator_t output_allocator);
void sb_add_substring(string_builder_t *this, char const *string, size_t substring_length);
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);
// file_io /////////////////////////////////////////////////////////////////////
char *read_entire_file(const char *path, allocator_t allocator);
char **read_all_file_lines(const char *path, allocator_t allocator);
char *read_entire_file(char const *path, allocator_t allocator);
char **read_all_file_lines(char const *path, allocator_t allocator);
#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);
if (ptr == NULL) {
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;
}
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);
if (ptr == NULL) {
fprintf(stderr, "%s:%d: alloc returned NULL\n", file, line);