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