1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#include "error.hh"
#include "nixexpr.hh"
#include <iostream>
#include <optional>
int main()
{
using namespace nix;
// In each program where errors occur, this has to be set.
ErrorInfo::programName = std::optional("error-demo");
// There are currently four error types:
//
// ProgramError, ProgramWarning, NixLangError, NixLangWarning.
//
// Each error type is created with a specific sequence of builder functions.
// Unlike with a constructor, each parameter is clearly named.
// If the sequence of function calls isn't followed, then there's a type
// error. This should make for a consistent look in the code when errors are
// created.
// ProgramError takes name, description, and an optional hint.
printErrorInfo(
ErrorInfo::ProgramError("name",
"error description",
std::nullopt));
// ProgramWarning takes name, description, and an optional hint.
// The hint is in the form of a hintfmt class, which wraps boost::format(),
// and makes all the substituted text yellow.
printErrorInfo(
ErrorInfo::ProgramWarning("name",
"warning description",
std::optional(hintfmt("there was a %1%", "warning"))));
// printErrorInfo( ProgramWarning()
// .name("warning name")
// .description("warning description")
// // the templated value, 'warning', is automatically colored yellow.
// .hint(hintfmt("there was a %1%", "warning"))
// );
/*
// some invalid errors:
// type error: no hint function.
ProgramError()
.name("name")
.description("error description");
// type error: description before name.
ProgramError()
.description("error description")
.name("name")
.nohint();
// type error: hint function with regular boost format, not special
hintfmt. ProgramError() .description("error description") .name("name")
.hint(format("there was a %1%") % "warning");
*/
// NixLangWarning adds nix file, line number, column range, and the lines of
// code where a warning occurred.
SymbolTable testTable;
auto problem_symbol = testTable.create("problem");
printErrorInfo(ErrorInfo::NixLangWarning(
"warning name",
"warning description",
Pos(problem_symbol, 40, 13),
std::nullopt,
"this is the problem line of code",
std::nullopt,
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
// // NixLangError is just the same as NixLangWarning, except for the Error
// // flag.
// printErrorInfo(NixLangError()
// .name("error name")
// .description("error description")
// .pos(Pos(problem_symbol, 40, 13))
// .linesOfCode(std::optional("previous line of code"),
// "this is the problem line of code",
// std::optional("next line of code"))
// .hint(hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
printErrorInfo(ErrorInfo::NixLangError(
"error name",
"error description",
Pos(problem_symbol, 40, 13),
std::optional("previous line of code"),
"this is the problem line of code",
std::optional("next line of code"),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
return 0;
}
|