diff options
author | thing1 <thing1@seacrossedlovers.xyz> | 2025-04-22 11:18:16 +0100 |
---|---|---|
committer | thing1 <thing1@seacrossedlovers.xyz> | 2025-04-22 11:18:16 +0100 |
commit | 69eae10356e807c61192d025eee4d88896881619 (patch) | |
tree | 4e7515c93718c77c92aa3ab8ce0533343eee9db2 /main.c |
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define STB_C_LEXER_SELF_TEST +#define STB_C_LEXER_IMPLEMENTATION +#include "stb_c_lexer.h" + +static int +filelen(FILE *f) { + fseek(f, 0, SEEK_END); + int len = ftell(f); + rewind(f); + return len; +} + +static char * +readfile(FILE *f) { + int l = filelen(f); + char *conts = malloc(l+1); + conts[l] = 0; + fread(conts, sizeof(char), l, f); + return conts; +} + +int +main() { + FILE *f = fopen("sample.b", "r"); + char *contents = readfile(f); + fclose(f); + + stb_lexer l = {0}; + char storage[1024] = {0}; + stb_c_lexer_init(&l, contents, &contents[strlen(contents)], storage, 1024); + + while (stb_c_lexer_get_token(&l) != 0) { + if (l.token == CLEX_parse_error) { + stb_lex_location loc = {0}; + stb_c_lexer_get_location(&l, l.where_firstchar, &loc); + printf("%d:%d, error\n", loc.line_number, loc.line_offset); + } + print_token(&l); + printf("\n"); + } + + return 0; +} |