Commits

Anonymous committed dea589a

Complex types (objects and arrays) now have also size - number of child elements

  • Participants
  • Parent commits 20591f1

Comments (0)

Files changed (4)

 
 	len = obj->end - obj->start;
 
-	printf("[%3d,%3d] (%c) ", obj->start, obj->end,
+	printf("[%3d,%3d - %2d] (%c) ", obj->start, obj->end, obj->size,
 		({
 			char c;
 			switch (obj->type) {
 	for (i = parser->curtoken; i<parser->num_tokens; i++) {
 		if (tokens[i].start == -1 && tokens[i].end == -1) {
 			parser->curtoken = i;
+			tokens[i].size = 0;
 			return &tokens[i];
 		}
 	}
 	parser->tokens = tokens;
 	parser->num_tokens = num_tokens;
 	parser->curtoken = 0;
+	parser->cursize = NULL;
 
 	for (i = 0; i < parser->num_tokens; i++) {
 		jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
  */
 jsmnerr_t jsmn_parse(jsmn_parser *parser) {
 	int r;
-	unsigned int i;
+	int i;
 	const char *js;
 	jsmntype_t type;
 	jsmntok_t *token;
 				token = jsmn_get_token(parser);
 				if (token == NULL)
 					return JSMN_ERROR_NOMEM;
+				if (parser->cursize != NULL)
+					(*parser->cursize)++;
 				token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
 				token->start = parser->pos;
+				parser->cursize = &token->size;
 				break;
 			case '}': case ']':
 				type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
 						if (token->type != type) {
 							return JSMN_ERROR_INVAL;
 						}
+						parser->cursize = NULL;
 						token->end = parser->pos + 1;
 						break;
 					}
 				}
+				for (; i >= 0; i--) {
+					token = &parser->tokens[i];
+					if (token->start != -1 && token->end == -1) {
+						parser->cursize = &token->size;
+						break;
+					}
+				}
 				break;
 			case '-': case '0': case '1' : case '2': case '3' : case '4':
 			case '5': case '6': case '7' : case '8': case '9':
 			case 't': case 'f': case 'n' :
 				r = jsmn_parse_primitive(parser);
 				if (r < 0) return r;
+				if (parser->cursize != NULL)
+					(*parser->cursize)++;
 				break;
 			case '\"':
 				r = jsmn_parse_string(parser);
 				if (r < 0) return r;
+				if (parser->cursize != NULL)
+					(*parser->cursize)++;
 				break;
 			case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ': 
 				break;
 	jsmntype_t type;
 	int start;
 	int end;
+	int size;
 } jsmntok_t;
 
 /**
 	unsigned int pos;
 	unsigned int num_tokens;
 	int curtoken;
+	int *cursize;
 	jsmntok_t *tokens;
 } jsmn_parser;
 
 FAILED=0
 
 function expect() {	
-	ret=$(./demo -t 10 -b 256 - | grep -A 1 "$1" | tail -n 1 | cut -c 15-)
+	ret=$(./demo -t 10 -b 256 - | grep -A 1 "$1" | tail -n 1 | cut -c 20-)
 	if [ "x$ret" = "x$2" ]; then
 		PASSED=$(($PASSED+1))
 	else