41 lines
1.1 KiB
C
41 lines
1.1 KiB
C
#include "token.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main(int argc, const char **argv) {
|
|
if (argc < 2) {
|
|
printf("Not enough args!\n");
|
|
return 1;
|
|
}
|
|
FILE *in = fopen(argv[1], "r");
|
|
fseek(in, 0, SEEK_END);
|
|
off_t length = ftello(in);
|
|
rewind(in);
|
|
char *text = malloc(length);
|
|
fread(text, 1, length, in);
|
|
fclose(in);
|
|
TokenStream stream;
|
|
ParseError err;
|
|
Token token;
|
|
token_stream_init(&stream, text, length);
|
|
while (!token_stream_is_eof(&stream)) {
|
|
token_stream_next(&stream, &token, &err);
|
|
if (parse_error_is_set(&err)) {
|
|
printf("Error at %zu:%zu: %s\n", err.pos.line, err.pos.column,
|
|
err.message);
|
|
free(text);
|
|
return 1;
|
|
} else {
|
|
char *ts = malloc(token.length + 1);
|
|
memcpy(ts, &text[token.pos.offset], token.length);
|
|
ts[token.length] = '\0';
|
|
printf("%s: %s\n", token_type_to_string(token.type), ts);
|
|
free(ts);
|
|
}
|
|
}
|
|
free(text);
|
|
return 0;
|
|
}
|