![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
DIYmall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module
Dependencies: Adafruit_GFX SDFileSystem
Fork of ATT_AWS_IoT_demo by
AWS_openssl/aws_iot_src/utils/jsmn.cpp@15:6f2798e45099, 2016-12-01 (annotated)
- Committer:
- ampembeng
- Date:
- Thu Dec 01 18:05:38 2016 +0000
- Revision:
- 15:6f2798e45099
Initial commit. Demo works with both the FRDM wired Ethernet and the Avnet Shield wireless modem.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ampembeng | 15:6f2798e45099 | 1 | /* |
ampembeng | 15:6f2798e45099 | 2 | * Copyright (c) 2010 Serge A. Zaitsev |
ampembeng | 15:6f2798e45099 | 3 | * |
ampembeng | 15:6f2798e45099 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
ampembeng | 15:6f2798e45099 | 5 | * of this software and associated documentation files (the "Software"), to deal |
ampembeng | 15:6f2798e45099 | 6 | * in the Software without restriction, including without limitation the rights |
ampembeng | 15:6f2798e45099 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ampembeng | 15:6f2798e45099 | 8 | * copies of the Software, and to permit persons to whom the Software is |
ampembeng | 15:6f2798e45099 | 9 | * furnished to do so, subject to the following conditions: |
ampembeng | 15:6f2798e45099 | 10 | * |
ampembeng | 15:6f2798e45099 | 11 | * The above copyright notice and this permission notice shall be included in |
ampembeng | 15:6f2798e45099 | 12 | * all copies or substantial portions of the Software. |
ampembeng | 15:6f2798e45099 | 13 | * |
ampembeng | 15:6f2798e45099 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ampembeng | 15:6f2798e45099 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ampembeng | 15:6f2798e45099 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ampembeng | 15:6f2798e45099 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ampembeng | 15:6f2798e45099 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ampembeng | 15:6f2798e45099 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ampembeng | 15:6f2798e45099 | 20 | * THE SOFTWARE. |
ampembeng | 15:6f2798e45099 | 21 | */ |
ampembeng | 15:6f2798e45099 | 22 | |
ampembeng | 15:6f2798e45099 | 23 | /** |
ampembeng | 15:6f2798e45099 | 24 | * @file jsmn.c |
ampembeng | 15:6f2798e45099 | 25 | * @brief Implementation of the JSMN (Jasmine) JSON parser. |
ampembeng | 15:6f2798e45099 | 26 | * |
ampembeng | 15:6f2798e45099 | 27 | * For more information on JSMN: |
ampembeng | 15:6f2798e45099 | 28 | * @see http://zserge.com/jsmn.html |
ampembeng | 15:6f2798e45099 | 29 | */ |
ampembeng | 15:6f2798e45099 | 30 | |
ampembeng | 15:6f2798e45099 | 31 | #include <stdlib.h> |
ampembeng | 15:6f2798e45099 | 32 | |
ampembeng | 15:6f2798e45099 | 33 | #include "jsmn.h" |
ampembeng | 15:6f2798e45099 | 34 | |
ampembeng | 15:6f2798e45099 | 35 | /** |
ampembeng | 15:6f2798e45099 | 36 | * Allocates a fresh unused token from the token pull. |
ampembeng | 15:6f2798e45099 | 37 | */ |
ampembeng | 15:6f2798e45099 | 38 | static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens, |
ampembeng | 15:6f2798e45099 | 39 | size_t num_tokens) { |
ampembeng | 15:6f2798e45099 | 40 | jsmntok_t *tok; |
ampembeng | 15:6f2798e45099 | 41 | if (parser->toknext >= num_tokens) { |
ampembeng | 15:6f2798e45099 | 42 | return NULL; |
ampembeng | 15:6f2798e45099 | 43 | } |
ampembeng | 15:6f2798e45099 | 44 | tok = &tokens[parser->toknext++]; |
ampembeng | 15:6f2798e45099 | 45 | tok->start = tok->end = -1; |
ampembeng | 15:6f2798e45099 | 46 | tok->size = 0; |
ampembeng | 15:6f2798e45099 | 47 | #ifdef JSMN_PARENT_LINKS |
ampembeng | 15:6f2798e45099 | 48 | tok->parent = -1; |
ampembeng | 15:6f2798e45099 | 49 | #endif |
ampembeng | 15:6f2798e45099 | 50 | return tok; |
ampembeng | 15:6f2798e45099 | 51 | } |
ampembeng | 15:6f2798e45099 | 52 | |
ampembeng | 15:6f2798e45099 | 53 | /** |
ampembeng | 15:6f2798e45099 | 54 | * Fills token type and boundaries. |
ampembeng | 15:6f2798e45099 | 55 | */ |
ampembeng | 15:6f2798e45099 | 56 | static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, int start, |
ampembeng | 15:6f2798e45099 | 57 | int end) { |
ampembeng | 15:6f2798e45099 | 58 | token->type = type; |
ampembeng | 15:6f2798e45099 | 59 | token->start = start; |
ampembeng | 15:6f2798e45099 | 60 | token->end = end; |
ampembeng | 15:6f2798e45099 | 61 | token->size = 0; |
ampembeng | 15:6f2798e45099 | 62 | } |
ampembeng | 15:6f2798e45099 | 63 | |
ampembeng | 15:6f2798e45099 | 64 | /** |
ampembeng | 15:6f2798e45099 | 65 | * Fills next available token with JSON primitive. |
ampembeng | 15:6f2798e45099 | 66 | */ |
ampembeng | 15:6f2798e45099 | 67 | static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js, |
ampembeng | 15:6f2798e45099 | 68 | size_t len, jsmntok_t *tokens, size_t num_tokens) { |
ampembeng | 15:6f2798e45099 | 69 | jsmntok_t *token; |
ampembeng | 15:6f2798e45099 | 70 | int start; |
ampembeng | 15:6f2798e45099 | 71 | |
ampembeng | 15:6f2798e45099 | 72 | start = parser->pos; |
ampembeng | 15:6f2798e45099 | 73 | |
ampembeng | 15:6f2798e45099 | 74 | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
ampembeng | 15:6f2798e45099 | 75 | switch (js[parser->pos]) { |
ampembeng | 15:6f2798e45099 | 76 | #ifndef JSMN_STRICT |
ampembeng | 15:6f2798e45099 | 77 | /* In strict mode primitive must be followed by "," or "}" or "]" */ |
ampembeng | 15:6f2798e45099 | 78 | case ':': |
ampembeng | 15:6f2798e45099 | 79 | #endif |
ampembeng | 15:6f2798e45099 | 80 | case '\t': |
ampembeng | 15:6f2798e45099 | 81 | case '\r': |
ampembeng | 15:6f2798e45099 | 82 | case '\n': |
ampembeng | 15:6f2798e45099 | 83 | case ' ': |
ampembeng | 15:6f2798e45099 | 84 | case ',': |
ampembeng | 15:6f2798e45099 | 85 | case ']': |
ampembeng | 15:6f2798e45099 | 86 | case '}': |
ampembeng | 15:6f2798e45099 | 87 | goto found; |
ampembeng | 15:6f2798e45099 | 88 | } |
ampembeng | 15:6f2798e45099 | 89 | if (js[parser->pos] < 32 || js[parser->pos] >= 127) { |
ampembeng | 15:6f2798e45099 | 90 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 91 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 92 | } |
ampembeng | 15:6f2798e45099 | 93 | } |
ampembeng | 15:6f2798e45099 | 94 | #ifdef JSMN_STRICT |
ampembeng | 15:6f2798e45099 | 95 | /* In strict mode primitive must be followed by a comma/object/array */ |
ampembeng | 15:6f2798e45099 | 96 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 97 | return JSMN_ERROR_PART; |
ampembeng | 15:6f2798e45099 | 98 | #endif |
ampembeng | 15:6f2798e45099 | 99 | |
ampembeng | 15:6f2798e45099 | 100 | found: if (tokens == NULL) { |
ampembeng | 15:6f2798e45099 | 101 | parser->pos--; |
ampembeng | 15:6f2798e45099 | 102 | return (jsmnerr_t) 0; |
ampembeng | 15:6f2798e45099 | 103 | } |
ampembeng | 15:6f2798e45099 | 104 | token = jsmn_alloc_token(parser, tokens, num_tokens); |
ampembeng | 15:6f2798e45099 | 105 | if (token == NULL) { |
ampembeng | 15:6f2798e45099 | 106 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 107 | return JSMN_ERROR_NOMEM; |
ampembeng | 15:6f2798e45099 | 108 | } |
ampembeng | 15:6f2798e45099 | 109 | jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); |
ampembeng | 15:6f2798e45099 | 110 | #ifdef JSMN_PARENT_LINKS |
ampembeng | 15:6f2798e45099 | 111 | token->parent = parser->toksuper; |
ampembeng | 15:6f2798e45099 | 112 | #endif |
ampembeng | 15:6f2798e45099 | 113 | parser->pos--; |
ampembeng | 15:6f2798e45099 | 114 | return (jsmnerr_t) 0; |
ampembeng | 15:6f2798e45099 | 115 | } |
ampembeng | 15:6f2798e45099 | 116 | |
ampembeng | 15:6f2798e45099 | 117 | /** |
ampembeng | 15:6f2798e45099 | 118 | * Filsl next token with JSON string. |
ampembeng | 15:6f2798e45099 | 119 | */ |
ampembeng | 15:6f2798e45099 | 120 | static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, |
ampembeng | 15:6f2798e45099 | 121 | size_t len, jsmntok_t *tokens, size_t num_tokens) { |
ampembeng | 15:6f2798e45099 | 122 | jsmntok_t *token; |
ampembeng | 15:6f2798e45099 | 123 | |
ampembeng | 15:6f2798e45099 | 124 | int start = parser->pos; |
ampembeng | 15:6f2798e45099 | 125 | |
ampembeng | 15:6f2798e45099 | 126 | parser->pos++; |
ampembeng | 15:6f2798e45099 | 127 | |
ampembeng | 15:6f2798e45099 | 128 | /* Skip starting quote */ |
ampembeng | 15:6f2798e45099 | 129 | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
ampembeng | 15:6f2798e45099 | 130 | char c = js[parser->pos]; |
ampembeng | 15:6f2798e45099 | 131 | |
ampembeng | 15:6f2798e45099 | 132 | /* Quote: end of string */ |
ampembeng | 15:6f2798e45099 | 133 | if (c == '\"') { |
ampembeng | 15:6f2798e45099 | 134 | if (tokens == NULL) { |
ampembeng | 15:6f2798e45099 | 135 | return (jsmnerr_t) 0; |
ampembeng | 15:6f2798e45099 | 136 | } |
ampembeng | 15:6f2798e45099 | 137 | token = jsmn_alloc_token(parser, tokens, num_tokens); |
ampembeng | 15:6f2798e45099 | 138 | if (token == NULL) { |
ampembeng | 15:6f2798e45099 | 139 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 140 | return JSMN_ERROR_NOMEM; |
ampembeng | 15:6f2798e45099 | 141 | } |
ampembeng | 15:6f2798e45099 | 142 | jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos); |
ampembeng | 15:6f2798e45099 | 143 | #ifdef JSMN_PARENT_LINKS |
ampembeng | 15:6f2798e45099 | 144 | token->parent = parser->toksuper; |
ampembeng | 15:6f2798e45099 | 145 | #endif |
ampembeng | 15:6f2798e45099 | 146 | return (jsmnerr_t) 0; |
ampembeng | 15:6f2798e45099 | 147 | } |
ampembeng | 15:6f2798e45099 | 148 | |
ampembeng | 15:6f2798e45099 | 149 | /* Backslash: Quoted symbol expected */ |
ampembeng | 15:6f2798e45099 | 150 | if (c == '\\') { |
ampembeng | 15:6f2798e45099 | 151 | parser->pos++; |
ampembeng | 15:6f2798e45099 | 152 | switch (js[parser->pos]) { |
ampembeng | 15:6f2798e45099 | 153 | /* Allowed escaped symbols */ |
ampembeng | 15:6f2798e45099 | 154 | case '\"': |
ampembeng | 15:6f2798e45099 | 155 | case '/': |
ampembeng | 15:6f2798e45099 | 156 | case '\\': |
ampembeng | 15:6f2798e45099 | 157 | case 'b': |
ampembeng | 15:6f2798e45099 | 158 | case 'f': |
ampembeng | 15:6f2798e45099 | 159 | case 'r': |
ampembeng | 15:6f2798e45099 | 160 | case 'n': |
ampembeng | 15:6f2798e45099 | 161 | case 't': |
ampembeng | 15:6f2798e45099 | 162 | break; |
ampembeng | 15:6f2798e45099 | 163 | /* Allows escaped symbol \uXXXX */ |
ampembeng | 15:6f2798e45099 | 164 | case 'u': |
ampembeng | 15:6f2798e45099 | 165 | parser->pos++; |
ampembeng | 15:6f2798e45099 | 166 | int i; |
ampembeng | 15:6f2798e45099 | 167 | for (i = 0; i < 4 && js[parser->pos] != '\0'; i++) { |
ampembeng | 15:6f2798e45099 | 168 | /* If it isn't a hex character we have an error */ |
ampembeng | 15:6f2798e45099 | 169 | if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ |
ampembeng | 15:6f2798e45099 | 170 | (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ |
ampembeng | 15:6f2798e45099 | 171 | (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ |
ampembeng | 15:6f2798e45099 | 172 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 173 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 174 | } |
ampembeng | 15:6f2798e45099 | 175 | parser->pos++; |
ampembeng | 15:6f2798e45099 | 176 | } |
ampembeng | 15:6f2798e45099 | 177 | parser->pos--; |
ampembeng | 15:6f2798e45099 | 178 | break; |
ampembeng | 15:6f2798e45099 | 179 | /* Unexpected symbol */ |
ampembeng | 15:6f2798e45099 | 180 | default: |
ampembeng | 15:6f2798e45099 | 181 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 182 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 183 | } |
ampembeng | 15:6f2798e45099 | 184 | } |
ampembeng | 15:6f2798e45099 | 185 | } |
ampembeng | 15:6f2798e45099 | 186 | parser->pos = start; |
ampembeng | 15:6f2798e45099 | 187 | return JSMN_ERROR_PART; |
ampembeng | 15:6f2798e45099 | 188 | } |
ampembeng | 15:6f2798e45099 | 189 | |
ampembeng | 15:6f2798e45099 | 190 | /** |
ampembeng | 15:6f2798e45099 | 191 | * Parse JSON string and fill tokens. |
ampembeng | 15:6f2798e45099 | 192 | */ |
ampembeng | 15:6f2798e45099 | 193 | jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, |
ampembeng | 15:6f2798e45099 | 194 | jsmntok_t *tokens, unsigned int num_tokens) { |
ampembeng | 15:6f2798e45099 | 195 | jsmnerr_t r; |
ampembeng | 15:6f2798e45099 | 196 | int i; |
ampembeng | 15:6f2798e45099 | 197 | jsmntok_t *token; |
ampembeng | 15:6f2798e45099 | 198 | int count = 0; |
ampembeng | 15:6f2798e45099 | 199 | |
ampembeng | 15:6f2798e45099 | 200 | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
ampembeng | 15:6f2798e45099 | 201 | char c; |
ampembeng | 15:6f2798e45099 | 202 | jsmntype_t type; |
ampembeng | 15:6f2798e45099 | 203 | |
ampembeng | 15:6f2798e45099 | 204 | c = js[parser->pos]; |
ampembeng | 15:6f2798e45099 | 205 | switch (c) { |
ampembeng | 15:6f2798e45099 | 206 | case '{': |
ampembeng | 15:6f2798e45099 | 207 | case '[': |
ampembeng | 15:6f2798e45099 | 208 | count++; |
ampembeng | 15:6f2798e45099 | 209 | if (tokens == NULL) { |
ampembeng | 15:6f2798e45099 | 210 | break; |
ampembeng | 15:6f2798e45099 | 211 | } |
ampembeng | 15:6f2798e45099 | 212 | token = jsmn_alloc_token(parser, tokens, num_tokens); |
ampembeng | 15:6f2798e45099 | 213 | if (token == NULL) |
ampembeng | 15:6f2798e45099 | 214 | return JSMN_ERROR_NOMEM; |
ampembeng | 15:6f2798e45099 | 215 | if (parser->toksuper != -1) { |
ampembeng | 15:6f2798e45099 | 216 | tokens[parser->toksuper].size++; |
ampembeng | 15:6f2798e45099 | 217 | #ifdef JSMN_PARENT_LINKS |
ampembeng | 15:6f2798e45099 | 218 | token->parent = parser->toksuper; |
ampembeng | 15:6f2798e45099 | 219 | #endif |
ampembeng | 15:6f2798e45099 | 220 | } |
ampembeng | 15:6f2798e45099 | 221 | token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); |
ampembeng | 15:6f2798e45099 | 222 | token->start = parser->pos; |
ampembeng | 15:6f2798e45099 | 223 | parser->toksuper = parser->toknext - 1; |
ampembeng | 15:6f2798e45099 | 224 | break; |
ampembeng | 15:6f2798e45099 | 225 | case '}': |
ampembeng | 15:6f2798e45099 | 226 | case ']': |
ampembeng | 15:6f2798e45099 | 227 | if (tokens == NULL) |
ampembeng | 15:6f2798e45099 | 228 | break; |
ampembeng | 15:6f2798e45099 | 229 | type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); |
ampembeng | 15:6f2798e45099 | 230 | #ifdef JSMN_PARENT_LINKS |
ampembeng | 15:6f2798e45099 | 231 | if (parser->toknext < 1) { |
ampembeng | 15:6f2798e45099 | 232 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 233 | } |
ampembeng | 15:6f2798e45099 | 234 | token = &tokens[parser->toknext - 1]; |
ampembeng | 15:6f2798e45099 | 235 | for (;;) { |
ampembeng | 15:6f2798e45099 | 236 | if (token->start != -1 && token->end == -1) { |
ampembeng | 15:6f2798e45099 | 237 | if (token->type != type) { |
ampembeng | 15:6f2798e45099 | 238 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 239 | } |
ampembeng | 15:6f2798e45099 | 240 | token->end = parser->pos + 1; |
ampembeng | 15:6f2798e45099 | 241 | parser->toksuper = token->parent; |
ampembeng | 15:6f2798e45099 | 242 | break; |
ampembeng | 15:6f2798e45099 | 243 | } |
ampembeng | 15:6f2798e45099 | 244 | if (token->parent == -1) { |
ampembeng | 15:6f2798e45099 | 245 | break; |
ampembeng | 15:6f2798e45099 | 246 | } |
ampembeng | 15:6f2798e45099 | 247 | token = &tokens[token->parent]; |
ampembeng | 15:6f2798e45099 | 248 | } |
ampembeng | 15:6f2798e45099 | 249 | #else |
ampembeng | 15:6f2798e45099 | 250 | for (i = parser->toknext - 1; i >= 0; i--) { |
ampembeng | 15:6f2798e45099 | 251 | token = &tokens[i]; |
ampembeng | 15:6f2798e45099 | 252 | if (token->start != -1 && token->end == -1) { |
ampembeng | 15:6f2798e45099 | 253 | if (token->type != type) { |
ampembeng | 15:6f2798e45099 | 254 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 255 | } |
ampembeng | 15:6f2798e45099 | 256 | parser->toksuper = -1; |
ampembeng | 15:6f2798e45099 | 257 | token->end = parser->pos + 1; |
ampembeng | 15:6f2798e45099 | 258 | break; |
ampembeng | 15:6f2798e45099 | 259 | } |
ampembeng | 15:6f2798e45099 | 260 | } |
ampembeng | 15:6f2798e45099 | 261 | /* Error if unmatched closing bracket */ |
ampembeng | 15:6f2798e45099 | 262 | if (i == -1) |
ampembeng | 15:6f2798e45099 | 263 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 264 | for (; i >= 0; i--) { |
ampembeng | 15:6f2798e45099 | 265 | token = &tokens[i]; |
ampembeng | 15:6f2798e45099 | 266 | if (token->start != -1 && token->end == -1) { |
ampembeng | 15:6f2798e45099 | 267 | parser->toksuper = i; |
ampembeng | 15:6f2798e45099 | 268 | break; |
ampembeng | 15:6f2798e45099 | 269 | } |
ampembeng | 15:6f2798e45099 | 270 | } |
ampembeng | 15:6f2798e45099 | 271 | #endif |
ampembeng | 15:6f2798e45099 | 272 | break; |
ampembeng | 15:6f2798e45099 | 273 | case '\"': |
ampembeng | 15:6f2798e45099 | 274 | r = jsmn_parse_string(parser, js, len, tokens, num_tokens); |
ampembeng | 15:6f2798e45099 | 275 | if (r < 0) |
ampembeng | 15:6f2798e45099 | 276 | return r; |
ampembeng | 15:6f2798e45099 | 277 | count++; |
ampembeng | 15:6f2798e45099 | 278 | if (parser->toksuper != -1 && tokens != NULL) |
ampembeng | 15:6f2798e45099 | 279 | tokens[parser->toksuper].size++; |
ampembeng | 15:6f2798e45099 | 280 | break; |
ampembeng | 15:6f2798e45099 | 281 | case '\t': |
ampembeng | 15:6f2798e45099 | 282 | case '\r': |
ampembeng | 15:6f2798e45099 | 283 | case '\n': |
ampembeng | 15:6f2798e45099 | 284 | case ':': |
ampembeng | 15:6f2798e45099 | 285 | case ',': |
ampembeng | 15:6f2798e45099 | 286 | case ' ': |
ampembeng | 15:6f2798e45099 | 287 | break; |
ampembeng | 15:6f2798e45099 | 288 | #ifdef JSMN_STRICT |
ampembeng | 15:6f2798e45099 | 289 | /* In strict mode primitives are: numbers and booleans */ |
ampembeng | 15:6f2798e45099 | 290 | case '-': |
ampembeng | 15:6f2798e45099 | 291 | case '0': |
ampembeng | 15:6f2798e45099 | 292 | case '1': |
ampembeng | 15:6f2798e45099 | 293 | case '2': |
ampembeng | 15:6f2798e45099 | 294 | case '3': |
ampembeng | 15:6f2798e45099 | 295 | case '4': |
ampembeng | 15:6f2798e45099 | 296 | case '5': |
ampembeng | 15:6f2798e45099 | 297 | case '6': |
ampembeng | 15:6f2798e45099 | 298 | case '7': |
ampembeng | 15:6f2798e45099 | 299 | case '8': |
ampembeng | 15:6f2798e45099 | 300 | case '9': |
ampembeng | 15:6f2798e45099 | 301 | case 't': |
ampembeng | 15:6f2798e45099 | 302 | case 'f': |
ampembeng | 15:6f2798e45099 | 303 | case 'n': |
ampembeng | 15:6f2798e45099 | 304 | #else |
ampembeng | 15:6f2798e45099 | 305 | /* In non-strict mode every unquoted value is a primitive */ |
ampembeng | 15:6f2798e45099 | 306 | default: |
ampembeng | 15:6f2798e45099 | 307 | #endif |
ampembeng | 15:6f2798e45099 | 308 | r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); |
ampembeng | 15:6f2798e45099 | 309 | if (r < 0) |
ampembeng | 15:6f2798e45099 | 310 | return r; |
ampembeng | 15:6f2798e45099 | 311 | count++; |
ampembeng | 15:6f2798e45099 | 312 | if (parser->toksuper != -1 && tokens != NULL) |
ampembeng | 15:6f2798e45099 | 313 | tokens[parser->toksuper].size++; |
ampembeng | 15:6f2798e45099 | 314 | break; |
ampembeng | 15:6f2798e45099 | 315 | |
ampembeng | 15:6f2798e45099 | 316 | #ifdef JSMN_STRICT |
ampembeng | 15:6f2798e45099 | 317 | /* Unexpected char in strict mode */ |
ampembeng | 15:6f2798e45099 | 318 | default: |
ampembeng | 15:6f2798e45099 | 319 | return JSMN_ERROR_INVAL; |
ampembeng | 15:6f2798e45099 | 320 | #endif |
ampembeng | 15:6f2798e45099 | 321 | } |
ampembeng | 15:6f2798e45099 | 322 | } |
ampembeng | 15:6f2798e45099 | 323 | |
ampembeng | 15:6f2798e45099 | 324 | for (i = parser->toknext - 1; i >= 0; i--) { |
ampembeng | 15:6f2798e45099 | 325 | /* Unmatched opened object or array */ |
ampembeng | 15:6f2798e45099 | 326 | if (tokens[i].start != -1 && tokens[i].end == -1) { |
ampembeng | 15:6f2798e45099 | 327 | return JSMN_ERROR_PART; |
ampembeng | 15:6f2798e45099 | 328 | } |
ampembeng | 15:6f2798e45099 | 329 | } |
ampembeng | 15:6f2798e45099 | 330 | |
ampembeng | 15:6f2798e45099 | 331 | return (jsmnerr_t) count; |
ampembeng | 15:6f2798e45099 | 332 | } |
ampembeng | 15:6f2798e45099 | 333 | |
ampembeng | 15:6f2798e45099 | 334 | /** |
ampembeng | 15:6f2798e45099 | 335 | * Creates a new parser based over a given buffer with an array of tokens |
ampembeng | 15:6f2798e45099 | 336 | * available. |
ampembeng | 15:6f2798e45099 | 337 | */ |
ampembeng | 15:6f2798e45099 | 338 | void jsmn_init(jsmn_parser *parser) { |
ampembeng | 15:6f2798e45099 | 339 | parser->pos = 0; |
ampembeng | 15:6f2798e45099 | 340 | parser->toknext = 0; |
ampembeng | 15:6f2798e45099 | 341 | parser->toksuper = -1; |
ampembeng | 15:6f2798e45099 | 342 | } |
ampembeng | 15:6f2798e45099 | 343 |