|
| 1 | +typedef struct onyx_error_details_t { |
| 2 | + onyx_error_t rank; |
| 3 | + const char *message; |
| 4 | + const char *filename; |
| 5 | + int line; |
| 6 | + int column; |
| 7 | + int length; |
| 8 | + char *line_text; |
| 9 | +} onyx_error_details_t; |
| 10 | + |
| 11 | +static void print_error_text(const char *text, b32 color) { |
| 12 | + if (!color) { |
| 13 | + bh_printf("%s", text); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + const char *ch = text; |
| 18 | + b32 in_color = 0; |
| 19 | + |
| 20 | + while (*ch != '\0') { |
| 21 | + if (*ch == '\'') { |
| 22 | + in_color = !in_color; |
| 23 | + if (in_color) bh_printf("\033[92m"); |
| 24 | + else bh_printf("\033[0m"); |
| 25 | + } else { |
| 26 | + bh_printf("%c", *ch); |
| 27 | + } |
| 28 | + |
| 29 | + ch++; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +static void print_underline(onyx_error_details_t *err, i32 len, i32 first_non_whitespace, b32 colored_printing) { |
| 34 | + len = bh_min(len, 1024); |
| 35 | + char* pointer_str = alloca(sizeof(char) * len); |
| 36 | + memset(pointer_str, ' ', len); |
| 37 | + |
| 38 | + int c = err->column - 1; |
| 39 | + int l = err->length; |
| 40 | + |
| 41 | + memcpy(pointer_str, err->line_text, first_non_whitespace); |
| 42 | + memset(pointer_str + c + 1, '~', l - 1); |
| 43 | + pointer_str[c] = '^'; |
| 44 | + pointer_str[c + l] = 0; |
| 45 | + |
| 46 | + if (colored_printing) bh_printf("\033[91m"); |
| 47 | + bh_printf("%s\n", pointer_str); |
| 48 | + if (colored_printing) bh_printf("\033[0m\n"); |
| 49 | +} |
| 50 | + |
| 51 | +static void print_detailed_message_v1(onyx_error_details_t *err, b32 colored_printing) { |
| 52 | + bh_printf("(%s:%l,%l) %s\n", err->filename, err->line, err->column, err->message); |
| 53 | + |
| 54 | + i32 linelength = 0; |
| 55 | + i32 first_char = 0; |
| 56 | + char* walker = err->line_text; |
| 57 | + while (*walker == ' ' || *walker == '\t') first_char++, linelength++, walker++; |
| 58 | + while (*walker && *walker != '\n') linelength++, walker++; |
| 59 | + |
| 60 | + if (colored_printing) bh_printf("\033[90m"); |
| 61 | + i32 numlen = bh_printf(" %d | ", err->line); |
| 62 | + if (colored_printing) bh_printf("\033[94m"); |
| 63 | + bh_printf("%b\n", err->line_text, linelength); |
| 64 | + |
| 65 | + fori (i, 0, numlen - 1) bh_printf(" "); |
| 66 | + print_underline(err, linelength, first_char, colored_printing); |
| 67 | +} |
| 68 | + |
| 69 | +static void print_detailed_message_v2(onyx_error_details_t* err, b32 colored_printing) { |
| 70 | + if (colored_printing) { |
| 71 | + switch (err->rank) { |
| 72 | + case ONYX_ERROR_WARNING: |
| 73 | + bh_printf("\033[93mwarning\033[0m: "); |
| 74 | + print_error_text(err->message, colored_printing); |
| 75 | + bh_printf("\n\033[90m at: %s:%l,%l\033[0m\n", err->filename, err->line, err->column); |
| 76 | + break; |
| 77 | + |
| 78 | + default: |
| 79 | + bh_printf("\033[91merror\033[0m: "); |
| 80 | + print_error_text(err->message, colored_printing); |
| 81 | + bh_printf("\n\033[90m at: %s:%l,%l\033[0m\n", err->filename, err->line, err->column); |
| 82 | + break; |
| 83 | + } |
| 84 | + } else { |
| 85 | + switch (err->rank) { |
| 86 | + case ONYX_ERROR_WARNING: |
| 87 | + bh_printf("warning: "); |
| 88 | + print_error_text(err->message, colored_printing); |
| 89 | + bh_printf("\n at: %s:%l,%l\n", err->filename, err->line, err->column); |
| 90 | + break; |
| 91 | + |
| 92 | + default: |
| 93 | + bh_printf("error: "); |
| 94 | + print_error_text(err->message, colored_printing); |
| 95 | + bh_printf("\n at: %s:%l,%l\n", err->filename, err->line, err->column); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + i32 linelength = 0; |
| 101 | + i32 first_char = 0; |
| 102 | + char* walker = err->line_text; |
| 103 | + while (*walker == ' ' || *walker == '\t') first_char++, linelength++, walker++; |
| 104 | + while (*walker && *walker != '\n') linelength++, walker++; |
| 105 | + |
| 106 | + char numbuf[32] = {0}; |
| 107 | + i32 numlen = bh_snprintf(numbuf, 31, " %d | ", err->line); |
| 108 | + |
| 109 | + if (colored_printing) bh_printf("\033[90m"); |
| 110 | + fori (i, 0, numlen - 3) bh_printf(" "); |
| 111 | + bh_printf("|\n%s", numbuf); |
| 112 | + if (colored_printing) bh_printf("\033[94m"); |
| 113 | + |
| 114 | + bh_printf("%b\n", err->line_text, linelength); |
| 115 | + |
| 116 | + if (colored_printing) bh_printf("\033[90m"); |
| 117 | + fori (i, 0, numlen - 3) bh_printf(" "); |
| 118 | + bh_printf("| "); |
| 119 | + if (colored_printing) bh_printf("\033[94m"); |
| 120 | + |
| 121 | + print_underline(err, linelength, first_char, colored_printing); |
| 122 | +} |
| 123 | + |
| 124 | +static void print_detailed_message_json(onyx_error_details_t* err, b32 colored_printing) { |
| 125 | + bh_printf( |
| 126 | + "{\"rank\":%d,\"file\":\"%s\",\"line\":%d,\"column\":%d,\"length\":%d,\"msg\":\"%s\"}", |
| 127 | + err->rank, |
| 128 | + err->filename, |
| 129 | + err->line, |
| 130 | + err->column, |
| 131 | + err->length, |
| 132 | + err->message |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +static void print_detailed_message(onyx_error_details_t* err, b32 colored_printing, char *error_format) { |
| 137 | + if (!err->filename) { |
| 138 | + // This makes the assumption that if a file is not specified for an error, |
| 139 | + // the error must have come from the command line. |
| 140 | + |
| 141 | + if (colored_printing) { |
| 142 | + bh_printf("\033[91merror\033[0m: "); |
| 143 | + bh_printf("%s\n", err->message); |
| 144 | + bh_printf("\033[90m at: command line argument\033[0m\n"); |
| 145 | + } else { |
| 146 | + bh_printf("error: "); |
| 147 | + bh_printf("%s\n", err->message); |
| 148 | + bh_printf(" at: command line argument\n"); |
| 149 | + } |
| 150 | + |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + if (!strcmp(error_format, "v2")) { |
| 155 | + print_detailed_message_v2(err, colored_printing); |
| 156 | + } |
| 157 | + else if (!strcmp(error_format, "v1")) { |
| 158 | + print_detailed_message_v1(err, colored_printing); |
| 159 | + } |
| 160 | + else if (!strcmp(error_format, "json")) { |
| 161 | + print_detailed_message_json(err, colored_printing); |
| 162 | + } |
| 163 | + else { |
| 164 | + bh_printf("Unknown error format: '%s'.\n", error_format); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +void onyx_errors_print(onyx_context_t *ctx, char *error_format, b32 colored_printing, b32 show_all_errors) { |
| 169 | + b32 error_format_json = !strcmp(error_format, "json"); |
| 170 | + if (error_format_json) bh_printf("["); |
| 171 | + |
| 172 | + onyx_error_t last_rank = onyx_error_rank(ctx, 0); |
| 173 | + fori (i, 0, onyx_error_count(ctx)) { |
| 174 | + onyx_error_details_t err; |
| 175 | + err.rank = onyx_error_rank(ctx, i); |
| 176 | + err.message = onyx_error_message(ctx, i); |
| 177 | + err.filename = onyx_error_filename(ctx, i); |
| 178 | + err.line = onyx_error_line(ctx, i); |
| 179 | + err.column = onyx_error_column(ctx, i); |
| 180 | + err.length = onyx_error_length(ctx, i); |
| 181 | + |
| 182 | + char line_text[256]; |
| 183 | + onyx_error_line_text(ctx, i, line_text, 255); |
| 184 | + err.line_text = line_text; |
| 185 | + |
| 186 | + if (!show_all_errors && last_rank != err.rank) break; |
| 187 | + if (error_format_json && i != 0) bh_printf(","); |
| 188 | + |
| 189 | + print_detailed_message(&err, colored_printing, error_format); |
| 190 | + |
| 191 | + last_rank = err.rank; |
| 192 | + } |
| 193 | + |
| 194 | + if (error_format_json) bh_printf("]"); |
| 195 | +} |
| 196 | + |
0 commit comments