static void parse_array_element(JsonLexContext *lex, JsonSemAction *sem) { json_aelem_action astart = sem->array_element_start; json_aelem_action aend = sem->array_element_end; JsonTokenType tok = lex_peek(lex); bool isnull; isnull = tok == JSON_TOKEN_NULL; if (astart != NULL) (*astart) (sem->semstate, isnull); /* an array element is any object, array or scalar */ switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); } if (aend != NULL) (*aend) (sem->semstate, isnull); }
/* * pg_parse_json * * Publicly visible entry point for the JSON parser. * * lex is a lexing context, set up for the json to be processed by calling * makeJsonLexContext(). sem is a strucure of function pointers to semantic * action routines to be called at appropriate spots during parsing, and a * pointer to a state object to be passed to those routines. */ void pg_parse_json(JsonLexContext *lex, JsonSemAction *sem) { JsonTokenType tok; /* get the initial token */ json_lex(lex); tok = lex_peek(lex); /* parse by recursive descent */ switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); /* json can be a bare scalar */ } lex_expect(JSON_PARSE_END, lex, JSON_TOKEN_END); }
static void parse_object_field(JsonLexContext *lex, JsonSemAction *sem) { /* * an object field is "fieldname" : value where value can be a scalar, * object or array */ char *fname = NULL; /* keep compiler quiet */ json_ofield_action ostart = sem->object_field_start; json_ofield_action oend = sem->object_field_end; bool isnull; char **fnameaddr = NULL; JsonTokenType tok; if (ostart != NULL || oend != NULL) fnameaddr = &fname; if (!lex_accept(lex, JSON_TOKEN_STRING, fnameaddr)) report_parse_error(JSON_PARSE_STRING, lex); lex_expect(JSON_PARSE_OBJECT_LABEL, lex, JSON_TOKEN_COLON); tok = lex_peek(lex); isnull = tok == JSON_TOKEN_NULL; if (ostart != NULL) (*ostart) (sem->semstate, fname, isnull); switch (tok) { case JSON_TOKEN_OBJECT_START: parse_object(lex, sem); break; case JSON_TOKEN_ARRAY_START: parse_array(lex, sem); break; default: parse_scalar(lex, sem); } if (oend != NULL) (*oend) (sem->semstate, fname, isnull); if (fname != NULL) pfree(fname); }
static VALUE parse_node(rb_yaml_parser_t *parser) { VALUE node; NEXT_EVENT(); switch (parser->event.type) { case YAML_DOCUMENT_END_EVENT: case YAML_MAPPING_END_EVENT: case YAML_SEQUENCE_END_EVENT: case YAML_STREAM_END_EVENT: return Qundef; case YAML_MAPPING_START_EVENT: node = parse_mapping(parser); break; case YAML_SEQUENCE_START_EVENT: node = parse_sequence(parser); break; case YAML_SCALAR_EVENT: node = parse_scalar(parser); break; case YAML_ALIAS_EVENT: // ignoring alias node = Qundef; break; default: rb_raise(rb_eArgError, "Invalid event %d at top level", (int)parser->event.type); } return node; }