* Recursive Descent parse routines. There is one for each structural * element in a json document: * - scalar (string, number, true, false, null) * - array ( [ ] ) * - array element * - object ( { } ) * - object field */
| 270 | * - object field |
| 271 | */ |
| 272 | static inline JsonParseErrorType |
| 273 | parse_scalar(JsonLexContext *lex, JsonSemAction *sem) |
| 274 | { |
| 275 | char *val = NULL; |
| 276 | json_scalar_action sfunc = sem->scalar; |
| 277 | JsonTokenType tok = lex_peek(lex); |
| 278 | JsonParseErrorType result; |
| 279 | |
| 280 | /* a scalar must be a string, a number, true, false, or null */ |
| 281 | if (tok != JSON_TOKEN_STRING && tok != JSON_TOKEN_NUMBER && |
| 282 | tok != JSON_TOKEN_TRUE && tok != JSON_TOKEN_FALSE && |
| 283 | tok != JSON_TOKEN_NULL) |
| 284 | return report_parse_error(JSON_PARSE_VALUE, lex); |
| 285 | |
| 286 | /* if no semantic function, just consume the token */ |
| 287 | if (sfunc == NULL) |
| 288 | return json_lex(lex); |
| 289 | |
| 290 | /* extract the de-escaped string value, or the raw lexeme */ |
| 291 | if (lex_peek(lex) == JSON_TOKEN_STRING) |
| 292 | { |
| 293 | if (lex->strval != NULL) |
| 294 | val = pstrdup(lex->strval->data); |
| 295 | } |
| 296 | else |
| 297 | { |
| 298 | int len = (lex->token_terminator - lex->token_start); |
| 299 | |
| 300 | val = palloc(len + 1); |
| 301 | memcpy(val, lex->token_start, len); |
| 302 | val[len] = '\0'; |
| 303 | } |
| 304 | |
| 305 | /* consume the token */ |
| 306 | result = json_lex(lex); |
| 307 | if (result != JSON_SUCCESS) |
| 308 | return result; |
| 309 | |
| 310 | /* invoke the callback */ |
| 311 | (*sfunc) (sem->semstate, val, tok); |
| 312 | |
| 313 | return JSON_SUCCESS; |
| 314 | } |
| 315 | |
| 316 | static JsonParseErrorType |
| 317 | parse_object_field(JsonLexContext *lex, JsonSemAction *sem) |
no test coverage detected