Skip to content

Commit

Permalink
feat(compile): print errors as json for yaksha compile command
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Apr 12, 2024
1 parent 0f0c0a0 commit c3630fa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
7 changes: 4 additions & 3 deletions compiler/src/comp_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ int main(int argc, char *argv[]) {
argparser::ARGS(PROGRAM_NAME, "Compile Yaksha code to C code", "");
auto help = argparser::OP_BOOL('h', "--help", "Print this help message");
auto no_main = argparser::OP_BOOL('N', "--no-main", "Disable main() check");
auto no_codegen =
argparser::OP_BOOL('d', "--disable-codegen",
"Do not output generated code, instead just print errors.");
auto no_codegen = argparser::OP_BOOL(
'd', "--disable-codegen",
"Do not output generated code, instead just print errors as JSON.");
args.optional_ = {&help, &no_main, &no_codegen};
auto code = argparser::PO("mainfile.yaka", "Yaksha code file.");
auto lib = argparser::PO_OPT("[LIBS_PARENT_PATH]",
Expand All @@ -72,6 +72,7 @@ int main(int argc, char *argv[]) {
multifile_compiler mc{};
try {
mc.main_required_ = !no_main.is_set_;
if (no_codegen.is_set_) { mc.error_printer_.json_output_ = true; }
codegen_c cg{};
do_nothing_codegen dn_cg{};
codegen *cg_ptr = &cg;
Expand Down
29 changes: 27 additions & 2 deletions compiler/src/utilities/error_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,34 @@ namespace yaksha::errors {
bool error_printer::has_no_errors() { return error_capture.empty(); }
#endif
void error_printer::print_errors(const std::vector<parsing_error> &errors) {
if (json_output_) {
std::cerr << errors_to_json_lines(errors);
return;
} else {
for (auto &err : errors) {
print_error(std::cerr, err);
std::cerr << "\n";
}
}
}
std::string
error_printer::errors_to_json_lines(const std::vector<parsing_error> &errors) {
std::stringstream ss{};
for (auto &err : errors) {
print_error(std::cerr, err);
std::cerr << "\n";
ss << "{";
if (err.token_set_) {
ss << "\"file\":\"" << string_utils::escape_json(err.tok_.file_) << "\",";
auto relative_file = std::filesystem::relative(err.tok_.file_, "./").string();
ss << "\"relative_file\":\"" << string_utils::escape_json(relative_file) << "\",";
ss << "\"line\":" << err.tok_.line_ + 1 << ",";
ss << "\"pos\":" << err.tok_.pos_ << ",";
ss << "\"message\":\"" << string_utils::escape_json(err.message_) << "\",";
ss << "\"token\":\"" << string_utils::escape_json(err.tok_.original_) << "\"";
} else {
ss << "\"message\":\"" << string_utils::escape_json(err.message_) << "\"";
}
ss << "}\n";
}
return ss.str();
}
}// namespace yaksha::errors
2 changes: 2 additions & 0 deletions compiler/src/utilities/error_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace yaksha::errors {
bool has_any_error();
bool has_no_errors();
#endif
bool json_output_ = false;
/**
* Print a vector of errors to std::cerr
* @param errors errors to print
Expand All @@ -78,6 +79,7 @@ namespace yaksha::errors {
// This is used for type checker testing
std::vector<std::string> error_capture{};
#endif
std::string errors_to_json_lines(const std::vector<parsing_error> &errors);
};
}// namespace yaksha::errors
#endif

0 comments on commit c3630fa

Please sign in to comment.