nixd
Loading...
Searching...
No Matches
Diagnostic.cpp.py
Go to the documentation of this file.
1# Generate "DiagnosticEnum.h"
2from functools import reduce
3from operator import add
4import sys
5
6from diagnostic import Diagnostic, diagnostics
7from support import lines, indent
8
9
10def 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
35def 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
59def 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
83def 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
107output = 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)
list[str] gen_parse_id()
list[str] gen_sname()
list[str] gen_serverity()
list[str] gen_message()