static int yaml_parser_load_mapping(yaml_parser_t *parser, yaml_event_t *first_event) { yaml_event_t event; yaml_node_t node; struct { yaml_node_pair_t *start; yaml_node_pair_t *end; yaml_node_pair_t *top; } pairs = { NULL, NULL, NULL }; int index; yaml_node_pair_t pair; yaml_char_t *tag = first_event->data.mapping_start.tag; if (!tag || strcmp((char *)tag, "!") == 0) { yaml_free(tag); tag = yaml_strdup((yaml_char_t *)YAML_DEFAULT_MAPPING_TAG); if (!tag) goto error; } if (!STACK_INIT(parser, pairs, INITIAL_STACK_SIZE)) goto error; MAPPING_NODE_INIT(node, tag, pairs.start, pairs.end, first_event->data.mapping_start.style, first_event->start_mark, first_event->end_mark); if (!PUSH(parser, parser->document->nodes, node)) goto error; index = (int) (parser->document->nodes.top - parser->document->nodes.start); if (!yaml_parser_register_anchor(parser, index, first_event->data.mapping_start.anchor)) return 0; if (!yaml_parser_parse(parser, &event)) return 0; while (event.type != YAML_MAPPING_END_EVENT) { pair.key = yaml_parser_load_node(parser, &event); if (!pair.key) return 0; if (!yaml_parser_parse(parser, &event)) return 0; pair.value = yaml_parser_load_node(parser, &event); if (!pair.value) return 0; if (!PUSH(parser, parser->document->nodes.start[index-1].data.mapping.pairs, pair)) return 0; if (!yaml_parser_parse(parser, &event)) return 0; } parser->document->nodes.start[index-1].end_mark = event.end_mark; return index; error: yaml_free(tag); yaml_free(first_event->data.mapping_start.anchor); return 0; }
yaml_document_add_mapping(yaml_document_t *document, yaml_char_t *tag, yaml_mapping_style_t style) { struct { yaml_error_type_t error; } context; yaml_mark_t mark = { 0, 0, 0 }; yaml_char_t *tag_copy = NULL; struct { yaml_node_pair_t *start; yaml_node_pair_t *end; yaml_node_pair_t *top; } pairs = { NULL, NULL, NULL }; yaml_node_t node; assert(document); /* Non-NULL document object is expected. */ if (!tag) { tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG; } if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; tag_copy = yaml_strdup(tag); if (!tag_copy) goto error; if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error; MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, style, mark, mark); if (!PUSH(&context, document->nodes, node)) goto error; return document->nodes.top - document->nodes.start; error: STACK_DEL(&context, pairs); yaml_free(tag_copy); return 0; }