#include "bnf-preprocessor.h" #include "string-builder.h" #include #include #include #include #include struct preprocessor { struct { struct open_file { struct string_view sv; const char *filename; const char *cursor; } *items; uint32_t count; uint32_t capacity; } open_files; struct string_builder sb; }; static const char import_cstr[] = "import"; static struct string_view import_sv = { .beg = import_cstr, .end = import_cstr + sizeof (import_cstr) - 1, }; static void preprocess_file (struct string_builder *sb, const char *filename) { struct string_view file_contents; const char *cursor; int fd = open (filename, O_RDONLY); if (fd == -1) DIE ("error: %s: %s", strerror (errno), filename); struct stat st; fstat (fd, &st); cursor = file_contents.beg = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); close (fd); file_contents.end = file_contents.beg + st.st_size; DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, STRING_VIEW_LEN (file_contents)); do { if (*cursor == '!') { cursor += 1; while (*cursor == ' ') cursor += 1; if () while (*cursor != '\n') cursor += 1; cursor += 1; preprocess_file (sb, import_filename); continue; } DYNAMIC_ARRAY_APPEND_FAST (*sb, *cursor++); } while (1); munmap ((char *) file_contents.beg, STRING_VIEW_LEN (file_contents)); } struct string_view bnf_preprocess_file (const char *filename) { struct string_builder sb = {0}; preprocess_file (&sb, filename); struct string_view output = { .beg = sb.string, .end = sb.string + sb.count, }; return output; }