summaryrefslogtreecommitdiff
path: root/main.c
blob: cd6eb22acd939beb18f054a14a544121d8cfb0c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
}