main.c (1152B)
1 #include <stdio.h> 2 #include <stdarg.h> 3 #include <stdlib.h> 4 #include <stddef.h> 5 6 #include "lex.h" 7 #include "lex_names.h" 8 9 FILE *in; 10 11 void 12 cleanup() { 13 free(input); 14 fclose(in); 15 } 16 17 void 18 errorf(char *fmt, ...) { 19 va_list ap; 20 char str[256]; 21 va_start(ap, fmt); 22 vsnprintf(str, 255, fmt, ap); 23 va_end(ap); 24 fprintf(stderr, "%s\n", str); 25 } 26 27 void 28 syntax_error() { 29 char *loc = startpos - 1, *line = loc, *linestart; 30 int i, off; 31 32 errorf("Syntax error: Expected %s, got %.*s", get_name(nextfn), endpos - startpos, loc); 33 34 while (line != input && *line != '\n') line--; 35 if (*line == '\n') line++; 36 linestart = line; 37 38 off = fprintf(stderr, "%d: ", get_line_num(loc)); 39 while (*line && *line != '\n') 40 putc(*line++, stderr); 41 42 putc('\n', stderr); 43 44 while (linestart - off != loc) { 45 putc(' ', stderr); 46 linestart++; 47 } 48 49 for (i = 0; i < get_err_len(); i++) 50 putc('^', stderr); 51 putc('\n', stderr); 52 53 } 54 55 int 56 main() { 57 lex_val *val; 58 59 in = fopen("test.hlc", "r"); 60 init_lexer(in); 61 62 while ((val = get_next())) { 63 if (val->type == EOI) 64 break; 65 if (val->type == UNKNOWN) { 66 syntax_error(); 67 cleanup(); 68 return 1; 69 } 70 } 71 72 cleanup(); 73 return 0; 74 }