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#pragma once
18
19#include <stddef.h>
20#include <stdbool.h>
21#include <stdio.h>
22
24 size_t line;
25 const char* start;
26};
27
28#define PERR_Str(line_, start_) \
29 (struct libprettyerr_str){ .line = line_, .start = start_ }
30#define PERR_Str_None() \
31 PERR_Str(0, NULL)
32
34 size_t index;
35 size_t length;
36};
37
38#define PERR_Pos(index_, length_) \
39 (struct libprettyerr_pos){ .index = index_, .length = length_ }
40
42 PERR_SUCCESS, // bold green
43 PERR_ERROR, // bold red
45 PERR_INFO, // bold white
46 PERR_HINT // bold yellow
47};
48
60
63
66
67 const char* main;
68 const char* explain;
70
71 const char* filename;
72};
73
74#define PERR_Error(type_, primary_, error_position_, main_, explain_, error_code_, filename_) \
75 (struct libprettyerr_error){ \
76 .type = type_, .primary = primary_, .error_position = error_position_, \
77 .main = main_, .explain = explain_, .error_code = error_code_, \
78 .filename = filename_ \
79 }
80
81#define PERR_Secondary(type_, primary_, error_position_, main_, filename_) \
82 (struct libprettyerr_error){ \
83 .type = type_, .primary = primary_, .error_position = error_position_, \
84 .main = main_, .filename = filename_ \
85 }
86
87// TODO: tertiary for warnings
89typedef void (*libprettyerr_runner_t)(const struct libprettyerr_printer*, const struct libprettyerr_error*);
91 const char* source;
92 FILE* stream;
93 const char** box_lookup;
95 bool color;
96 bool utf8;
97 bool rounded;
98};
99
100#define PRETTYERR_EXPORT
101
102// Initializes a printer
104void perr_printer_init(struct libprettyerr_printer* printer, FILE* stream,
105 const char* source, bool utf8, libprettyerr_runner_t style);
106
107// Uses the printer to display the provided error in
109void perr_print_error(const struct libprettyerr_printer* printer,
110 const struct libprettyerr_error* err);
111
112// prints source filename and position
115 const struct libprettyerr_error* err,
116 const char *color);
117
perr_runner_t perr_runner_secondary_style
Definition prettyerr.c:246
perr_runner_t perr_runner_basic_style
Definition prettyerr.c:245
#define PRETTYERR_EXPORT
Definition prettyerr.h:100
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:49
@ PERR_BOX_THIN_VERT
Definition prettyerr.h:57
@ PERR_BOX_THIN_UL_ROUNDED
Definition prettyerr.h:53
@ PERR_BOX_THICK_VERT
Definition prettyerr.h:50
@ PERR_BOX_THIN_BL
Definition prettyerr.h:55
@ PERR_BOX_THIN_BL_ROUNDED
Definition prettyerr.h:56
@ PERR_BOX_MAX
Definition prettyerr.h:58
@ PERR_BOX_THIN_HIGH
Definition prettyerr.h:51
@ PERR_BOX_THIN_UL
Definition prettyerr.h:52
@ PERR_BOX_THIN_HORIZ
Definition prettyerr.h:54
struct libprettyerr_printer perr_printer_t
Definition prettyerr.h:119
struct libprettyerr_error perr_t
Definition prettyerr.h:118
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:41
@ PERR_INFO
Definition prettyerr.h:45
@ PERR_SUCCESS
Definition prettyerr.h:42
@ PERR_HINT
Definition prettyerr.h:46
@ PERR_WARNING
Definition prettyerr.h:44
@ PERR_ERROR
Definition prettyerr.h:43
libprettyerr_runner_t perr_runner_t
Definition prettyerr.h:120
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:89
const char * filename
Definition prettyerr.h:71
const char * main
Definition prettyerr.h:67
struct libprettyerr_str primary
Definition prettyerr.h:64
const char * explain
Definition prettyerr.h:68
enum libprettyerr_errtype type
Definition prettyerr.h:62
struct libprettyerr_pos error_position
Definition prettyerr.h:65
const char ** box_lookup
Definition prettyerr.h:93
libprettyerr_runner_t runner
Definition prettyerr.h:94
const char * source
Definition prettyerr.h:91
const char * start
Definition prettyerr.h:25