Skip to content

Commit

Permalink
Revert "jsonnet rm exceptions"
Browse files Browse the repository at this point in the history
This reverts commit dc78e77.
  • Loading branch information
beicause committed Aug 4, 2024
1 parent 9fdc9db commit aa07abe
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 237 deletions.
2 changes: 1 addition & 1 deletion modules/a_jsonnet/thirdparty/jsonnet/core/desugarer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ class Desugarer {

} else if (auto *ast = dynamic_cast<Dollar *>(ast_)) {
if (obj_level == 0) {
jsonnet_throw(StaticError(ast->location, "No top-level object found."));
throw StaticError(ast->location, "No top-level object found.");
}
ast_ = var(id(U"$"));

Expand Down
30 changes: 15 additions & 15 deletions modules/a_jsonnet/thirdparty/jsonnet/core/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ std::string lex_number(const char *&c, const std::string &filename, const Locati
case '8':
case '9': state = AFTER_ONE_TO_NINE; break;

default: jsonnet_throw(StaticError(filename, begin, "couldn't lex number"));
default: throw StaticError(filename, begin, "couldn't lex number");
}
break;

Expand Down Expand Up @@ -304,7 +304,7 @@ std::string lex_number(const char *&c, const std::string &filename, const Locati
default: {
std::stringstream ss;
ss << "couldn't lex number, junk after decimal point: " << *c;
jsonnet_throw(StaticError(filename, begin, ss.str()));
throw StaticError(filename, begin, ss.str());
}
}
break;
Expand Down Expand Up @@ -348,7 +348,7 @@ std::string lex_number(const char *&c, const std::string &filename, const Locati
default: {
std::stringstream ss;
ss << "couldn't lex number, junk after 'E': " << *c;
jsonnet_throw(StaticError(filename, begin, ss.str()));
throw StaticError(filename, begin, ss.str());
}
}
break;
Expand All @@ -369,7 +369,7 @@ std::string lex_number(const char *&c, const std::string &filename, const Locati
default: {
std::stringstream ss;
ss << "couldn't lex number, junk after exponent sign: " << *c;
jsonnet_throw(StaticError(filename, begin, ss.str()));
throw StaticError(filename, begin, ss.str());
}
}
break;
Expand Down Expand Up @@ -527,7 +527,7 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
c++;
for (;; ++c) {
if (*c == '\0') {
jsonnet_throw(StaticError(filename, begin, "unterminated string"));
throw StaticError(filename, begin, "unterminated string");
}
if (*c == '"') {
break;
Expand All @@ -552,7 +552,7 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
c++;
for (;; ++c) {
if (*c == '\0') {
jsonnet_throw(StaticError(filename, begin, "unterminated string"));
throw StaticError(filename, begin, "unterminated string");
}
if (*c == '\'') {
break;
Expand Down Expand Up @@ -583,13 +583,13 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
if (*c != '"' && *c != '\'') {
std::stringstream ss;
ss << "couldn't lex verbatim string, junk after '@': " << *c;
jsonnet_throw(StaticError(filename, begin, ss.str()));
throw StaticError(filename, begin, ss.str());
}
const char quot = *c;
c++; // Advance beyond the opening quote.
for (;; ++c) {
if (*c == '\0') {
jsonnet_throw(StaticError(filename, begin, "unterminated verbatim string"));
throw StaticError(filename, begin, "unterminated verbatim string");
}
if (*c == quot) {
if (*(c + 1) == quot) {
Expand Down Expand Up @@ -641,7 +641,7 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
while (!(*c == '*' && *(c + 1) == '/')) {
if (*c == '\0') {
auto msg = "multi-line comment has no terminating */.";
jsonnet_throw(StaticError(filename, begin, msg));
throw StaticError(filename, begin, msg);
}
if (*c == '\n') {
// Just keep track of the line / column counters.
Expand Down Expand Up @@ -707,7 +707,7 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
while (is_horz_ws(*c)) ++c; // Chomp whitespace at end of line.
if (*c != '\n') {
auto msg = "text block syntax requires new line after |||.";
jsonnet_throw(StaticError(filename, begin, msg));
throw StaticError(filename, begin, msg);
}
std::stringstream block;
c++; // Skip the "\n"
Expand All @@ -724,14 +724,14 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
string_block_indent = std::string(first_line, ws_chars);
if (ws_chars == 0) {
auto msg = "text block's first line must start with whitespace.";
jsonnet_throw(StaticError(filename, begin, msg));
throw StaticError(filename, begin, msg);
}
while (true) {
assert(ws_chars > 0);
// Read up to the \n
for (c = &c[ws_chars]; *c != '\n'; ++c) {
if (*c == '\0')
jsonnet_throw(StaticError(filename, begin, "unexpected EOF"));
throw StaticError(filename, begin, "unexpected EOF");
block << *c;
}
// Add the \n
Expand All @@ -757,7 +757,7 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
// Expect |||
if (!(*c == '|' && *(c + 1) == '|' && *(c + 2) == '|')) {
auto msg = "text block not terminated with |||";
jsonnet_throw(StaticError(filename, begin, msg));
throw StaticError(filename, begin, msg);
}
c += 3; // Leave after the last |
data = block.str();
Expand Down Expand Up @@ -801,14 +801,14 @@ Tokens jsonnet_lex(const std::string &filename, const char *input)
ss << "code " << unsigned(uc);
else
ss << "'" << *c << "'";
jsonnet_throw(StaticError(filename, begin, ss.str()));
throw StaticError(filename, begin, ss.str());
}
}

// Ensure that a bug in the above code does not cause an infinite memory consuming loop due
// to pushing empty tokens.
if (c == original_c) {
jsonnet_throw(StaticError(filename, begin, "internal lexing error: pointer did not advance"));
throw StaticError(filename, begin, "internal lexing error: pointer did not advance");
}

Location end(line_number, (c + 1) - line_start);
Expand Down
78 changes: 39 additions & 39 deletions modules/a_jsonnet/thirdparty/jsonnet/core/libjsonnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ static enum ImportStatus try_path(const std::string &dir, const std::string &rel
f.open(abs_path.c_str());
if (!f.good())
return IMPORT_STATUS_FILE_NOT_FOUND;
// try {
try {
content.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
// } catch (const std::ios_base::failure &io_err) {
// err_msg = io_err.what();
// return IMPORT_STATUS_IO_ERROR;
// }
} catch (const std::ios_base::failure &io_err) {
err_msg = io_err.what();
return IMPORT_STATUS_IO_ERROR;
}
if (!f.good()) {
err_msg = strerror(errno);
return IMPORT_STATUS_IO_ERROR;
Expand Down Expand Up @@ -315,10 +315,10 @@ static int default_import_callback(void *ctx, const char *dir, const char *file,
return 0; // success
}

#define TRY {//try {
#define TRY try {
#define CATCH(func) \
} \
// catch (const std::bad_alloc &) \
catch (const std::bad_alloc &) \
{ \
memory_panic(); \
} \
Expand Down Expand Up @@ -471,7 +471,7 @@ void jsonnet_jpath_add(JsonnetVm *vm, const char *path_)
static char *jsonnet_fmt_snippet_aux(JsonnetVm *vm, const char *filename, const char *snippet,
int *error)
{
// try {
try {
Allocator alloc;
std::string json_str;
AST *expr;
Expand All @@ -491,12 +491,12 @@ static char *jsonnet_fmt_snippet_aux(JsonnetVm *vm, const char *filename, const
*error = false;
return from_string(vm, json_str);

// } catch (StaticError &e) {
// std::stringstream ss;
// ss << "STATIC ERROR: " << e << std::endl;
// *error = true;
// return from_string(vm, ss.str());
// }
} catch (StaticError &e) {
std::stringstream ss;
ss << "STATIC ERROR: " << e << std::endl;
*error = true;
return from_string(vm, ss.str());
}
}

char *jsonnet_fmt_file(JsonnetVm *vm, const char *filename, int *error)
Expand Down Expand Up @@ -533,7 +533,7 @@ enum EvalKind { REGULAR, MULTI, STREAM };
static char *jsonnet_evaluate_snippet_aux(JsonnetVm *vm, const char *filename, const char *snippet,
int *error, EvalKind kind)
{
// try {
try {
Allocator alloc;
AST *expr;
Tokens tokens = jsonnet_lex(filename, snippet);
Expand Down Expand Up @@ -642,30 +642,30 @@ static char *jsonnet_evaluate_snippet_aux(JsonnetVm *vm, const char *filename, c
abort();
}

// } catch (StaticError &e) {
// std::stringstream ss;
// ss << "STATIC ERROR: " << e << std::endl;
// *error = true;
// return from_string(vm, ss.str());

// } catch (RuntimeError &e) {
// std::stringstream ss;
// ss << "RUNTIME ERROR: " << e.msg << std::endl;
// const long max_above = vm->maxTrace / 2;
// const long max_below = vm->maxTrace - max_above;
// const long sz = e.stackTrace.size();
// for (long i = 0; i < sz; ++i) {
// const auto &f = e.stackTrace[i];
// if (vm->maxTrace > 0 && i >= max_above && i < sz - max_below) {
// if (i == max_above)
// ss << "\t..." << std::endl;
// } else {
// ss << "\t" << f.location << "\t" << f.name << std::endl;
// }
// }
// *error = true;
// return from_string(vm, ss.str());
// }
} catch (StaticError &e) {
std::stringstream ss;
ss << "STATIC ERROR: " << e << std::endl;
*error = true;
return from_string(vm, ss.str());

} catch (RuntimeError &e) {
std::stringstream ss;
ss << "RUNTIME ERROR: " << e.msg << std::endl;
const long max_above = vm->maxTrace / 2;
const long max_below = vm->maxTrace - max_above;
const long sz = e.stackTrace.size();
for (long i = 0; i < sz; ++i) {
const auto &f = e.stackTrace[i];
if (vm->maxTrace > 0 && i >= max_above && i < sz - max_below) {
if (i == max_above)
ss << "\t..." << std::endl;
} else {
ss << "\t" << f.location << "\t" << f.name << std::endl;
}
}
*error = true;
return from_string(vm, ss.str());
}

return nullptr; // Quiet, compiler.
}
Expand Down
Loading

0 comments on commit aa07abe

Please sign in to comment.