github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/encoding/kmgYaml/yaml_emitter_emit.go (about) 1 package kmgYaml 2 3 // Emit an event. 4 func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool { 5 emitter.events = append(emitter.events, *event) 6 for !yaml_emitter_need_more_events(emitter) { 7 event := &emitter.events[emitter.events_head] 8 if !yaml_emitter_analyze_event(emitter, event) { 9 return false 10 } 11 if !yaml_emitter_state_machine(emitter, event) { 12 return false 13 } 14 yaml_event_delete(event) 15 emitter.events_head++ 16 } 17 return true 18 } 19 20 // Expect STREAM-START. 21 func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { 22 if event.typ != yaml_STREAM_START_EVENT { 23 return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START") 24 } 25 if emitter.encoding == yaml_ANY_ENCODING { 26 emitter.encoding = event.encoding 27 if emitter.encoding == yaml_ANY_ENCODING { 28 emitter.encoding = yaml_UTF8_ENCODING 29 } 30 } 31 if emitter.best_indent < 2 || emitter.best_indent > 9 { 32 emitter.best_indent = 2 33 } 34 if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 { 35 emitter.best_width = 80 36 } 37 if emitter.best_width < 0 { 38 emitter.best_width = 1<<31 - 1 39 } 40 if emitter.line_break == yaml_ANY_BREAK { 41 emitter.line_break = yaml_LN_BREAK 42 } 43 44 emitter.indent = -1 45 emitter.line = 0 46 emitter.column = 0 47 emitter.whitespace = true 48 emitter.indention = true 49 50 if emitter.encoding != yaml_UTF8_ENCODING { 51 if !yaml_emitter_write_bom(emitter) { 52 return false 53 } 54 } 55 emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE 56 return true 57 } 58 59 // Expect DOCUMENT-START or STREAM-END. 60 func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { 61 62 if event.typ == yaml_DOCUMENT_START_EVENT { 63 64 if event.version_directive != nil { 65 if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) { 66 return false 67 } 68 } 69 70 for i := 0; i < len(event.tag_directives); i++ { 71 tag_directive := &event.tag_directives[i] 72 if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) { 73 return false 74 } 75 if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) { 76 return false 77 } 78 } 79 80 for i := 0; i < len(default_tag_directives); i++ { 81 tag_directive := &default_tag_directives[i] 82 if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) { 83 return false 84 } 85 } 86 87 implicit := event.implicit 88 if !first || emitter.canonical { 89 implicit = false 90 } 91 92 if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) { 93 if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { 94 return false 95 } 96 if !yaml_emitter_write_indent(emitter) { 97 return false 98 } 99 } 100 101 if event.version_directive != nil { 102 implicit = false 103 if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) { 104 return false 105 } 106 if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) { 107 return false 108 } 109 if !yaml_emitter_write_indent(emitter) { 110 return false 111 } 112 } 113 114 if len(event.tag_directives) > 0 { 115 implicit = false 116 for i := 0; i < len(event.tag_directives); i++ { 117 tag_directive := &event.tag_directives[i] 118 if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) { 119 return false 120 } 121 if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) { 122 return false 123 } 124 if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) { 125 return false 126 } 127 if !yaml_emitter_write_indent(emitter) { 128 return false 129 } 130 } 131 } 132 133 if yaml_emitter_check_empty_document(emitter) { 134 implicit = false 135 } 136 if !implicit { 137 if !yaml_emitter_write_indent(emitter) { 138 return false 139 } 140 if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) { 141 return false 142 } 143 if emitter.canonical { 144 if !yaml_emitter_write_indent(emitter) { 145 return false 146 } 147 } 148 } 149 150 emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE 151 return true 152 } 153 154 if event.typ == yaml_STREAM_END_EVENT { 155 if emitter.open_ended { 156 if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { 157 return false 158 } 159 if !yaml_emitter_write_indent(emitter) { 160 return false 161 } 162 } 163 if !yaml_emitter_flush(emitter) { 164 return false 165 } 166 emitter.state = yaml_EMIT_END_STATE 167 return true 168 } 169 170 return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END") 171 } 172 173 // Expect the root node. 174 func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool { 175 emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE) 176 return yaml_emitter_emit_node(emitter, event, true, false, false, false) 177 } 178 179 // Expect DOCUMENT-END. 180 func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool { 181 if event.typ != yaml_DOCUMENT_END_EVENT { 182 return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END") 183 } 184 if !yaml_emitter_write_indent(emitter) { 185 return false 186 } 187 if !event.implicit { 188 // [Go] Allocate the slice elsewhere. 189 if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { 190 return false 191 } 192 if !yaml_emitter_write_indent(emitter) { 193 return false 194 } 195 } 196 if !yaml_emitter_flush(emitter) { 197 return false 198 } 199 emitter.state = yaml_EMIT_DOCUMENT_START_STATE 200 emitter.tag_directives = emitter.tag_directives[:0] 201 return true 202 } 203 204 // Expect a flow item node. 205 func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { 206 if first { 207 if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) { 208 return false 209 } 210 if !yaml_emitter_increase_indent(emitter, true, false) { 211 return false 212 } 213 emitter.flow_level++ 214 } 215 216 if event.typ == yaml_SEQUENCE_END_EVENT { 217 emitter.flow_level-- 218 emitter.indent = emitter.indents[len(emitter.indents)-1] 219 emitter.indents = emitter.indents[:len(emitter.indents)-1] 220 if emitter.canonical && !first { 221 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { 222 return false 223 } 224 if !yaml_emitter_write_indent(emitter) { 225 return false 226 } 227 } 228 if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) { 229 return false 230 } 231 emitter.state = emitter.states[len(emitter.states)-1] 232 emitter.states = emitter.states[:len(emitter.states)-1] 233 234 return true 235 } 236 237 if !first { 238 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { 239 return false 240 } 241 } 242 243 if emitter.canonical || emitter.column > emitter.best_width { 244 if !yaml_emitter_write_indent(emitter) { 245 return false 246 } 247 } 248 emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE) 249 return yaml_emitter_emit_node(emitter, event, false, true, false, false) 250 } 251 252 // Expect a flow key node. 253 func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { 254 if first { 255 if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) { 256 return false 257 } 258 if !yaml_emitter_increase_indent(emitter, true, false) { 259 return false 260 } 261 emitter.flow_level++ 262 } 263 264 if event.typ == yaml_MAPPING_END_EVENT { 265 emitter.flow_level-- 266 emitter.indent = emitter.indents[len(emitter.indents)-1] 267 emitter.indents = emitter.indents[:len(emitter.indents)-1] 268 if emitter.canonical && !first { 269 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { 270 return false 271 } 272 if !yaml_emitter_write_indent(emitter) { 273 return false 274 } 275 } 276 if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) { 277 return false 278 } 279 emitter.state = emitter.states[len(emitter.states)-1] 280 emitter.states = emitter.states[:len(emitter.states)-1] 281 return true 282 } 283 284 if !first { 285 if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) { 286 return false 287 } 288 } 289 if emitter.canonical || emitter.column > emitter.best_width { 290 if !yaml_emitter_write_indent(emitter) { 291 return false 292 } 293 } 294 295 if !emitter.canonical && yaml_emitter_check_simple_key(emitter) { 296 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE) 297 return yaml_emitter_emit_node(emitter, event, false, false, true, true) 298 } 299 if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) { 300 return false 301 } 302 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE) 303 return yaml_emitter_emit_node(emitter, event, false, false, true, false) 304 } 305 306 // Expect a flow value node. 307 func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool { 308 if simple { 309 if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) { 310 return false 311 } 312 } else { 313 if emitter.canonical || emitter.column > emitter.best_width { 314 if !yaml_emitter_write_indent(emitter) { 315 return false 316 } 317 } 318 if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) { 319 return false 320 } 321 } 322 emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE) 323 return yaml_emitter_emit_node(emitter, event, false, false, true, false) 324 } 325 326 // Expect a block item node. 327 func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { 328 if first { 329 if !yaml_emitter_increase_indent(emitter, false, emitter.mapping_context && !emitter.indention) { 330 return false 331 } 332 } 333 if event.typ == yaml_SEQUENCE_END_EVENT { 334 emitter.indent = emitter.indents[len(emitter.indents)-1] 335 emitter.indents = emitter.indents[:len(emitter.indents)-1] 336 emitter.state = emitter.states[len(emitter.states)-1] 337 emitter.states = emitter.states[:len(emitter.states)-1] 338 return true 339 } 340 if !yaml_emitter_write_indent(emitter) { 341 return false 342 } 343 if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) { 344 return false 345 } 346 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE) 347 return yaml_emitter_emit_node(emitter, event, false, true, false, false) 348 } 349 350 // Expect a block key node. 351 func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { 352 if first { 353 if !yaml_emitter_increase_indent(emitter, false, false) { 354 return false 355 } 356 } 357 if event.typ == yaml_MAPPING_END_EVENT { 358 emitter.indent = emitter.indents[len(emitter.indents)-1] 359 emitter.indents = emitter.indents[:len(emitter.indents)-1] 360 emitter.state = emitter.states[len(emitter.states)-1] 361 emitter.states = emitter.states[:len(emitter.states)-1] 362 return true 363 } 364 if !yaml_emitter_write_indent(emitter) { 365 return false 366 } 367 if yaml_emitter_check_simple_key(emitter) { 368 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE) 369 return yaml_emitter_emit_node(emitter, event, false, false, true, true) 370 } 371 if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) { 372 return false 373 } 374 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE) 375 return yaml_emitter_emit_node(emitter, event, false, false, true, false) 376 } 377 378 // Expect a block value node. 379 func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool { 380 if simple { 381 if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) { 382 return false 383 } 384 } else { 385 if !yaml_emitter_write_indent(emitter) { 386 return false 387 } 388 if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) { 389 return false 390 } 391 } 392 emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE) 393 return yaml_emitter_emit_node(emitter, event, false, false, true, false) 394 } 395 396 // Expect a node. 397 func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t, 398 root bool, sequence bool, mapping bool, simple_key bool) bool { 399 400 emitter.root_context = root 401 emitter.sequence_context = sequence 402 emitter.mapping_context = mapping 403 emitter.simple_key_context = simple_key 404 405 switch event.typ { 406 case yaml_ALIAS_EVENT: 407 return yaml_emitter_emit_alias(emitter, event) 408 case yaml_SCALAR_EVENT: 409 return yaml_emitter_emit_scalar(emitter, event) 410 case yaml_SEQUENCE_START_EVENT: 411 return yaml_emitter_emit_sequence_start(emitter, event) 412 case yaml_MAPPING_START_EVENT: 413 return yaml_emitter_emit_mapping_start(emitter, event) 414 default: 415 return yaml_emitter_set_emitter_error(emitter, 416 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS") 417 } 418 } 419 420 // Expect ALIAS. 421 func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool { 422 if !yaml_emitter_process_anchor(emitter) { 423 return false 424 } 425 emitter.state = emitter.states[len(emitter.states)-1] 426 emitter.states = emitter.states[:len(emitter.states)-1] 427 return true 428 } 429 430 // Expect SCALAR. 431 func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool { 432 if !yaml_emitter_select_scalar_style(emitter, event) { 433 return false 434 } 435 if !yaml_emitter_process_anchor(emitter) { 436 return false 437 } 438 if !yaml_emitter_process_tag(emitter) { 439 return false 440 } 441 if !yaml_emitter_increase_indent(emitter, true, false) { 442 return false 443 } 444 if !yaml_emitter_process_scalar(emitter) { 445 return false 446 } 447 emitter.indent = emitter.indents[len(emitter.indents)-1] 448 emitter.indents = emitter.indents[:len(emitter.indents)-1] 449 emitter.state = emitter.states[len(emitter.states)-1] 450 emitter.states = emitter.states[:len(emitter.states)-1] 451 return true 452 } 453 454 // Expect SEQUENCE-START. 455 func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { 456 if !yaml_emitter_process_anchor(emitter) { 457 return false 458 } 459 if !yaml_emitter_process_tag(emitter) { 460 return false 461 } 462 if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE || 463 yaml_emitter_check_empty_sequence(emitter) { 464 emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE 465 } else { 466 emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE 467 } 468 return true 469 } 470 471 // Expect MAPPING-START. 472 func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool { 473 if !yaml_emitter_process_anchor(emitter) { 474 return false 475 } 476 if !yaml_emitter_process_tag(emitter) { 477 return false 478 } 479 if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE || 480 yaml_emitter_check_empty_mapping(emitter) { 481 emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE 482 } else { 483 emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE 484 } 485 return true 486 }