2 #include "yaml_private.h"
5 * Flush the buffer if needed.
8 #define FLUSH(emitter) \
9 ((emitter->buffer.pointer+5 < emitter->buffer.end) \
10 || yaml_emitter_flush(emitter))
13 * Put a character to the output buffer.
16 #define PUT(emitter,value) \
18 && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \
23 * Put a line break to the output buffer.
26 #define PUT_BREAK(emitter) \
28 && ((emitter->line_break == YAML_CR_BREAK ? \
29 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \
30 emitter->line_break == YAML_LN_BREAK ? \
31 (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \
32 emitter->line_break == YAML_CRLN_BREAK ? \
33 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \
34 *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \
35 emitter->column = 0, \
40 * Copy a character from a string into buffer.
43 #define WRITE(emitter,string) \
45 && (COPY(emitter->buffer,string), \
50 * Copy a line break character from a string into buffer.
53 #define WRITE_BREAK(emitter,string) \
55 && (COPY(emitter->buffer,string), \
56 emitter->column = 0, \
65 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event);
72 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem);
75 yaml_emitter_need_more_events(yaml_emitter_t *emitter);
78 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
79 yaml_tag_directive_t value, int allow_duplicates);
82 yaml_emitter_increase_indent(yaml_emitter_t *emitter,
83 int flow, int indentless);
90 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event);
93 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
97 yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
98 yaml_event_t *event, int first);
101 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
102 yaml_event_t *event);
105 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
106 yaml_event_t *event);
109 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
110 yaml_event_t *event, int first);
113 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
114 yaml_event_t *event, int first);
117 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
118 yaml_event_t *event, int simple);
121 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
122 yaml_event_t *event, int first);
125 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
126 yaml_event_t *event, int first);
129 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
130 yaml_event_t *event, int simple);
133 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
134 int root, int sequence, int mapping, int simple_key);
137 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event);
140 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event);
143 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event);
146 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event);
153 yaml_emitter_check_empty_document(yaml_emitter_t *emitter);
156 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter);
159 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter);
162 yaml_emitter_check_simple_key(yaml_emitter_t *emitter);
165 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event);
172 yaml_emitter_process_anchor(yaml_emitter_t *emitter);
175 yaml_emitter_process_tag(yaml_emitter_t *emitter);
178 yaml_emitter_process_scalar(yaml_emitter_t *emitter);
185 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
186 yaml_version_directive_t version_directive);
189 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
190 yaml_tag_directive_t tag_directive);
193 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
194 yaml_char_t *anchor, int alias);
197 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
201 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
202 yaml_char_t *value, size_t length);
205 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
206 yaml_event_t *event);
213 yaml_emitter_write_bom(yaml_emitter_t *emitter);
216 yaml_emitter_write_indent(yaml_emitter_t *emitter);
219 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
220 char *indicator, int need_whitespace,
221 int is_whitespace, int is_indention);
224 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
225 yaml_char_t *value, size_t length);
228 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
229 yaml_char_t *value, size_t length);
232 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
233 yaml_char_t *value, size_t length);
236 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
237 yaml_char_t *value, size_t length, int allow_breaks);
240 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
241 yaml_char_t *value, size_t length, int allow_breaks);
244 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
245 yaml_char_t *value, size_t length, int allow_breaks);
248 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
249 yaml_char_t *value, size_t length);
252 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
253 yaml_char_t *value, size_t length);
256 * Set an emitter error and return 0.
260 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem)
262 emitter->error = YAML_EMITTER_ERROR;
263 emitter->problem = problem;
273 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event)
275 if (!ENQUEUE(emitter, emitter->events, *event)) {
276 yaml_event_delete(event);
280 while (!yaml_emitter_need_more_events(emitter)) {
281 if (!yaml_emitter_analyze_event(emitter, emitter->events.head))
283 if (!yaml_emitter_state_machine(emitter, emitter->events.head))
285 DEQUEUE(emitter, emitter->events);
292 * Check if we need to accumulate more events before emitting.
294 * We accumulate extra
295 * - 1 event for DOCUMENT-START
296 * - 2 events for SEQUENCE-START
297 * - 3 events for MAPPING-START
301 yaml_emitter_need_more_events(yaml_emitter_t *emitter)
307 if (QUEUE_EMPTY(emitter, emitter->events))
310 switch (emitter->events.head->type) {
311 case YAML_DOCUMENT_START_EVENT:
314 case YAML_SEQUENCE_START_EVENT:
317 case YAML_MAPPING_START_EVENT:
324 if (emitter->events.tail - emitter->events.head > accumulate)
327 for (event = emitter->events.head; event != emitter->events.tail; event ++) {
328 switch (event->type) {
329 case YAML_STREAM_START_EVENT:
330 case YAML_DOCUMENT_START_EVENT:
331 case YAML_SEQUENCE_START_EVENT:
332 case YAML_MAPPING_START_EVENT:
335 case YAML_STREAM_END_EVENT:
336 case YAML_DOCUMENT_END_EVENT:
337 case YAML_SEQUENCE_END_EVENT:
338 case YAML_MAPPING_END_EVENT:
352 * Append a directive to the directives stack.
356 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
357 yaml_tag_directive_t value, int allow_duplicates)
359 yaml_tag_directive_t *tag_directive;
360 yaml_tag_directive_t copy = { NULL, NULL };
362 for (tag_directive = emitter->tag_directives.start;
363 tag_directive != emitter->tag_directives.top; tag_directive ++) {
364 if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) {
365 if (allow_duplicates)
367 return yaml_emitter_set_emitter_error(emitter,
368 "duplicate %TAG directive");
372 copy.handle = yaml_strdup(value.handle);
373 copy.prefix = yaml_strdup(value.prefix);
374 if (!copy.handle || !copy.prefix) {
375 emitter->error = YAML_MEMORY_ERROR;
379 if (!PUSH(emitter, emitter->tag_directives, copy))
385 yaml_free(copy.handle);
386 yaml_free(copy.prefix);
391 * Increase the indentation level.
395 yaml_emitter_increase_indent(yaml_emitter_t *emitter,
396 int flow, int indentless)
398 if (!PUSH(emitter, emitter->indents, emitter->indent))
401 if (emitter->indent < 0) {
402 emitter->indent = flow ? emitter->best_indent : 0;
404 else if (!indentless) {
405 emitter->indent += emitter->best_indent;
416 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event)
418 switch (emitter->state)
420 case YAML_EMIT_STREAM_START_STATE:
421 return yaml_emitter_emit_stream_start(emitter, event);
423 case YAML_EMIT_FIRST_DOCUMENT_START_STATE:
424 return yaml_emitter_emit_document_start(emitter, event, 1);
426 case YAML_EMIT_DOCUMENT_START_STATE:
427 return yaml_emitter_emit_document_start(emitter, event, 0);
429 case YAML_EMIT_DOCUMENT_CONTENT_STATE:
430 return yaml_emitter_emit_document_content(emitter, event);
432 case YAML_EMIT_DOCUMENT_END_STATE:
433 return yaml_emitter_emit_document_end(emitter, event);
435 case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
436 return yaml_emitter_emit_flow_sequence_item(emitter, event, 1);
438 case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE:
439 return yaml_emitter_emit_flow_sequence_item(emitter, event, 0);
441 case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
442 return yaml_emitter_emit_flow_mapping_key(emitter, event, 1);
444 case YAML_EMIT_FLOW_MAPPING_KEY_STATE:
445 return yaml_emitter_emit_flow_mapping_key(emitter, event, 0);
447 case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
448 return yaml_emitter_emit_flow_mapping_value(emitter, event, 1);
450 case YAML_EMIT_FLOW_MAPPING_VALUE_STATE:
451 return yaml_emitter_emit_flow_mapping_value(emitter, event, 0);
453 case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
454 return yaml_emitter_emit_block_sequence_item(emitter, event, 1);
456 case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
457 return yaml_emitter_emit_block_sequence_item(emitter, event, 0);
459 case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
460 return yaml_emitter_emit_block_mapping_key(emitter, event, 1);
462 case YAML_EMIT_BLOCK_MAPPING_KEY_STATE:
463 return yaml_emitter_emit_block_mapping_key(emitter, event, 0);
465 case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
466 return yaml_emitter_emit_block_mapping_value(emitter, event, 1);
468 case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE:
469 return yaml_emitter_emit_block_mapping_value(emitter, event, 0);
471 case YAML_EMIT_END_STATE:
472 return yaml_emitter_set_emitter_error(emitter,
473 "expected nothing after STREAM-END");
476 assert(1); /* Invalid state. */
483 * Expect STREAM-START.
487 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
490 if (event->type == YAML_STREAM_START_EVENT)
492 if (!emitter->encoding) {
493 emitter->encoding = event->data.stream_start.encoding;
496 if (!emitter->encoding) {
497 emitter->encoding = YAML_UTF8_ENCODING;
500 if (emitter->best_indent < 2 || emitter->best_indent > 9) {
501 emitter->best_indent = 2;
504 if (emitter->best_width >= 0
505 && emitter->best_width <= emitter->best_indent*2) {
506 emitter->best_width = 80;
509 if (emitter->best_width < 0) {
510 emitter->best_width = INT_MAX;
513 if (!emitter->line_break) {
514 emitter->line_break = YAML_LN_BREAK;
517 emitter->indent = -1;
521 emitter->whitespace = 1;
522 emitter->indention = 1;
524 if (emitter->encoding != YAML_UTF8_ENCODING) {
525 if (!yaml_emitter_write_bom(emitter))
529 emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
534 return yaml_emitter_set_emitter_error(emitter,
535 "expected STREAM-START");
539 * Expect DOCUMENT-START or STREAM-END.
543 yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
544 yaml_event_t *event, int first)
546 if (event->type == YAML_DOCUMENT_START_EVENT)
548 yaml_tag_directive_t default_tag_directives[] = {
549 {(yaml_char_t *)"!", (yaml_char_t *)"!"},
550 {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"},
553 yaml_tag_directive_t *tag_directive;
556 if (event->data.document_start.version_directive) {
557 if (!yaml_emitter_analyze_version_directive(emitter,
558 *event->data.document_start.version_directive))
562 for (tag_directive = event->data.document_start.tag_directives.start;
563 tag_directive != event->data.document_start.tag_directives.end;
565 if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive))
567 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0))
571 for (tag_directive = default_tag_directives;
572 tag_directive->handle; tag_directive ++) {
573 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1))
577 implicit = event->data.document_start.implicit;
578 if (!first || emitter->canonical) {
582 if (event->data.document_start.version_directive) {
584 if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
586 if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
588 if (!yaml_emitter_write_indent(emitter))
592 if (event->data.document_start.tag_directives.start
593 != event->data.document_start.tag_directives.end) {
595 for (tag_directive = event->data.document_start.tag_directives.start;
596 tag_directive != event->data.document_start.tag_directives.end;
598 if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
600 if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
601 strlen((char *)tag_directive->handle)))
603 if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
604 strlen((char *)tag_directive->prefix)))
606 if (!yaml_emitter_write_indent(emitter))
611 if (yaml_emitter_check_empty_document(emitter)) {
616 if (!yaml_emitter_write_indent(emitter))
618 if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
620 if (emitter->canonical) {
621 if (!yaml_emitter_write_indent(emitter))
626 emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
631 else if (event->type == YAML_STREAM_END_EVENT)
633 if (!yaml_emitter_flush(emitter))
636 emitter->state = YAML_EMIT_END_STATE;
641 return yaml_emitter_set_emitter_error(emitter,
642 "expected DOCUMENT-START or STREAM-END");
646 * Expect the root node.
650 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
653 if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
656 return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
660 * Expect DOCUMENT-END.
664 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
667 if (event->type == YAML_DOCUMENT_END_EVENT)
669 if (!yaml_emitter_write_indent(emitter))
671 if (!event->data.document_end.implicit) {
672 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
674 if (!yaml_emitter_write_indent(emitter))
677 if (!yaml_emitter_flush(emitter))
680 emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
685 return yaml_emitter_set_emitter_error(emitter,
686 "expected DOCUMENT-END");
690 * Expect a flow item node.
694 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
695 yaml_event_t *event, int first)
699 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
701 if (!yaml_emitter_increase_indent(emitter, 1, 0))
703 emitter->flow_level ++;
706 if (event->type == YAML_SEQUENCE_END_EVENT)
708 emitter->flow_level --;
709 emitter->indent = POP(emitter, emitter->indents);
710 if (emitter->canonical && !first) {
711 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
713 if (!yaml_emitter_write_indent(emitter))
716 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
718 emitter->state = POP(emitter, emitter->states);
723 if (emitter->canonical || emitter->column > emitter->best_width) {
724 if (!yaml_emitter_write_indent(emitter))
727 if (PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
730 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
734 * Expect a flow key node.
738 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
739 yaml_event_t *event, int first)
743 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
745 if (!yaml_emitter_increase_indent(emitter, 1, 0))
747 emitter->flow_level ++;
750 if (event->type == YAML_MAPPING_END_EVENT)
752 emitter->flow_level --;
753 emitter->indent = POP(emitter, emitter->indents);
754 if (emitter->canonical && !first) {
755 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
757 if (!yaml_emitter_write_indent(emitter))
760 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
762 emitter->state = POP(emitter, emitter->states);
768 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
771 if (emitter->canonical || emitter->column > emitter->best_width) {
772 if (!yaml_emitter_write_indent(emitter))
776 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
778 if (!PUSH(emitter, emitter->states,
779 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
782 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
786 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
788 if (!PUSH(emitter, emitter->states,
789 YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
792 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
797 * Expect a flow value node.
801 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
802 yaml_event_t *event, int simple)
805 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
809 if (emitter->canonical || emitter->column > emitter->best_width) {
810 if (!yaml_emitter_write_indent(emitter))
813 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
816 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
818 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
822 * Expect a block item node.
826 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
827 yaml_event_t *event, int first)
831 if (!yaml_emitter_increase_indent(emitter, 0,
832 (emitter->mapping_context && !emitter->indention)))
836 if (event->type == YAML_SEQUENCE_END_EVENT)
838 emitter->indent = POP(emitter, emitter->indents);
839 emitter->state = POP(emitter, emitter->states);
844 if (!yaml_emitter_write_indent(emitter))
846 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
848 if (!PUSH(emitter, emitter->states,
849 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
852 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
856 * Expect a block key node.
860 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
861 yaml_event_t *event, int first)
865 if (!yaml_emitter_increase_indent(emitter, 0, 0))
869 if (event->type == YAML_MAPPING_END_EVENT)
871 emitter->indent = POP(emitter, emitter->indents);
872 emitter->state = POP(emitter, emitter->states);
877 if (!yaml_emitter_write_indent(emitter))
880 if (yaml_emitter_check_simple_key(emitter))
882 if (!PUSH(emitter, emitter->states,
883 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
886 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
890 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
892 if (!PUSH(emitter, emitter->states,
893 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
896 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
901 * Expect a block value node.
905 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
906 yaml_event_t *event, int simple)
909 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
913 if (!yaml_emitter_write_indent(emitter))
915 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
918 if (!PUSH(emitter, emitter->states,
919 YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
922 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
930 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
931 int root, int sequence, int mapping, int simple_key)
933 emitter->root_context = root;
934 emitter->sequence_context = sequence;
935 emitter->mapping_context = mapping;
936 emitter->simple_key_context = simple_key;
940 case YAML_ALIAS_EVENT:
941 return yaml_emitter_emit_alias(emitter, event);
943 case YAML_SCALAR_EVENT:
944 return yaml_emitter_emit_scalar(emitter, event);
946 case YAML_SEQUENCE_START_EVENT:
947 return yaml_emitter_emit_sequence_start(emitter, event);
949 case YAML_MAPPING_START_EVENT:
950 return yaml_emitter_emit_mapping_start(emitter, event);
953 return yaml_emitter_set_emitter_error(emitter,
954 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
965 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event)
967 if (!yaml_emitter_process_anchor(emitter))
969 emitter->state = POP(emitter, emitter->states);
979 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
981 if (!yaml_emitter_select_scalar_style(emitter, event))
983 if (!yaml_emitter_process_anchor(emitter))
985 if (!yaml_emitter_process_tag(emitter))
987 if (!yaml_emitter_increase_indent(emitter, 1, 0))
989 if (!yaml_emitter_process_scalar(emitter))
991 emitter->indent = POP(emitter, emitter->indents);
992 emitter->state = POP(emitter, emitter->states);
998 * Expect SEQUENCE-START.
1002 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1004 if (!yaml_emitter_process_anchor(emitter))
1006 if (!yaml_emitter_process_tag(emitter))
1009 if (emitter->flow_level || emitter->canonical
1010 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1011 || yaml_emitter_check_empty_sequence(emitter)) {
1012 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1015 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1022 * Expect MAPPING-START.
1026 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1028 if (!yaml_emitter_process_anchor(emitter))
1030 if (!yaml_emitter_process_tag(emitter))
1033 if (emitter->flow_level || emitter->canonical
1034 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1035 || yaml_emitter_check_empty_mapping(emitter)) {
1036 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1039 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1046 * Check if the document content is an empty scalar.
1050 yaml_emitter_check_empty_document(yaml_emitter_t *emitter)
1056 * Check if the next events represent an empty sequence.
1060 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1062 if (emitter->events.tail - emitter->events.head < 2)
1065 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1066 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1070 * Check if the next events represent an empty mapping.
1074 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1076 if (emitter->events.tail - emitter->events.head < 2)
1079 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1080 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1084 * Check if the next node can be expressed as a simple key.
1088 yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1090 yaml_event_t *event = emitter->events.head;
1093 switch (event->type)
1095 case YAML_ALIAS_EVENT:
1096 length += emitter->anchor_data.anchor_length;
1099 case YAML_SCALAR_EVENT:
1100 if (emitter->scalar_data.multiline)
1102 length += emitter->anchor_data.anchor_length
1103 + emitter->tag_data.handle_length
1104 + emitter->tag_data.suffix_length
1105 + emitter->scalar_data.length;
1108 case YAML_SEQUENCE_START_EVENT:
1109 if (!yaml_emitter_check_empty_sequence(emitter))
1111 length += emitter->anchor_data.anchor_length
1112 + emitter->tag_data.handle_length
1113 + emitter->tag_data.suffix_length;
1116 case YAML_MAPPING_START_EVENT:
1117 if (!yaml_emitter_check_empty_sequence(emitter))
1119 length += emitter->anchor_data.anchor_length
1120 + emitter->tag_data.handle_length
1121 + emitter->tag_data.suffix_length;
1135 * Determine an acceptable scalar style.
1139 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1141 yaml_scalar_style_t style = event->data.scalar.style;
1143 if (style == YAML_ANY_SCALAR_STYLE)
1144 style = YAML_PLAIN_SCALAR_STYLE;
1146 if (emitter->canonical)
1147 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1149 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1150 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1152 if (style == YAML_PLAIN_SCALAR_STYLE)
1154 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1155 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1156 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1157 if (!emitter->scalar_data.length
1158 && (emitter->flow_level || emitter->simple_key_context))
1159 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1160 if (!event->data.scalar.plain_implicit
1161 && !emitter->tag_data.handle && !emitter->tag_data.suffix)
1162 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1165 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1167 if (!emitter->scalar_data.single_quoted_allowed)
1168 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1171 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1173 if (!emitter->scalar_data.block_allowed)
1174 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1177 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1179 if (!event->data.scalar.plain_implicit
1180 && !event->data.scalar.quoted_implicit) {
1181 return yaml_emitter_set_emitter_error(emitter,
1182 "neither tag nor implicit flags are specified");
1185 if (event->data.scalar.plain_implicit
1186 && style != YAML_PLAIN_SCALAR_STYLE) {
1187 emitter->tag_data.handle = (yaml_char_t *)"!";
1188 emitter->tag_data.handle_length = 1;
1192 emitter->scalar_data.style = style;
1202 yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1204 if (!emitter->anchor_data.anchor)
1207 if (!yaml_emitter_write_indicator(emitter,
1208 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1211 return yaml_emitter_write_anchor(emitter,
1212 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1220 yaml_emitter_process_tag(yaml_emitter_t *emitter)
1222 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1225 if (emitter->tag_data.handle)
1227 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1228 emitter->tag_data.handle_length))
1230 if (emitter->tag_data.suffix) {
1231 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1232 emitter->tag_data.suffix_length))
1238 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1240 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1241 emitter->tag_data.suffix_length))
1243 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1255 yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1257 switch (emitter->scalar_data.style)
1259 case YAML_PLAIN_SCALAR_STYLE:
1260 return yaml_emitter_write_plain_scalar(emitter,
1261 emitter->scalar_data.value, emitter->scalar_data.length,
1262 !emitter->simple_key_context);
1264 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1265 return yaml_emitter_write_single_quoted_scalar(emitter,
1266 emitter->scalar_data.value, emitter->scalar_data.length,
1267 !emitter->simple_key_context);
1269 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1270 return yaml_emitter_write_double_quoted_scalar(emitter,
1271 emitter->scalar_data.value, emitter->scalar_data.length,
1272 !emitter->simple_key_context);
1274 case YAML_LITERAL_SCALAR_STYLE:
1275 return yaml_emitter_write_literal_scalar(emitter,
1276 emitter->scalar_data.value, emitter->scalar_data.length);
1278 case YAML_FOLDED_SCALAR_STYLE:
1279 return yaml_emitter_write_folded_scalar(emitter,
1280 emitter->scalar_data.value, emitter->scalar_data.length);
1283 assert(1); /* Impossible. */
1290 * Check if a %YAML directive is valid.
1294 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1295 yaml_version_directive_t version_directive)
1297 if (version_directive.major != 1 || version_directive.minor != 1) {
1298 return yaml_emitter_set_emitter_error(emitter,
1299 "incompatible %YAML directive");
1306 * Check if a %TAG directive is valid.
1310 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1311 yaml_tag_directive_t tag_directive)
1313 yaml_string_t handle = STRING(tag_directive.handle,
1314 strlen((char *)tag_directive.handle));
1315 yaml_string_t prefix = STRING(tag_directive.prefix,
1316 strlen((char *)tag_directive.prefix));
1318 if (handle.start == handle.end) {
1319 return yaml_emitter_set_emitter_error(emitter,
1320 "tag handle must not be empty");
1323 if (handle.start[0] != '!') {
1324 return yaml_emitter_set_emitter_error(emitter,
1325 "tag handle must start with '!'");
1328 if (handle.end[-1] != '!') {
1329 return yaml_emitter_set_emitter_error(emitter,
1330 "tag handle must end with '!'");
1335 while (handle.pointer != handle.end-1) {
1336 if (!IS_ALPHA(handle)) {
1337 return yaml_emitter_set_emitter_error(emitter,
1338 "tag handle must contain alphanumerical characters only");
1343 if (prefix.start == prefix.end) {
1344 return yaml_emitter_set_emitter_error(emitter,
1345 "tag prefix must not be empty");
1352 * Check if an anchor is valid.
1356 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1357 yaml_char_t *anchor, int alias)
1359 yaml_string_t string = STRING(anchor, strlen((char *)anchor));
1361 if (string.start == string.end) {
1362 return yaml_emitter_set_emitter_error(emitter, alias ?
1363 "alias value must not be empty" :
1364 "anchor value must not be empty");
1367 while (string.pointer != string.end) {
1368 if (!IS_ALPHA(string)) {
1369 return yaml_emitter_set_emitter_error(emitter, alias ?
1370 "alias value must contain alphanumerical characters only" :
1371 "anchor value must contain alphanumerical characters only");
1378 * Check if a tag is valid.
1382 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1385 yaml_string_t string = STRING(tag, strlen((char *)tag));
1386 yaml_tag_directive_t *tag_directive;
1388 if (string.start == string.end) {
1389 return yaml_emitter_set_emitter_error(emitter,
1390 "tag value must not be empty");
1393 for (tag_directive = emitter->tag_directives.start;
1394 tag_directive != emitter->tag_directives.end; tag_directive ++) {
1395 size_t prefix_length = strlen((char *)tag_directive->prefix);
1396 if (prefix_length < (string.end - string.start)
1397 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1398 prefix_length) == 0)
1400 emitter->tag_data.handle = tag_directive->handle;
1401 emitter->tag_data.handle_length =
1402 strlen((char *)tag_directive->handle);
1403 emitter->tag_data.suffix = string.start + prefix_length;
1404 emitter->tag_data.suffix_length =
1405 (string.end - string.start) - prefix_length;
1410 emitter->tag_data.suffix = string.start;
1411 emitter->tag_data.suffix_length = string.end - string.start;
1417 * Check if a scalar is valid.
1421 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1422 yaml_char_t *value, size_t length)
1424 yaml_string_t string = STRING(value, length);
1426 int block_indicators = 0;
1427 int flow_indicators = 0;
1428 int line_breaks = 0;
1429 int special_characters = 0;
1431 int inline_spaces = 0;
1432 int inline_breaks = 0;
1433 int leading_spaces = 0;
1434 int leading_breaks = 0;
1435 int trailing_spaces = 0;
1436 int trailing_breaks = 0;
1437 int inline_breaks_spaces = 0;
1438 int mixed_breaks_spaces = 0;
1440 int preceeded_by_space = 0;
1441 int followed_by_space = 0;
1447 emitter->scalar_data.value = value;
1448 emitter->scalar_data.length = length;
1450 if (string.start == string.end)
1452 emitter->scalar_data.multiline = 0;
1453 emitter->scalar_data.flow_plain_allowed = 0;
1454 emitter->scalar_data.block_plain_allowed = 1;
1455 emitter->scalar_data.single_quoted_allowed = 1;
1456 emitter->scalar_data.block_allowed = 0;
1461 if ((CHECK_AT(string, '-', 0)
1462 && CHECK_AT(string, '-', 1)
1463 && CHECK_AT(string, '-', 2))
1464 || (CHECK_AT(string, '.', 0)
1465 && CHECK_AT(string, '.', 1)
1466 && CHECK_AT(string, '.', 2))) {
1467 block_indicators = 1;
1468 flow_indicators = 1;
1471 preceeded_by_space = 1;
1472 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1474 while (string.pointer != string.end)
1476 if (string.start == string.pointer)
1478 if (CHECK(string, '#') || CHECK(string, ',')
1479 || CHECK(string, '[') || CHECK(string, ']')
1480 || CHECK(string, '{') || CHECK(string, '}')
1481 || CHECK(string, '&') || CHECK(string, '*')
1482 || CHECK(string, '!') || CHECK(string, '|')
1483 || CHECK(string, '>') || CHECK(string, '\'')
1484 || CHECK(string, '"') || CHECK(string, '%')
1485 || CHECK(string, '@') || CHECK(string, '`')) {
1486 flow_indicators = 1;
1487 block_indicators = 1;
1490 if (CHECK(string, '?') || CHECK(string, ':')) {
1491 flow_indicators = 1;
1492 if (followed_by_space) {
1493 block_indicators = 1;
1497 if (CHECK(string, '-') && followed_by_space) {
1498 flow_indicators = 1;
1499 block_indicators = 1;
1504 if (CHECK(string, ',') || CHECK(string, '?')
1505 || CHECK(string, '[') || CHECK(string, ']')
1506 || CHECK(string, '{') || CHECK(string, '}')) {
1507 flow_indicators = 1;
1510 if (CHECK(string, ':')) {
1511 flow_indicators = 1;
1512 if (followed_by_space) {
1513 block_indicators = 1;
1517 if (CHECK(string, '#') && preceeded_by_space) {
1518 flow_indicators = 1;
1519 block_indicators = 1;
1523 if (!IS_PRINTABLE(string)
1524 || (!IS_ASCII(string) && !emitter->unicode)) {
1525 special_characters = 1;
1528 if (IS_BREAK(string)) {
1532 if (IS_SPACE(string))
1535 if (string.start == string.pointer) {
1540 else if (IS_BREAK(string))
1546 if (string.start == string.pointer) {
1551 else if (spaces || breaks)
1554 if (spaces && breaks) {
1555 mixed_breaks_spaces = 1;
1566 mixed_breaks_spaces = 1;
1568 else if (spaces && breaks) {
1569 inline_breaks_spaces = 1;
1578 spaces = breaks = mixed = leading = 0;
1581 preceeded_by_space = IS_BLANKZ(string);
1583 if (string.pointer != string.end) {
1584 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1588 emitter->scalar_data.multiline = line_breaks;
1590 emitter->scalar_data.flow_plain_allowed = 1;
1591 emitter->scalar_data.block_plain_allowed = 1;
1592 emitter->scalar_data.single_quoted_allowed = 1;
1593 emitter->scalar_data.block_allowed = 1;
1595 if (leading_spaces || leading_breaks || trailing_spaces) {
1596 emitter->scalar_data.flow_plain_allowed = 0;
1597 emitter->scalar_data.block_plain_allowed = 0;
1598 emitter->scalar_data.block_allowed = 0;
1601 if (trailing_breaks) {
1602 emitter->scalar_data.flow_plain_allowed = 0;
1603 emitter->scalar_data.block_plain_allowed = 0;
1606 if (inline_breaks_spaces) {
1607 emitter->scalar_data.flow_plain_allowed = 0;
1608 emitter->scalar_data.block_plain_allowed = 0;
1609 emitter->scalar_data.single_quoted_allowed = 0;
1612 if (mixed_breaks_spaces || special_characters) {
1613 emitter->scalar_data.flow_plain_allowed = 0;
1614 emitter->scalar_data.block_plain_allowed = 0;
1615 emitter->scalar_data.single_quoted_allowed = 0;
1616 emitter->scalar_data.block_allowed = 0;
1620 emitter->scalar_data.flow_plain_allowed = 0;
1621 emitter->scalar_data.block_plain_allowed = 0;
1624 if (flow_indicators) {
1625 emitter->scalar_data.flow_plain_allowed = 0;
1628 if (block_indicators) {
1629 emitter->scalar_data.block_plain_allowed = 0;
1636 * Check if the event data is valid.
1640 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1641 yaml_event_t *event)
1643 emitter->anchor_data.anchor = NULL;
1644 emitter->anchor_data.anchor_length = 0;
1645 emitter->tag_data.handle = NULL;
1646 emitter->tag_data.handle_length = 0;
1647 emitter->tag_data.suffix = NULL;
1648 emitter->tag_data.suffix_length = 0;
1649 emitter->scalar_data.value = NULL;
1650 emitter->scalar_data.length = 0;
1652 switch (event->type)
1654 case YAML_ALIAS_EVENT:
1655 if (!yaml_emitter_analyze_anchor(emitter,
1656 event->data.alias.anchor, 1))
1660 case YAML_SCALAR_EVENT:
1661 if (event->data.scalar.anchor) {
1662 if (!yaml_emitter_analyze_anchor(emitter,
1663 event->data.scalar.anchor, 0))
1666 if (event->data.scalar.tag && (emitter->canonical ||
1667 (!event->data.scalar.plain_implicit
1668 && !event->data.scalar.quoted_implicit))) {
1669 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1672 if (!yaml_emitter_analyze_scalar(emitter,
1673 event->data.scalar.value, event->data.scalar.length))
1677 case YAML_SEQUENCE_START_EVENT:
1678 if (event->data.sequence_start.anchor) {
1679 if (!yaml_emitter_analyze_anchor(emitter,
1680 event->data.sequence_start.anchor, 0))
1683 if (event->data.sequence_start.tag && (emitter->canonical ||
1684 !event->data.sequence_start.implicit)) {
1685 if (!yaml_emitter_analyze_tag(emitter,
1686 event->data.sequence_start.tag))
1691 case YAML_MAPPING_START_EVENT:
1692 if (event->data.mapping_start.anchor) {
1693 if (!yaml_emitter_analyze_anchor(emitter,
1694 event->data.mapping_start.anchor, 0))
1697 if (event->data.mapping_start.tag && (emitter->canonical ||
1698 !event->data.mapping_start.implicit)) {
1699 if (!yaml_emitter_analyze_tag(emitter,
1700 event->data.mapping_start.tag))
1711 * Write the BOM character.
1715 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1717 if (!FLUSH(emitter)) return 0;
1719 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1720 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1721 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1727 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1729 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1731 if (!emitter->indention || emitter->column > indent
1732 || (emitter->column == indent && !emitter->whitespace)) {
1733 if (!PUT_BREAK(emitter)) return 0;
1736 while (emitter->column < indent) {
1737 if (!PUT(emitter, ' ')) return 0;
1740 emitter->whitespace = 1;
1741 emitter->indention = 1;
1747 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1748 char *indicator, int need_whitespace,
1749 int is_whitespace, int is_indention)
1751 yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator));
1753 if (need_whitespace && !emitter->whitespace) {
1754 if (!PUT(emitter, ' ')) return 0;
1757 while (string.pointer != string.end) {
1758 if (!WRITE(emitter, string)) return 0;
1761 emitter->whitespace = is_whitespace;
1762 emitter->indention = (emitter->indention && is_indention);
1768 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1769 yaml_char_t *value, size_t length)
1771 yaml_string_t string = STRING(value, length);
1773 while (string.pointer != string.end) {
1774 if (!WRITE(emitter, string)) return 0;
1777 emitter->whitespace = 0;
1778 emitter->indention = 0;
1784 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1785 yaml_char_t *value, size_t length)
1787 yaml_string_t string = STRING(value, length);
1789 while (string.pointer != string.end) {
1790 if (!WRITE(emitter, string)) return 0;
1793 emitter->whitespace = 0;
1794 emitter->indention = 0;
1800 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1801 yaml_char_t *value, size_t length)
1807 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1808 yaml_char_t *value, size_t length, int allow_breaks)
1814 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1815 yaml_char_t *value, size_t length, int allow_breaks)
1821 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
1822 yaml_char_t *value, size_t length, int allow_breaks)
1828 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
1829 yaml_char_t *value, size_t length)
1835 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
1836 yaml_char_t *value, size_t length)