This commit is contained in:
2026-09-27 20:27:38 +02:00
parent 27c5c4a55b
commit 2f0a1f5087
9 changed files with 143 additions and 126 deletions
+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) {
+69 -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,54 @@ typedef union scan_value {
char *string_literal; char *string_literal;
} scan_value_t; } scan_value_t;
typedef enum scan_result_type {
scan_result_type_value,
scan_result_type_error,
scan_result_type_none,
count_scan_result_type
} scan_result_type_t;
typedef struct scan_result {
scan_result_type_t type;
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 scanner_error(scanner_t *s, char const *message);
void scanner_error_and_recover(scanner_t *s, const char *message); void scanner_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); void scanner_print_errors(scan_error_t error, FILE *fp);
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 +348,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 +359,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 +368,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 +386,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);
+50 -50
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,37 +22,28 @@ 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 scanner_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) {
.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_error(s, message);
scanner_recover(s); scanner_recover(s);
} }
@@ -63,8 +52,8 @@ 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 +65,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 +74,36 @@ 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_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++; 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_result_t) {
.type=scan_result_type_none,
};
// TODO: make macros for each of the scan_result_t types.
} }
s->cur = save; s->cur = save;
char *end; char *end;
@@ -112,11 +111,12 @@ 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_result_t){
.type=scan_result_type_none,
};
} }
if (errno == ERANGE) { if (errno == ERANGE) {
scanner_error(s, "integer does not fit in i64 value"); return scanner_error(s, "integer does not fit in i64 value");
return false;
} }
s->cur = end; s->cur = end;
s->value.i64 = val; s->value.i64 = val;
@@ -166,7 +166,7 @@ bool scan_i8(scanner_t *s) {
} }
bool scan_u64(scanner_t *s) { bool 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"); scanner_error(s, "- is not allowed for unsigned integers");
return false; return false;
@@ -236,7 +236,7 @@ bool scan_u8(scanner_t *s) {
} }
bool scan_f64(scanner_t *s) { bool 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);
@@ -271,7 +271,7 @@ bool scan_identifier(scanner_t *s) {
if (!isalpha((unsigned char)*s->cur) && *s->cur != '_') { if (!isalpha((unsigned char)*s->cur) && *s->cur != '_') {
return false; return false;
} }
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++;
} }
@@ -285,7 +285,7 @@ bool scan_identifier(scanner_t *s) {
bool scan_string_literal(scanner_t *s) { bool 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 false;
s->cur++; // skip opening quote 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) { 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 +358,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) {
+1 -1
View File
@@ -3,7 +3,7 @@
#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"