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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// examples/lex.c
#include <stddef.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define CMMM__RS274NGC__LEXEME__STRIP_VENDOR
#define CMMM__RS274NGC__LEXER__STRIP_VENDOR
#include "utils.h"
#include "cmmm/rs274ngc/lexer.h"
int
main (int argc, char *argv[])
{
const char *filename;
struct cmmm__sv rs274ngc;
struct cmmm__arena a = {0};
if (argc == 1) {
filename = "-";
rs274ngc = read_until_eof (&a, 0);
} else if (argc == 2) {
filename = argv[1];
int fd = open (filename, O_RDONLY);
rs274ngc = mmap_whole (fd);
} else {
exit (EXIT_FAILURE); // TODO(cmmm): better DIE
}
// printf (SV_FMT, SV_FMTA (rs274ngc));
// return 0;
struct rs274ngc__lexer lexer =
rs274ngc__lexer__from_string_view (rs274ngc, filename);
int line = 1;
const char *bol = lexer.lexeme.sv.begin;
for (;; rs274ngc__lexer__next_lexeme (&lexer)) {
struct rs274ngc__lexeme lexeme = lexer.lexeme;
if (lexeme.kind == RS274NGC__LEXEME__KIND__END_OF_FILE) {
printf ("%s:%d:%ld: %s\n",
lexer.filename, line, lexeme.sv.begin - bol,
rs274ngc__lexeme__kind_names[lexeme.kind]);
break;
};
if (lexeme.kind == RS274NGC__LEXEME__KIND__END_OF_LINE) {
printf ("%s:%d:%ld: %s\n",
lexer.filename, line, lexeme.sv.begin - bol + 1,
rs274ngc__lexeme__kind_names[lexeme.kind]);
bol = lexeme.sv.end;
line += 1;
continue;
}
printf ("%s:%d:%ld-%ld: %s: |"SV_FMT"|\n",
lexer.filename, line, lexeme.sv.begin - bol + 1, lexeme.sv.end - bol,
rs274ngc__lexeme__kind_names[lexeme.kind], SV_FMTA (lexeme.sv));
}
return EXIT_SUCCESS;
}
// examples/lex.c ends here
|