gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
prettyerr.h
Go to the documentation of this file.
1// libprettyerr: error.h
2// Copyright (C) 2021 Ethan Uppal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef _LIBPRETTYERR_ERROR_H
18#define _LIBPRETTYERR_ERROR_H
19
20#include <stddef.h>
21#include <stdbool.h>
22#include <stdio.h>
23
25 size_t line;
26 const char* start;
27};
28
29#define PERR_Str(line_, start_) \
30 (struct libprettyerr_str){ .line = line_, .start = start_ }
31#define PERR_Str_None() \
32 PERR_Str(0, NULL)
33
35 size_t index;
36 size_t length;
37};
38
39#define PERR_Pos(index_, length_) \
40 (struct libprettyerr_pos){ .index = index_, .length = length_ }
41
43 PERR_SUCCESS, // bold green
44 PERR_ERROR, // bold red
46 PERR_INFO, // bold white
47 PERR_HINT // bold yellow
48};
49
61
64
67
68 const char* main;
69 const char* explain;
71
72 const char* filename;
73};
74
75#define PERR_Error(type_, primary_, error_position_, main_, explain_, error_code_, filename_) \
76 (struct libprettyerr_error){ \
77 .type = type_, .primary = primary_, .error_position = error_position_, \
78 .main = main_, .explain = explain_, .error_code = error_code_, \
79 .filename = filename_ \
80 }
81
82#define PERR_Secondary(type_, primary_, error_position_, main_, filename_) \
83 (struct libprettyerr_error){ \
84 .type = type_, .primary = primary_, .error_position = error_position_, \
85 .main = main_, .filename = filename_ \
86 }
87
88// TODO: tertiary for warnings
90typedef void (*libprettyerr_runner_t)(const struct libprettyerr_printer*, const struct libprettyerr_error*);
92 const char* source;
93 FILE* stream;
94 const char** box_lookup;
96 bool color;
97 bool utf8;
98 bool rounded;
99};
100
101#define PRETTYERR_EXPORT
102
103// Initializes a printer
105void perr_printer_init(struct libprettyerr_printer* printer, FILE* stream,
106 const char* source, bool utf8, libprettyerr_runner_t style);
107
108// Uses the printer to display the provided error in
110void perr_print_error(const struct libprettyerr_printer* printer,
111 const struct libprettyerr_error* err);
112
113// prints source filename and position
116 const struct libprettyerr_error* err,
117 const char *color);
118
124
125#endif /* _LIBPRETTYERR_PRETTYERR_H */
#define PRETTYERR_EXPORT
Definition prettyerr.h:101
PRETTYERR_EXPORT perr_runner_t perr_runner_basic_style
Definition prettyerr.c:245
PRETTYERR_EXPORT void perr_print_line_number(const struct libprettyerr_printer *printer, const struct libprettyerr_error *err, const char *color)
libprettyerr_boxtype
Definition prettyerr.h:50
@ PERR_BOX_THIN_VERT
Definition prettyerr.h:58
@ PERR_BOX_THIN_UL_ROUNDED
Definition prettyerr.h:54
@ PERR_BOX_THICK_VERT
Definition prettyerr.h:51
@ PERR_BOX_THIN_BL
Definition prettyerr.h:56
@ PERR_BOX_THIN_BL_ROUNDED
Definition prettyerr.h:57
@ PERR_BOX_MAX
Definition prettyerr.h:59
@ PERR_BOX_THIN_HIGH
Definition prettyerr.h:52
@ PERR_BOX_THIN_UL
Definition prettyerr.h:53
@ PERR_BOX_THIN_HORIZ
Definition prettyerr.h:55
PRETTYERR_EXPORT perr_runner_t perr_runner_secondary_style
Definition prettyerr.c:246
PRETTYERR_EXPORT void perr_printer_init(struct libprettyerr_printer *printer, FILE *stream, const char *source, bool utf8, libprettyerr_runner_t style)
Definition prettyerr.c:42
libprettyerr_errtype
Definition prettyerr.h:42
@ PERR_INFO
Definition prettyerr.h:46
@ PERR_SUCCESS
Definition prettyerr.h:43
@ PERR_HINT
Definition prettyerr.h:47
@ PERR_WARNING
Definition prettyerr.h:45
@ PERR_ERROR
Definition prettyerr.h:44
libprettyerr_runner_t perr_runner_t
Definition prettyerr.h:121
PRETTYERR_EXPORT void perr_print_error(const struct libprettyerr_printer *printer, const struct libprettyerr_error *err)
void(* libprettyerr_runner_t)(const struct libprettyerr_printer *, const struct libprettyerr_error *)
Definition prettyerr.h:90
const char * filename
Definition prettyerr.h:72
const char * main
Definition prettyerr.h:68
struct libprettyerr_str primary
Definition prettyerr.h:65
const char * explain
Definition prettyerr.h:69
enum libprettyerr_errtype type
Definition prettyerr.h:63
struct libprettyerr_pos error_position
Definition prettyerr.h:66
const char ** box_lookup
Definition prettyerr.h:94
libprettyerr_runner_t runner
Definition prettyerr.h:95
const char * source
Definition prettyerr.h:92
const char * start
Definition prettyerr.h:26