Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error if a scope mismatch happens #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions jim.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef enum {
JIM_SCOPES_OVERFLOW,
JIM_SCOPES_UNDERFLOW,
JIM_OUT_OF_SCOPE_KEY,
JIM_SCOPE_MISMATCH,
JIM_DOUBLE_KEY
} Jim_Error;

Expand Down Expand Up @@ -169,6 +170,8 @@ const char *jim_error_string(Jim_Error error)
return "Stack of Scopes Underflow";
case JIM_OUT_OF_SCOPE_KEY:
return "Out of Scope key";
case JIM_SCOPE_MISMATCH:
return "Scope mismatch";
case JIM_DOUBLE_KEY:
return "Tried to set the member key twice";
default:
Expand Down Expand Up @@ -333,6 +336,17 @@ void jim_array_begin(Jim *jim)
void jim_array_end(Jim *jim)
{
if (jim->error == JIM_OK) {
Jim_Scope *scope = jim_current_scope(jim);
HalidOdat marked this conversation as resolved.
Show resolved Hide resolved
if (!scope) {
jim->error = JIM_SCOPES_UNDERFLOW;
return;
}

if (scope->kind != JIM_ARRAY_SCOPE) {
jim->error = JIM_SCOPE_MISMATCH;
return;
}

jim_write_cstr(jim, "]");
jim_scope_pop(jim);
jim_element_end(jim);
Expand Down Expand Up @@ -377,6 +391,17 @@ void jim_member_key_sized(Jim *jim, const char *str, size_t size)
void jim_object_end(Jim *jim)
{
if (jim->error == JIM_OK) {
Jim_Scope *scope = jim_current_scope(jim);
if (!scope) {
jim->error = JIM_SCOPES_UNDERFLOW;
return;
}

if (scope->kind != JIM_OBJECT_SCOPE) {
jim->error = JIM_SCOPE_MISMATCH;
return;
}

jim_write_cstr(jim, "}");
jim_scope_pop(jim);
jim_element_end(jim);
Expand Down