#include #include #include #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; }