diff --git a/fec/src/diag.c b/fec/src/diag.c index 21612ce..c9d35b1 100644 --- a/fec/src/diag.c +++ b/fec/src/diag.c @@ -1,9 +1,62 @@ #include "diag.h" +static unsigned long digits(unsigned long n) +{ + unsigned long count=1; + while(n>=10UL){n/=10UL;++count;} + return count; +} + +static void excerpt(const FeDiags *d, FeLoc loc) +{ + const char *src; + unsigned long len; + unsigned long i=0; + unsigned long line=1; + unsigned long start; + unsigned long end; + unsigned long gutter; + unsigned long col; + char c; + if(!d || !d->source || !d->source_len || !loc.line || !loc.col) return; + src=d->source; + len=d->source_len; + while(ilen) return; + start=i; + end=start; + while(endstart) fwrite(src+start,1,(size_t)(end-start),stderr); + fputc('\n',stderr); + fputs(" ",stderr); + for(i=0;ierrors=0; + d->warnings=0; + d->source=source; + d->source_len=source_len; +} + void fe_diag_error(FeDiags *d, FeLoc loc, const char *msg) { d->errors++; fprintf(stderr, "%s:%lu:%lu: error: %s\n", loc.file ? loc.file : "", loc.line, loc.col, msg); + excerpt(d,loc); } void fe_diag_errorf(FeDiags *d, FeLoc loc, const char *msg, const char *arg) @@ -12,9 +65,16 @@ void fe_diag_errorf(FeDiags *d, FeLoc loc, const char *msg, const char *arg) fprintf(stderr, "%s:%lu:%lu: error: ", loc.file ? loc.file : "", loc.line, loc.col); fprintf(stderr, msg, arg); fputc('\n', stderr); + excerpt(d,loc); } void fe_diag_note(FeLoc loc, const char *msg) { fprintf(stderr, "%s:%lu:%lu: note: %s\n", loc.file ? loc.file : "", loc.line, loc.col, msg); } + +void fe_diag_note_src(FeDiags *d, FeLoc loc, const char *msg) +{ + fe_diag_note(loc,msg); + excerpt(d,loc); +}