blob: d2c2308f1f4f84dae5b0e5bc48bd3b033edb92a2 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001package 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 best_width: -1,
90 }
91}
92
93// Destroy an emitter object.
94func yaml_emitter_delete(emitter *yaml_emitter_t) {
95 *emitter = yaml_emitter_t{}
96}
97
98// String write handler.
99func yaml_string_write_handler(emitter *yaml_emitter_t, buffer []byte) error {
100 *emitter.output_buffer = append(*emitter.output_buffer, buffer...)
101 return nil
102}
103
104// yaml_writer_write_handler uses emitter.output_writer to write the
105// emitted text.
106func yaml_writer_write_handler(emitter *yaml_emitter_t, buffer []byte) error {
107 _, err := emitter.output_writer.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_writer(emitter *yaml_emitter_t, w io.Writer) {
122 if emitter.write_handler != nil {
123 panic("must set the output target only once")
124 }
125 emitter.write_handler = yaml_writer_write_handler
126 emitter.output_writer = w
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) {
256 *event = yaml_event_t{
257 typ: yaml_STREAM_START_EVENT,
258 encoding: encoding,
259 }
260}
261
262// Create STREAM-END.
263func yaml_stream_end_event_initialize(event *yaml_event_t) {
264 *event = yaml_event_t{
265 typ: yaml_STREAM_END_EVENT,
266 }
267}
268
269// Create DOCUMENT-START.
270func yaml_document_start_event_initialize(
271 event *yaml_event_t,
272 version_directive *yaml_version_directive_t,
273 tag_directives []yaml_tag_directive_t,
274 implicit bool,
275) {
276 *event = yaml_event_t{
277 typ: yaml_DOCUMENT_START_EVENT,
278 version_directive: version_directive,
279 tag_directives: tag_directives,
280 implicit: implicit,
281 }
282}
283
284// Create DOCUMENT-END.
285func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) {
286 *event = yaml_event_t{
287 typ: yaml_DOCUMENT_END_EVENT,
288 implicit: implicit,
289 }
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) {
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}
360
361// Create MAPPING-END.
362func yaml_mapping_end_event_initialize(event *yaml_event_t) {
363 *event = yaml_event_t{
364 typ: yaml_MAPPING_END_EVENT,
365 }
366}
367
368// Destroy an event object.
369func yaml_event_delete(event *yaml_event_t) {
370 *event = yaml_event_t{}
371}
372
373///*
374// * Create a document object.
375// */
376//
377//YAML_DECLARE(int)
378//yaml_document_initialize(document *yaml_document_t,
379// version_directive *yaml_version_directive_t,
380// tag_directives_start *yaml_tag_directive_t,
381// tag_directives_end *yaml_tag_directive_t,
382// start_implicit int, end_implicit int)
383//{
384// struct {
385// error yaml_error_type_t
386// } context
387// struct {
388// start *yaml_node_t
389// end *yaml_node_t
390// top *yaml_node_t
391// } nodes = { NULL, NULL, NULL }
392// version_directive_copy *yaml_version_directive_t = NULL
393// struct {
394// start *yaml_tag_directive_t
395// end *yaml_tag_directive_t
396// top *yaml_tag_directive_t
397// } tag_directives_copy = { NULL, NULL, NULL }
398// value yaml_tag_directive_t = { NULL, NULL }
399// mark yaml_mark_t = { 0, 0, 0 }
400//
401// assert(document) // Non-NULL document object is expected.
402// assert((tag_directives_start && tag_directives_end) ||
403// (tag_directives_start == tag_directives_end))
404// // Valid tag directives are expected.
405//
406// if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error
407//
408// if (version_directive) {
409// version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t))
410// if (!version_directive_copy) goto error
411// version_directive_copy.major = version_directive.major
412// version_directive_copy.minor = version_directive.minor
413// }
414//
415// if (tag_directives_start != tag_directives_end) {
416// tag_directive *yaml_tag_directive_t
417// if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
418// goto error
419// for (tag_directive = tag_directives_start
420// tag_directive != tag_directives_end; tag_directive ++) {
421// assert(tag_directive.handle)
422// assert(tag_directive.prefix)
423// if (!yaml_check_utf8(tag_directive.handle,
424// strlen((char *)tag_directive.handle)))
425// goto error
426// if (!yaml_check_utf8(tag_directive.prefix,
427// strlen((char *)tag_directive.prefix)))
428// goto error
429// value.handle = yaml_strdup(tag_directive.handle)
430// value.prefix = yaml_strdup(tag_directive.prefix)
431// if (!value.handle || !value.prefix) goto error
432// if (!PUSH(&context, tag_directives_copy, value))
433// goto error
434// value.handle = NULL
435// value.prefix = NULL
436// }
437// }
438//
439// DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy,
440// tag_directives_copy.start, tag_directives_copy.top,
441// start_implicit, end_implicit, mark, mark)
442//
443// return 1
444//
445//error:
446// STACK_DEL(&context, nodes)
447// yaml_free(version_directive_copy)
448// while (!STACK_EMPTY(&context, tag_directives_copy)) {
449// value yaml_tag_directive_t = POP(&context, tag_directives_copy)
450// yaml_free(value.handle)
451// yaml_free(value.prefix)
452// }
453// STACK_DEL(&context, tag_directives_copy)
454// yaml_free(value.handle)
455// yaml_free(value.prefix)
456//
457// return 0
458//}
459//
460///*
461// * Destroy a document object.
462// */
463//
464//YAML_DECLARE(void)
465//yaml_document_delete(document *yaml_document_t)
466//{
467// struct {
468// error yaml_error_type_t
469// } context
470// tag_directive *yaml_tag_directive_t
471//
472// context.error = YAML_NO_ERROR // Eliminate a compiler warning.
473//
474// assert(document) // Non-NULL document object is expected.
475//
476// while (!STACK_EMPTY(&context, document.nodes)) {
477// node yaml_node_t = POP(&context, document.nodes)
478// yaml_free(node.tag)
479// switch (node.type) {
480// case YAML_SCALAR_NODE:
481// yaml_free(node.data.scalar.value)
482// break
483// case YAML_SEQUENCE_NODE:
484// STACK_DEL(&context, node.data.sequence.items)
485// break
486// case YAML_MAPPING_NODE:
487// STACK_DEL(&context, node.data.mapping.pairs)
488// break
489// default:
490// assert(0) // Should not happen.
491// }
492// }
493// STACK_DEL(&context, document.nodes)
494//
495// yaml_free(document.version_directive)
496// for (tag_directive = document.tag_directives.start
497// tag_directive != document.tag_directives.end
498// tag_directive++) {
499// yaml_free(tag_directive.handle)
500// yaml_free(tag_directive.prefix)
501// }
502// yaml_free(document.tag_directives.start)
503//
504// memset(document, 0, sizeof(yaml_document_t))
505//}
506//
507///**
508// * Get a document node.
509// */
510//
511//YAML_DECLARE(yaml_node_t *)
512//yaml_document_get_node(document *yaml_document_t, index int)
513//{
514// assert(document) // Non-NULL document object is expected.
515//
516// if (index > 0 && document.nodes.start + index <= document.nodes.top) {
517// return document.nodes.start + index - 1
518// }
519// return NULL
520//}
521//
522///**
523// * Get the root object.
524// */
525//
526//YAML_DECLARE(yaml_node_t *)
527//yaml_document_get_root_node(document *yaml_document_t)
528//{
529// assert(document) // Non-NULL document object is expected.
530//
531// if (document.nodes.top != document.nodes.start) {
532// return document.nodes.start
533// }
534// return NULL
535//}
536//
537///*
538// * Add a scalar node to a document.
539// */
540//
541//YAML_DECLARE(int)
542//yaml_document_add_scalar(document *yaml_document_t,
543// tag *yaml_char_t, value *yaml_char_t, length int,
544// style yaml_scalar_style_t)
545//{
546// struct {
547// error yaml_error_type_t
548// } context
549// mark yaml_mark_t = { 0, 0, 0 }
550// tag_copy *yaml_char_t = NULL
551// value_copy *yaml_char_t = NULL
552// node yaml_node_t
553//
554// assert(document) // Non-NULL document object is expected.
555// assert(value) // Non-NULL value is expected.
556//
557// if (!tag) {
558// tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG
559// }
560//
561// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
562// tag_copy = yaml_strdup(tag)
563// if (!tag_copy) goto error
564//
565// if (length < 0) {
566// length = strlen((char *)value)
567// }
568//
569// if (!yaml_check_utf8(value, length)) goto error
570// value_copy = yaml_malloc(length+1)
571// if (!value_copy) goto error
572// memcpy(value_copy, value, length)
573// value_copy[length] = '\0'
574//
575// SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark)
576// if (!PUSH(&context, document.nodes, node)) goto error
577//
578// return document.nodes.top - document.nodes.start
579//
580//error:
581// yaml_free(tag_copy)
582// yaml_free(value_copy)
583//
584// return 0
585//}
586//
587///*
588// * Add a sequence node to a document.
589// */
590//
591//YAML_DECLARE(int)
592//yaml_document_add_sequence(document *yaml_document_t,
593// tag *yaml_char_t, style yaml_sequence_style_t)
594//{
595// struct {
596// error yaml_error_type_t
597// } context
598// mark yaml_mark_t = { 0, 0, 0 }
599// tag_copy *yaml_char_t = NULL
600// struct {
601// start *yaml_node_item_t
602// end *yaml_node_item_t
603// top *yaml_node_item_t
604// } items = { NULL, NULL, NULL }
605// node yaml_node_t
606//
607// assert(document) // Non-NULL document object is expected.
608//
609// if (!tag) {
610// tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG
611// }
612//
613// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
614// tag_copy = yaml_strdup(tag)
615// if (!tag_copy) goto error
616//
617// if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error
618//
619// SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end,
620// style, mark, mark)
621// if (!PUSH(&context, document.nodes, node)) goto error
622//
623// return document.nodes.top - document.nodes.start
624//
625//error:
626// STACK_DEL(&context, items)
627// yaml_free(tag_copy)
628//
629// return 0
630//}
631//
632///*
633// * Add a mapping node to a document.
634// */
635//
636//YAML_DECLARE(int)
637//yaml_document_add_mapping(document *yaml_document_t,
638// tag *yaml_char_t, style yaml_mapping_style_t)
639//{
640// struct {
641// error yaml_error_type_t
642// } context
643// mark yaml_mark_t = { 0, 0, 0 }
644// tag_copy *yaml_char_t = NULL
645// struct {
646// start *yaml_node_pair_t
647// end *yaml_node_pair_t
648// top *yaml_node_pair_t
649// } pairs = { NULL, NULL, NULL }
650// node yaml_node_t
651//
652// assert(document) // Non-NULL document object is expected.
653//
654// if (!tag) {
655// tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG
656// }
657//
658// if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error
659// tag_copy = yaml_strdup(tag)
660// if (!tag_copy) goto error
661//
662// if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error
663//
664// MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end,
665// style, mark, mark)
666// if (!PUSH(&context, document.nodes, node)) goto error
667//
668// return document.nodes.top - document.nodes.start
669//
670//error:
671// STACK_DEL(&context, pairs)
672// yaml_free(tag_copy)
673//
674// return 0
675//}
676//
677///*
678// * Append an item to a sequence node.
679// */
680//
681//YAML_DECLARE(int)
682//yaml_document_append_sequence_item(document *yaml_document_t,
683// sequence int, item int)
684//{
685// struct {
686// error yaml_error_type_t
687// } context
688//
689// assert(document) // Non-NULL document is required.
690// assert(sequence > 0
691// && document.nodes.start + sequence <= document.nodes.top)
692// // Valid sequence id is required.
693// assert(document.nodes.start[sequence-1].type == YAML_SEQUENCE_NODE)
694// // A sequence node is required.
695// assert(item > 0 && document.nodes.start + item <= document.nodes.top)
696// // Valid item id is required.
697//
698// if (!PUSH(&context,
699// document.nodes.start[sequence-1].data.sequence.items, item))
700// return 0
701//
702// return 1
703//}
704//
705///*
706// * Append a pair of a key and a value to a mapping node.
707// */
708//
709//YAML_DECLARE(int)
710//yaml_document_append_mapping_pair(document *yaml_document_t,
711// mapping int, key int, value int)
712//{
713// struct {
714// error yaml_error_type_t
715// } context
716//
717// pair yaml_node_pair_t
718//
719// assert(document) // Non-NULL document is required.
720// assert(mapping > 0
721// && document.nodes.start + mapping <= document.nodes.top)
722// // Valid mapping id is required.
723// assert(document.nodes.start[mapping-1].type == YAML_MAPPING_NODE)
724// // A mapping node is required.
725// assert(key > 0 && document.nodes.start + key <= document.nodes.top)
726// // Valid key id is required.
727// assert(value > 0 && document.nodes.start + value <= document.nodes.top)
728// // Valid value id is required.
729//
730// pair.key = key
731// pair.value = value
732//
733// if (!PUSH(&context,
734// document.nodes.start[mapping-1].data.mapping.pairs, pair))
735// return 0
736//
737// return 1
738//}
739//
740//