libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
error_handling.c
Go to the documentation of this file.
1#include <stdbool.h>
2#include <stdio.h>
3
4#include "error_handling.h"
5#include "module.h"
6
7
8#define LBUFF_SIZE 127
9
10
11static inline int
12_error_print_line(module_t* module, unsigned int line, unsigned int err_column, bool has_error);
13
14static void
15_error_print_indicator(unsigned int start_column, unsigned int length);
16
17static void
18_error_print_info(module_t* module, unsigned int err_line, unsigned int err_column);
19
20static void
21_error_remove_line(module_t* module);
22
23void
24error_module_ctx(module_t* module, unsigned int line, unsigned int column, unsigned int length, const char* message)
25{
26 unsigned int ctx_start_line = (line > ERR_CTX_SIZE) ? line - ERR_CTX_SIZE : 0;
27 unsigned int ctx_end_line = line + ERR_CTX_SIZE;
28 unsigned int current_line = 0;
29
30 fseek(module->file, 0, SEEK_SET);
31
32
33 while (feof(module->file) == 0 && current_line < line - 1) {
34 current_line++;
35
36 if (current_line >= ctx_start_line) {
37 _error_print_line(module, current_line, 0, false);
38 continue;
39 }
40
41 _error_remove_line(module);
42 }
43
44 current_line++;
45
46 unsigned int correction = _error_print_line(module, current_line, column, true);
47 _error_print_indicator(column + correction, length);
48 fprintf(stderr, "%s%s\n", ERR_PREFIX, message);
49
50 while (feof(module->file) == 0 && current_line < ctx_end_line) {
51 current_line++;
52
53 _error_print_line(module, current_line, 0, false);
54 }
55
56 _error_print_info(module, line, column);
57}
58
59#define COL_PREFIX_SIZE 7 // The size of the prefix "xxxx | " printed on the start of the line.
60
67static inline int
68_error_print_line(module_t* module, unsigned int line, unsigned int err_column, bool has_error)
69{
70 int ch;
71 unsigned int column = COL_PREFIX_SIZE;
72 unsigned int column_correction = COL_PREFIX_SIZE;
73 unsigned int buffer_index = 0;
74 char buffer[LBUFF_SIZE + 1];
75
76 if (has_error) {
77 fprintf(stderr, "%4d" C_RED "->" C_NORMAL " ", line);
78 } else {
79 fprintf(stderr, "%4d | ", line);
80 }
81
82 while ((ch = fgetc(module->file)) != EOF) {
83 column++;
84
85 if (column > ERR_CTX_LINE_WRAP) {
86 column_correction -= ERR_CTX_LINE_WRAP;
87 column = 1;
88
89 buffer[buffer_index] = '\n';
90 buffer[buffer_index + 1] = '\0';
91 fputs(buffer, stderr);
92
93 buffer_index = 0;
94 }
95
96 buffer[buffer_index++] = ch;
97
98 if (ch == '\n') {
99 break;
100 }
101
102 if (buffer_index >= LBUFF_SIZE) {
103 buffer[LBUFF_SIZE] = '\0';
104 fputs(buffer, stderr);
105
106 buffer_index = 0;
107 }
108 }
109
110 if (buffer_index > 0) {
111 buffer[buffer_index] = '\0';
112 fputs(buffer, stderr);
113 }
114
115 return column_correction;
116}
117
118static void
119_error_print_indicator(unsigned int start_column, unsigned int length)
120{
121 // unsigned int size = end_column - start_column + 1;
122
123 fprintf(stderr, "%*c" C_RED, start_column - 1, ' ');
124
125 for (int i = 0; i < length; i++) {
126 fputc('^', stderr);
127 }
128
129 fprintf(stderr, C_NORMAL "\n");
130}
131
132static void
133_error_print_info(module_t* module, unsigned int err_line, unsigned int err_column)
134{
135 fprintf(stderr, "On module %s:%d:%d.\n", module->filepath, err_line, err_column);
136}
137
138static void
139_error_remove_line(module_t* module)
140{
141 int ch;
142
143 do {
144 ch = fgetc(module->file);
145 } while (ch != '\n' && ch != EOF);
146}
#define C_RED
Definition colors.h:16
#define C_NORMAL
Definition colors.h:14
#define ERR_CTX_SIZE
Definition err.h:5
#define ERR_PREFIX
Definition err.h:8
#define ERR_CTX_LINE_WRAP
Definition err.h:6
void error_module_ctx(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Print a error message formated to show the code context inside the module.
#define COL_PREFIX_SIZE
#define LBUFF_SIZE
Struct that represents a Mya module.
Definition module.h:34
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:44
FILE * file
Module's file handler.
Definition module.h:35
struct module module_t
Struct that represents a Mya module.