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