1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package yaml
24
25 import (
26 "bytes"
27 "fmt"
28 )
29
30
31 func flush(emitter *yaml_emitter_t) bool {
32 if emitter.buffer_pos+5 >= len(emitter.buffer) {
33 return yaml_emitter_flush(emitter)
34 }
35 return true
36 }
37
38
39 func put(emitter *yaml_emitter_t, value byte) bool {
40 if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
41 return false
42 }
43 emitter.buffer[emitter.buffer_pos] = value
44 emitter.buffer_pos++
45 emitter.column++
46 return true
47 }
48
49
50 func put_break(emitter *yaml_emitter_t) bool {
51 if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
52 return false
53 }
54 switch emitter.line_break {
55 case yaml_CR_BREAK:
56 emitter.buffer[emitter.buffer_pos] = '\r'
57 emitter.buffer_pos += 1
58 case yaml_LN_BREAK:
59 emitter.buffer[emitter.buffer_pos] = '\n'
60 emitter.buffer_pos += 1
61 case yaml_CRLN_BREAK:
62 emitter.buffer[emitter.buffer_pos+0] = '\r'
63 emitter.buffer[emitter.buffer_pos+1] = '\n'
64 emitter.buffer_pos += 2
65 default:
66 panic("unknown line break setting")
67 }
68 if emitter.column == 0 {
69 emitter.space_above = true
70 }
71 emitter.column = 0
72 emitter.line++
73
74 emitter.indention = true
75 return true
76 }
77
78
79 func write(emitter *yaml_emitter_t, s []byte, i *int) bool {
80 if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
81 return false
82 }
83 p := emitter.buffer_pos
84 w := width(s[*i])
85 switch w {
86 case 4:
87 emitter.buffer[p+3] = s[*i+3]
88 fallthrough
89 case 3:
90 emitter.buffer[p+2] = s[*i+2]
91 fallthrough
92 case 2:
93 emitter.buffer[p+1] = s[*i+1]
94 fallthrough
95 case 1:
96 emitter.buffer[p+0] = s[*i+0]
97 default:
98 panic("unknown character width")
99 }
100 emitter.column++
101 emitter.buffer_pos += w
102 *i += w
103 return true
104 }
105
106
107 func write_all(emitter *yaml_emitter_t, s []byte) bool {
108 for i := 0; i < len(s); {
109 if !write(emitter, s, &i) {
110 return false
111 }
112 }
113 return true
114 }
115
116
117 func write_break(emitter *yaml_emitter_t, s []byte, i *int) bool {
118 if s[*i] == '\n' {
119 if !put_break(emitter) {
120 return false
121 }
122 *i++
123 } else {
124 if !write(emitter, s, i) {
125 return false
126 }
127 if emitter.column == 0 {
128 emitter.space_above = true
129 }
130 emitter.column = 0
131 emitter.line++
132
133 emitter.indention = true
134 }
135 return true
136 }
137
138
139 func yaml_emitter_set_emitter_error(emitter *yaml_emitter_t, problem string) bool {
140 emitter.error = yaml_EMITTER_ERROR
141 emitter.problem = problem
142 return false
143 }
144
145
146 func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool {
147 emitter.events = append(emitter.events, *event)
148 for !yaml_emitter_need_more_events(emitter) {
149 event := &emitter.events[emitter.events_head]
150 if !yaml_emitter_analyze_event(emitter, event) {
151 return false
152 }
153 if !yaml_emitter_state_machine(emitter, event) {
154 return false
155 }
156 yaml_event_delete(event)
157 emitter.events_head++
158 }
159 return true
160 }
161
162
163
164
165
166
167
168
169 func yaml_emitter_need_more_events(emitter *yaml_emitter_t) bool {
170 if emitter.events_head == len(emitter.events) {
171 return true
172 }
173 var accumulate int
174 switch emitter.events[emitter.events_head].typ {
175 case yaml_DOCUMENT_START_EVENT:
176 accumulate = 1
177 break
178 case yaml_SEQUENCE_START_EVENT:
179 accumulate = 2
180 break
181 case yaml_MAPPING_START_EVENT:
182 accumulate = 3
183 break
184 default:
185 return false
186 }
187 if len(emitter.events)-emitter.events_head > accumulate {
188 return false
189 }
190 var level int
191 for i := emitter.events_head; i < len(emitter.events); i++ {
192 switch emitter.events[i].typ {
193 case yaml_STREAM_START_EVENT, yaml_DOCUMENT_START_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT:
194 level++
195 case yaml_STREAM_END_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT:
196 level--
197 }
198 if level == 0 {
199 return false
200 }
201 }
202 return true
203 }
204
205
206 func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_directive_t, allow_duplicates bool) bool {
207 for i := 0; i < len(emitter.tag_directives); i++ {
208 if bytes.Equal(value.handle, emitter.tag_directives[i].handle) {
209 if allow_duplicates {
210 return true
211 }
212 return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive")
213 }
214 }
215
216
217
218 tag_copy := yaml_tag_directive_t{
219 handle: make([]byte, len(value.handle)),
220 prefix: make([]byte, len(value.prefix)),
221 }
222 copy(tag_copy.handle, value.handle)
223 copy(tag_copy.prefix, value.prefix)
224 emitter.tag_directives = append(emitter.tag_directives, tag_copy)
225 return true
226 }
227
228
229 func yaml_emitter_increase_indent_compact(emitter *yaml_emitter_t, flow, indentless bool, compact_seq bool) bool {
230 emitter.indents = append(emitter.indents, emitter.indent)
231 if emitter.indent < 0 {
232 if flow {
233 emitter.indent = emitter.best_indent
234 } else {
235 emitter.indent = 0
236 }
237 } else if !indentless {
238
239 if emitter.states[len(emitter.states)-1] == yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE {
240
241 emitter.indent += 2
242 } else {
243
244 emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent)
245 if compact_seq {
246
247
248
249
250 emitter.indent = emitter.indent - 2
251 }
252 }
253 }
254 return true
255 }
256
257
258 func yaml_emitter_state_machine(emitter *yaml_emitter_t, event *yaml_event_t) bool {
259 switch emitter.state {
260 default:
261 case yaml_EMIT_STREAM_START_STATE:
262 return yaml_emitter_emit_stream_start(emitter, event)
263
264 case yaml_EMIT_FIRST_DOCUMENT_START_STATE:
265 return yaml_emitter_emit_document_start(emitter, event, true)
266
267 case yaml_EMIT_DOCUMENT_START_STATE:
268 return yaml_emitter_emit_document_start(emitter, event, false)
269
270 case yaml_EMIT_DOCUMENT_CONTENT_STATE:
271 return yaml_emitter_emit_document_content(emitter, event)
272
273 case yaml_EMIT_DOCUMENT_END_STATE:
274 return yaml_emitter_emit_document_end(emitter, event)
275
276 case yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
277 return yaml_emitter_emit_flow_sequence_item(emitter, event, true, false)
278
279 case yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE:
280 return yaml_emitter_emit_flow_sequence_item(emitter, event, false, true)
281
282 case yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE:
283 return yaml_emitter_emit_flow_sequence_item(emitter, event, false, false)
284
285 case yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
286 return yaml_emitter_emit_flow_mapping_key(emitter, event, true, false)
287
288 case yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE:
289 return yaml_emitter_emit_flow_mapping_key(emitter, event, false, true)
290
291 case yaml_EMIT_FLOW_MAPPING_KEY_STATE:
292 return yaml_emitter_emit_flow_mapping_key(emitter, event, false, false)
293
294 case yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
295 return yaml_emitter_emit_flow_mapping_value(emitter, event, true)
296
297 case yaml_EMIT_FLOW_MAPPING_VALUE_STATE:
298 return yaml_emitter_emit_flow_mapping_value(emitter, event, false)
299
300 case yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
301 return yaml_emitter_emit_block_sequence_item(emitter, event, true)
302
303 case yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
304 return yaml_emitter_emit_block_sequence_item(emitter, event, false)
305
306 case yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
307 return yaml_emitter_emit_block_mapping_key(emitter, event, true)
308
309 case yaml_EMIT_BLOCK_MAPPING_KEY_STATE:
310 return yaml_emitter_emit_block_mapping_key(emitter, event, false)
311
312 case yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
313 return yaml_emitter_emit_block_mapping_value(emitter, event, true)
314
315 case yaml_EMIT_BLOCK_MAPPING_VALUE_STATE:
316 return yaml_emitter_emit_block_mapping_value(emitter, event, false)
317
318 case yaml_EMIT_END_STATE:
319 return yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END")
320 }
321 panic("invalid emitter state")
322 }
323
324
325 func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
326 if event.typ != yaml_STREAM_START_EVENT {
327 return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START")
328 }
329 if emitter.encoding == yaml_ANY_ENCODING {
330 emitter.encoding = event.encoding
331 if emitter.encoding == yaml_ANY_ENCODING {
332 emitter.encoding = yaml_UTF8_ENCODING
333 }
334 }
335 if emitter.best_indent < 2 || emitter.best_indent > 9 {
336 emitter.best_indent = 2
337 }
338 if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 {
339 emitter.best_width = 80
340 }
341 if emitter.best_width < 0 {
342 emitter.best_width = 1<<31 - 1
343 }
344 if emitter.line_break == yaml_ANY_BREAK {
345 emitter.line_break = yaml_LN_BREAK
346 }
347
348 emitter.indent = -1
349 emitter.line = 0
350 emitter.column = 0
351 emitter.whitespace = true
352 emitter.indention = true
353 emitter.space_above = true
354 emitter.foot_indent = -1
355
356 if emitter.encoding != yaml_UTF8_ENCODING {
357 if !yaml_emitter_write_bom(emitter) {
358 return false
359 }
360 }
361 emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE
362 return true
363 }
364
365
366 func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
367
368 if event.typ == yaml_DOCUMENT_START_EVENT {
369
370 if event.version_directive != nil {
371 if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) {
372 return false
373 }
374 }
375
376 for i := 0; i < len(event.tag_directives); i++ {
377 tag_directive := &event.tag_directives[i]
378 if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) {
379 return false
380 }
381 if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) {
382 return false
383 }
384 }
385
386 for i := 0; i < len(default_tag_directives); i++ {
387 tag_directive := &default_tag_directives[i]
388 if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) {
389 return false
390 }
391 }
392
393 implicit := event.implicit
394 if !first || emitter.canonical {
395 implicit = false
396 }
397
398 if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) {
399 if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
400 return false
401 }
402 if !yaml_emitter_write_indent(emitter) {
403 return false
404 }
405 }
406
407 if event.version_directive != nil {
408 implicit = false
409 if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) {
410 return false
411 }
412 if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) {
413 return false
414 }
415 if !yaml_emitter_write_indent(emitter) {
416 return false
417 }
418 }
419
420 if len(event.tag_directives) > 0 {
421 implicit = false
422 for i := 0; i < len(event.tag_directives); i++ {
423 tag_directive := &event.tag_directives[i]
424 if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) {
425 return false
426 }
427 if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) {
428 return false
429 }
430 if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) {
431 return false
432 }
433 if !yaml_emitter_write_indent(emitter) {
434 return false
435 }
436 }
437 }
438
439 if yaml_emitter_check_empty_document(emitter) {
440 implicit = false
441 }
442 if !implicit {
443 if !yaml_emitter_write_indent(emitter) {
444 return false
445 }
446 if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) {
447 return false
448 }
449 if emitter.canonical || true {
450 if !yaml_emitter_write_indent(emitter) {
451 return false
452 }
453 }
454 }
455
456 if len(emitter.head_comment) > 0 {
457 if !yaml_emitter_process_head_comment(emitter) {
458 return false
459 }
460 if !put_break(emitter) {
461 return false
462 }
463 }
464
465 emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE
466 return true
467 }
468
469 if event.typ == yaml_STREAM_END_EVENT {
470 if emitter.open_ended {
471 if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
472 return false
473 }
474 if !yaml_emitter_write_indent(emitter) {
475 return false
476 }
477 }
478 if !yaml_emitter_flush(emitter) {
479 return false
480 }
481 emitter.state = yaml_EMIT_END_STATE
482 return true
483 }
484
485 return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END")
486 }
487
488
489 func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool {
490 emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE)
491
492 if !yaml_emitter_process_head_comment(emitter) {
493 return false
494 }
495 if !yaml_emitter_emit_node(emitter, event, true, false, false, false) {
496 return false
497 }
498 if !yaml_emitter_process_line_comment(emitter) {
499 return false
500 }
501 if !yaml_emitter_process_foot_comment(emitter) {
502 return false
503 }
504 return true
505 }
506
507
508 func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool {
509 if event.typ != yaml_DOCUMENT_END_EVENT {
510 return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END")
511 }
512
513 emitter.foot_indent = 0
514 if !yaml_emitter_process_foot_comment(emitter) {
515 return false
516 }
517 emitter.foot_indent = -1
518 if !yaml_emitter_write_indent(emitter) {
519 return false
520 }
521 if !event.implicit {
522
523 if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
524 return false
525 }
526 if !yaml_emitter_write_indent(emitter) {
527 return false
528 }
529 }
530 if !yaml_emitter_flush(emitter) {
531 return false
532 }
533 emitter.state = yaml_EMIT_DOCUMENT_START_STATE
534 emitter.tag_directives = emitter.tag_directives[:0]
535 return true
536 }
537
538
539 func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool {
540 if first {
541 if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
542 return false
543 }
544 if !yaml_emitter_increase_indent(emitter, true, false) {
545 return false
546 }
547 emitter.flow_level++
548 }
549
550 if event.typ == yaml_SEQUENCE_END_EVENT {
551 if emitter.canonical && !first && !trail {
552 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
553 return false
554 }
555 }
556 emitter.flow_level--
557 emitter.indent = emitter.indents[len(emitter.indents)-1]
558 emitter.indents = emitter.indents[:len(emitter.indents)-1]
559 if emitter.column == 0 || emitter.canonical && !first {
560 if !yaml_emitter_write_indent(emitter) {
561 return false
562 }
563 }
564 if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) {
565 return false
566 }
567 if !yaml_emitter_process_line_comment(emitter) {
568 return false
569 }
570 if !yaml_emitter_process_foot_comment(emitter) {
571 return false
572 }
573 emitter.state = emitter.states[len(emitter.states)-1]
574 emitter.states = emitter.states[:len(emitter.states)-1]
575
576 return true
577 }
578
579 if !first && !trail {
580 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
581 return false
582 }
583 }
584
585 if !yaml_emitter_process_head_comment(emitter) {
586 return false
587 }
588 if emitter.column == 0 {
589 if !yaml_emitter_write_indent(emitter) {
590 return false
591 }
592 }
593
594 if emitter.canonical || emitter.column > emitter.best_width {
595 if !yaml_emitter_write_indent(emitter) {
596 return false
597 }
598 }
599 if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
600 emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE)
601 } else {
602 emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE)
603 }
604 if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
605 return false
606 }
607 if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
608 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
609 return false
610 }
611 }
612 if !yaml_emitter_process_line_comment(emitter) {
613 return false
614 }
615 if !yaml_emitter_process_foot_comment(emitter) {
616 return false
617 }
618 return true
619 }
620
621
622 func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool {
623 if first {
624 if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
625 return false
626 }
627 if !yaml_emitter_increase_indent(emitter, true, false) {
628 return false
629 }
630 emitter.flow_level++
631 }
632
633 if event.typ == yaml_MAPPING_END_EVENT {
634 if (emitter.canonical || len(emitter.head_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0) && !first && !trail {
635 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
636 return false
637 }
638 }
639 if !yaml_emitter_process_head_comment(emitter) {
640 return false
641 }
642 emitter.flow_level--
643 emitter.indent = emitter.indents[len(emitter.indents)-1]
644 emitter.indents = emitter.indents[:len(emitter.indents)-1]
645 if emitter.canonical && !first {
646 if !yaml_emitter_write_indent(emitter) {
647 return false
648 }
649 }
650 if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) {
651 return false
652 }
653 if !yaml_emitter_process_line_comment(emitter) {
654 return false
655 }
656 if !yaml_emitter_process_foot_comment(emitter) {
657 return false
658 }
659 emitter.state = emitter.states[len(emitter.states)-1]
660 emitter.states = emitter.states[:len(emitter.states)-1]
661 return true
662 }
663
664 if !first && !trail {
665 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
666 return false
667 }
668 }
669
670 if !yaml_emitter_process_head_comment(emitter) {
671 return false
672 }
673
674 if emitter.column == 0 {
675 if !yaml_emitter_write_indent(emitter) {
676 return false
677 }
678 }
679
680 if emitter.canonical || emitter.column > emitter.best_width {
681 if !yaml_emitter_write_indent(emitter) {
682 return false
683 }
684 }
685
686 if !emitter.canonical && yaml_emitter_check_simple_key(emitter) {
687 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)
688 return yaml_emitter_emit_node(emitter, event, false, false, true, true)
689 }
690 if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) {
691 return false
692 }
693 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE)
694 return yaml_emitter_emit_node(emitter, event, false, false, true, false)
695 }
696
697
698 func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
699 if simple {
700 if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
701 return false
702 }
703 } else {
704 if emitter.canonical || emitter.column > emitter.best_width {
705 if !yaml_emitter_write_indent(emitter) {
706 return false
707 }
708 }
709 if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) {
710 return false
711 }
712 }
713 if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
714 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE)
715 } else {
716 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE)
717 }
718 if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
719 return false
720 }
721 if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
722 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
723 return false
724 }
725 }
726 if !yaml_emitter_process_line_comment(emitter) {
727 return false
728 }
729 if !yaml_emitter_process_foot_comment(emitter) {
730 return false
731 }
732 return true
733 }
734
735
736 func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
737 if first {
738
739
740
741
742
743
744
745 seq := emitter.mapping_context && (emitter.column == 0 || !emitter.indention) &&
746 emitter.compact_sequence_indent
747 if !yaml_emitter_increase_indent_compact(emitter, false, false, seq) {
748 return false
749 }
750 }
751 if event.typ == yaml_SEQUENCE_END_EVENT {
752 emitter.indent = emitter.indents[len(emitter.indents)-1]
753 emitter.indents = emitter.indents[:len(emitter.indents)-1]
754 emitter.state = emitter.states[len(emitter.states)-1]
755 emitter.states = emitter.states[:len(emitter.states)-1]
756 return true
757 }
758 if !yaml_emitter_process_head_comment(emitter) {
759 return false
760 }
761 if !yaml_emitter_write_indent(emitter) {
762 return false
763 }
764 if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) {
765 return false
766 }
767 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE)
768 if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
769 return false
770 }
771 if !yaml_emitter_process_line_comment(emitter) {
772 return false
773 }
774 if !yaml_emitter_process_foot_comment(emitter) {
775 return false
776 }
777 return true
778 }
779
780
781 func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
782 if first {
783 if !yaml_emitter_increase_indent(emitter, false, false) {
784 return false
785 }
786 }
787 if !yaml_emitter_process_head_comment(emitter) {
788 return false
789 }
790 if event.typ == yaml_MAPPING_END_EVENT {
791 emitter.indent = emitter.indents[len(emitter.indents)-1]
792 emitter.indents = emitter.indents[:len(emitter.indents)-1]
793 emitter.state = emitter.states[len(emitter.states)-1]
794 emitter.states = emitter.states[:len(emitter.states)-1]
795 return true
796 }
797 if !yaml_emitter_write_indent(emitter) {
798 return false
799 }
800 if len(emitter.line_comment) > 0 {
801
802
803
804 emitter.key_line_comment = emitter.line_comment
805 emitter.line_comment = nil
806 }
807 if yaml_emitter_check_simple_key(emitter) {
808 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)
809 return yaml_emitter_emit_node(emitter, event, false, false, true, true)
810 }
811 if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) {
812 return false
813 }
814 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE)
815 return yaml_emitter_emit_node(emitter, event, false, false, true, false)
816 }
817
818
819 func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
820 if simple {
821 if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
822 return false
823 }
824 } else {
825 if !yaml_emitter_write_indent(emitter) {
826 return false
827 }
828 if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) {
829 return false
830 }
831 }
832 if len(emitter.key_line_comment) > 0 {
833
834
835
836 if event.typ == yaml_SCALAR_EVENT {
837 if len(emitter.line_comment) == 0 {
838
839
840
841 emitter.line_comment = emitter.key_line_comment
842 emitter.key_line_comment = nil
843 }
844 } else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) {
845
846 emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
847 if !yaml_emitter_process_line_comment(emitter) {
848 return false
849 }
850 emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
851 }
852 }
853 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE)
854 if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
855 return false
856 }
857 if !yaml_emitter_process_line_comment(emitter) {
858 return false
859 }
860 if !yaml_emitter_process_foot_comment(emitter) {
861 return false
862 }
863 return true
864 }
865
866 func yaml_emitter_silent_nil_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
867 return event.typ == yaml_SCALAR_EVENT && event.implicit && !emitter.canonical && len(emitter.scalar_data.value) == 0
868 }
869
870
871 func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t,
872 root bool, sequence bool, mapping bool, simple_key bool) bool {
873
874 emitter.root_context = root
875 emitter.sequence_context = sequence
876 emitter.mapping_context = mapping
877 emitter.simple_key_context = simple_key
878
879 switch event.typ {
880 case yaml_ALIAS_EVENT:
881 return yaml_emitter_emit_alias(emitter, event)
882 case yaml_SCALAR_EVENT:
883 return yaml_emitter_emit_scalar(emitter, event)
884 case yaml_SEQUENCE_START_EVENT:
885 return yaml_emitter_emit_sequence_start(emitter, event)
886 case yaml_MAPPING_START_EVENT:
887 return yaml_emitter_emit_mapping_start(emitter, event)
888 default:
889 return yaml_emitter_set_emitter_error(emitter,
890 fmt.Sprintf("expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS, but got %v", event.typ))
891 }
892 }
893
894
895 func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool {
896 if !yaml_emitter_process_anchor(emitter) {
897 return false
898 }
899 emitter.state = emitter.states[len(emitter.states)-1]
900 emitter.states = emitter.states[:len(emitter.states)-1]
901 return true
902 }
903
904
905 func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool {
906 if !yaml_emitter_select_scalar_style(emitter, event) {
907 return false
908 }
909 if !yaml_emitter_process_anchor(emitter) {
910 return false
911 }
912 if !yaml_emitter_process_tag(emitter) {
913 return false
914 }
915 if !yaml_emitter_increase_indent(emitter, true, false) {
916 return false
917 }
918 if !yaml_emitter_process_scalar(emitter) {
919 return false
920 }
921 emitter.indent = emitter.indents[len(emitter.indents)-1]
922 emitter.indents = emitter.indents[:len(emitter.indents)-1]
923 emitter.state = emitter.states[len(emitter.states)-1]
924 emitter.states = emitter.states[:len(emitter.states)-1]
925 return true
926 }
927
928
929 func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
930 if !yaml_emitter_process_anchor(emitter) {
931 return false
932 }
933 if !yaml_emitter_process_tag(emitter) {
934 return false
935 }
936 if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE ||
937 yaml_emitter_check_empty_sequence(emitter) {
938 emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE
939 } else {
940 emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE
941 }
942 return true
943 }
944
945
946 func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
947 if !yaml_emitter_process_anchor(emitter) {
948 return false
949 }
950 if !yaml_emitter_process_tag(emitter) {
951 return false
952 }
953 if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE ||
954 yaml_emitter_check_empty_mapping(emitter) {
955 emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE
956 } else {
957 emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE
958 }
959 return true
960 }
961
962
963 func yaml_emitter_check_empty_document(emitter *yaml_emitter_t) bool {
964 return false
965 }
966
967
968 func yaml_emitter_check_empty_sequence(emitter *yaml_emitter_t) bool {
969 if len(emitter.events)-emitter.events_head < 2 {
970 return false
971 }
972 return emitter.events[emitter.events_head].typ == yaml_SEQUENCE_START_EVENT &&
973 emitter.events[emitter.events_head+1].typ == yaml_SEQUENCE_END_EVENT
974 }
975
976
977 func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool {
978 if len(emitter.events)-emitter.events_head < 2 {
979 return false
980 }
981 return emitter.events[emitter.events_head].typ == yaml_MAPPING_START_EVENT &&
982 emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT
983 }
984
985
986 func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool {
987 length := 0
988 switch emitter.events[emitter.events_head].typ {
989 case yaml_ALIAS_EVENT:
990 length += len(emitter.anchor_data.anchor)
991 case yaml_SCALAR_EVENT:
992 if emitter.scalar_data.multiline {
993 return false
994 }
995 length += len(emitter.anchor_data.anchor) +
996 len(emitter.tag_data.handle) +
997 len(emitter.tag_data.suffix) +
998 len(emitter.scalar_data.value)
999 case yaml_SEQUENCE_START_EVENT:
1000 if !yaml_emitter_check_empty_sequence(emitter) {
1001 return false
1002 }
1003 length += len(emitter.anchor_data.anchor) +
1004 len(emitter.tag_data.handle) +
1005 len(emitter.tag_data.suffix)
1006 case yaml_MAPPING_START_EVENT:
1007 if !yaml_emitter_check_empty_mapping(emitter) {
1008 return false
1009 }
1010 length += len(emitter.anchor_data.anchor) +
1011 len(emitter.tag_data.handle) +
1012 len(emitter.tag_data.suffix)
1013 default:
1014 return false
1015 }
1016 return length <= 128
1017 }
1018
1019
1020 func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event_t) bool {
1021
1022 no_tag := len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0
1023 if no_tag && !event.implicit && !event.quoted_implicit {
1024 return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified")
1025 }
1026
1027 style := event.scalar_style()
1028 if style == yaml_ANY_SCALAR_STYLE {
1029 style = yaml_PLAIN_SCALAR_STYLE
1030 }
1031 if emitter.canonical {
1032 style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
1033 }
1034 if emitter.simple_key_context && emitter.scalar_data.multiline {
1035 style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
1036 }
1037
1038 if style == yaml_PLAIN_SCALAR_STYLE {
1039 if emitter.flow_level > 0 && !emitter.scalar_data.flow_plain_allowed ||
1040 emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed {
1041 style = yaml_SINGLE_QUOTED_SCALAR_STYLE
1042 }
1043 if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) {
1044 style = yaml_SINGLE_QUOTED_SCALAR_STYLE
1045 }
1046 if no_tag && !event.implicit {
1047 style = yaml_SINGLE_QUOTED_SCALAR_STYLE
1048 }
1049 }
1050 if style == yaml_SINGLE_QUOTED_SCALAR_STYLE {
1051 if !emitter.scalar_data.single_quoted_allowed {
1052 style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
1053 }
1054 }
1055 if style == yaml_LITERAL_SCALAR_STYLE || style == yaml_FOLDED_SCALAR_STYLE {
1056 if !emitter.scalar_data.block_allowed || emitter.flow_level > 0 || emitter.simple_key_context {
1057 style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
1058 }
1059 }
1060
1061 if no_tag && !event.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE {
1062 emitter.tag_data.handle = []byte{'!'}
1063 }
1064 emitter.scalar_data.style = style
1065 return true
1066 }
1067
1068
1069 func yaml_emitter_process_anchor(emitter *yaml_emitter_t) bool {
1070 if emitter.anchor_data.anchor == nil {
1071 return true
1072 }
1073 c := []byte{'&'}
1074 if emitter.anchor_data.alias {
1075 c[0] = '*'
1076 }
1077 if !yaml_emitter_write_indicator(emitter, c, true, false, false) {
1078 return false
1079 }
1080 return yaml_emitter_write_anchor(emitter, emitter.anchor_data.anchor)
1081 }
1082
1083
1084 func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
1085 if len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 {
1086 return true
1087 }
1088 if len(emitter.tag_data.handle) > 0 {
1089 if !yaml_emitter_write_tag_handle(emitter, emitter.tag_data.handle) {
1090 return false
1091 }
1092 if len(emitter.tag_data.suffix) > 0 {
1093 if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
1094 return false
1095 }
1096 }
1097 } else {
1098
1099 if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) {
1100 return false
1101 }
1102 if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
1103 return false
1104 }
1105 if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) {
1106 return false
1107 }
1108 }
1109 return true
1110 }
1111
1112
1113 func yaml_emitter_process_scalar(emitter *yaml_emitter_t) bool {
1114 switch emitter.scalar_data.style {
1115 case yaml_PLAIN_SCALAR_STYLE:
1116 return yaml_emitter_write_plain_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
1117
1118 case yaml_SINGLE_QUOTED_SCALAR_STYLE:
1119 return yaml_emitter_write_single_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
1120
1121 case yaml_DOUBLE_QUOTED_SCALAR_STYLE:
1122 return yaml_emitter_write_double_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
1123
1124 case yaml_LITERAL_SCALAR_STYLE:
1125 return yaml_emitter_write_literal_scalar(emitter, emitter.scalar_data.value)
1126
1127 case yaml_FOLDED_SCALAR_STYLE:
1128 return yaml_emitter_write_folded_scalar(emitter, emitter.scalar_data.value)
1129 }
1130 panic("unknown scalar style")
1131 }
1132
1133
1134 func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
1135 if len(emitter.tail_comment) > 0 {
1136 if !yaml_emitter_write_indent(emitter) {
1137 return false
1138 }
1139 if !yaml_emitter_write_comment(emitter, emitter.tail_comment) {
1140 return false
1141 }
1142 emitter.tail_comment = emitter.tail_comment[:0]
1143 emitter.foot_indent = emitter.indent
1144 if emitter.foot_indent < 0 {
1145 emitter.foot_indent = 0
1146 }
1147 }
1148
1149 if len(emitter.head_comment) == 0 {
1150 return true
1151 }
1152 if !yaml_emitter_write_indent(emitter) {
1153 return false
1154 }
1155 if !yaml_emitter_write_comment(emitter, emitter.head_comment) {
1156 return false
1157 }
1158 emitter.head_comment = emitter.head_comment[:0]
1159 return true
1160 }
1161
1162
1163 func yaml_emitter_process_line_comment_linebreak(emitter *yaml_emitter_t, linebreak bool) bool {
1164 if len(emitter.line_comment) == 0 {
1165
1166
1167
1168
1169 if linebreak && !put_break(emitter) {
1170 return false
1171 }
1172 return true
1173 }
1174 if !emitter.whitespace {
1175 if !put(emitter, ' ') {
1176 return false
1177 }
1178 }
1179 if !yaml_emitter_write_comment(emitter, emitter.line_comment) {
1180 return false
1181 }
1182 emitter.line_comment = emitter.line_comment[:0]
1183 return true
1184 }
1185
1186
1187 func yaml_emitter_process_foot_comment(emitter *yaml_emitter_t) bool {
1188 if len(emitter.foot_comment) == 0 {
1189 return true
1190 }
1191 if !yaml_emitter_write_indent(emitter) {
1192 return false
1193 }
1194 if !yaml_emitter_write_comment(emitter, emitter.foot_comment) {
1195 return false
1196 }
1197 emitter.foot_comment = emitter.foot_comment[:0]
1198 emitter.foot_indent = emitter.indent
1199 if emitter.foot_indent < 0 {
1200 emitter.foot_indent = 0
1201 }
1202 return true
1203 }
1204
1205
1206 func yaml_emitter_analyze_version_directive(emitter *yaml_emitter_t, version_directive *yaml_version_directive_t) bool {
1207 if version_directive.major != 1 || version_directive.minor != 1 {
1208 return yaml_emitter_set_emitter_error(emitter, "incompatible %YAML directive")
1209 }
1210 return true
1211 }
1212
1213
1214 func yaml_emitter_analyze_tag_directive(emitter *yaml_emitter_t, tag_directive *yaml_tag_directive_t) bool {
1215 handle := tag_directive.handle
1216 prefix := tag_directive.prefix
1217 if len(handle) == 0 {
1218 return yaml_emitter_set_emitter_error(emitter, "tag handle must not be empty")
1219 }
1220 if handle[0] != '!' {
1221 return yaml_emitter_set_emitter_error(emitter, "tag handle must start with '!'")
1222 }
1223 if handle[len(handle)-1] != '!' {
1224 return yaml_emitter_set_emitter_error(emitter, "tag handle must end with '!'")
1225 }
1226 for i := 1; i < len(handle)-1; i += width(handle[i]) {
1227 if !is_alpha(handle, i) {
1228 return yaml_emitter_set_emitter_error(emitter, "tag handle must contain alphanumerical characters only")
1229 }
1230 }
1231 if len(prefix) == 0 {
1232 return yaml_emitter_set_emitter_error(emitter, "tag prefix must not be empty")
1233 }
1234 return true
1235 }
1236
1237
1238 func yaml_emitter_analyze_anchor(emitter *yaml_emitter_t, anchor []byte, alias bool) bool {
1239 if len(anchor) == 0 {
1240 problem := "anchor value must not be empty"
1241 if alias {
1242 problem = "alias value must not be empty"
1243 }
1244 return yaml_emitter_set_emitter_error(emitter, problem)
1245 }
1246 for i := 0; i < len(anchor); i += width(anchor[i]) {
1247 if !is_alpha(anchor, i) {
1248 problem := "anchor value must contain alphanumerical characters only"
1249 if alias {
1250 problem = "alias value must contain alphanumerical characters only"
1251 }
1252 return yaml_emitter_set_emitter_error(emitter, problem)
1253 }
1254 }
1255 emitter.anchor_data.anchor = anchor
1256 emitter.anchor_data.alias = alias
1257 return true
1258 }
1259
1260
1261 func yaml_emitter_analyze_tag(emitter *yaml_emitter_t, tag []byte) bool {
1262 if len(tag) == 0 {
1263 return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty")
1264 }
1265 for i := 0; i < len(emitter.tag_directives); i++ {
1266 tag_directive := &emitter.tag_directives[i]
1267 if bytes.HasPrefix(tag, tag_directive.prefix) {
1268 emitter.tag_data.handle = tag_directive.handle
1269 emitter.tag_data.suffix = tag[len(tag_directive.prefix):]
1270 return true
1271 }
1272 }
1273 emitter.tag_data.suffix = tag
1274 return true
1275 }
1276
1277
1278 func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool {
1279 var (
1280 block_indicators = false
1281 flow_indicators = false
1282 line_breaks = false
1283 special_characters = false
1284 tab_characters = false
1285
1286 leading_space = false
1287 leading_break = false
1288 trailing_space = false
1289 trailing_break = false
1290 break_space = false
1291 space_break = false
1292
1293 preceded_by_whitespace = false
1294 followed_by_whitespace = false
1295 previous_space = false
1296 previous_break = false
1297 )
1298
1299 emitter.scalar_data.value = value
1300
1301 if len(value) == 0 {
1302 emitter.scalar_data.multiline = false
1303 emitter.scalar_data.flow_plain_allowed = false
1304 emitter.scalar_data.block_plain_allowed = true
1305 emitter.scalar_data.single_quoted_allowed = true
1306 emitter.scalar_data.block_allowed = false
1307 return true
1308 }
1309
1310 if len(value) >= 3 && ((value[0] == '-' && value[1] == '-' && value[2] == '-') || (value[0] == '.' && value[1] == '.' && value[2] == '.')) {
1311 block_indicators = true
1312 flow_indicators = true
1313 }
1314
1315 preceded_by_whitespace = true
1316 for i, w := 0, 0; i < len(value); i += w {
1317 w = width(value[i])
1318 followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w)
1319
1320 if i == 0 {
1321 switch value[i] {
1322 case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`':
1323 flow_indicators = true
1324 block_indicators = true
1325 case '?', ':':
1326 flow_indicators = true
1327 if followed_by_whitespace {
1328 block_indicators = true
1329 }
1330 case '-':
1331 if followed_by_whitespace {
1332 flow_indicators = true
1333 block_indicators = true
1334 }
1335 }
1336 } else {
1337 switch value[i] {
1338 case ',', '?', '[', ']', '{', '}':
1339 flow_indicators = true
1340 case ':':
1341 flow_indicators = true
1342 if followed_by_whitespace {
1343 block_indicators = true
1344 }
1345 case '#':
1346 if preceded_by_whitespace {
1347 flow_indicators = true
1348 block_indicators = true
1349 }
1350 }
1351 }
1352
1353 if value[i] == '\t' {
1354 tab_characters = true
1355 } else if !is_printable(value, i) || !is_ascii(value, i) && !emitter.unicode {
1356 special_characters = true
1357 }
1358 if is_space(value, i) {
1359 if i == 0 {
1360 leading_space = true
1361 }
1362 if i+width(value[i]) == len(value) {
1363 trailing_space = true
1364 }
1365 if previous_break {
1366 break_space = true
1367 }
1368 previous_space = true
1369 previous_break = false
1370 } else if is_break(value, i) {
1371 line_breaks = true
1372 if i == 0 {
1373 leading_break = true
1374 }
1375 if i+width(value[i]) == len(value) {
1376 trailing_break = true
1377 }
1378 if previous_space {
1379 space_break = true
1380 }
1381 previous_space = false
1382 previous_break = true
1383 } else {
1384 previous_space = false
1385 previous_break = false
1386 }
1387
1388
1389 preceded_by_whitespace = is_blankz(value, i)
1390 }
1391
1392 emitter.scalar_data.multiline = line_breaks
1393 emitter.scalar_data.flow_plain_allowed = true
1394 emitter.scalar_data.block_plain_allowed = true
1395 emitter.scalar_data.single_quoted_allowed = true
1396 emitter.scalar_data.block_allowed = true
1397
1398 if leading_space || leading_break || trailing_space || trailing_break {
1399 emitter.scalar_data.flow_plain_allowed = false
1400 emitter.scalar_data.block_plain_allowed = false
1401 }
1402 if trailing_space {
1403 emitter.scalar_data.block_allowed = false
1404 }
1405 if break_space {
1406 emitter.scalar_data.flow_plain_allowed = false
1407 emitter.scalar_data.block_plain_allowed = false
1408 emitter.scalar_data.single_quoted_allowed = false
1409 }
1410 if space_break || tab_characters || special_characters {
1411 emitter.scalar_data.flow_plain_allowed = false
1412 emitter.scalar_data.block_plain_allowed = false
1413 emitter.scalar_data.single_quoted_allowed = false
1414 }
1415 if space_break || special_characters {
1416 emitter.scalar_data.block_allowed = false
1417 }
1418 if line_breaks {
1419 emitter.scalar_data.flow_plain_allowed = false
1420 emitter.scalar_data.block_plain_allowed = false
1421 }
1422 if flow_indicators {
1423 emitter.scalar_data.flow_plain_allowed = false
1424 }
1425 if block_indicators {
1426 emitter.scalar_data.block_plain_allowed = false
1427 }
1428 return true
1429 }
1430
1431
1432 func yaml_emitter_analyze_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
1433
1434 emitter.anchor_data.anchor = nil
1435 emitter.tag_data.handle = nil
1436 emitter.tag_data.suffix = nil
1437 emitter.scalar_data.value = nil
1438
1439 if len(event.head_comment) > 0 {
1440 emitter.head_comment = event.head_comment
1441 }
1442 if len(event.line_comment) > 0 {
1443 emitter.line_comment = event.line_comment
1444 }
1445 if len(event.foot_comment) > 0 {
1446 emitter.foot_comment = event.foot_comment
1447 }
1448 if len(event.tail_comment) > 0 {
1449 emitter.tail_comment = event.tail_comment
1450 }
1451
1452 switch event.typ {
1453 case yaml_ALIAS_EVENT:
1454 if !yaml_emitter_analyze_anchor(emitter, event.anchor, true) {
1455 return false
1456 }
1457
1458 case yaml_SCALAR_EVENT:
1459 if len(event.anchor) > 0 {
1460 if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
1461 return false
1462 }
1463 }
1464 if len(event.tag) > 0 && (emitter.canonical || (!event.implicit && !event.quoted_implicit)) {
1465 if !yaml_emitter_analyze_tag(emitter, event.tag) {
1466 return false
1467 }
1468 }
1469 if !yaml_emitter_analyze_scalar(emitter, event.value) {
1470 return false
1471 }
1472
1473 case yaml_SEQUENCE_START_EVENT:
1474 if len(event.anchor) > 0 {
1475 if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
1476 return false
1477 }
1478 }
1479 if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
1480 if !yaml_emitter_analyze_tag(emitter, event.tag) {
1481 return false
1482 }
1483 }
1484
1485 case yaml_MAPPING_START_EVENT:
1486 if len(event.anchor) > 0 {
1487 if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
1488 return false
1489 }
1490 }
1491 if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
1492 if !yaml_emitter_analyze_tag(emitter, event.tag) {
1493 return false
1494 }
1495 }
1496 }
1497 return true
1498 }
1499
1500
1501 func yaml_emitter_write_bom(emitter *yaml_emitter_t) bool {
1502 if !flush(emitter) {
1503 return false
1504 }
1505 pos := emitter.buffer_pos
1506 emitter.buffer[pos+0] = '\xEF'
1507 emitter.buffer[pos+1] = '\xBB'
1508 emitter.buffer[pos+2] = '\xBF'
1509 emitter.buffer_pos += 3
1510 return true
1511 }
1512
1513 func yaml_emitter_write_indent(emitter *yaml_emitter_t) bool {
1514 indent := emitter.indent
1515 if indent < 0 {
1516 indent = 0
1517 }
1518 if !emitter.indention || emitter.column > indent || (emitter.column == indent && !emitter.whitespace) {
1519 if !put_break(emitter) {
1520 return false
1521 }
1522 }
1523 if emitter.foot_indent == indent {
1524 if !put_break(emitter) {
1525 return false
1526 }
1527 }
1528 for emitter.column < indent {
1529 if !put(emitter, ' ') {
1530 return false
1531 }
1532 }
1533 emitter.whitespace = true
1534
1535 emitter.space_above = false
1536 emitter.foot_indent = -1
1537 return true
1538 }
1539
1540 func yaml_emitter_write_indicator(emitter *yaml_emitter_t, indicator []byte, need_whitespace, is_whitespace, is_indention bool) bool {
1541 if need_whitespace && !emitter.whitespace {
1542 if !put(emitter, ' ') {
1543 return false
1544 }
1545 }
1546 if !write_all(emitter, indicator) {
1547 return false
1548 }
1549 emitter.whitespace = is_whitespace
1550 emitter.indention = (emitter.indention && is_indention)
1551 emitter.open_ended = false
1552 return true
1553 }
1554
1555 func yaml_emitter_write_anchor(emitter *yaml_emitter_t, value []byte) bool {
1556 if !write_all(emitter, value) {
1557 return false
1558 }
1559 emitter.whitespace = false
1560 emitter.indention = false
1561 return true
1562 }
1563
1564 func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool {
1565 if !emitter.whitespace {
1566 if !put(emitter, ' ') {
1567 return false
1568 }
1569 }
1570 if !write_all(emitter, value) {
1571 return false
1572 }
1573 emitter.whitespace = false
1574 emitter.indention = false
1575 return true
1576 }
1577
1578 func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool {
1579 if need_whitespace && !emitter.whitespace {
1580 if !put(emitter, ' ') {
1581 return false
1582 }
1583 }
1584 for i := 0; i < len(value); {
1585 var must_write bool
1586 switch value[i] {
1587 case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']':
1588 must_write = true
1589 default:
1590 must_write = is_alpha(value, i)
1591 }
1592 if must_write {
1593 if !write(emitter, value, &i) {
1594 return false
1595 }
1596 } else {
1597 w := width(value[i])
1598 for k := 0; k < w; k++ {
1599 octet := value[i]
1600 i++
1601 if !put(emitter, '%') {
1602 return false
1603 }
1604
1605 c := octet >> 4
1606 if c < 10 {
1607 c += '0'
1608 } else {
1609 c += 'A' - 10
1610 }
1611 if !put(emitter, c) {
1612 return false
1613 }
1614
1615 c = octet & 0x0f
1616 if c < 10 {
1617 c += '0'
1618 } else {
1619 c += 'A' - 10
1620 }
1621 if !put(emitter, c) {
1622 return false
1623 }
1624 }
1625 }
1626 }
1627 emitter.whitespace = false
1628 emitter.indention = false
1629 return true
1630 }
1631
1632 func yaml_emitter_write_plain_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
1633 if len(value) > 0 && !emitter.whitespace {
1634 if !put(emitter, ' ') {
1635 return false
1636 }
1637 }
1638
1639 spaces := false
1640 breaks := false
1641 for i := 0; i < len(value); {
1642 if is_space(value, i) {
1643 if allow_breaks && !spaces && emitter.column > emitter.best_width && !is_space(value, i+1) {
1644 if !yaml_emitter_write_indent(emitter) {
1645 return false
1646 }
1647 i += width(value[i])
1648 } else {
1649 if !write(emitter, value, &i) {
1650 return false
1651 }
1652 }
1653 spaces = true
1654 } else if is_break(value, i) {
1655 if !breaks && value[i] == '\n' {
1656 if !put_break(emitter) {
1657 return false
1658 }
1659 }
1660 if !write_break(emitter, value, &i) {
1661 return false
1662 }
1663
1664 breaks = true
1665 } else {
1666 if breaks {
1667 if !yaml_emitter_write_indent(emitter) {
1668 return false
1669 }
1670 }
1671 if !write(emitter, value, &i) {
1672 return false
1673 }
1674 emitter.indention = false
1675 spaces = false
1676 breaks = false
1677 }
1678 }
1679
1680 if len(value) > 0 {
1681 emitter.whitespace = false
1682 }
1683 emitter.indention = false
1684 if emitter.root_context {
1685 emitter.open_ended = true
1686 }
1687
1688 return true
1689 }
1690
1691 func yaml_emitter_write_single_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
1692
1693 if !yaml_emitter_write_indicator(emitter, []byte{'\''}, true, false, false) {
1694 return false
1695 }
1696
1697 spaces := false
1698 breaks := false
1699 for i := 0; i < len(value); {
1700 if is_space(value, i) {
1701 if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 && !is_space(value, i+1) {
1702 if !yaml_emitter_write_indent(emitter) {
1703 return false
1704 }
1705 i += width(value[i])
1706 } else {
1707 if !write(emitter, value, &i) {
1708 return false
1709 }
1710 }
1711 spaces = true
1712 } else if is_break(value, i) {
1713 if !breaks && value[i] == '\n' {
1714 if !put_break(emitter) {
1715 return false
1716 }
1717 }
1718 if !write_break(emitter, value, &i) {
1719 return false
1720 }
1721
1722 breaks = true
1723 } else {
1724 if breaks {
1725 if !yaml_emitter_write_indent(emitter) {
1726 return false
1727 }
1728 }
1729 if value[i] == '\'' {
1730 if !put(emitter, '\'') {
1731 return false
1732 }
1733 }
1734 if !write(emitter, value, &i) {
1735 return false
1736 }
1737 emitter.indention = false
1738 spaces = false
1739 breaks = false
1740 }
1741 }
1742 if !yaml_emitter_write_indicator(emitter, []byte{'\''}, false, false, false) {
1743 return false
1744 }
1745 emitter.whitespace = false
1746 emitter.indention = false
1747 return true
1748 }
1749
1750 func yaml_emitter_write_double_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
1751 spaces := false
1752 if !yaml_emitter_write_indicator(emitter, []byte{'"'}, true, false, false) {
1753 return false
1754 }
1755
1756 for i := 0; i < len(value); {
1757 if !is_printable(value, i) || (!emitter.unicode && !is_ascii(value, i)) ||
1758 is_bom(value, i) || is_break(value, i) ||
1759 value[i] == '"' || value[i] == '\\' {
1760
1761 octet := value[i]
1762
1763 var w int
1764 var v rune
1765 switch {
1766 case octet&0x80 == 0x00:
1767 w, v = 1, rune(octet&0x7F)
1768 case octet&0xE0 == 0xC0:
1769 w, v = 2, rune(octet&0x1F)
1770 case octet&0xF0 == 0xE0:
1771 w, v = 3, rune(octet&0x0F)
1772 case octet&0xF8 == 0xF0:
1773 w, v = 4, rune(octet&0x07)
1774 }
1775 for k := 1; k < w; k++ {
1776 octet = value[i+k]
1777 v = (v << 6) + (rune(octet) & 0x3F)
1778 }
1779 i += w
1780
1781 if !put(emitter, '\\') {
1782 return false
1783 }
1784
1785 var ok bool
1786 switch v {
1787 case 0x00:
1788 ok = put(emitter, '0')
1789 case 0x07:
1790 ok = put(emitter, 'a')
1791 case 0x08:
1792 ok = put(emitter, 'b')
1793 case 0x09:
1794 ok = put(emitter, 't')
1795 case 0x0A:
1796 ok = put(emitter, 'n')
1797 case 0x0b:
1798 ok = put(emitter, 'v')
1799 case 0x0c:
1800 ok = put(emitter, 'f')
1801 case 0x0d:
1802 ok = put(emitter, 'r')
1803 case 0x1b:
1804 ok = put(emitter, 'e')
1805 case 0x22:
1806 ok = put(emitter, '"')
1807 case 0x5c:
1808 ok = put(emitter, '\\')
1809 case 0x85:
1810 ok = put(emitter, 'N')
1811 case 0xA0:
1812 ok = put(emitter, '_')
1813 case 0x2028:
1814 ok = put(emitter, 'L')
1815 case 0x2029:
1816 ok = put(emitter, 'P')
1817 default:
1818 if v <= 0xFF {
1819 ok = put(emitter, 'x')
1820 w = 2
1821 } else if v <= 0xFFFF {
1822 ok = put(emitter, 'u')
1823 w = 4
1824 } else {
1825 ok = put(emitter, 'U')
1826 w = 8
1827 }
1828 for k := (w - 1) * 4; ok && k >= 0; k -= 4 {
1829 digit := byte((v >> uint(k)) & 0x0F)
1830 if digit < 10 {
1831 ok = put(emitter, digit+'0')
1832 } else {
1833 ok = put(emitter, digit+'A'-10)
1834 }
1835 }
1836 }
1837 if !ok {
1838 return false
1839 }
1840 spaces = false
1841 } else if is_space(value, i) {
1842 if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 {
1843 if !yaml_emitter_write_indent(emitter) {
1844 return false
1845 }
1846 if is_space(value, i+1) {
1847 if !put(emitter, '\\') {
1848 return false
1849 }
1850 }
1851 i += width(value[i])
1852 } else if !write(emitter, value, &i) {
1853 return false
1854 }
1855 spaces = true
1856 } else {
1857 if !write(emitter, value, &i) {
1858 return false
1859 }
1860 spaces = false
1861 }
1862 }
1863 if !yaml_emitter_write_indicator(emitter, []byte{'"'}, false, false, false) {
1864 return false
1865 }
1866 emitter.whitespace = false
1867 emitter.indention = false
1868 return true
1869 }
1870
1871 func yaml_emitter_write_block_scalar_hints(emitter *yaml_emitter_t, value []byte) bool {
1872 if is_space(value, 0) || is_break(value, 0) {
1873 indent_hint := []byte{'0' + byte(emitter.best_indent)}
1874 if !yaml_emitter_write_indicator(emitter, indent_hint, false, false, false) {
1875 return false
1876 }
1877 }
1878
1879 emitter.open_ended = false
1880
1881 var chomp_hint [1]byte
1882 if len(value) == 0 {
1883 chomp_hint[0] = '-'
1884 } else {
1885 i := len(value) - 1
1886 for value[i]&0xC0 == 0x80 {
1887 i--
1888 }
1889 if !is_break(value, i) {
1890 chomp_hint[0] = '-'
1891 } else if i == 0 {
1892 chomp_hint[0] = '+'
1893 emitter.open_ended = true
1894 } else {
1895 i--
1896 for value[i]&0xC0 == 0x80 {
1897 i--
1898 }
1899 if is_break(value, i) {
1900 chomp_hint[0] = '+'
1901 emitter.open_ended = true
1902 }
1903 }
1904 }
1905 if chomp_hint[0] != 0 {
1906 if !yaml_emitter_write_indicator(emitter, chomp_hint[:], false, false, false) {
1907 return false
1908 }
1909 }
1910 return true
1911 }
1912
1913 func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bool {
1914 if !yaml_emitter_write_indicator(emitter, []byte{'|'}, true, false, false) {
1915 return false
1916 }
1917 if !yaml_emitter_write_block_scalar_hints(emitter, value) {
1918 return false
1919 }
1920 if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
1921 return false
1922 }
1923
1924 emitter.whitespace = true
1925 breaks := true
1926 for i := 0; i < len(value); {
1927 if is_break(value, i) {
1928 if !write_break(emitter, value, &i) {
1929 return false
1930 }
1931
1932 breaks = true
1933 } else {
1934 if breaks {
1935 if !yaml_emitter_write_indent(emitter) {
1936 return false
1937 }
1938 }
1939 if !write(emitter, value, &i) {
1940 return false
1941 }
1942 emitter.indention = false
1943 breaks = false
1944 }
1945 }
1946
1947 return true
1948 }
1949
1950 func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) bool {
1951 if !yaml_emitter_write_indicator(emitter, []byte{'>'}, true, false, false) {
1952 return false
1953 }
1954 if !yaml_emitter_write_block_scalar_hints(emitter, value) {
1955 return false
1956 }
1957 if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
1958 return false
1959 }
1960
1961
1962 emitter.whitespace = true
1963
1964 breaks := true
1965 leading_spaces := true
1966 for i := 0; i < len(value); {
1967 if is_break(value, i) {
1968 if !breaks && !leading_spaces && value[i] == '\n' {
1969 k := 0
1970 for is_break(value, k) {
1971 k += width(value[k])
1972 }
1973 if !is_blankz(value, k) {
1974 if !put_break(emitter) {
1975 return false
1976 }
1977 }
1978 }
1979 if !write_break(emitter, value, &i) {
1980 return false
1981 }
1982
1983 breaks = true
1984 } else {
1985 if breaks {
1986 if !yaml_emitter_write_indent(emitter) {
1987 return false
1988 }
1989 leading_spaces = is_blank(value, i)
1990 }
1991 if !breaks && is_space(value, i) && !is_space(value, i+1) && emitter.column > emitter.best_width {
1992 if !yaml_emitter_write_indent(emitter) {
1993 return false
1994 }
1995 i += width(value[i])
1996 } else {
1997 if !write(emitter, value, &i) {
1998 return false
1999 }
2000 }
2001 emitter.indention = false
2002 breaks = false
2003 }
2004 }
2005 return true
2006 }
2007
2008 func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool {
2009 breaks := false
2010 pound := false
2011 for i := 0; i < len(comment); {
2012 if is_break(comment, i) {
2013 if !write_break(emitter, comment, &i) {
2014 return false
2015 }
2016
2017 breaks = true
2018 pound = false
2019 } else {
2020 if breaks && !yaml_emitter_write_indent(emitter) {
2021 return false
2022 }
2023 if !pound {
2024 if comment[i] != '#' && (!put(emitter, '#') || !put(emitter, ' ')) {
2025 return false
2026 }
2027 pound = true
2028 }
2029 if !write(emitter, comment, &i) {
2030 return false
2031 }
2032 emitter.indention = false
2033 breaks = false
2034 }
2035 }
2036 if !breaks && !put_break(emitter) {
2037 return false
2038 }
2039
2040 emitter.whitespace = true
2041
2042 return true
2043 }
2044
View as plain text