From 6ecea40c849effd171c1d27c961f5c1ee100481f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 09:02:38 +0300 Subject: publish --- src/bnf-preprocessor.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/bnf-preprocessor.c (limited to 'src/bnf-preprocessor.c') diff --git a/src/bnf-preprocessor.c b/src/bnf-preprocessor.c new file mode 100644 index 0000000..67fafc7 --- /dev/null +++ b/src/bnf-preprocessor.c @@ -0,0 +1,76 @@ +#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; +} -- cgit v1.3.1