8 func 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))
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:])
16 parser.tokens = parser.tokens[:len(parser.tokens)-parser.tokens_head]
17 parser.tokens_head = 0
19 parser.tokens = append(parser.tokens, *token)
23 copy(parser.tokens[parser.tokens_head+pos+1:], parser.tokens[parser.tokens_head+pos:])
24 parser.tokens[parser.tokens_head+pos] = *token
27 // Create a new parser object.
28 func 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),
36 // Destroy a parser object.
37 func yaml_parser_delete(parser *yaml_parser_t) {
38 *parser = yaml_parser_t{}
41 // String read handler.
42 func yaml_string_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) {
43 if parser.input_pos == len(parser.input) {
46 n = copy(buffer, parser.input[parser.input_pos:])
52 func yaml_file_read_handler(parser *yaml_parser_t, buffer []byte) (n int, err error) {
53 return parser.input_file.Read(buffer)
56 // Set a string input.
57 func 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")
61 parser.read_handler = yaml_string_read_handler
67 func 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")
71 parser.read_handler = yaml_file_read_handler
72 parser.input_file = file
75 // Set the source encoding.
76 func 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")
80 parser.encoding = encoding
83 // Create a new emitter object.
84 func 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),
94 // Destroy an emitter object.
95 func yaml_emitter_delete(emitter *yaml_emitter_t) {
96 *emitter = yaml_emitter_t{}
99 // String write handler.
100 func yaml_string_write_handler(emitter *yaml_emitter_t, buffer []byte) error {
101 *emitter.output_buffer = append(*emitter.output_buffer, buffer...)
105 // File write handler.
106 func yaml_file_write_handler(emitter *yaml_emitter_t, buffer []byte) error {
107 _, err := emitter.output_file.Write(buffer)
111 // Set a string output.
112 func 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")
116 emitter.write_handler = yaml_string_write_handler
117 emitter.output_buffer = output_buffer
120 // Set a file output.
121 func 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")
125 emitter.write_handler = yaml_file_write_handler
126 emitter.output_file = file
129 // Set the output encoding.
130 func 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")
134 emitter.encoding = encoding
137 // Set the canonical output style.
138 func yaml_emitter_set_canonical(emitter *yaml_emitter_t, canonical bool) {
139 emitter.canonical = canonical
142 //// Set the indentation increment.
143 func yaml_emitter_set_indent(emitter *yaml_emitter_t, indent int) {
144 if indent < 2 || indent > 9 {
147 emitter.best_indent = indent
150 // Set the preferred line width.
151 func yaml_emitter_set_width(emitter *yaml_emitter_t, width int) {
155 emitter.best_width = width
158 // Set if unescaped non-ASCII characters are allowed.
159 func yaml_emitter_set_unicode(emitter *yaml_emitter_t, unicode bool) {
160 emitter.unicode = unicode
163 // Set the preferred line break character.
164 func yaml_emitter_set_break(emitter *yaml_emitter_t, line_break yaml_break_t) {
165 emitter.line_break = line_break
169 // * Destroy a token object.
173 //yaml_token_delete(yaml_token_t *token)
175 // assert(token); // Non-NULL token object expected.
177 // switch (token.type)
179 // case YAML_TAG_DIRECTIVE_TOKEN:
180 // yaml_free(token.data.tag_directive.handle);
181 // yaml_free(token.data.tag_directive.prefix);
184 // case YAML_ALIAS_TOKEN:
185 // yaml_free(token.data.alias.value);
188 // case YAML_ANCHOR_TOKEN:
189 // yaml_free(token.data.anchor.value);
192 // case YAML_TAG_TOKEN:
193 // yaml_free(token.data.tag.handle);
194 // yaml_free(token.data.tag.suffix);
197 // case YAML_SCALAR_TOKEN:
198 // yaml_free(token.data.scalar.value);
205 // memset(token, 0, sizeof(yaml_token_t));
209 // * Check if a string is a valid UTF-8 sequence.
211 // * Check 'reader.c' for more details on UTF-8 encoding.
215 //yaml_check_utf8(yaml_char_t *start, size_t length)
217 // yaml_char_t *end = start+length;
218 // yaml_char_t *pointer = start;
220 // while (pointer < end) {
221 // unsigned char octet;
222 // unsigned int width;
223 // unsigned int value;
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);
242 // if (!((width == 1) ||
243 // (width == 2 && value >= 0x80) ||
244 // (width == 3 && value >= 0x800) ||
245 // (width == 4 && value >= 0x10000))) return 0;
254 // Create STREAM-START.
255 func 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,
263 // Create STREAM-END.
264 func yaml_stream_end_event_initialize(event *yaml_event_t) bool {
265 *event = yaml_event_t{
266 typ: yaml_STREAM_END_EVENT,
271 // Create DOCUMENT-START.
272 func 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,
283 // Create DOCUMENT-END.
284 func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) bool {
285 *event = yaml_event_t{
286 typ: yaml_DOCUMENT_END_EVENT,
297 //yaml_alias_event_initialize(event *yaml_event_t, anchor *yaml_char_t)
299 // mark yaml_mark_t = { 0, 0, 0 }
300 // anchor_copy *yaml_char_t = NULL
302 // assert(event) // Non-NULL event object is expected.
303 // assert(anchor) // Non-NULL anchor is expected.
305 // if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0
307 // anchor_copy = yaml_strdup(anchor)
311 // ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark)
317 func 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,
323 implicit: plain_implicit,
324 quoted_implicit: quoted_implicit,
325 style: yaml_style_t(style),
330 // Create SEQUENCE-START.
331 func 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,
337 style: yaml_style_t(style),
342 // Create SEQUENCE-END.
343 func yaml_sequence_end_event_initialize(event *yaml_event_t) bool {
344 *event = yaml_event_t{
345 typ: yaml_SEQUENCE_END_EVENT,
350 // Create MAPPING-START.
351 func 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,
357 style: yaml_style_t(style),
362 // Create MAPPING-END.
363 func yaml_mapping_end_event_initialize(event *yaml_event_t) bool {
364 *event = yaml_event_t{
365 typ: yaml_MAPPING_END_EVENT,
370 // Destroy an event object.
371 func yaml_event_delete(event *yaml_event_t) {
372 *event = yaml_event_t{}
376 // * Create a document object.
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)
387 // error yaml_error_type_t
390 // start *yaml_node_t
393 // } nodes = { NULL, NULL, NULL }
394 // version_directive_copy *yaml_version_directive_t = NULL
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 }
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.
408 // if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error
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
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))
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)))
428 // if (!yaml_check_utf8(tag_directive.prefix,
429 // strlen((char *)tag_directive.prefix)))
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))
436 // value.handle = NULL
437 // value.prefix = NULL
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)
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)
455 // STACK_DEL(&context, tag_directives_copy)
456 // yaml_free(value.handle)
457 // yaml_free(value.prefix)
463 // * Destroy a document object.
467 //yaml_document_delete(document *yaml_document_t)
470 // error yaml_error_type_t
472 // tag_directive *yaml_tag_directive_t
474 // context.error = YAML_NO_ERROR // Eliminate a compliler warning.
476 // assert(document) // Non-NULL document object is expected.
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)
485 // case YAML_SEQUENCE_NODE:
486 // STACK_DEL(&context, node.data.sequence.items)
488 // case YAML_MAPPING_NODE:
489 // STACK_DEL(&context, node.data.mapping.pairs)
492 // assert(0) // Should not happen.
495 // STACK_DEL(&context, document.nodes)
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)
504 // yaml_free(document.tag_directives.start)
506 // memset(document, 0, sizeof(yaml_document_t))
510 // * Get a document node.
513 //YAML_DECLARE(yaml_node_t *)
514 //yaml_document_get_node(document *yaml_document_t, index int)
516 // assert(document) // Non-NULL document object is expected.
518 // if (index > 0 && document.nodes.start + index <= document.nodes.top) {
519 // return document.nodes.start + index - 1
525 // * Get the root object.
528 //YAML_DECLARE(yaml_node_t *)
529 //yaml_document_get_root_node(document *yaml_document_t)
531 // assert(document) // Non-NULL document object is expected.
533 // if (document.nodes.top != document.nodes.start) {
534 // return document.nodes.start
540 // * Add a scalar node to a document.
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)
549 // error yaml_error_type_t
551 // mark yaml_mark_t = { 0, 0, 0 }
552 // tag_copy *yaml_char_t = NULL
553 // value_copy *yaml_char_t = NULL
556 // assert(document) // Non-NULL document object is expected.
557 // assert(value) // Non-NULL value is expected.
560 // tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG
563 // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
564 // tag_copy = yaml_strdup(tag)
565 // if (!tag_copy) goto error
568 // length = strlen((char *)value)
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'
577 // SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark)
578 // if (!PUSH(&context, document.nodes, node)) goto error
580 // return document.nodes.top - document.nodes.start
583 // yaml_free(tag_copy)
584 // yaml_free(value_copy)
590 // * Add a sequence node to a document.
594 //yaml_document_add_sequence(document *yaml_document_t,
595 // tag *yaml_char_t, style yaml_sequence_style_t)
598 // error yaml_error_type_t
600 // mark yaml_mark_t = { 0, 0, 0 }
601 // tag_copy *yaml_char_t = NULL
603 // start *yaml_node_item_t
604 // end *yaml_node_item_t
605 // top *yaml_node_item_t
606 // } items = { NULL, NULL, NULL }
609 // assert(document) // Non-NULL document object is expected.
612 // tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG
615 // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
616 // tag_copy = yaml_strdup(tag)
617 // if (!tag_copy) goto error
619 // if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error
621 // SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end,
622 // style, mark, mark)
623 // if (!PUSH(&context, document.nodes, node)) goto error
625 // return document.nodes.top - document.nodes.start
628 // STACK_DEL(&context, items)
629 // yaml_free(tag_copy)
635 // * Add a mapping node to a document.
639 //yaml_document_add_mapping(document *yaml_document_t,
640 // tag *yaml_char_t, style yaml_mapping_style_t)
643 // error yaml_error_type_t
645 // mark yaml_mark_t = { 0, 0, 0 }
646 // tag_copy *yaml_char_t = NULL
648 // start *yaml_node_pair_t
649 // end *yaml_node_pair_t
650 // top *yaml_node_pair_t
651 // } pairs = { NULL, NULL, NULL }
654 // assert(document) // Non-NULL document object is expected.
657 // tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG
660 // if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
661 // tag_copy = yaml_strdup(tag)
662 // if (!tag_copy) goto error
664 // if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error
666 // MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end,
667 // style, mark, mark)
668 // if (!PUSH(&context, document.nodes, node)) goto error
670 // return document.nodes.top - document.nodes.start
673 // STACK_DEL(&context, pairs)
674 // yaml_free(tag_copy)
680 // * Append an item to a sequence node.
684 //yaml_document_append_sequence_item(document *yaml_document_t,
685 // sequence int, item int)
688 // error yaml_error_type_t
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.
700 // if (!PUSH(&context,
701 // document.nodes.start[sequence-1].data.sequence.items, item))
708 // * Append a pair of a key and a value to a mapping node.
712 //yaml_document_append_mapping_pair(document *yaml_document_t,
713 // mapping int, key int, value int)
716 // error yaml_error_type_t
719 // pair yaml_node_pair_t
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.
733 // pair.value = value
735 // if (!PUSH(&context,
736 // document.nodes.start[mapping-1].data.mapping.pairs, pair))