-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libnixf: generate diagnostic declarations from python (#548)
- Loading branch information
Showing
12 changed files
with
421 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/build/ | ||
|
||
/html/ | ||
|
||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Generate "DiagnosticEnum.h" | ||
from functools import reduce | ||
from operator import add | ||
import sys | ||
|
||
from diagnostic import Diagnostic, diagnostics | ||
from support import lines, indent | ||
|
||
|
||
def gen_parse_id() -> list[str]: | ||
def gen_case(d: Diagnostic) -> list[str]: | ||
return [ | ||
"{" f'"{d["sname"]}", Diagnostic::DK_{d["cname"]}' "},", | ||
] | ||
|
||
return [ | ||
"std::optional<Diagnostic::DiagnosticKind> Diagnostic::parseKind(std::string_view SName) {", | ||
*map( | ||
indent, | ||
[ | ||
"static std::unordered_map<std::string_view, nixf::Diagnostic::DiagnosticKind> DKMap {", | ||
*map(indent, reduce(add, map(gen_case, diagnostics))), | ||
"};", | ||
"", | ||
"auto It = DKMap.find(SName);", | ||
"if (It != DKMap.end())", | ||
" return It->second;", | ||
"return std::nullopt;", | ||
], | ||
), | ||
"}", | ||
] | ||
|
||
|
||
def gen_message() -> list[str]: | ||
"Generate nixf::Diagnostic::message implementation" | ||
|
||
def gen_case(d: Diagnostic) -> list[str]: | ||
return [ | ||
f'case DK_{d["cname"]}:', | ||
indent(f'return "{d["message"]}";'), | ||
] | ||
|
||
return [ | ||
"const char *Diagnostic::message(DiagnosticKind Kind) {", | ||
*map( | ||
indent, | ||
[ | ||
"switch(Kind) {", | ||
*reduce(add, map(gen_case, diagnostics)), | ||
"}", | ||
"__builtin_unreachable();", | ||
], | ||
), | ||
"}", | ||
] | ||
|
||
|
||
def gen_serverity() -> list[str]: | ||
"Generate nixf::Diagnostic::severity implementation" | ||
|
||
def gen_case(d: Diagnostic) -> list[str]: | ||
return [ | ||
f'case DK_{d["cname"]}:', | ||
indent(f'return DS_{d["severity"]};'), | ||
] | ||
|
||
return [ | ||
"Diagnostic::Severity Diagnostic::severity(DiagnosticKind Kind) {", | ||
*map( | ||
indent, | ||
[ | ||
"switch(Kind) {", | ||
*reduce(add, map(gen_case, diagnostics)), | ||
"}", | ||
"__builtin_unreachable();", | ||
], | ||
), | ||
"}", | ||
] | ||
|
||
|
||
def gen_sname() -> list[str]: | ||
"Generate nixf::Diagnostic::sname implementation" | ||
|
||
def gen_case(d: Diagnostic) -> list[str]: | ||
return [ | ||
f'case DK_{d["cname"]}:', | ||
indent(f'return "{d["sname"]}";'), | ||
] | ||
|
||
return [ | ||
"const char *Diagnostic::sname(DiagnosticKind Kind) {", | ||
*map( | ||
indent, | ||
[ | ||
"switch(Kind) {", | ||
*reduce(add, map(gen_case, diagnostics)), | ||
"}", | ||
"__builtin_unreachable();", | ||
], | ||
), | ||
"}", | ||
] | ||
|
||
|
||
output = open(sys.argv[1], "w") | ||
_ = output.write( | ||
lines( | ||
[ | ||
'#include "nixf/Basic/Diagnostic.h"', | ||
"#include <unordered_map>", | ||
"using namespace nixf;", | ||
*gen_sname(), | ||
*gen_serverity(), | ||
*gen_message(), | ||
*gen_parse_id(), | ||
] | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generate "DiagnosticEnum.h" | ||
import sys | ||
|
||
from support import lines | ||
from diagnostic import diagnostics | ||
|
||
|
||
output = open(sys.argv[1], "w") | ||
_ = output.write( | ||
lines( | ||
[ | ||
"enum DiagnosticKind {", | ||
*map(lambda x: f" DK_{x['cname']},", diagnostics), | ||
"};", | ||
] | ||
) | ||
) |
Oops, something went wrong.