From 4058f9b1704322f8185136c2558c2ab96a4d835c Mon Sep 17 00:00:00 2001 From: mattkae Date: Sun, 23 Apr 2023 20:23:54 -0400 Subject: Initial commit with a working parser --- src/main.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/main.cpp (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c92321c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "code_point.h" +#include "html_token.hpp" +#include "tokenizer.hpp" +#include + +using namespace matte; + +const char *argp_program_version = "html_parser 0.1"; +const char *argp_program_bug_address = ""; +static char doc[] = "A description of your program."; +static char args_doc[] = "-f [FILENAME]..."; +static struct argp_option options[] = { + { "file", 'f', "FILE", 0, "File to parse."}, + { 0 } +}; + +struct Arguments { + char* filename; +}; + +static error_t parse_opt(int key, char *arg, struct argp_state *state) { + Arguments* a = (Arguments*)state->input; + switch (key) { + case 'f': { + a->filename = arg; + break; + } + case ARGP_KEY_ARG: + return 0; + default: + return ARGP_ERR_UNKNOWN; + } + return 0; +} + +static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 }; + +int main(int argc, char *argv[]) { + Arguments arguments; + arguments.filename = nullptr; + auto error = argp_parse(&argp, argc, argv, 0, 0, &arguments); + + if (arguments.filename == nullptr) { + exit(EXIT_FAILURE); + } + + FILE* file = fopen(arguments.filename, "rb"); + if (file == NULL) { + exit(EXIT_FAILURE); + } + + code_point_t wc; + code_point_t buffer[1024]; + size_t ptr = 0; + while ((wc=fgetwc(file))!=WEOF) { + buffer[ptr++] = wc; + } + + buffer[ptr] = '\0'; + fclose(file); + + Tokenizer tokenizer = create(buffer); + while (true ) { + auto token = read_next(&tokenizer); + token.print(); + if (token.type == HtmlTokenType_EOF) { + break; + } + } + + return EXIT_SUCCESS; +} -- cgit v1.2.1