// examples/lex.c #include #include #include #include #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