cuelang.org/go@v0.10.1/internal/third_party/yaml/parserc.go (about) 1 package yaml 2 3 import ( 4 "bytes" 5 ) 6 7 // The parser implements the following grammar: 8 // 9 // stream ::= STREAM-START implicit_document? explicit_document* STREAM-END 10 // implicit_document ::= block_node DOCUMENT-END* 11 // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* 12 // block_node_or_indentless_sequence ::= 13 // ALIAS 14 // | properties (block_content | indentless_block_sequence)? 15 // | block_content 16 // | indentless_block_sequence 17 // block_node ::= ALIAS 18 // | properties block_content? 19 // | block_content 20 // flow_node ::= ALIAS 21 // | properties flow_content? 22 // | flow_content 23 // properties ::= TAG ANCHOR? | ANCHOR TAG? 24 // block_content ::= block_collection | flow_collection | SCALAR 25 // flow_content ::= flow_collection | SCALAR 26 // block_collection ::= block_sequence | block_mapping 27 // flow_collection ::= flow_sequence | flow_mapping 28 // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END 29 // indentless_sequence ::= (BLOCK-ENTRY block_node?)+ 30 // block_mapping ::= BLOCK-MAPPING_START 31 // ((KEY block_node_or_indentless_sequence?)? 32 // (VALUE block_node_or_indentless_sequence?)?)* 33 // BLOCK-END 34 // flow_sequence ::= FLOW-SEQUENCE-START 35 // (flow_sequence_entry FLOW-ENTRY)* 36 // flow_sequence_entry? 37 // FLOW-SEQUENCE-END 38 // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 39 // flow_mapping ::= FLOW-MAPPING-START 40 // (flow_mapping_entry FLOW-ENTRY)* 41 // flow_mapping_entry? 42 // FLOW-MAPPING-END 43 // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 44 45 // Peek the next token in the token queue. 46 func peek_token(parser *yaml_parser_t) *yaml_token_t { 47 if parser.token_available || yaml_parser_fetch_more_tokens(parser) { 48 return &parser.tokens[parser.tokens_head] 49 } 50 return nil 51 } 52 53 // Remove the next token from the queue (must be called after peek_token). 54 func skip_token(parser *yaml_parser_t) { 55 parser.token_available = false 56 parser.tokens_parsed++ 57 parser.stream_end_produced = parser.tokens[parser.tokens_head].typ == yaml_STREAM_END_TOKEN 58 parser.tokens_head++ 59 } 60 61 func add_comment(parser *yaml_parser_t, m yaml_mark_t, text string) { 62 parser.comments = append(parser.comments, yaml_comment_t{ 63 mark: m, 64 text: text, 65 }) 66 } 67 68 // Get the next event. 69 func yaml_parser_parse(parser *yaml_parser_t, event *yaml_event_t) bool { 70 // Erase the event object. 71 *event = yaml_event_t{} 72 73 // No events after the end of the stream or error. 74 if parser.stream_end_produced || parser.error != yaml_NO_ERROR || parser.state == yaml_PARSE_END_STATE { 75 return true 76 } 77 78 // Generate the next event. 79 return yaml_parser_state_machine(parser, event) 80 } 81 82 // Set parser error. 83 func yaml_parser_set_parser_error(parser *yaml_parser_t, problem string, problem_mark yaml_mark_t) bool { 84 parser.error = yaml_PARSER_ERROR 85 parser.problem = problem 86 parser.problem_mark = problem_mark 87 return false 88 } 89 90 func yaml_parser_set_parser_error_context(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string, problem_mark yaml_mark_t) bool { 91 parser.error = yaml_PARSER_ERROR 92 parser.context = context 93 parser.context_mark = context_mark 94 parser.problem = problem 95 parser.problem_mark = problem_mark 96 return false 97 } 98 99 // State dispatcher. 100 func yaml_parser_state_machine(parser *yaml_parser_t, event *yaml_event_t) bool { 101 //trace("yaml_parser_state_machine", "state:", parser.state.String()) 102 103 switch parser.state { 104 case yaml_PARSE_STREAM_START_STATE: 105 return yaml_parser_parse_stream_start(parser, event) 106 107 case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE: 108 return yaml_parser_parse_document_start(parser, event, true) 109 110 case yaml_PARSE_DOCUMENT_START_STATE: 111 return yaml_parser_parse_document_start(parser, event, false) 112 113 case yaml_PARSE_DOCUMENT_CONTENT_STATE: 114 return yaml_parser_parse_document_content(parser, event) 115 116 case yaml_PARSE_DOCUMENT_END_STATE: 117 return yaml_parser_parse_document_end(parser, event) 118 119 case yaml_PARSE_BLOCK_NODE_STATE: 120 return yaml_parser_parse_node(parser, event, true, false) 121 122 case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE: 123 return yaml_parser_parse_node(parser, event, true, true) 124 125 case yaml_PARSE_FLOW_NODE_STATE: 126 return yaml_parser_parse_node(parser, event, false, false) 127 128 case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE: 129 return yaml_parser_parse_block_sequence_entry(parser, event, true) 130 131 case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE: 132 return yaml_parser_parse_block_sequence_entry(parser, event, false) 133 134 case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE: 135 return yaml_parser_parse_indentless_sequence_entry(parser, event) 136 137 case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE: 138 return yaml_parser_parse_block_mapping_key(parser, event, true) 139 140 case yaml_PARSE_BLOCK_MAPPING_KEY_STATE: 141 return yaml_parser_parse_block_mapping_key(parser, event, false) 142 143 case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE: 144 return yaml_parser_parse_block_mapping_value(parser, event) 145 146 case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE: 147 return yaml_parser_parse_flow_sequence_entry(parser, event, true) 148 149 case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE: 150 return yaml_parser_parse_flow_sequence_entry(parser, event, false) 151 152 case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE: 153 return yaml_parser_parse_flow_sequence_entry_mapping_key(parser, event) 154 155 case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE: 156 return yaml_parser_parse_flow_sequence_entry_mapping_value(parser, event) 157 158 case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE: 159 return yaml_parser_parse_flow_sequence_entry_mapping_end(parser, event) 160 161 case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE: 162 return yaml_parser_parse_flow_mapping_key(parser, event, true) 163 164 case yaml_PARSE_FLOW_MAPPING_KEY_STATE: 165 return yaml_parser_parse_flow_mapping_key(parser, event, false) 166 167 case yaml_PARSE_FLOW_MAPPING_VALUE_STATE: 168 return yaml_parser_parse_flow_mapping_value(parser, event, false) 169 170 case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE: 171 return yaml_parser_parse_flow_mapping_value(parser, event, true) 172 173 default: 174 panic("invalid parser state") 175 } 176 } 177 178 // Parse the production: 179 // stream ::= STREAM-START implicit_document? explicit_document* STREAM-END 180 // 181 // ************ 182 func yaml_parser_parse_stream_start(parser *yaml_parser_t, event *yaml_event_t) bool { 183 token := peek_token(parser) 184 if token == nil { 185 return false 186 } 187 if token.typ != yaml_STREAM_START_TOKEN { 188 return yaml_parser_set_parser_error(parser, "did not find expected <stream-start>", token.start_mark) 189 } 190 parser.state = yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE 191 *event = yaml_event_t{ 192 typ: yaml_STREAM_START_EVENT, 193 start_mark: token.start_mark, 194 end_mark: token.end_mark, 195 encoding: token.encoding, 196 } 197 skip_token(parser) 198 return true 199 } 200 201 // Parse the productions: 202 // implicit_document ::= block_node DOCUMENT-END* 203 // 204 // * 205 // 206 // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* 207 // 208 // ************************* 209 func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t, implicit bool) bool { 210 211 token := peek_token(parser) 212 if token == nil { 213 return false 214 } 215 216 // Parse extra document end indicators. 217 if !implicit { 218 for token.typ == yaml_DOCUMENT_END_TOKEN { 219 skip_token(parser) 220 token = peek_token(parser) 221 if token == nil { 222 return false 223 } 224 } 225 } 226 227 if implicit && token.typ != yaml_VERSION_DIRECTIVE_TOKEN && 228 token.typ != yaml_TAG_DIRECTIVE_TOKEN && 229 token.typ != yaml_DOCUMENT_START_TOKEN && 230 token.typ != yaml_STREAM_END_TOKEN { 231 // Parse an implicit document. 232 if !yaml_parser_process_directives(parser, nil, nil) { 233 return false 234 } 235 parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE) 236 parser.state = yaml_PARSE_BLOCK_NODE_STATE 237 238 *event = yaml_event_t{ 239 typ: yaml_DOCUMENT_START_EVENT, 240 start_mark: token.start_mark, 241 end_mark: token.end_mark, 242 } 243 244 } else if token.typ != yaml_STREAM_END_TOKEN { 245 // Parse an explicit document. 246 var version_directive *yaml_version_directive_t 247 var tag_directives []yaml_tag_directive_t 248 start_mark := token.start_mark 249 if !yaml_parser_process_directives(parser, &version_directive, &tag_directives) { 250 return false 251 } 252 token = peek_token(parser) 253 if token == nil { 254 return false 255 } 256 if token.typ != yaml_DOCUMENT_START_TOKEN { 257 yaml_parser_set_parser_error(parser, 258 "did not find expected <document start>", token.start_mark) 259 return false 260 } 261 parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE) 262 parser.state = yaml_PARSE_DOCUMENT_CONTENT_STATE 263 end_mark := token.end_mark 264 265 *event = yaml_event_t{ 266 typ: yaml_DOCUMENT_START_EVENT, 267 start_mark: start_mark, 268 end_mark: end_mark, 269 version_directive: version_directive, 270 tag_directives: tag_directives, 271 implicit: false, 272 } 273 skip_token(parser) 274 275 } else { 276 // Parse the stream end. 277 parser.state = yaml_PARSE_END_STATE 278 *event = yaml_event_t{ 279 typ: yaml_STREAM_END_EVENT, 280 start_mark: token.start_mark, 281 end_mark: token.end_mark, 282 } 283 skip_token(parser) 284 } 285 286 return true 287 } 288 289 // Parse the productions: 290 // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* 291 // 292 // *********** 293 func yaml_parser_parse_document_content(parser *yaml_parser_t, event *yaml_event_t) bool { 294 token := peek_token(parser) 295 if token == nil { 296 return false 297 } 298 if token.typ == yaml_VERSION_DIRECTIVE_TOKEN || 299 token.typ == yaml_TAG_DIRECTIVE_TOKEN || 300 token.typ == yaml_DOCUMENT_START_TOKEN || 301 token.typ == yaml_DOCUMENT_END_TOKEN || 302 token.typ == yaml_STREAM_END_TOKEN { 303 parser.state = parser.states[len(parser.states)-1] 304 parser.states = parser.states[:len(parser.states)-1] 305 return yaml_parser_process_empty_scalar(parser, event, 306 token.start_mark) 307 } 308 return yaml_parser_parse_node(parser, event, true, false) 309 } 310 311 // Parse the productions: 312 // implicit_document ::= block_node DOCUMENT-END* 313 // 314 // ************* 315 // 316 // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* 317 func yaml_parser_parse_document_end(parser *yaml_parser_t, event *yaml_event_t) bool { 318 token := peek_token(parser) 319 if token == nil { 320 return false 321 } 322 323 start_mark := token.start_mark 324 end_mark := token.start_mark 325 326 implicit := true 327 if token.typ == yaml_DOCUMENT_END_TOKEN { 328 end_mark = token.end_mark 329 skip_token(parser) 330 implicit = false 331 } 332 333 parser.tag_directives = parser.tag_directives[:0] 334 335 parser.state = yaml_PARSE_DOCUMENT_START_STATE 336 *event = yaml_event_t{ 337 typ: yaml_DOCUMENT_END_EVENT, 338 start_mark: start_mark, 339 end_mark: end_mark, 340 implicit: implicit, 341 } 342 return true 343 } 344 345 // Parse the productions: 346 // block_node_or_indentless_sequence ::= 347 // 348 // ALIAS 349 // ***** 350 // | properties (block_content | indentless_block_sequence)? 351 // ********** * 352 // | block_content | indentless_block_sequence 353 // * 354 // 355 // block_node ::= ALIAS 356 // 357 // ***** 358 // | properties block_content? 359 // ********** * 360 // | block_content 361 // * 362 // 363 // flow_node ::= ALIAS 364 // 365 // ***** 366 // | properties flow_content? 367 // ********** * 368 // | flow_content 369 // * 370 // 371 // properties ::= TAG ANCHOR? | ANCHOR TAG? 372 // 373 // ************************* 374 // 375 // block_content ::= block_collection | flow_collection | SCALAR 376 // 377 // ****** 378 // 379 // flow_content ::= flow_collection | SCALAR 380 // 381 // ****** 382 func yaml_parser_parse_node(parser *yaml_parser_t, event *yaml_event_t, block, indentless_sequence bool) bool { 383 //defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)() 384 385 token := peek_token(parser) 386 if token == nil { 387 return false 388 } 389 390 if token.typ == yaml_ALIAS_TOKEN { 391 parser.state = parser.states[len(parser.states)-1] 392 parser.states = parser.states[:len(parser.states)-1] 393 *event = yaml_event_t{ 394 typ: yaml_ALIAS_EVENT, 395 start_mark: token.start_mark, 396 end_mark: token.end_mark, 397 anchor: token.value, 398 } 399 skip_token(parser) 400 return true 401 } 402 403 start_mark := token.start_mark 404 end_mark := token.start_mark 405 406 var tag_token bool 407 var tag_handle, tag_suffix, anchor []byte 408 var tag_mark yaml_mark_t 409 if token.typ == yaml_ANCHOR_TOKEN { 410 anchor = token.value 411 start_mark = token.start_mark 412 end_mark = token.end_mark 413 skip_token(parser) 414 token = peek_token(parser) 415 if token == nil { 416 return false 417 } 418 if token.typ == yaml_TAG_TOKEN { 419 tag_token = true 420 tag_handle = token.value 421 tag_suffix = token.suffix 422 tag_mark = token.start_mark 423 end_mark = token.end_mark 424 skip_token(parser) 425 token = peek_token(parser) 426 if token == nil { 427 return false 428 } 429 } 430 } else if token.typ == yaml_TAG_TOKEN { 431 tag_token = true 432 tag_handle = token.value 433 tag_suffix = token.suffix 434 start_mark = token.start_mark 435 tag_mark = token.start_mark 436 end_mark = token.end_mark 437 skip_token(parser) 438 token = peek_token(parser) 439 if token == nil { 440 return false 441 } 442 if token.typ == yaml_ANCHOR_TOKEN { 443 anchor = token.value 444 end_mark = token.end_mark 445 skip_token(parser) 446 token = peek_token(parser) 447 if token == nil { 448 return false 449 } 450 } 451 } 452 453 var tag []byte 454 if tag_token { 455 if len(tag_handle) == 0 { 456 tag = tag_suffix 457 tag_suffix = nil 458 } else { 459 for i := range parser.tag_directives { 460 if bytes.Equal(parser.tag_directives[i].handle, tag_handle) { 461 tag = append([]byte(nil), parser.tag_directives[i].prefix...) 462 tag = append(tag, tag_suffix...) 463 break 464 } 465 } 466 if len(tag) == 0 { 467 yaml_parser_set_parser_error_context(parser, 468 "while parsing a node", start_mark, 469 "found undefined tag handle", tag_mark) 470 return false 471 } 472 } 473 } 474 475 implicit := len(tag) == 0 476 if indentless_sequence && token.typ == yaml_BLOCK_ENTRY_TOKEN { 477 end_mark = token.end_mark 478 parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE 479 *event = yaml_event_t{ 480 typ: yaml_SEQUENCE_START_EVENT, 481 start_mark: start_mark, 482 end_mark: end_mark, 483 anchor: anchor, 484 tag: tag, 485 implicit: implicit, 486 style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE), 487 } 488 return true 489 } 490 if token.typ == yaml_SCALAR_TOKEN { 491 var plain_implicit, quoted_implicit bool 492 end_mark = token.end_mark 493 if (len(tag) == 0 && token.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') { 494 plain_implicit = true 495 } else if len(tag) == 0 { 496 quoted_implicit = true 497 } 498 parser.state = parser.states[len(parser.states)-1] 499 parser.states = parser.states[:len(parser.states)-1] 500 501 *event = yaml_event_t{ 502 typ: yaml_SCALAR_EVENT, 503 start_mark: start_mark, 504 end_mark: end_mark, 505 anchor: anchor, 506 tag: tag, 507 value: token.value, 508 implicit: plain_implicit, 509 quoted_implicit: quoted_implicit, 510 style: yaml_style_t(token.style), 511 } 512 skip_token(parser) 513 return true 514 } 515 if token.typ == yaml_FLOW_SEQUENCE_START_TOKEN { 516 // [Go] Some of the events below can be merged as they differ only on style. 517 end_mark = token.end_mark 518 parser.state = yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE 519 *event = yaml_event_t{ 520 typ: yaml_SEQUENCE_START_EVENT, 521 start_mark: start_mark, 522 end_mark: end_mark, 523 anchor: anchor, 524 tag: tag, 525 implicit: implicit, 526 style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE), 527 } 528 return true 529 } 530 if token.typ == yaml_FLOW_MAPPING_START_TOKEN { 531 end_mark = token.end_mark 532 parser.state = yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE 533 *event = yaml_event_t{ 534 typ: yaml_MAPPING_START_EVENT, 535 start_mark: start_mark, 536 end_mark: end_mark, 537 anchor: anchor, 538 tag: tag, 539 implicit: implicit, 540 style: yaml_style_t(yaml_FLOW_MAPPING_STYLE), 541 } 542 return true 543 } 544 if block && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN { 545 end_mark = token.end_mark 546 parser.state = yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE 547 *event = yaml_event_t{ 548 typ: yaml_SEQUENCE_START_EVENT, 549 start_mark: start_mark, 550 end_mark: end_mark, 551 anchor: anchor, 552 tag: tag, 553 implicit: implicit, 554 style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE), 555 } 556 return true 557 } 558 if block && token.typ == yaml_BLOCK_MAPPING_START_TOKEN { 559 end_mark = token.end_mark 560 parser.state = yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE 561 *event = yaml_event_t{ 562 typ: yaml_MAPPING_START_EVENT, 563 start_mark: start_mark, 564 end_mark: end_mark, 565 anchor: anchor, 566 tag: tag, 567 implicit: implicit, 568 style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE), 569 } 570 return true 571 } 572 if len(anchor) > 0 || len(tag) > 0 { 573 parser.state = parser.states[len(parser.states)-1] 574 parser.states = parser.states[:len(parser.states)-1] 575 576 *event = yaml_event_t{ 577 typ: yaml_SCALAR_EVENT, 578 start_mark: start_mark, 579 end_mark: end_mark, 580 anchor: anchor, 581 tag: tag, 582 implicit: implicit, 583 quoted_implicit: false, 584 style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE), 585 } 586 return true 587 } 588 589 context := "while parsing a flow node" 590 if block { 591 context = "while parsing a block node" 592 } 593 yaml_parser_set_parser_error_context(parser, context, start_mark, 594 "did not find expected node content", token.start_mark) 595 return false 596 } 597 598 // Parse the productions: 599 // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END 600 // 601 // ******************** *********** * ********* 602 func yaml_parser_parse_block_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { 603 if first { 604 token := peek_token(parser) 605 parser.marks = append(parser.marks, token.start_mark) 606 skip_token(parser) 607 } 608 609 token := peek_token(parser) 610 if token == nil { 611 return false 612 } 613 614 if token.typ == yaml_BLOCK_ENTRY_TOKEN { 615 mark := token.end_mark 616 skip_token(parser) 617 token = peek_token(parser) 618 if token == nil { 619 return false 620 } 621 if token.typ != yaml_BLOCK_ENTRY_TOKEN && token.typ != yaml_BLOCK_END_TOKEN { 622 parser.states = append(parser.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE) 623 return yaml_parser_parse_node(parser, event, true, false) 624 } else { 625 parser.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE 626 return yaml_parser_process_empty_scalar(parser, event, mark) 627 } 628 } 629 if token.typ == yaml_BLOCK_END_TOKEN { 630 parser.state = parser.states[len(parser.states)-1] 631 parser.states = parser.states[:len(parser.states)-1] 632 parser.marks = parser.marks[:len(parser.marks)-1] 633 634 *event = yaml_event_t{ 635 typ: yaml_SEQUENCE_END_EVENT, 636 start_mark: token.start_mark, 637 end_mark: token.end_mark, 638 } 639 640 skip_token(parser) 641 return true 642 } 643 644 context_mark := parser.marks[len(parser.marks)-1] 645 parser.marks = parser.marks[:len(parser.marks)-1] 646 return yaml_parser_set_parser_error_context(parser, 647 "while parsing a block collection", context_mark, 648 "did not find expected '-' indicator", token.start_mark) 649 } 650 651 // Parse the productions: 652 // indentless_sequence ::= (BLOCK-ENTRY block_node?)+ 653 // 654 // *********** * 655 func yaml_parser_parse_indentless_sequence_entry(parser *yaml_parser_t, event *yaml_event_t) bool { 656 token := peek_token(parser) 657 if token == nil { 658 return false 659 } 660 661 if token.typ == yaml_BLOCK_ENTRY_TOKEN { 662 mark := token.end_mark 663 skip_token(parser) 664 token = peek_token(parser) 665 if token == nil { 666 return false 667 } 668 if token.typ != yaml_BLOCK_ENTRY_TOKEN && 669 token.typ != yaml_KEY_TOKEN && 670 token.typ != yaml_VALUE_TOKEN && 671 token.typ != yaml_BLOCK_END_TOKEN { 672 parser.states = append(parser.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE) 673 return yaml_parser_parse_node(parser, event, true, false) 674 } 675 parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE 676 return yaml_parser_process_empty_scalar(parser, event, mark) 677 } 678 parser.state = parser.states[len(parser.states)-1] 679 parser.states = parser.states[:len(parser.states)-1] 680 681 *event = yaml_event_t{ 682 typ: yaml_SEQUENCE_END_EVENT, 683 start_mark: token.start_mark, 684 end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark? 685 } 686 return true 687 } 688 689 // Parse the productions: 690 // block_mapping ::= BLOCK-MAPPING_START 691 // 692 // ******************* 693 // ((KEY block_node_or_indentless_sequence?)? 694 // *** * 695 // (VALUE block_node_or_indentless_sequence?)?)* 696 // 697 // BLOCK-END 698 // ********* 699 func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { 700 if first { 701 token := peek_token(parser) 702 parser.marks = append(parser.marks, token.start_mark) 703 skip_token(parser) 704 } 705 706 token := peek_token(parser) 707 if token == nil { 708 return false 709 } 710 711 if token.typ == yaml_KEY_TOKEN { 712 mark := token.end_mark 713 skip_token(parser) 714 token = peek_token(parser) 715 if token == nil { 716 return false 717 } 718 if token.typ != yaml_KEY_TOKEN && 719 token.typ != yaml_VALUE_TOKEN && 720 token.typ != yaml_BLOCK_END_TOKEN { 721 parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE) 722 return yaml_parser_parse_node(parser, event, true, true) 723 } else { 724 parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE 725 return yaml_parser_process_empty_scalar(parser, event, mark) 726 } 727 } else if token.typ == yaml_BLOCK_END_TOKEN { 728 parser.state = parser.states[len(parser.states)-1] 729 parser.states = parser.states[:len(parser.states)-1] 730 parser.marks = parser.marks[:len(parser.marks)-1] 731 *event = yaml_event_t{ 732 typ: yaml_MAPPING_END_EVENT, 733 start_mark: token.start_mark, 734 end_mark: token.end_mark, 735 } 736 skip_token(parser) 737 return true 738 } 739 740 context_mark := parser.marks[len(parser.marks)-1] 741 parser.marks = parser.marks[:len(parser.marks)-1] 742 return yaml_parser_set_parser_error_context(parser, 743 "while parsing a block mapping", context_mark, 744 "did not find expected key", token.start_mark) 745 } 746 747 // Parse the productions: 748 // block_mapping ::= BLOCK-MAPPING_START 749 // 750 // ((KEY block_node_or_indentless_sequence?)? 751 // 752 // (VALUE block_node_or_indentless_sequence?)?)* 753 // ***** * 754 // BLOCK-END 755 func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool { 756 token := peek_token(parser) 757 if token == nil { 758 return false 759 } 760 if token.typ == yaml_VALUE_TOKEN { 761 mark := token.end_mark 762 skip_token(parser) 763 token = peek_token(parser) 764 if token == nil { 765 return false 766 } 767 if token.typ != yaml_KEY_TOKEN && 768 token.typ != yaml_VALUE_TOKEN && 769 token.typ != yaml_BLOCK_END_TOKEN { 770 parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE) 771 return yaml_parser_parse_node(parser, event, true, true) 772 } 773 parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE 774 return yaml_parser_process_empty_scalar(parser, event, mark) 775 } 776 parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE 777 return yaml_parser_process_empty_scalar(parser, event, token.start_mark) 778 } 779 780 // Parse the productions: 781 // flow_sequence ::= FLOW-SEQUENCE-START 782 // 783 // ******************* 784 // (flow_sequence_entry FLOW-ENTRY)* 785 // * ********** 786 // flow_sequence_entry? 787 // * 788 // FLOW-SEQUENCE-END 789 // ***************** 790 // 791 // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 792 // 793 // * 794 func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { 795 if first { 796 token := peek_token(parser) 797 parser.marks = append(parser.marks, token.start_mark) 798 skip_token(parser) 799 } 800 token := peek_token(parser) 801 if token == nil { 802 return false 803 } 804 if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { 805 if !first { 806 if token.typ == yaml_FLOW_ENTRY_TOKEN { 807 skip_token(parser) 808 token = peek_token(parser) 809 if token == nil { 810 return false 811 } 812 } else { 813 context_mark := parser.marks[len(parser.marks)-1] 814 parser.marks = parser.marks[:len(parser.marks)-1] 815 return yaml_parser_set_parser_error_context(parser, 816 "while parsing a flow sequence", context_mark, 817 "did not find expected ',' or ']'", token.start_mark) 818 } 819 } 820 821 if token.typ == yaml_KEY_TOKEN { 822 parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE 823 *event = yaml_event_t{ 824 typ: yaml_MAPPING_START_EVENT, 825 start_mark: token.start_mark, 826 end_mark: token.end_mark, 827 implicit: true, 828 style: yaml_style_t(yaml_FLOW_MAPPING_STYLE), 829 } 830 skip_token(parser) 831 return true 832 } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { 833 parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE) 834 return yaml_parser_parse_node(parser, event, false, false) 835 } 836 } 837 838 parser.state = parser.states[len(parser.states)-1] 839 parser.states = parser.states[:len(parser.states)-1] 840 parser.marks = parser.marks[:len(parser.marks)-1] 841 842 *event = yaml_event_t{ 843 typ: yaml_SEQUENCE_END_EVENT, 844 start_mark: token.start_mark, 845 end_mark: token.end_mark, 846 } 847 848 skip_token(parser) 849 return true 850 } 851 852 // Parse the productions: 853 // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 854 // 855 // *** * 856 func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool { 857 token := peek_token(parser) 858 if token == nil { 859 return false 860 } 861 if token.typ != yaml_VALUE_TOKEN && 862 token.typ != yaml_FLOW_ENTRY_TOKEN && 863 token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { 864 parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE) 865 return yaml_parser_parse_node(parser, event, false, false) 866 } 867 mark := token.end_mark 868 skip_token(parser) 869 parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE 870 return yaml_parser_process_empty_scalar(parser, event, mark) 871 } 872 873 // Parse the productions: 874 // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 875 // 876 // ***** * 877 func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool { 878 token := peek_token(parser) 879 if token == nil { 880 return false 881 } 882 if token.typ == yaml_VALUE_TOKEN { 883 skip_token(parser) 884 token := peek_token(parser) 885 if token == nil { 886 return false 887 } 888 if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { 889 parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE) 890 return yaml_parser_parse_node(parser, event, false, false) 891 } 892 } 893 parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE 894 return yaml_parser_process_empty_scalar(parser, event, token.start_mark) 895 } 896 897 // Parse the productions: 898 // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 899 // 900 // * 901 func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool { 902 token := peek_token(parser) 903 if token == nil { 904 return false 905 } 906 parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE 907 *event = yaml_event_t{ 908 typ: yaml_MAPPING_END_EVENT, 909 start_mark: token.start_mark, 910 end_mark: token.start_mark, // [Go] Shouldn't this be end_mark? 911 } 912 return true 913 } 914 915 // Parse the productions: 916 // flow_mapping ::= FLOW-MAPPING-START 917 // 918 // ****************** 919 // (flow_mapping_entry FLOW-ENTRY)* 920 // * ********** 921 // flow_mapping_entry? 922 // ****************** 923 // FLOW-MAPPING-END 924 // **************** 925 // 926 // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 927 // - *** * 928 func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool { 929 if first { 930 token := peek_token(parser) 931 parser.marks = append(parser.marks, token.start_mark) 932 skip_token(parser) 933 } 934 935 token := peek_token(parser) 936 if token == nil { 937 return false 938 } 939 940 if token.typ != yaml_FLOW_MAPPING_END_TOKEN { 941 if !first { 942 if token.typ == yaml_FLOW_ENTRY_TOKEN { 943 skip_token(parser) 944 token = peek_token(parser) 945 if token == nil { 946 return false 947 } 948 } else { 949 context_mark := parser.marks[len(parser.marks)-1] 950 parser.marks = parser.marks[:len(parser.marks)-1] 951 return yaml_parser_set_parser_error_context(parser, 952 "while parsing a flow mapping", context_mark, 953 "did not find expected ',' or '}'", token.start_mark) 954 } 955 } 956 957 if token.typ == yaml_KEY_TOKEN { 958 skip_token(parser) 959 token = peek_token(parser) 960 if token == nil { 961 return false 962 } 963 if token.typ != yaml_VALUE_TOKEN && 964 token.typ != yaml_FLOW_ENTRY_TOKEN && 965 token.typ != yaml_FLOW_MAPPING_END_TOKEN { 966 parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE) 967 return yaml_parser_parse_node(parser, event, false, false) 968 } else { 969 parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE 970 return yaml_parser_process_empty_scalar(parser, event, token.start_mark) 971 } 972 } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN { 973 parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE) 974 return yaml_parser_parse_node(parser, event, false, false) 975 } 976 } 977 978 parser.state = parser.states[len(parser.states)-1] 979 parser.states = parser.states[:len(parser.states)-1] 980 parser.marks = parser.marks[:len(parser.marks)-1] 981 *event = yaml_event_t{ 982 typ: yaml_MAPPING_END_EVENT, 983 start_mark: token.start_mark, 984 end_mark: token.end_mark, 985 } 986 skip_token(parser) 987 return true 988 } 989 990 // Parse the productions: 991 // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)? 992 // - ***** * 993 func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool { 994 token := peek_token(parser) 995 if token == nil { 996 return false 997 } 998 if empty { 999 parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE 1000 return yaml_parser_process_empty_scalar(parser, event, token.start_mark) 1001 } 1002 if token.typ == yaml_VALUE_TOKEN { 1003 skip_token(parser) 1004 token = peek_token(parser) 1005 if token == nil { 1006 return false 1007 } 1008 if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN { 1009 parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE) 1010 return yaml_parser_parse_node(parser, event, false, false) 1011 } 1012 } 1013 parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE 1014 return yaml_parser_process_empty_scalar(parser, event, token.start_mark) 1015 } 1016 1017 // Generate an empty scalar event. 1018 func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool { 1019 *event = yaml_event_t{ 1020 typ: yaml_SCALAR_EVENT, 1021 start_mark: mark, 1022 end_mark: mark, 1023 value: nil, // Empty 1024 implicit: true, 1025 style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE), 1026 } 1027 return true 1028 } 1029 1030 var default_tag_directives = []yaml_tag_directive_t{ 1031 {[]byte("!"), []byte("!")}, 1032 {[]byte("!!"), []byte("tag:yaml.org,2002:")}, 1033 } 1034 1035 // Parse directives. 1036 func yaml_parser_process_directives(parser *yaml_parser_t, version_directive_ref **yaml_version_directive_t, 1037 tag_directives_ref *[]yaml_tag_directive_t) bool { 1038 1039 var version_directive *yaml_version_directive_t 1040 var tag_directives []yaml_tag_directive_t 1041 1042 token := peek_token(parser) 1043 if token == nil { 1044 return false 1045 } 1046 1047 for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN { 1048 if token.typ == yaml_VERSION_DIRECTIVE_TOKEN { 1049 if version_directive != nil { 1050 yaml_parser_set_parser_error(parser, 1051 "found duplicate %YAML directive", token.start_mark) 1052 return false 1053 } 1054 if token.major != 1 || token.minor != 1 { 1055 yaml_parser_set_parser_error(parser, 1056 "found incompatible YAML document", token.start_mark) 1057 return false 1058 } 1059 version_directive = &yaml_version_directive_t{ 1060 major: token.major, 1061 minor: token.minor, 1062 } 1063 } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN { 1064 value := yaml_tag_directive_t{ 1065 handle: token.value, 1066 prefix: token.prefix, 1067 } 1068 if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) { 1069 return false 1070 } 1071 tag_directives = append(tag_directives, value) 1072 } 1073 1074 skip_token(parser) 1075 token = peek_token(parser) 1076 if token == nil { 1077 return false 1078 } 1079 } 1080 1081 for i := range default_tag_directives { 1082 if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) { 1083 return false 1084 } 1085 } 1086 1087 if version_directive_ref != nil { 1088 *version_directive_ref = version_directive 1089 } 1090 if tag_directives_ref != nil { 1091 *tag_directives_ref = tag_directives 1092 } 1093 return true 1094 } 1095 1096 // Append a tag directive to the directives stack. 1097 func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool { 1098 for i := range parser.tag_directives { 1099 if bytes.Equal(value.handle, parser.tag_directives[i].handle) { 1100 if allow_duplicates { 1101 return true 1102 } 1103 return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark) 1104 } 1105 } 1106 1107 // [Go] I suspect the copy is unnecessary. This was likely done 1108 // because there was no way to track ownership of the data. 1109 value_copy := yaml_tag_directive_t{ 1110 handle: make([]byte, len(value.handle)), 1111 prefix: make([]byte, len(value.prefix)), 1112 } 1113 copy(value_copy.handle, value.handle) 1114 copy(value_copy.prefix, value.prefix) 1115 parser.tag_directives = append(parser.tag_directives, value_copy) 1116 return true 1117 }