| 1 | |
|---|
| 2 | #include "yaml_private.h" |
|---|
| 3 | |
|---|
| 4 | /* |
|---|
| 5 | * API functions. |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | YAML_DECLARE(int) |
|---|
| 9 | yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 10 | |
|---|
| 11 | /* |
|---|
| 12 | * Utility functions. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | static int |
|---|
| 16 | yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem); |
|---|
| 17 | |
|---|
| 18 | static int |
|---|
| 19 | yaml_emitter_need_more_events(yaml_emitter_t *emitter); |
|---|
| 20 | |
|---|
| 21 | static int |
|---|
| 22 | yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, |
|---|
| 23 | yaml_tag_directive_t value, int allow_duplicates); |
|---|
| 24 | |
|---|
| 25 | static int |
|---|
| 26 | yaml_emitter_increase_indent(yaml_emitter_t *emitter, |
|---|
| 27 | int flow, int indentless); |
|---|
| 28 | |
|---|
| 29 | /* |
|---|
| 30 | * State functions. |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | static int |
|---|
| 34 | yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 35 | |
|---|
| 36 | static int |
|---|
| 37 | yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, |
|---|
| 38 | yaml_event_t *event); |
|---|
| 39 | |
|---|
| 40 | static int |
|---|
| 41 | yaml_emitter_emit_document_start(yaml_emitter_t *emitter, |
|---|
| 42 | yaml_event_t *event, int first); |
|---|
| 43 | |
|---|
| 44 | static int |
|---|
| 45 | yaml_emitter_emit_document_content(yaml_emitter_t *emitter, |
|---|
| 46 | yaml_event_t *event); |
|---|
| 47 | |
|---|
| 48 | static int |
|---|
| 49 | yaml_emitter_emit_document_end(yaml_emitter_t *emitter, |
|---|
| 50 | yaml_event_t *event); |
|---|
| 51 | |
|---|
| 52 | static int |
|---|
| 53 | yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, |
|---|
| 54 | yaml_event_t *event, int first); |
|---|
| 55 | |
|---|
| 56 | static int |
|---|
| 57 | yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, |
|---|
| 58 | yaml_event_t *event, int first); |
|---|
| 59 | |
|---|
| 60 | static int |
|---|
| 61 | yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, |
|---|
| 62 | yaml_event_t *event, int simple); |
|---|
| 63 | |
|---|
| 64 | static int |
|---|
| 65 | yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, |
|---|
| 66 | yaml_event_t *event, int first); |
|---|
| 67 | |
|---|
| 68 | static int |
|---|
| 69 | yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, |
|---|
| 70 | yaml_event_t *event, int first); |
|---|
| 71 | |
|---|
| 72 | static int |
|---|
| 73 | yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, |
|---|
| 74 | yaml_event_t *event, int simple); |
|---|
| 75 | |
|---|
| 76 | static int |
|---|
| 77 | yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, |
|---|
| 78 | int root, int sequence, int mapping, int simple_key); |
|---|
| 79 | |
|---|
| 80 | static int |
|---|
| 81 | yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 82 | |
|---|
| 83 | static int |
|---|
| 84 | yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 85 | |
|---|
| 86 | static int |
|---|
| 87 | yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 88 | |
|---|
| 89 | static int |
|---|
| 90 | yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event); |
|---|
| 91 | |
|---|
| 92 | /* |
|---|
| 93 | * Checkers. |
|---|
| 94 | */ |
|---|
| 95 | |
|---|
| 96 | static int |
|---|
| 97 | yaml_emitter_check_empty_document(yaml_emitter_t *emitter); |
|---|
| 98 | |
|---|
| 99 | static int |
|---|
| 100 | yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter); |
|---|
| 101 | |
|---|
| 102 | static int |
|---|
| 103 | yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter); |
|---|
| 104 | |
|---|
| 105 | static int |
|---|
| 106 | yaml_emitter_check_simple_key(yaml_emitter_t *emitter); |
|---|
| 107 | |
|---|
| 108 | /* |
|---|
| 109 | * Processors. |
|---|
| 110 | */ |
|---|
| 111 | |
|---|
| 112 | static int |
|---|
| 113 | yaml_emitter_process_anchor(yaml_emitter_t *emitter, |
|---|
| 114 | yaml_char_t *anchor, int alias); |
|---|
| 115 | |
|---|
| 116 | static int |
|---|
| 117 | yaml_emitter_process_tag(yaml_emitter_t *emitter, |
|---|
| 118 | yaml_char_t *tag); |
|---|
| 119 | |
|---|
| 120 | static int |
|---|
| 121 | yaml_emitter_process_scalar(yaml_emitter_t *emitter, |
|---|
| 122 | yaml_char_t *value, size_t length, |
|---|
| 123 | int plain_implicit, int quoted_implicit, |
|---|
| 124 | yaml_scalar_style_t style); |
|---|
| 125 | |
|---|
| 126 | /* |
|---|
| 127 | * Writers. |
|---|
| 128 | */ |
|---|
| 129 | |
|---|
| 130 | static int |
|---|
| 131 | yaml_emitter_write_bom(yaml_emitter_t *emitter); |
|---|
| 132 | |
|---|
| 133 | static int |
|---|
| 134 | yaml_emitter_write_version_directive(yaml_emitter_t *emitter, |
|---|
| 135 | yaml_version_directive_t version_directive); |
|---|
| 136 | |
|---|
| 137 | static int |
|---|
| 138 | yaml_emitter_write_tag_directive(yaml_emitter_t *emitter, |
|---|
| 139 | yaml_tag_directive_t tag_directive); |
|---|
| 140 | |
|---|
| 141 | static int |
|---|
| 142 | yaml_emitter_write_indent(yaml_emitter_t *emitter); |
|---|
| 143 | |
|---|
| 144 | static int |
|---|
| 145 | yaml_emitter_write_indicator(yaml_emitter_t *emitter, |
|---|
| 146 | char *indicator, int need_whitespace, |
|---|
| 147 | int is_whitespace, int is_indention); |
|---|
| 148 | |
|---|
| 149 | /* |
|---|
| 150 | * Set an emitter error and return 0. |
|---|
| 151 | */ |
|---|
| 152 | |
|---|
| 153 | static int |
|---|
| 154 | yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem) |
|---|
| 155 | { |
|---|
| 156 | emitter->error = YAML_EMITTER_ERROR; |
|---|
| 157 | emitter->problem = problem; |
|---|
| 158 | |
|---|
| 159 | return 0; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /* |
|---|
| 163 | * Emit an event. |
|---|
| 164 | */ |
|---|
| 165 | |
|---|
| 166 | YAML_DECLARE(int) |
|---|
| 167 | yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 168 | { |
|---|
| 169 | if (!ENQUEUE(emitter, emitter->events, *event)) { |
|---|
| 170 | yaml_event_delete(event); |
|---|
| 171 | return 0; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | while (!yaml_emitter_need_more_events(emitter)) { |
|---|
| 175 | if (!yaml_emitter_state_machine(emitter, emitter->events.head)) { |
|---|
| 176 | return 0; |
|---|
| 177 | } |
|---|
| 178 | DEQUEUE(emitter, emitter->events); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | return 1; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /* |
|---|
| 185 | * Check if we need to accumulate more events before emitting. |
|---|
| 186 | * |
|---|
| 187 | * We accumulate extra |
|---|
| 188 | * - 1 event for DOCUMENT-START |
|---|
| 189 | * - 2 events for SEQUENCE-START |
|---|
| 190 | * - 3 events for MAPPING-START |
|---|
| 191 | */ |
|---|
| 192 | |
|---|
| 193 | static int |
|---|
| 194 | yaml_emitter_need_more_events(yaml_emitter_t *emitter) |
|---|
| 195 | { |
|---|
| 196 | int level = 0; |
|---|
| 197 | int accumulate = 0; |
|---|
| 198 | yaml_event_t *event; |
|---|
| 199 | |
|---|
| 200 | if (QUEUE_EMPTY(emitter, emitter->events)) |
|---|
| 201 | return 1; |
|---|
| 202 | |
|---|
| 203 | switch (emitter->events.head->type) { |
|---|
| 204 | case YAML_DOCUMENT_START_EVENT: |
|---|
| 205 | accumulate = 1; |
|---|
| 206 | break; |
|---|
| 207 | case YAML_SEQUENCE_START_EVENT: |
|---|
| 208 | accumulate = 2; |
|---|
| 209 | break; |
|---|
| 210 | case YAML_MAPPING_START_EVENT: |
|---|
| 211 | accumulate = 3; |
|---|
| 212 | break; |
|---|
| 213 | default: |
|---|
| 214 | return 0; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if (emitter->events.tail - emitter->events.head > accumulate) |
|---|
| 218 | return 0; |
|---|
| 219 | |
|---|
| 220 | for (event = emitter->events.head; event != emitter->events.tail; event ++) { |
|---|
| 221 | switch (event->type) { |
|---|
| 222 | case YAML_STREAM_START_EVENT: |
|---|
| 223 | case YAML_DOCUMENT_START_EVENT: |
|---|
| 224 | case YAML_SEQUENCE_START_EVENT: |
|---|
| 225 | case YAML_MAPPING_START_EVENT: |
|---|
| 226 | level += 1; |
|---|
| 227 | break; |
|---|
| 228 | case YAML_STREAM_END_EVENT: |
|---|
| 229 | case YAML_DOCUMENT_END_EVENT: |
|---|
| 230 | case YAML_SEQUENCE_END_EVENT: |
|---|
| 231 | case YAML_MAPPING_END_EVENT: |
|---|
| 232 | level -= 1; |
|---|
| 233 | break; |
|---|
| 234 | default: |
|---|
| 235 | break; |
|---|
| 236 | } |
|---|
| 237 | if (!level) |
|---|
| 238 | return 0; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | return 1; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /* |
|---|
| 245 | * Append a directive to the directives stack. |
|---|
| 246 | */ |
|---|
| 247 | |
|---|
| 248 | static int |
|---|
| 249 | yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, |
|---|
| 250 | yaml_tag_directive_t value, int allow_duplicates) |
|---|
| 251 | { |
|---|
| 252 | yaml_tag_directive_t *tag_directive; |
|---|
| 253 | yaml_tag_directive_t copy = { NULL, NULL }; |
|---|
| 254 | |
|---|
| 255 | for (tag_directive = emitter->tag_directives.start; |
|---|
| 256 | tag_directive != emitter->tag_directives.top; tag_directive ++) { |
|---|
| 257 | if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { |
|---|
| 258 | if (allow_duplicates) |
|---|
| 259 | return 1; |
|---|
| 260 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 261 | "duplicate %TAG directive"); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | copy.handle = yaml_strdup(value.handle); |
|---|
| 266 | copy.prefix = yaml_strdup(value.prefix); |
|---|
| 267 | if (!copy.handle || !copy.prefix) { |
|---|
| 268 | emitter->error = YAML_MEMORY_ERROR; |
|---|
| 269 | goto error; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | if (!PUSH(emitter, emitter->tag_directives, copy)) |
|---|
| 273 | goto error; |
|---|
| 274 | |
|---|
| 275 | return 1; |
|---|
| 276 | |
|---|
| 277 | error: |
|---|
| 278 | yaml_free(copy.handle); |
|---|
| 279 | yaml_free(copy.prefix); |
|---|
| 280 | return 0; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | /* |
|---|
| 284 | * Increase the indentation level. |
|---|
| 285 | */ |
|---|
| 286 | |
|---|
| 287 | static int |
|---|
| 288 | yaml_emitter_increase_indent(yaml_emitter_t *emitter, |
|---|
| 289 | int flow, int indentless) |
|---|
| 290 | { |
|---|
| 291 | if (!PUSH(emitter, emitter->indents, emitter->indent)) |
|---|
| 292 | return 0; |
|---|
| 293 | |
|---|
| 294 | if (emitter->indent < 0) { |
|---|
| 295 | emitter->indent = flow ? emitter->best_indent : 0; |
|---|
| 296 | } |
|---|
| 297 | else if (!indentless) { |
|---|
| 298 | emitter->indent += emitter->best_indent; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | return 1; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | /* |
|---|
| 305 | * State dispatcher. |
|---|
| 306 | */ |
|---|
| 307 | |
|---|
| 308 | static int |
|---|
| 309 | yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 310 | { |
|---|
| 311 | switch (emitter->state) |
|---|
| 312 | { |
|---|
| 313 | case YAML_EMIT_STREAM_START_STATE: |
|---|
| 314 | return yaml_emitter_emit_stream_start(emitter, event); |
|---|
| 315 | |
|---|
| 316 | case YAML_EMIT_FIRST_DOCUMENT_START_STATE: |
|---|
| 317 | return yaml_emitter_emit_document_start(emitter, event, 1); |
|---|
| 318 | |
|---|
| 319 | case YAML_EMIT_DOCUMENT_START_STATE: |
|---|
| 320 | return yaml_emitter_emit_document_start(emitter, event, 0); |
|---|
| 321 | |
|---|
| 322 | case YAML_EMIT_DOCUMENT_CONTENT_STATE: |
|---|
| 323 | return yaml_emitter_emit_document_content(emitter, event); |
|---|
| 324 | |
|---|
| 325 | case YAML_EMIT_DOCUMENT_END_STATE: |
|---|
| 326 | return yaml_emitter_emit_document_end(emitter, event); |
|---|
| 327 | |
|---|
| 328 | case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: |
|---|
| 329 | return yaml_emitter_emit_flow_sequence_item(emitter, event, 1); |
|---|
| 330 | |
|---|
| 331 | case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE: |
|---|
| 332 | return yaml_emitter_emit_flow_sequence_item(emitter, event, 0); |
|---|
| 333 | |
|---|
| 334 | case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: |
|---|
| 335 | return yaml_emitter_emit_flow_mapping_key(emitter, event, 1); |
|---|
| 336 | |
|---|
| 337 | case YAML_EMIT_FLOW_MAPPING_KEY_STATE: |
|---|
| 338 | return yaml_emitter_emit_flow_mapping_key(emitter, event, 0); |
|---|
| 339 | |
|---|
| 340 | case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: |
|---|
| 341 | return yaml_emitter_emit_flow_mapping_value(emitter, event, 1); |
|---|
| 342 | |
|---|
| 343 | case YAML_EMIT_FLOW_MAPPING_VALUE_STATE: |
|---|
| 344 | return yaml_emitter_emit_flow_mapping_value(emitter, event, 0); |
|---|
| 345 | |
|---|
| 346 | case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: |
|---|
| 347 | return yaml_emitter_emit_block_sequence_item(emitter, event, 1); |
|---|
| 348 | |
|---|
| 349 | case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE: |
|---|
| 350 | return yaml_emitter_emit_block_sequence_item(emitter, event, 0); |
|---|
| 351 | |
|---|
| 352 | case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: |
|---|
| 353 | return yaml_emitter_emit_block_mapping_key(emitter, event, 1); |
|---|
| 354 | |
|---|
| 355 | case YAML_EMIT_BLOCK_MAPPING_KEY_STATE: |
|---|
| 356 | return yaml_emitter_emit_block_mapping_key(emitter, event, 0); |
|---|
| 357 | |
|---|
| 358 | case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: |
|---|
| 359 | return yaml_emitter_emit_block_mapping_value(emitter, event, 1); |
|---|
| 360 | |
|---|
| 361 | case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE: |
|---|
| 362 | return yaml_emitter_emit_block_mapping_value(emitter, event, 0); |
|---|
| 363 | |
|---|
| 364 | case YAML_EMIT_END_STATE: |
|---|
| 365 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 366 | "expected nothing after STREAM-END"); |
|---|
| 367 | |
|---|
| 368 | default: |
|---|
| 369 | assert(1); /* Invalid state. */ |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | return 0; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | /* |
|---|
| 376 | * Expect STREAM-START. |
|---|
| 377 | */ |
|---|
| 378 | |
|---|
| 379 | static int |
|---|
| 380 | yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, |
|---|
| 381 | yaml_event_t *event) |
|---|
| 382 | { |
|---|
| 383 | if (event->type == YAML_STREAM_START_EVENT) |
|---|
| 384 | { |
|---|
| 385 | if (!emitter->encoding) { |
|---|
| 386 | emitter->encoding = event->data.stream_start.encoding; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | if (!emitter->encoding) { |
|---|
| 390 | emitter->encoding = YAML_UTF8_ENCODING; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | if (emitter->best_indent < 2 || emitter->best_indent > 9) { |
|---|
| 394 | emitter->best_indent = 2; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | if (emitter->best_width >= 0 |
|---|
| 398 | && emitter->best_width <= emitter->best_indent*2) { |
|---|
| 399 | emitter->best_width = 80; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | if (emitter->best_width < 0) { |
|---|
| 403 | emitter->best_width = INT_MAX; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | if (!emitter->line_break) { |
|---|
| 407 | emitter->line_break = YAML_LN_BREAK; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | emitter->indent = -1; |
|---|
| 411 | |
|---|
| 412 | emitter->line = 0; |
|---|
| 413 | emitter->column = 0; |
|---|
| 414 | emitter->whitespace = 1; |
|---|
| 415 | emitter->indention = 1; |
|---|
| 416 | |
|---|
| 417 | if (emitter->encoding != YAML_UTF8_ENCODING) { |
|---|
| 418 | if (!yaml_emitter_write_bom(emitter)) |
|---|
| 419 | return 0; |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE; |
|---|
| 423 | |
|---|
| 424 | return 1; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 428 | "expected STREAM-START"); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | static int |
|---|
| 432 | yaml_emitter_emit_document_start(yaml_emitter_t *emitter, |
|---|
| 433 | yaml_event_t *event, int first) |
|---|
| 434 | { |
|---|
| 435 | if (event->type == YAML_DOCUMENT_START_EVENT) |
|---|
| 436 | { |
|---|
| 437 | yaml_tag_directive_t default_tag_directives[] = { |
|---|
| 438 | {(yaml_char_t *)"!", (yaml_char_t *)"!"}, |
|---|
| 439 | {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"}, |
|---|
| 440 | {NULL, NULL} |
|---|
| 441 | }; |
|---|
| 442 | yaml_tag_directive_t *tag_directive; |
|---|
| 443 | int implicit; |
|---|
| 444 | |
|---|
| 445 | if (event->data.document_start.version_directive) { |
|---|
| 446 | if (event->data.document_start.version_directive->major != 1 |
|---|
| 447 | || event->data.document_start.version_directive-> minor != 1) { |
|---|
| 448 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 449 | "incompatible %YAML directive"); |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | for (tag_directive = event->data.document_start.tag_directives.start; |
|---|
| 454 | tag_directive != event->data.document_start.tag_directives.end; |
|---|
| 455 | tag_directive ++) { |
|---|
| 456 | if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0)) |
|---|
| 457 | return 0; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | for (tag_directive = default_tag_directives; |
|---|
| 461 | tag_directive->handle; tag_directive ++) { |
|---|
| 462 | if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1)) |
|---|
| 463 | return 0; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | implicit = event->data.document_start.implicit; |
|---|
| 467 | if (!first || emitter->canonical) { |
|---|
| 468 | implicit = 0; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | if (event->data.document_start.version_directive) { |
|---|
| 472 | implicit = 0; |
|---|
| 473 | if (!yaml_emitter_write_version_directive(emitter, |
|---|
| 474 | *event->data.document_start.version_directive)) |
|---|
| 475 | return 0; |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | if (event->data.document_start.tag_directives.start |
|---|
| 479 | != event->data.document_start.tag_directives.end) { |
|---|
| 480 | implicit = 0; |
|---|
| 481 | for (tag_directive = event->data.document_start.tag_directives.start; |
|---|
| 482 | tag_directive != event->data.document_start.tag_directives.end; |
|---|
| 483 | tag_directive ++) { |
|---|
| 484 | if (!yaml_emitter_write_tag_directive(emitter, *tag_directive)) |
|---|
| 485 | return 0; |
|---|
| 486 | } |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | if (yaml_emitter_check_empty_document(emitter)) { |
|---|
| 490 | implicit = 0; |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | if (!implicit) { |
|---|
| 494 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 495 | return 0; |
|---|
| 496 | if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0)) |
|---|
| 497 | return 0; |
|---|
| 498 | if (emitter->canonical) { |
|---|
| 499 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 500 | return 0; |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE; |
|---|
| 505 | |
|---|
| 506 | return 1; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | else if (event->type == YAML_STREAM_END_EVENT) |
|---|
| 510 | { |
|---|
| 511 | if (!yaml_emitter_flush(emitter)) |
|---|
| 512 | return 0; |
|---|
| 513 | |
|---|
| 514 | emitter->state = YAML_EMIT_END_STATE; |
|---|
| 515 | |
|---|
| 516 | return 1; |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 520 | "expected DOCUMENT-START or STREAM-END"); |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | static int |
|---|
| 524 | yaml_emitter_emit_document_content(yaml_emitter_t *emitter, |
|---|
| 525 | yaml_event_t *event) |
|---|
| 526 | { |
|---|
| 527 | if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE)) |
|---|
| 528 | return 0; |
|---|
| 529 | |
|---|
| 530 | return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0); |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | static int |
|---|
| 534 | yaml_emitter_emit_document_end(yaml_emitter_t *emitter, |
|---|
| 535 | yaml_event_t *event) |
|---|
| 536 | { |
|---|
| 537 | if (event->type == YAML_DOCUMENT_END_EVENT) |
|---|
| 538 | { |
|---|
| 539 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 540 | return 0; |
|---|
| 541 | if (!event->data.document_end.implicit) { |
|---|
| 542 | if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) |
|---|
| 543 | return 0; |
|---|
| 544 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 545 | return 0; |
|---|
| 546 | } |
|---|
| 547 | if (!yaml_emitter_flush(emitter)) |
|---|
| 548 | return 0; |
|---|
| 549 | |
|---|
| 550 | emitter->state = YAML_EMIT_DOCUMENT_START_STATE; |
|---|
| 551 | |
|---|
| 552 | return 1; |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 556 | "expected DOCUMENT-END"); |
|---|
| 557 | } |
|---|
| 558 | |
|---|
| 559 | static int |
|---|
| 560 | yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, |
|---|
| 561 | yaml_event_t *event, int first) |
|---|
| 562 | { |
|---|
| 563 | if (first) |
|---|
| 564 | { |
|---|
| 565 | if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0)) |
|---|
| 566 | return 0; |
|---|
| 567 | if (!yaml_emitter_increase_indent(emitter, 1, 0)) |
|---|
| 568 | return 0; |
|---|
| 569 | emitter->flow_level ++; |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | if (event->type == YAML_SEQUENCE_END_EVENT) |
|---|
| 573 | { |
|---|
| 574 | emitter->flow_level --; |
|---|
| 575 | emitter->indent = POP(emitter, emitter->indents); |
|---|
| 576 | if (emitter->canonical && !first) { |
|---|
| 577 | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
|---|
| 578 | return 0; |
|---|
| 579 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 580 | return 0; |
|---|
| 581 | } |
|---|
| 582 | if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0)) |
|---|
| 583 | return 0; |
|---|
| 584 | emitter->state = POP(emitter, emitter->states); |
|---|
| 585 | |
|---|
| 586 | return 1; |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | if (emitter->canonical || emitter->column > emitter->best_width) { |
|---|
| 590 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 591 | return 0; |
|---|
| 592 | } |
|---|
| 593 | if (PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE)) |
|---|
| 594 | return 0; |
|---|
| 595 | |
|---|
| 596 | return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | static int |
|---|
| 600 | yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, |
|---|
| 601 | yaml_event_t *event, int first) |
|---|
| 602 | { |
|---|
| 603 | if (first) |
|---|
| 604 | { |
|---|
| 605 | if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0)) |
|---|
| 606 | return 0; |
|---|
| 607 | if (!yaml_emitter_increase_indent(emitter, 1, 0)) |
|---|
| 608 | return 0; |
|---|
| 609 | emitter->flow_level ++; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | if (event->type == YAML_MAPPING_END_EVENT) |
|---|
| 613 | { |
|---|
| 614 | emitter->flow_level --; |
|---|
| 615 | emitter->indent = POP(emitter, emitter->indents); |
|---|
| 616 | if (emitter->canonical && !first) { |
|---|
| 617 | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
|---|
| 618 | return 0; |
|---|
| 619 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 620 | return 0; |
|---|
| 621 | } |
|---|
| 622 | if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0)) |
|---|
| 623 | return 0; |
|---|
| 624 | emitter->state = POP(emitter, emitter->states); |
|---|
| 625 | |
|---|
| 626 | return 1; |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | if (!first) { |
|---|
| 630 | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
|---|
| 631 | return 0; |
|---|
| 632 | } |
|---|
| 633 | if (emitter->canonical || emitter->column > emitter->best_width) { |
|---|
| 634 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 635 | return 0; |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | if (!emitter->canonical && yaml_emitter_check_simple_key(emitter)) |
|---|
| 639 | { |
|---|
| 640 | if (!PUSH(emitter, emitter->states, |
|---|
| 641 | YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)) |
|---|
| 642 | return 0; |
|---|
| 643 | |
|---|
| 644 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); |
|---|
| 645 | } |
|---|
| 646 | else |
|---|
| 647 | { |
|---|
| 648 | if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0)) |
|---|
| 649 | return 0; |
|---|
| 650 | if (!PUSH(emitter, emitter->states, |
|---|
| 651 | YAML_EMIT_FLOW_MAPPING_VALUE_STATE)) |
|---|
| 652 | return 0; |
|---|
| 653 | |
|---|
| 654 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
|---|
| 655 | } |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | static int |
|---|
| 659 | yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, |
|---|
| 660 | yaml_event_t *event, int simple) |
|---|
| 661 | { |
|---|
| 662 | if (simple) { |
|---|
| 663 | if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) |
|---|
| 664 | return 0; |
|---|
| 665 | } |
|---|
| 666 | else { |
|---|
| 667 | if (emitter->canonical || emitter->column > emitter->best_width) { |
|---|
| 668 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 669 | return 0; |
|---|
| 670 | } |
|---|
| 671 | if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0)) |
|---|
| 672 | return 0; |
|---|
| 673 | } |
|---|
| 674 | if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE)) |
|---|
| 675 | return 0; |
|---|
| 676 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
|---|
| 677 | } |
|---|
| 678 | |
|---|
| 679 | static int |
|---|
| 680 | yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, |
|---|
| 681 | yaml_event_t *event, int first) |
|---|
| 682 | { |
|---|
| 683 | if (first) |
|---|
| 684 | { |
|---|
| 685 | if (!yaml_emitter_increase_indent(emitter, 0, |
|---|
| 686 | (emitter->mapping_context && !emitter->indention))) |
|---|
| 687 | return 0; |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | if (event->type == YAML_SEQUENCE_END_EVENT) |
|---|
| 691 | { |
|---|
| 692 | emitter->indent = POP(emitter, emitter->indents); |
|---|
| 693 | emitter->state = POP(emitter, emitter->states); |
|---|
| 694 | |
|---|
| 695 | return 1; |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 699 | return 0; |
|---|
| 700 | if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1)) |
|---|
| 701 | return 0; |
|---|
| 702 | if (!PUSH(emitter, emitter->states, |
|---|
| 703 | YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE)) |
|---|
| 704 | return 0; |
|---|
| 705 | |
|---|
| 706 | return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | static int |
|---|
| 710 | yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, |
|---|
| 711 | yaml_event_t *event, int first) |
|---|
| 712 | { |
|---|
| 713 | if (first) |
|---|
| 714 | { |
|---|
| 715 | if (!yaml_emitter_increase_indent(emitter, 0, 0)) |
|---|
| 716 | return 0; |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | if (event->type == YAML_MAPPING_END_EVENT) |
|---|
| 720 | { |
|---|
| 721 | emitter->indent = POP(emitter, emitter->indents); |
|---|
| 722 | emitter->state = POP(emitter, emitter->states); |
|---|
| 723 | |
|---|
| 724 | return 1; |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 728 | return 0; |
|---|
| 729 | |
|---|
| 730 | if (yaml_emitter_check_simple_key(emitter)) |
|---|
| 731 | { |
|---|
| 732 | if (!PUSH(emitter, emitter->states, |
|---|
| 733 | YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)) |
|---|
| 734 | return 0; |
|---|
| 735 | |
|---|
| 736 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); |
|---|
| 737 | } |
|---|
| 738 | else |
|---|
| 739 | { |
|---|
| 740 | if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1)) |
|---|
| 741 | return 0; |
|---|
| 742 | if (!PUSH(emitter, emitter->states, |
|---|
| 743 | YAML_EMIT_BLOCK_MAPPING_VALUE_STATE)) |
|---|
| 744 | return 0; |
|---|
| 745 | |
|---|
| 746 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
|---|
| 747 | } |
|---|
| 748 | } |
|---|
| 749 | |
|---|
| 750 | static int |
|---|
| 751 | yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, |
|---|
| 752 | yaml_event_t *event, int simple) |
|---|
| 753 | { |
|---|
| 754 | if (simple) { |
|---|
| 755 | if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) |
|---|
| 756 | return 0; |
|---|
| 757 | } |
|---|
| 758 | else { |
|---|
| 759 | if (!yaml_emitter_write_indent(emitter)) |
|---|
| 760 | return 0; |
|---|
| 761 | if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1)) |
|---|
| 762 | return 0; |
|---|
| 763 | } |
|---|
| 764 | if (!PUSH(emitter, emitter->states, |
|---|
| 765 | YAML_EMIT_BLOCK_MAPPING_KEY_STATE)) |
|---|
| 766 | return 0; |
|---|
| 767 | |
|---|
| 768 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | static int |
|---|
| 772 | yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, |
|---|
| 773 | int root, int sequence, int mapping, int simple_key) |
|---|
| 774 | { |
|---|
| 775 | emitter->root_context = root; |
|---|
| 776 | emitter->sequence_context = sequence; |
|---|
| 777 | emitter->mapping_context = mapping; |
|---|
| 778 | emitter->simple_key_context = simple_key; |
|---|
| 779 | |
|---|
| 780 | switch (event->type) |
|---|
| 781 | { |
|---|
| 782 | case YAML_ALIAS_EVENT: |
|---|
| 783 | return yaml_emitter_emit_alias(emitter, event); |
|---|
| 784 | |
|---|
| 785 | case YAML_SCALAR_EVENT: |
|---|
| 786 | return yaml_emitter_emit_scalar(emitter, event); |
|---|
| 787 | |
|---|
| 788 | case YAML_SEQUENCE_START_EVENT: |
|---|
| 789 | return yaml_emitter_emit_sequence_start(emitter, event); |
|---|
| 790 | |
|---|
| 791 | case YAML_MAPPING_START_EVENT: |
|---|
| 792 | return yaml_emitter_emit_mapping_start(emitter, event); |
|---|
| 793 | |
|---|
| 794 | default: |
|---|
| 795 | return yaml_emitter_set_emitter_error(emitter, |
|---|
| 796 | "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | return 0; |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | static int |
|---|
| 803 | yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 804 | { |
|---|
| 805 | if (!yaml_emitter_process_anchor(emitter, event->data.alias.anchor, 1)) |
|---|
| 806 | return 0; |
|---|
| 807 | emitter->state = POP(emitter, emitter->states); |
|---|
| 808 | |
|---|
| 809 | return 1; |
|---|
| 810 | } |
|---|
| 811 | |
|---|
| 812 | static int |
|---|
| 813 | yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 814 | { |
|---|
| 815 | if (!yaml_emitter_process_anchor(emitter, event->data.scalar.anchor, 0)) |
|---|
| 816 | return 0; |
|---|
| 817 | if (!yaml_emitter_process_tag(emitter, event->data.scalar.tag)) |
|---|
| 818 | return 0; |
|---|
| 819 | if (!yaml_emitter_increase_indent(emitter, 1, 0)) |
|---|
| 820 | return 0; |
|---|
| 821 | if (!yaml_emitter_process_scalar(emitter, |
|---|
| 822 | event->data.scalar.value, event->data.scalar.length, |
|---|
| 823 | event->data.scalar.plain_implicit, |
|---|
| 824 | event->data.scalar.quoted_implicit, |
|---|
| 825 | event->data.scalar.style)) |
|---|
| 826 | return 0; |
|---|
| 827 | emitter->indent = POP(emitter, emitter->indents); |
|---|
| 828 | emitter->state = POP(emitter, emitter->states); |
|---|
| 829 | |
|---|
| 830 | return 1; |
|---|
| 831 | } |
|---|
| 832 | |
|---|
| 833 | static int |
|---|
| 834 | yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 835 | { |
|---|
| 836 | if (!yaml_emitter_process_anchor(emitter, |
|---|
| 837 | event->data.sequence_start.anchor, 0)) |
|---|
| 838 | return 0; |
|---|
| 839 | if (!yaml_emitter_process_tag(emitter, |
|---|
| 840 | event->data.sequence_start.tag)) |
|---|
| 841 | return 0; |
|---|
| 842 | |
|---|
| 843 | if (emitter->flow_level || emitter->canonical |
|---|
| 844 | || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE |
|---|
| 845 | || yaml_emitter_check_empty_sequence(emitter)) { |
|---|
| 846 | emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE; |
|---|
| 847 | } |
|---|
| 848 | else { |
|---|
| 849 | emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE; |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | return 1; |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | static int |
|---|
| 856 | yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) |
|---|
| 857 | { |
|---|
| 858 | if (!yaml_emitter_process_anchor(emitter, |
|---|
| 859 | event->data.mapping_start.anchor, 0)) |
|---|
| 860 | return 0; |
|---|
| 861 | if (!yaml_emitter_process_tag(emitter, |
|---|
| 862 | event->data.mapping_start.tag)) |
|---|
| 863 | return 0; |
|---|
| 864 | |
|---|
| 865 | if (emitter->flow_level || emitter->canonical |
|---|
| 866 | || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE |
|---|
| 867 | || yaml_emitter_check_empty_mapping(emitter)) { |
|---|
| 868 | emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE; |
|---|
| 869 | } |
|---|
| 870 | else { |
|---|
| 871 | emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE; |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | return 1; |
|---|
| 875 | } |
|---|
| 876 | |
|---|