nixd
Loading...
Searching...
No Matches
libnixf
src
Basic
Diagnostic.cpp.py
Go to the documentation of this file.
1
# Generate "DiagnosticEnum.h"
2
from
functools
import
reduce
3
from
operator
import
add
4
import
sys
5
6
from
diagnostic
import
Diagnostic, diagnostics
7
from
support
import
lines, indent
8
9
10
def
gen_parse_id
() -> list[str]:
11
def
gen_case(d: Diagnostic) -> list[str]:
12
return
[
13
"{"
f
'"{d["sname"]}", Diagnostic::DK_{d["cname"]}'
"},"
,
14
]
15
16
return
[
17
"std::optional<Diagnostic::DiagnosticKind> Diagnostic::parseKind(std::string_view SName) {"
,
18
*map(
19
indent,
20
[
21
"static std::unordered_map<std::string_view, nixf::Diagnostic::DiagnosticKind> DKMap {"
,
22
*map(indent, reduce(add, map(gen_case, diagnostics))),
23
"};"
,
24
""
,
25
"auto It = DKMap.find(SName);"
,
26
"if (It != DKMap.end())"
,
27
" return It->second;"
,
28
"return std::nullopt;"
,
29
],
30
),
31
"}"
,
32
]
33
34
35
def
gen_message
() -> list[str]:
36
"Generate nixf::Diagnostic::message implementation"
37
38
def
gen_case(d: Diagnostic) -> list[str]:
39
return
[
40
f
'case DK_{d["cname"]}:'
,
41
indent(f
'return "{d["message"]}";'
),
42
]
43
44
return
[
45
"const char *Diagnostic::message(DiagnosticKind Kind) {"
,
46
*map(
47
indent,
48
[
49
"switch(Kind) {"
,
50
*reduce(add, map(gen_case, diagnostics)),
51
"}"
,
52
"__builtin_unreachable();"
,
53
],
54
),
55
"}"
,
56
]
57
58
59
def
gen_serverity
() -> list[str]:
60
"Generate nixf::Diagnostic::severity implementation"
61
62
def
gen_case(d: Diagnostic) -> list[str]:
63
return
[
64
f
'case DK_{d["cname"]}:'
,
65
indent(f
'return DS_{d["severity"]};'
),
66
]
67
68
return
[
69
"Diagnostic::Severity Diagnostic::severity(DiagnosticKind Kind) {"
,
70
*map(
71
indent,
72
[
73
"switch(Kind) {"
,
74
*reduce(add, map(gen_case, diagnostics)),
75
"}"
,
76
"__builtin_unreachable();"
,
77
],
78
),
79
"}"
,
80
]
81
82
83
def
gen_sname
() -> list[str]:
84
"Generate nixf::Diagnostic::sname implementation"
85
86
def
gen_case(d: Diagnostic) -> list[str]:
87
return
[
88
f
'case DK_{d["cname"]}:'
,
89
indent(f
'return "{d["sname"]}";'
),
90
]
91
92
return
[
93
"const char *Diagnostic::sname(DiagnosticKind Kind) {"
,
94
*map(
95
indent,
96
[
97
"switch(Kind) {"
,
98
*reduce(add, map(gen_case, diagnostics)),
99
"}"
,
100
"__builtin_unreachable();"
,
101
],
102
),
103
"}"
,
104
]
105
106
107
output = open(sys.argv[1],
"w"
)
108
_ = output.write(
109
lines(
110
[
111
'#include "nixf/Basic/Diagnostic.h"'
,
112
"#include <unordered_map>"
,
113
"using namespace nixf;"
,
114
*
gen_sname
(),
115
*
gen_serverity
(),
116
*
gen_message
(),
117
*
gen_parse_id
(),
118
]
119
)
120
)
Diagnostic.gen_parse_id
list[str] gen_parse_id()
Definition
Diagnostic.cpp.py:10
Diagnostic.gen_sname
list[str] gen_sname()
Definition
Diagnostic.cpp.py:83
Diagnostic.gen_serverity
list[str] gen_serverity()
Definition
Diagnostic.cpp.py:59
Diagnostic.gen_message
list[str] gen_message()
Definition
Diagnostic.cpp.py:35
Generated by
1.12.0