1package yaml 2 3import ( 4 "io" 5 "os" 6) 7 8func yaml_insert_token(parser *yaml_parser_t, pos int, token *yaml_token_t) { 9 //fmt.Println("yaml_insert_token", "pos:", pos, "typ:", token.typ, "head:", parser.tokens_head, "len:", len(parser.tokens)) 10 11 // Check if we can move the queue at the beginning of the buffer. 12 if parser.tokens_head > 0 && len(parser.tokens) == cap(parser.tokens) { 13 if parser.tokens_head != len(parser.tokens) { 14 copy(parser.tokens, parser.tokens[parser.tokens_head:]) 15 } 16 parser.tokens = parser.tokens[:len(parser.tokens)-parser.tokens_head] 17 parser.tokens_head = 0 18 } 19 parser.tokens = append(parser.tokens, *token) 20 if pos < 0 { 21 return 22 } 23 copy(parser.tokens[parser.tokens_head+pos+1:], parser.tokens[parser.tokens_head+pos:]) 24 parser.tokens[parser.tokens_head+pos] = *token 25} 26 27// Create a new parser object. 28func yaml_parser_initialize(parser *yaml_parser_t) bool { 29 *parser = yaml_parser_t{ 30 raw_buffer: make([]byte, 0, input_raw_buffer_size), 31 buffer: make([]byte, 0, input_buffer_size), 32 } 33 return true 34} 35 36// Destroy a parser object. 37func yaml_parser_delete(parser *yaml_parser_t) { 38 *parser = yaml_parser_t{} 39} 40 41// String read handler. 42func yaml_string_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) { 43 if parser.input_pos == len(parser.input) { 44 return 0, io.EOF 45 } 46 n = copy(buffer, parser.input[parser.input_pos:]) 47 parser.input_pos += n 48 return n, nil 49} 50 51// File read handler. 52func yaml_file_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) { 53 return parser.input_file.Read(buffer) 54} 55 56// Set a string input. 57func yaml_parser_set_input_string(parser *yaml_parser_t, input []byte) { 58 if parser.read_handler != nil { 59 panic("must set the input source only once") 60 } 61 parser.read_handler = yaml_string_read_handler 62 parser.input = input 63 parser.input_pos = 0 64} 65 66// Set a file input. 67func yaml_parser_set_input_file(parser *yaml_parser_t, file *os.File) { 68 if parser.read_handler != nil { 69 panic("must set the input source only once") 70 } 71 parser.read_handler = yaml_file_read_handler 72 parser.input_file = file 73} 74 75// Set the source encoding. 76func yaml_parser_set_encoding(parser *yaml_parser_t, encoding yaml_encoding_t) { 77 if parser.encoding != yaml_ANY_ENCODING { 78 panic("must set the encoding only once") 79 } 80 parser.encoding = encoding 81} 82 83// Create a new emitter object. 84func yaml_emitter_initialize(emitter *yaml_emitter_t) bool { 85 *emitter = yaml_emitter_t{ 86 buffer: make([]byte, output_buffer_size), 87 raw_buffer: make([]byte, 0, output_raw_buffer_size), 88 states: make([]yaml_emitter_state_t, 0, initial_stack_size), 89 events: make([]yaml_event_t, 0, initial_queue_size), 90 } 91 return true 92} 93 94// Destroy an emitter object. 95func yaml_emitter_delete(emitter *yaml_emitter_t) { 96 *emitter = yaml_emitter_t{} 97} 98 99// String write handler. 100func yaml_string_write_handler(emitter *yaml_emitter_t, buffer []byte) error { 101 *emitter.output_buffer = append(*emitter.output_buffer, buffer...) 102 return nil 103} 104 105// File write handler. 106func yaml_file_write_handler(emitter *yaml_emitter_t, buffer []byte) error { 107 _, err := emitter.output_file.Write(buffer) 108 return err 109} 110 111// Set a string output. 112func yaml_emitter_set_output_string(emitter *yaml_emitter_t, output_buffer *[]byte) { 113 if emitter.write_handler != nil { 114 panic("must set the output target only once") 115 } 116 emitter.write_handler = yaml_string_write_handler 117 emitter.output_buffer = output_buffer 118} 119 120// Set a file output. 121func yaml_emitter_set_output_file(emitter *yaml_emitter_t, file io.Writer) { 122 if emitter.write_handler != nil { 123 panic("must set the output target only once") 124 } 125 emitter.write_handler = yaml_file_write_handler 126 emitter.output_file = file 127} 128 129// Set the output encoding. 130func yaml_emitter_set_encoding(emitter *yaml_emitter_t, encoding yaml_encoding_t) { 131 if emitter.encoding != yaml_ANY_ENCODING { 132 panic("must set the output encoding only once") 133 } 134 emitter.encoding = encoding 135} 136 137// Set the canonical output style. 138func yaml_emitter_set_canonical(emitter *yaml_emitter_t, canonical bool) { 139 emitter.canonical = canonical 140} 141 142//// Set the indentation increment. 143func yaml_emitter_set_indent(emitter *yaml_emitter_t, indent int) { 144 if indent < 2 || indent > 9 { 145 indent = 2 146 } 147 emitter.best_indent = indent 148} 149 150// Set the preferred line width. 151func yaml_emitter_set_width(emitter *yaml_emitter_t, width int) { 152 if width < 0 { 153 width = -1 154 } 155 emitter.best_width = width 156} 157 158// Set if unescaped non-ASCII characters are allowed. 159func yaml_emitter_set_unicode(emitter *yaml_emitter_t, unicode bool) { 160 emitter.unicode = unicode 161} 162 163// Set the preferred line break character. 164func yaml_emitter_set_break(emitter *yaml_emitter_t, line_break yaml_break_t) { 165 emitter.line_break = line_break 166} 167 168///* 169// * Destroy a token object. 170// */ 171// 172//YAML_DECLARE(void) 173//yaml_token_delete(yaml_token_t *token) 174//{ 175// assert(token); // Non-NULL token object expected. 176// 177// switch (token.type) 178// { 179// case YAML_TAG_DIRECTIVE_TOKEN: 180// yaml_free(token.data.tag_directive.handle); 181// yaml_free(token.data.tag_directive.prefix); 182// break; 183// 184// case YAML_ALIAS_TOKEN: 185// yaml_free(token.data.alias.value); 186// break; 187// 188// case YAML_ANCHOR_TOKEN: 189// yaml_free(token.data.anchor.value); 190// break; 191// 192// case YAML_TAG_TOKEN: 193// yaml_free(token.data.tag.handle); 194// yaml_free(token.data.tag.suffix); 195// break; 196// 197// case YAML_SCALAR_TOKEN: 198// yaml_free(token.data.scalar.value); 199// break; 200// 201// default: 202// break; 203// } 204// 205// memset(token, 0, sizeof(yaml_token_t)); 206//} 207// 208///* 209// * Check if a string is a valid UTF-8 sequence. 210// * 211// * Check 'reader.c' for more details on UTF-8 encoding. 212// */ 213// 214//static int 215//yaml_check_utf8(yaml_char_t *start, size_t length) 216//{ 217// yaml_char_t *end = start+length; 218// yaml_char_t *pointer = start; 219// 220// while (pointer < end) { 221// unsigned char octet; 222// unsigned int width; 223// unsigned int value; 224// size_t k; 225// 226// octet = pointer[0]; 227// width = (octet & 0x80) == 0x00 ? 1 : 228// (octet & 0xE0) == 0xC0 ? 2 : 229// (octet & 0xF0) == 0xE0 ? 3 : 230// (octet & 0xF8) == 0xF0 ? 4 : 0; 231// value = (octet & 0x80) == 0x00 ? octet & 0x7F : 232// (octet & 0xE0) == 0xC0 ? octet & 0x1F : 233// (octet & 0xF0) == 0xE0 ? octet & 0x0F : 234// (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; 235// if (!width) return 0; 236// if (pointer+width > end) return 0; 237// for (k = 1; k < width; k ++) { 238// octet = pointer[k]; 239// if ((octet & 0xC0) != 0x80) return 0; 240// value = (value << 6) + (octet & 0x3F); 241// } 242// if (!((width == 1) || 243// (width == 2 && value >= 0x80) || 244// (width == 3 && value >= 0x800) || 245// (width == 4 && value >= 0x10000))) return 0; 246// 247// pointer += width; 248// } 249// 250// return 1; 251//} 252// 253 254// Create STREAM-START. 255func yaml_stream_start_event_initialize(event *yaml_event_t, encoding yaml_encoding_t) bool { 256 *event = yaml_event_t{ 257 typ: yaml_STREAM_START_EVENT, 258 encoding: encoding, 259 } 260 return true 261} 262 263// Create STREAM-END. 264func yaml_stream_end_event_initialize(event *yaml_event_t) bool { 265 *event = yaml_event_t{ 266 typ: yaml_STREAM_END_EVENT, 267 } 268 return true 269} 270 271// Create DOCUMENT-START. 272func yaml_document_start_event_initialize(event *yaml_event_t, version_directive *yaml_version_directive_t, 273 tag_directives []yaml_tag_directive_t, implicit bool) bool { 274 *event = yaml_event_t{ 275 typ: yaml_DOCUMENT_START_EVENT, 276 version_directive: version_directive, 277 tag_directives: tag_directives, 278 implicit: implicit, 279 } 280 return true 281} 282 283// Create DOCUMENT-END. 284func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) bool { 285 *event = yaml_event_t{ 286 typ: yaml_DOCUMENT_END_EVENT, 287 implicit: implicit, 288 } 289 return true 290} 291 292///* 293// * Create ALIAS. 294// */ 295// 296//YAML_DECLARE(int) 297//yaml_alias_event_initialize(event *yaml_event_t, anchor *yaml_char_t) 298//{ 299// mark yaml_mark_t = { 0, 0, 0 } 300// anchor_copy *yaml_char_t = NULL 301// 302// assert(event) // Non-NULL event object is expected. 303// assert(anchor) // Non-NULL anchor is expected. 304// 305// if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0 306// 307// anchor_copy = yaml_strdup(anchor) 308// if (!anchor_copy) 309// return 0 310// 311// ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark) 312// 313// return 1 314//} 315 316// Create SCALAR. 317func yaml_scalar_event_initialize(event *yaml_event_t, anchor, tag, value []byte, plain_implicit, quoted_implicit bool, style yaml_scalar_style_t) bool { 318 *event = yaml_event_t{ 319 typ: yaml_SCALAR_EVENT, 320 anchor: anchor, 321 tag: tag, 322 value: value, 323 implicit: plain_implicit, 324 quoted_implicit: quoted_implicit, 325 style: yaml_style_t(style), 326 } 327 return true 328} 329 330// Create SEQUENCE-START. 331func yaml_sequence_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_sequence_style_t) bool { 332 *event = yaml_event_t{ 333 typ: yaml_SEQUENCE_START_EVENT, 334 anchor: anchor, 335 tag: tag, 336 implicit: implicit, 337 style: yaml_style_t(style), 338 } 339 return true 340} 341 342// Create SEQUENCE-END. 343func yaml_sequence_end_event_initialize(event *yaml_event_t) bool { 344 *event = yaml_event_t{ 345 typ: yaml_SEQUENCE_END_EVENT, 346 } 347 return true 348} 349 350// Create MAPPING-START. 351func yaml_mapping_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_mapping_style_t) bool { 352 *event = yaml_event_t{ 353 typ: yaml_MAPPING_START_EVENT, 354 anchor: anchor, 355 tag: tag, 356 implicit: implicit, 357 style: yaml_style_t(style), 358 } 359 return true 360} 361 362// Create MAPPING-END. 363func yaml_mapping_end_event_initialize(event *yaml_event_t) bool { 364 *event = yaml_event_t{ 365 typ: yaml_MAPPING_END_EVENT, 366 } 367 return true 368} 369 370// Destroy an event object. 371func yaml_event_delete(event *yaml_event_t) { 372 *event = yaml_event_t{} 373} 374 375///* 376// * Create a document object. 377// */ 378// 379//YAML_DECLARE(int) 380//yaml_document_initialize(document *yaml_document_t, 381// version_directive *yaml_version_directive_t, 382// tag_directives_start *yaml_tag_directive_t, 383// tag_directives_end *yaml_tag_directive_t, 384// start_implicit int, end_implicit int) 385//{ 386// struct { 387// error yaml_error_type_t 388// } context 389// struct { 390// start *yaml_node_t 391// end *yaml_node_t 392// top *yaml_node_t 393// } nodes = { NULL, NULL, NULL } 394// version_directive_copy *yaml_version_directive_t = NULL 395// struct { 396// start *yaml_tag_directive_t 397// end *yaml_tag_directive_t 398// top *yaml_tag_directive_t 399// } tag_directives_copy = { NULL, NULL, NULL } 400// value yaml_tag_directive_t = { NULL, NULL } 401// mark yaml_mark_t = { 0, 0, 0 } 402// 403// assert(document) // Non-NULL document object is expected. 404// assert((tag_directives_start && tag_directives_end) || 405// (tag_directives_start == tag_directives_end)) 406// // Valid tag directives are expected. 407// 408// if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error 409// 410// if (version_directive) { 411// version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t)) 412// if (!version_directive_copy) goto error 413// version_directive_copy.major = version_directive.major 414// version_directive_copy.minor = version_directive.minor 415// } 416// 417// if (tag_directives_start != tag_directives_end) { 418// tag_directive *yaml_tag_directive_t 419// if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE)) 420// goto error 421// for (tag_directive = tag_directives_start 422// tag_directive != tag_directives_end; tag_directive ++) { 423// assert(tag_directive.handle) 424// assert(tag_directive.prefix) 425// if (!yaml_check_utf8(tag_directive.handle, 426// strlen((char *)tag_directive.handle))) 427// goto error 428// if (!yaml_check_utf8(tag_directive.prefix, 429// strlen((char *)tag_directive.prefix))) 430// goto error 431// value.handle = yaml_strdup(tag_directive.handle) 432// value.prefix = yaml_strdup(tag_directive.prefix) 433// if (!value.handle || !value.prefix) goto error 434// if (!PUSH(&context, tag_directives_copy, value)) 435// goto error 436// value.handle = NULL 437// value.prefix = NULL 438// } 439// } 440// 441// DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy, 442// tag_directives_copy.start, tag_directives_copy.top, 443// start_implicit, end_implicit, mark, mark) 444// 445// return 1 446// 447//error: 448// STACK_DEL(&context, nodes) 449// yaml_free(version_directive_copy) 450// while (!STACK_EMPTY(&context, tag_directives_copy)) { 451// value yaml_tag_directive_t = POP(&context, tag_directives_copy) 452// yaml_free(value.handle) 453// yaml_free(value.prefix) 454// } 455// STACK_DEL(&context, tag_directives_copy) 456// yaml_free(value.handle) 457// yaml_free(value.prefix) 458// 459// return 0 460//} 461// 462///* 463// * Destroy a document object. 464// */ 465// 466//YAML_DECLARE(void) 467//yaml_document_delete(document *yaml_document_t) 468//{ 469// struct { 470// error yaml_error_type_t 471// } context 472// tag_directive *yaml_tag_directive_t 473// 474// context.error = YAML_NO_ERROR // Eliminate a compliler warning. 475// 476// assert(document) // Non-NULL document object is expected. 477// 478// while (!STACK_EMPTY(&context, document.nodes)) { 479// node yaml_node_t = POP(&context, document.nodes) 480// yaml_free(node.tag) 481// switch (node.type) { 482// case YAML_SCALAR_NODE: 483// yaml_free(node.data.scalar.value) 484// break 485// case YAML_SEQUENCE_NODE: 486// STACK_DEL(&context, node.data.sequence.items) 487// break 488// case YAML_MAPPING_NODE: 489// STACK_DEL(&context, node.data.mapping.pairs) 490// break 491// default: 492// assert(0) // Should not happen. 493// } 494// } 495// STACK_DEL(&context, document.nodes) 496// 497// yaml_free(document.version_directive) 498// for (tag_directive = document.tag_directives.start 499// tag_directive != document.tag_directives.end 500// tag_directive++) { 501// yaml_free(tag_directive.handle) 502// yaml_free(tag_directive.prefix) 503// } 504// yaml_free(document.tag_directives.start) 505// 506// memset(document, 0, sizeof(yaml_document_t)) 507//} 508// 509///** 510// * Get a document node. 511// */ 512// 513//YAML_DECLARE(yaml_node_t *) 514//yaml_document_get_node(document *yaml_document_t, index int) 515//{ 516// assert(document) // Non-NULL document object is expected. 517// 518// if (index > 0 && document.nodes.start + index <= document.nodes.top) { 519// return document.nodes.start + index - 1 520// } 521// return NULL 522//} 523// 524///** 525// * Get the root object. 526// */ 527// 528//YAML_DECLARE(yaml_node_t *) 529//yaml_document_get_root_node(document *yaml_document_t) 530//{ 531// assert(document) // Non-NULL document object is expected. 532// 533// if (document.nodes.top != document.nodes.start) { 534// return document.nodes.start 535// } 536// return NULL 537//} 538// 539///* 540// * Add a scalar node to a document. 541// */ 542// 543//YAML_DECLARE(int) 544//yaml_document_add_scalar(document *yaml_document_t, 545// tag *yaml_char_t, value *yaml_char_t, length int, 546// style yaml_scalar_style_t) 547//{ 548// struct { 549// error yaml_error_type_t 550// } context 551// mark yaml_mark_t = { 0, 0, 0 } 552// tag_copy *yaml_char_t = NULL 553// value_copy *yaml_char_t = NULL 554// node yaml_node_t 555// 556// assert(document) // Non-NULL document object is expected. 557// assert(value) // Non-NULL value is expected. 558// 559// if (!tag) { 560// tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG 561// } 562// 563// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error 564// tag_copy = yaml_strdup(tag) 565// if (!tag_copy) goto error 566// 567// if (length < 0) { 568// length = strlen((char *)value) 569// } 570// 571// if (!yaml_check_utf8(value, length)) goto error 572// value_copy = yaml_malloc(length+1) 573// if (!value_copy) goto error 574// memcpy(value_copy, value, length) 575// value_copy[length] = '\0' 576// 577// SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark) 578// if (!PUSH(&context, document.nodes, node)) goto error 579// 580// return document.nodes.top - document.nodes.start 581// 582//error: 583// yaml_free(tag_copy) 584// yaml_free(value_copy) 585// 586// return 0 587//} 588// 589///* 590// * Add a sequence node to a document. 591// */ 592// 593//YAML_DECLARE(int) 594//yaml_document_add_sequence(document *yaml_document_t, 595// tag *yaml_char_t, style yaml_sequence_style_t) 596//{ 597// struct { 598// error yaml_error_type_t 599// } context 600// mark yaml_mark_t = { 0, 0, 0 } 601// tag_copy *yaml_char_t = NULL 602// struct { 603// start *yaml_node_item_t 604// end *yaml_node_item_t 605// top *yaml_node_item_t 606// } items = { NULL, NULL, NULL } 607// node yaml_node_t 608// 609// assert(document) // Non-NULL document object is expected. 610// 611// if (!tag) { 612// tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG 613// } 614// 615// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error 616// tag_copy = yaml_strdup(tag) 617// if (!tag_copy) goto error 618// 619// if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error 620// 621// SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, 622// style, mark, mark) 623// if (!PUSH(&context, document.nodes, node)) goto error 624// 625// return document.nodes.top - document.nodes.start 626// 627//error: 628// STACK_DEL(&context, items) 629// yaml_free(tag_copy) 630// 631// return 0 632//} 633// 634///* 635// * Add a mapping node to a document. 636// */ 637// 638//YAML_DECLARE(int) 639//yaml_document_add_mapping(document *yaml_document_t, 640// tag *yaml_char_t, style yaml_mapping_style_t) 641//{ 642// struct { 643// error yaml_error_type_t 644// } context 645// mark yaml_mark_t = { 0, 0, 0 } 646// tag_copy *yaml_char_t = NULL 647// struct { 648// start *yaml_node_pair_t 649// end *yaml_node_pair_t 650// top *yaml_node_pair_t 651// } pairs = { NULL, NULL, NULL } 652// node yaml_node_t 653// 654// assert(document) // Non-NULL document object is expected. 655// 656// if (!tag) { 657// tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG 658// } 659// 660// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error 661// tag_copy = yaml_strdup(tag) 662// if (!tag_copy) goto error 663// 664// if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error 665// 666// MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, 667// style, mark, mark) 668// if (!PUSH(&context, document.nodes, node)) goto error 669// 670// return document.nodes.top - document.nodes.start 671// 672//error: 673// STACK_DEL(&context, pairs) 674// yaml_free(tag_copy) 675// 676// return 0 677//} 678// 679///* 680// * Append an item to a sequence node. 681// */ 682// 683//YAML_DECLARE(int) 684//yaml_document_append_sequence_item(document *yaml_document_t, 685// sequence int, item int) 686//{ 687// struct { 688// error yaml_error_type_t 689// } context 690// 691// assert(document) // Non-NULL document is required. 692// assert(sequence > 0 693// && document.nodes.start + sequence <= document.nodes.top) 694// // Valid sequence id is required. 695// assert(document.nodes.start[sequence-1].type == YAML_SEQUENCE_NODE) 696// // A sequence node is required. 697// assert(item > 0 && document.nodes.start + item <= document.nodes.top) 698// // Valid item id is required. 699// 700// if (!PUSH(&context, 701// document.nodes.start[sequence-1].data.sequence.items, item)) 702// return 0 703// 704// return 1 705//} 706// 707///* 708// * Append a pair of a key and a value to a mapping node. 709// */ 710// 711//YAML_DECLARE(int) 712//yaml_document_append_mapping_pair(document *yaml_document_t, 713// mapping int, key int, value int) 714//{ 715// struct { 716// error yaml_error_type_t 717// } context 718// 719// pair yaml_node_pair_t 720// 721// assert(document) // Non-NULL document is required. 722// assert(mapping > 0 723// && document.nodes.start + mapping <= document.nodes.top) 724// // Valid mapping id is required. 725// assert(document.nodes.start[mapping-1].type == YAML_MAPPING_NODE) 726// // A mapping node is required. 727// assert(key > 0 && document.nodes.start + key <= document.nodes.top) 728// // Valid key id is required. 729// assert(value > 0 && document.nodes.start + value <= document.nodes.top) 730// // Valid value id is required. 731// 732// pair.key = key 733// pair.value = value 734// 735// if (!PUSH(&context, 736// document.nodes.start[mapping-1].data.mapping.pairs, pair)) 737// return 0 738// 739// return 1 740//} 741// 742// 743