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

First child, next sibling #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions jsmn.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
#include "jsmn.h"
#include "stdio.h"

#ifdef JSMN_FIRST_CHILD_NEXT_SIBLING
/**
* Connects a (non-root) node either as a child or a sibling, given its parent.
* Helper function that is called from the parse routine,
*/
static void
jsmn_fill_first_child_next_sibling(jsmntok_t* const tokens, int parent, int me)
{
//If there is no parent, there's nothing we can do.
//This will result in several trees, that are unconnected to each other.
//Happens when there's no outer object.
if (parent < 0) {
return;
}

//if parent has no other children, then we're the first
if (tokens[parent].first_child == -1) {
tokens[parent].first_child = me;
}
//if we're not the first child, we must be a sibling
else {
int my_sibling = tokens[parent].first_child;
while (tokens[my_sibling].next_sibling != -1) {
my_sibling = tokens[my_sibling].next_sibling;
}
tokens[my_sibling].next_sibling = me;
}
}
#endif

/**
* Allocates a fresh unused token from the token pull.
Expand All @@ -14,6 +45,10 @@ static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
tok->size = 0;
#ifdef JSMN_PARENT_LINKS
tok->parent = -1;
#endif
#ifdef JSMN_FIRST_CHILD_NEXT_SIBLING
tok->first_child = -1;
tok->next_sibling = -1;
#endif
return tok;
}
Expand Down Expand Up @@ -73,6 +108,9 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
#ifdef JSMN_FIRST_CHILD_NEXT_SIBLING
jsmn_fill_first_child_next_sibling(tokens, parser->toksuper, parser->toknext - 1);
#endif
parser->pos--;
return 0;
Expand Down Expand Up @@ -106,6 +144,9 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
#ifdef JSMN_FIRST_CHILD_NEXT_SIBLING
jsmn_fill_first_child_next_sibling(tokens, parser->toksuper, parser->toknext - 1);
#endif
return 0;
}
Expand Down Expand Up @@ -173,6 +214,9 @@ int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
tokens[parser->toksuper].size++;
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
#ifdef JSMN_FIRST_CHILD_NEXT_SIBLING
jsmn_fill_first_child_next_sibling(tokens, parser->toksuper, parser->toknext - 1);
#endif
}
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
Expand Down
4 changes: 4 additions & 0 deletions jsmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ typedef struct {
#ifdef JSMN_PARENT_LINKS
int parent;
#endif
#ifdef JSMN_FIRST_CHILD_NEXT_SIBLING
int first_child;
int next_sibling;
#endif
} jsmntok_t;

/**
Expand Down