Skip to content

Commit

Permalink
Treat TRY/CATCH macros as block start and end
Browse files Browse the repository at this point in the history
  • Loading branch information
sbarzowski committed Jul 24, 2017
1 parent be8c7a2 commit 5561bef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MacroBlockBegin: '^TRY$'
MacroBlockEnd: '^CATCH$'
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 2
Expand Down
50 changes: 30 additions & 20 deletions core/libjsonnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,16 @@ const char *jsonnet_version(void)

JsonnetVm *jsonnet_make(void)
{
TRY return new JsonnetVm();
TRY
return new JsonnetVm();
CATCH("jsonnet_make")
return nullptr;
}

void jsonnet_destroy(JsonnetVm *vm)
{
TRY delete vm;
TRY
delete vm;
CATCH("jsonnet_destroy")
}

Expand Down Expand Up @@ -444,25 +446,27 @@ static char *jsonnet_fmt_snippet_aux(JsonnetVm *vm, const char *filename, const

char *jsonnet_fmt_file(JsonnetVm *vm, const char *filename, int *error)
{
TRY std::ifstream f;
f.open(filename);
if (!f.good()) {
std::stringstream ss;
ss << "Opening input file: " << filename << ": " << strerror(errno);
*error = true;
return from_string(vm, ss.str());
}
std::string input;
input.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
TRY
std::ifstream f;
f.open(filename);
if (!f.good()) {
std::stringstream ss;
ss << "Opening input file: " << filename << ": " << strerror(errno);
*error = true;
return from_string(vm, ss.str());
}
std::string input;
input.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());

return jsonnet_fmt_snippet_aux(vm, filename, input.c_str(), error);
return jsonnet_fmt_snippet_aux(vm, filename, input.c_str(), error);
CATCH("jsonnet_fmt_file")
return nullptr; // Never happens.
}

char *jsonnet_fmt_snippet(JsonnetVm *vm, const char *filename, const char *snippet, int *error)
{
TRY return jsonnet_fmt_snippet_aux(vm, filename, snippet, error);
TRY
return jsonnet_fmt_snippet_aux(vm, filename, snippet, error);
CATCH("jsonnet_fmt_snippet")
return nullptr; // Never happens.
}
Expand Down Expand Up @@ -621,44 +625,50 @@ static char *jsonnet_evaluate_file_aux(JsonnetVm *vm, const char *filename, int

char *jsonnet_evaluate_file(JsonnetVm *vm, const char *filename, int *error)
{
TRY return jsonnet_evaluate_file_aux(vm, filename, error, REGULAR);
TRY
return jsonnet_evaluate_file_aux(vm, filename, error, REGULAR);
CATCH("jsonnet_evaluate_file")
return nullptr; // Never happens.
}

char *jsonnet_evaluate_file_multi(JsonnetVm *vm, const char *filename, int *error)
{
TRY return jsonnet_evaluate_file_aux(vm, filename, error, MULTI);
TRY
return jsonnet_evaluate_file_aux(vm, filename, error, MULTI);
CATCH("jsonnet_evaluate_file_multi")
return nullptr; // Never happens.
}

char *jsonnet_evaluate_file_stream(JsonnetVm *vm, const char *filename, int *error)
{
TRY return jsonnet_evaluate_file_aux(vm, filename, error, STREAM);
TRY
return jsonnet_evaluate_file_aux(vm, filename, error, STREAM);
CATCH("jsonnet_evaluate_file_stream")
return nullptr; // Never happens.
}

char *jsonnet_evaluate_snippet(JsonnetVm *vm, const char *filename, const char *snippet, int *error)
{
TRY return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, REGULAR);
TRY
return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, REGULAR);
CATCH("jsonnet_evaluate_snippet")
return nullptr; // Never happens.
}

char *jsonnet_evaluate_snippet_multi(JsonnetVm *vm, const char *filename, const char *snippet,
int *error)
{
TRY return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, MULTI);
TRY
return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, MULTI);
CATCH("jsonnet_evaluate_snippet_multi")
return nullptr; // Never happens.
}

char *jsonnet_evaluate_snippet_stream(JsonnetVm *vm, const char *filename, const char *snippet,
int *error)
{
TRY return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, STREAM);
TRY
return jsonnet_evaluate_snippet_aux(vm, filename, snippet, error, STREAM);
CATCH("jsonnet_evaluate_snippet_stream")
return nullptr; // Never happens.
}
Expand Down

0 comments on commit 5561bef

Please sign in to comment.