1//
2// Copyright (c) 2011-2019 Canonical Ltd
3// Copyright (c) 2006-2010 Kirill Simonov
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy of
6// this software and associated documentation files (the "Software"), to deal in
7// the Software without restriction, including without limitation the rights to
8// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9// of the Software, and to permit persons to whom the Software is furnished to do
10// so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23package yaml
24
25import (
26	"fmt"
27	"io"
28)
29
30// The version directive data.
31type yaml_version_directive_t struct {
32	major int8 // The major version number.
33	minor int8 // The minor version number.
34}
35
36// The tag directive data.
37type yaml_tag_directive_t struct {
38	handle []byte // The tag handle.
39	prefix []byte // The tag prefix.
40}
41
42type yaml_encoding_t int
43
44// The stream encoding.
45const (
46	// Let the parser choose the encoding.
47	yaml_ANY_ENCODING yaml_encoding_t = iota
48
49	yaml_UTF8_ENCODING    // The default UTF-8 encoding.
50	yaml_UTF16LE_ENCODING // The UTF-16-LE encoding with BOM.
51	yaml_UTF16BE_ENCODING // The UTF-16-BE encoding with BOM.
52)
53
54type yaml_break_t int
55
56// Line break types.
57const (
58	// Let the parser choose the break type.
59	yaml_ANY_BREAK yaml_break_t = iota
60
61	yaml_CR_BREAK   // Use CR for line breaks (Mac style).
62	yaml_LN_BREAK   // Use LN for line breaks (Unix style).
63	yaml_CRLN_BREAK // Use CR LN for line breaks (DOS style).
64)
65
66type yaml_error_type_t int
67
68// Many bad things could happen with the parser and emitter.
69const (
70	// No error is produced.
71	yaml_NO_ERROR yaml_error_type_t = iota
72
73	yaml_MEMORY_ERROR   // Cannot allocate or reallocate a block of memory.
74	yaml_READER_ERROR   // Cannot read or decode the input stream.
75	yaml_SCANNER_ERROR  // Cannot scan the input stream.
76	yaml_PARSER_ERROR   // Cannot parse the input stream.
77	yaml_COMPOSER_ERROR // Cannot compose a YAML document.
78	yaml_WRITER_ERROR   // Cannot write to the output stream.
79	yaml_EMITTER_ERROR  // Cannot emit a YAML stream.
80)
81
82// The pointer position.
83type yaml_mark_t struct {
84	index  int // The position index.
85	line   int // The position line.
86	column int // The position column.
87}
88
89// Node Styles
90
91type yaml_style_t int8
92
93type yaml_scalar_style_t yaml_style_t
94
95// Scalar styles.
96const (
97	// Let the emitter choose the style.
98	yaml_ANY_SCALAR_STYLE yaml_scalar_style_t = 0
99
100	yaml_PLAIN_SCALAR_STYLE         yaml_scalar_style_t = 1 << iota // The plain scalar style.
101	yaml_SINGLE_QUOTED_SCALAR_STYLE                                 // The single-quoted scalar style.
102	yaml_DOUBLE_QUOTED_SCALAR_STYLE                                 // The double-quoted scalar style.
103	yaml_LITERAL_SCALAR_STYLE                                       // The literal scalar style.
104	yaml_FOLDED_SCALAR_STYLE                                        // The folded scalar style.
105)
106
107type yaml_sequence_style_t yaml_style_t
108
109// Sequence styles.
110const (
111	// Let the emitter choose the style.
112	yaml_ANY_SEQUENCE_STYLE yaml_sequence_style_t = iota
113
114	yaml_BLOCK_SEQUENCE_STYLE // The block sequence style.
115	yaml_FLOW_SEQUENCE_STYLE  // The flow sequence style.
116)
117
118type yaml_mapping_style_t yaml_style_t
119
120// Mapping styles.
121const (
122	// Let the emitter choose the style.
123	yaml_ANY_MAPPING_STYLE yaml_mapping_style_t = iota
124
125	yaml_BLOCK_MAPPING_STYLE // The block mapping style.
126	yaml_FLOW_MAPPING_STYLE  // The flow mapping style.
127)
128
129// Tokens
130
131type yaml_token_type_t int
132
133// Token types.
134const (
135	// An empty token.
136	yaml_NO_TOKEN yaml_token_type_t = iota
137
138	yaml_STREAM_START_TOKEN // A STREAM-START token.
139	yaml_STREAM_END_TOKEN   // A STREAM-END token.
140
141	yaml_VERSION_DIRECTIVE_TOKEN // A VERSION-DIRECTIVE token.
142	yaml_TAG_DIRECTIVE_TOKEN     // A TAG-DIRECTIVE token.
143	yaml_DOCUMENT_START_TOKEN    // A DOCUMENT-START token.
144	yaml_DOCUMENT_END_TOKEN      // A DOCUMENT-END token.
145
146	yaml_BLOCK_SEQUENCE_START_TOKEN // A BLOCK-SEQUENCE-START token.
147	yaml_BLOCK_MAPPING_START_TOKEN  // A BLOCK-SEQUENCE-END token.
148	yaml_BLOCK_END_TOKEN            // A BLOCK-END token.
149
150	yaml_FLOW_SEQUENCE_START_TOKEN // A FLOW-SEQUENCE-START token.
151	yaml_FLOW_SEQUENCE_END_TOKEN   // A FLOW-SEQUENCE-END token.
152	yaml_FLOW_MAPPING_START_TOKEN  // A FLOW-MAPPING-START token.
153	yaml_FLOW_MAPPING_END_TOKEN    // A FLOW-MAPPING-END token.
154
155	yaml_BLOCK_ENTRY_TOKEN // A BLOCK-ENTRY token.
156	yaml_FLOW_ENTRY_TOKEN  // A FLOW-ENTRY token.
157	yaml_KEY_TOKEN         // A KEY token.
158	yaml_VALUE_TOKEN       // A VALUE token.
159
160	yaml_ALIAS_TOKEN  // An ALIAS token.
161	yaml_ANCHOR_TOKEN // An ANCHOR token.
162	yaml_TAG_TOKEN    // A TAG token.
163	yaml_SCALAR_TOKEN // A SCALAR token.
164)
165
166func (tt yaml_token_type_t) String() string {
167	switch tt {
168	case yaml_NO_TOKEN:
169		return "yaml_NO_TOKEN"
170	case yaml_STREAM_START_TOKEN:
171		return "yaml_STREAM_START_TOKEN"
172	case yaml_STREAM_END_TOKEN:
173		return "yaml_STREAM_END_TOKEN"
174	case yaml_VERSION_DIRECTIVE_TOKEN:
175		return "yaml_VERSION_DIRECTIVE_TOKEN"
176	case yaml_TAG_DIRECTIVE_TOKEN:
177		return "yaml_TAG_DIRECTIVE_TOKEN"
178	case yaml_DOCUMENT_START_TOKEN:
179		return "yaml_DOCUMENT_START_TOKEN"
180	case yaml_DOCUMENT_END_TOKEN:
181		return "yaml_DOCUMENT_END_TOKEN"
182	case yaml_BLOCK_SEQUENCE_START_TOKEN:
183		return "yaml_BLOCK_SEQUENCE_START_TOKEN"
184	case yaml_BLOCK_MAPPING_START_TOKEN:
185		return "yaml_BLOCK_MAPPING_START_TOKEN"
186	case yaml_BLOCK_END_TOKEN:
187		return "yaml_BLOCK_END_TOKEN"
188	case yaml_FLOW_SEQUENCE_START_TOKEN:
189		return "yaml_FLOW_SEQUENCE_START_TOKEN"
190	case yaml_FLOW_SEQUENCE_END_TOKEN:
191		return "yaml_FLOW_SEQUENCE_END_TOKEN"
192	case yaml_FLOW_MAPPING_START_TOKEN:
193		return "yaml_FLOW_MAPPING_START_TOKEN"
194	case yaml_FLOW_MAPPING_END_TOKEN:
195		return "yaml_FLOW_MAPPING_END_TOKEN"
196	case yaml_BLOCK_ENTRY_TOKEN:
197		return "yaml_BLOCK_ENTRY_TOKEN"
198	case yaml_FLOW_ENTRY_TOKEN:
199		return "yaml_FLOW_ENTRY_TOKEN"
200	case yaml_KEY_TOKEN:
201		return "yaml_KEY_TOKEN"
202	case yaml_VALUE_TOKEN:
203		return "yaml_VALUE_TOKEN"
204	case yaml_ALIAS_TOKEN:
205		return "yaml_ALIAS_TOKEN"
206	case yaml_ANCHOR_TOKEN:
207		return "yaml_ANCHOR_TOKEN"
208	case yaml_TAG_TOKEN:
209		return "yaml_TAG_TOKEN"
210	case yaml_SCALAR_TOKEN:
211		return "yaml_SCALAR_TOKEN"
212	}
213	return "<unknown token>"
214}
215
216// The token structure.
217type yaml_token_t struct {
218	// The token type.
219	typ yaml_token_type_t
220
221	// The start/end of the token.
222	start_mark, end_mark yaml_mark_t
223
224	// The stream encoding (for yaml_STREAM_START_TOKEN).
225	encoding yaml_encoding_t
226
227	// The alias/anchor/scalar value or tag/tag directive handle
228	// (for yaml_ALIAS_TOKEN, yaml_ANCHOR_TOKEN, yaml_SCALAR_TOKEN, yaml_TAG_TOKEN, yaml_TAG_DIRECTIVE_TOKEN).
229	value []byte
230
231	// The tag suffix (for yaml_TAG_TOKEN).
232	suffix []byte
233
234	// The tag directive prefix (for yaml_TAG_DIRECTIVE_TOKEN).
235	prefix []byte
236
237	// The scalar style (for yaml_SCALAR_TOKEN).
238	style yaml_scalar_style_t
239
240	// The version directive major/minor (for yaml_VERSION_DIRECTIVE_TOKEN).
241	major, minor int8
242}
243
244// Events
245
246type yaml_event_type_t int8
247
248// Event types.
249const (
250	// An empty event.
251	yaml_NO_EVENT yaml_event_type_t = iota
252
253	yaml_STREAM_START_EVENT   // A STREAM-START event.
254	yaml_STREAM_END_EVENT     // A STREAM-END event.
255	yaml_DOCUMENT_START_EVENT // A DOCUMENT-START event.
256	yaml_DOCUMENT_END_EVENT   // A DOCUMENT-END event.
257	yaml_ALIAS_EVENT          // An ALIAS event.
258	yaml_SCALAR_EVENT         // A SCALAR event.
259	yaml_SEQUENCE_START_EVENT // A SEQUENCE-START event.
260	yaml_SEQUENCE_END_EVENT   // A SEQUENCE-END event.
261	yaml_MAPPING_START_EVENT  // A MAPPING-START event.
262	yaml_MAPPING_END_EVENT    // A MAPPING-END event.
263	yaml_TAIL_COMMENT_EVENT
264)
265
266var eventStrings = []string{
267	yaml_NO_EVENT:             "none",
268	yaml_STREAM_START_EVENT:   "stream start",
269	yaml_STREAM_END_EVENT:     "stream end",
270	yaml_DOCUMENT_START_EVENT: "document start",
271	yaml_DOCUMENT_END_EVENT:   "document end",
272	yaml_ALIAS_EVENT:          "alias",
273	yaml_SCALAR_EVENT:         "scalar",
274	yaml_SEQUENCE_START_EVENT: "sequence start",
275	yaml_SEQUENCE_END_EVENT:   "sequence end",
276	yaml_MAPPING_START_EVENT:  "mapping start",
277	yaml_MAPPING_END_EVENT:    "mapping end",
278	yaml_TAIL_COMMENT_EVENT:   "tail comment",
279}
280
281func (e yaml_event_type_t) String() string {
282	if e < 0 || int(e) >= len(eventStrings) {
283		return fmt.Sprintf("unknown event %d", e)
284	}
285	return eventStrings[e]
286}
287
288// The event structure.
289type yaml_event_t struct {
290
291	// The event type.
292	typ yaml_event_type_t
293
294	// The start and end of the event.
295	start_mark, end_mark yaml_mark_t
296
297	// The document encoding (for yaml_STREAM_START_EVENT).
298	encoding yaml_encoding_t
299
300	// The version directive (for yaml_DOCUMENT_START_EVENT).
301	version_directive *yaml_version_directive_t
302
303	// The list of tag directives (for yaml_DOCUMENT_START_EVENT).
304	tag_directives []yaml_tag_directive_t
305
306	// The comments
307	head_comment []byte
308	line_comment []byte
309	foot_comment []byte
310	tail_comment []byte
311
312	// The anchor (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_ALIAS_EVENT).
313	anchor []byte
314
315	// The tag (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT).
316	tag []byte
317
318	// The scalar value (for yaml_SCALAR_EVENT).
319	value []byte
320
321	// Is the document start/end indicator implicit, or the tag optional?
322	// (for yaml_DOCUMENT_START_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_SCALAR_EVENT).
323	implicit bool
324
325	// Is the tag optional for any non-plain style? (for yaml_SCALAR_EVENT).
326	quoted_implicit bool
327
328	// The style (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT).
329	style yaml_style_t
330}
331
332func (e *yaml_event_t) scalar_style() yaml_scalar_style_t     { return yaml_scalar_style_t(e.style) }
333func (e *yaml_event_t) sequence_style() yaml_sequence_style_t { return yaml_sequence_style_t(e.style) }
334func (e *yaml_event_t) mapping_style() yaml_mapping_style_t   { return yaml_mapping_style_t(e.style) }
335
336// Nodes
337
338const (
339	yaml_NULL_TAG      = "tag:yaml.org,2002:null"      // The tag !!null with the only possible value: null.
340	yaml_BOOL_TAG      = "tag:yaml.org,2002:bool"      // The tag !!bool with the values: true and false.
341	yaml_STR_TAG       = "tag:yaml.org,2002:str"       // The tag !!str for string values.
342	yaml_INT_TAG       = "tag:yaml.org,2002:int"       // The tag !!int for integer values.
343	yaml_FLOAT_TAG     = "tag:yaml.org,2002:float"     // The tag !!float for float values.
344	yaml_TIMESTAMP_TAG = "tag:yaml.org,2002:timestamp" // The tag !!timestamp for date and time values.
345
346	yaml_SEQ_TAG = "tag:yaml.org,2002:seq" // The tag !!seq is used to denote sequences.
347	yaml_MAP_TAG = "tag:yaml.org,2002:map" // The tag !!map is used to denote mapping.
348
349	// Not in original libyaml.
350	yaml_BINARY_TAG = "tag:yaml.org,2002:binary"
351	yaml_MERGE_TAG  = "tag:yaml.org,2002:merge"
352
353	yaml_DEFAULT_SCALAR_TAG   = yaml_STR_TAG // The default scalar tag is !!str.
354	yaml_DEFAULT_SEQUENCE_TAG = yaml_SEQ_TAG // The default sequence tag is !!seq.
355	yaml_DEFAULT_MAPPING_TAG  = yaml_MAP_TAG // The default mapping tag is !!map.
356)
357
358type yaml_node_type_t int
359
360// Node types.
361const (
362	// An empty node.
363	yaml_NO_NODE yaml_node_type_t = iota
364
365	yaml_SCALAR_NODE   // A scalar node.
366	yaml_SEQUENCE_NODE // A sequence node.
367	yaml_MAPPING_NODE  // A mapping node.
368)
369
370// An element of a sequence node.
371type yaml_node_item_t int
372
373// An element of a mapping node.
374type yaml_node_pair_t struct {
375	key   int // The key of the element.
376	value int // The value of the element.
377}
378
379// The node structure.
380type yaml_node_t struct {
381	typ yaml_node_type_t // The node type.
382	tag []byte           // The node tag.
383
384	// The node data.
385
386	// The scalar parameters (for yaml_SCALAR_NODE).
387	scalar struct {
388		value  []byte              // The scalar value.
389		length int                 // The length of the scalar value.
390		style  yaml_scalar_style_t // The scalar style.
391	}
392
393	// The sequence parameters (for YAML_SEQUENCE_NODE).
394	sequence struct {
395		items_data []yaml_node_item_t    // The stack of sequence items.
396		style      yaml_sequence_style_t // The sequence style.
397	}
398
399	// The mapping parameters (for yaml_MAPPING_NODE).
400	mapping struct {
401		pairs_data  []yaml_node_pair_t   // The stack of mapping pairs (key, value).
402		pairs_start *yaml_node_pair_t    // The beginning of the stack.
403		pairs_end   *yaml_node_pair_t    // The end of the stack.
404		pairs_top   *yaml_node_pair_t    // The top of the stack.
405		style       yaml_mapping_style_t // The mapping style.
406	}
407
408	start_mark yaml_mark_t // The beginning of the node.
409	end_mark   yaml_mark_t // The end of the node.
410
411}
412
413// The document structure.
414type yaml_document_t struct {
415
416	// The document nodes.
417	nodes []yaml_node_t
418
419	// The version directive.
420	version_directive *yaml_version_directive_t
421
422	// The list of tag directives.
423	tag_directives_data  []yaml_tag_directive_t
424	tag_directives_start int // The beginning of the tag directives list.
425	tag_directives_end   int // The end of the tag directives list.
426
427	start_implicit int // Is the document start indicator implicit?
428	end_implicit   int // Is the document end indicator implicit?
429
430	// The start/end of the document.
431	start_mark, end_mark yaml_mark_t
432}
433
434// The prototype of a read handler.
435//
436// The read handler is called when the parser needs to read more bytes from the
437// source. The handler should write not more than size bytes to the buffer.
438// The number of written bytes should be set to the size_read variable.
439//
440// [in,out]   data        A pointer to an application data specified by
441//                        yaml_parser_set_input().
442// [out]      buffer      The buffer to write the data from the source.
443// [in]       size        The size of the buffer.
444// [out]      size_read   The actual number of bytes read from the source.
445//
446// On success, the handler should return 1.  If the handler failed,
447// the returned value should be 0. On EOF, the handler should set the
448// size_read to 0 and return 1.
449type yaml_read_handler_t func(parser *yaml_parser_t, buffer []byte) (n int, err error)
450
451// This structure holds information about a potential simple key.
452type yaml_simple_key_t struct {
453	possible     bool        // Is a simple key possible?
454	required     bool        // Is a simple key required?
455	token_number int         // The number of the token.
456	mark         yaml_mark_t // The position mark.
457}
458
459// The states of the parser.
460type yaml_parser_state_t int
461
462const (
463	yaml_PARSE_STREAM_START_STATE yaml_parser_state_t = iota
464
465	yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE           // Expect the beginning of an implicit document.
466	yaml_PARSE_DOCUMENT_START_STATE                    // Expect DOCUMENT-START.
467	yaml_PARSE_DOCUMENT_CONTENT_STATE                  // Expect the content of a document.
468	yaml_PARSE_DOCUMENT_END_STATE                      // Expect DOCUMENT-END.
469	yaml_PARSE_BLOCK_NODE_STATE                        // Expect a block node.
470	yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE // Expect a block node or indentless sequence.
471	yaml_PARSE_FLOW_NODE_STATE                         // Expect a flow node.
472	yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE        // Expect the first entry of a block sequence.
473	yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE              // Expect an entry of a block sequence.
474	yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE         // Expect an entry of an indentless sequence.
475	yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE           // Expect the first key of a block mapping.
476	yaml_PARSE_BLOCK_MAPPING_KEY_STATE                 // Expect a block mapping key.
477	yaml_PARSE_BLOCK_MAPPING_VALUE_STATE               // Expect a block mapping value.
478	yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE         // Expect the first entry of a flow sequence.
479	yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE               // Expect an entry of a flow sequence.
480	yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE   // Expect a key of an ordered mapping.
481	yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE // Expect a value of an ordered mapping.
482	yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE   // Expect the and of an ordered mapping entry.
483	yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE            // Expect the first key of a flow mapping.
484	yaml_PARSE_FLOW_MAPPING_KEY_STATE                  // Expect a key of a flow mapping.
485	yaml_PARSE_FLOW_MAPPING_VALUE_STATE                // Expect a value of a flow mapping.
486	yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE          // Expect an empty value of a flow mapping.
487	yaml_PARSE_END_STATE                               // Expect nothing.
488)
489
490func (ps yaml_parser_state_t) String() string {
491	switch ps {
492	case yaml_PARSE_STREAM_START_STATE:
493		return "yaml_PARSE_STREAM_START_STATE"
494	case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE:
495		return "yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE"
496	case yaml_PARSE_DOCUMENT_START_STATE:
497		return "yaml_PARSE_DOCUMENT_START_STATE"
498	case yaml_PARSE_DOCUMENT_CONTENT_STATE:
499		return "yaml_PARSE_DOCUMENT_CONTENT_STATE"
500	case yaml_PARSE_DOCUMENT_END_STATE:
501		return "yaml_PARSE_DOCUMENT_END_STATE"
502	case yaml_PARSE_BLOCK_NODE_STATE:
503		return "yaml_PARSE_BLOCK_NODE_STATE"
504	case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE:
505		return "yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE"
506	case yaml_PARSE_FLOW_NODE_STATE:
507		return "yaml_PARSE_FLOW_NODE_STATE"
508	case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE:
509		return "yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE"
510	case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE:
511		return "yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE"
512	case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE:
513		return "yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE"
514	case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE:
515		return "yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE"
516	case yaml_PARSE_BLOCK_MAPPING_KEY_STATE:
517		return "yaml_PARSE_BLOCK_MAPPING_KEY_STATE"
518	case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE:
519		return "yaml_PARSE_BLOCK_MAPPING_VALUE_STATE"
520	case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE:
521		return "yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE"
522	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE:
523		return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE"
524	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE:
525		return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE"
526	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE:
527		return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE"
528	case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE:
529		return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE"
530	case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE:
531		return "yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE"
532	case yaml_PARSE_FLOW_MAPPING_KEY_STATE:
533		return "yaml_PARSE_FLOW_MAPPING_KEY_STATE"
534	case yaml_PARSE_FLOW_MAPPING_VALUE_STATE:
535		return "yaml_PARSE_FLOW_MAPPING_VALUE_STATE"
536	case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE:
537		return "yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE"
538	case yaml_PARSE_END_STATE:
539		return "yaml_PARSE_END_STATE"
540	}
541	return "<unknown parser state>"
542}
543
544// This structure holds aliases data.
545type yaml_alias_data_t struct {
546	anchor []byte      // The anchor.
547	index  int         // The node id.
548	mark   yaml_mark_t // The anchor mark.
549}
550
551// The parser structure.
552//
553// All members are internal. Manage the structure using the
554// yaml_parser_ family of functions.
555type yaml_parser_t struct {
556
557	// Error handling
558
559	error yaml_error_type_t // Error type.
560
561	problem string // Error description.
562
563	// The byte about which the problem occurred.
564	problem_offset int
565	problem_value  int
566	problem_mark   yaml_mark_t
567
568	// The error context.
569	context      string
570	context_mark yaml_mark_t
571
572	// Reader stuff
573
574	read_handler yaml_read_handler_t // Read handler.
575
576	input_reader io.Reader // File input data.
577	input        []byte    // String input data.
578	input_pos    int
579
580	eof bool // EOF flag
581
582	buffer     []byte // The working buffer.
583	buffer_pos int    // The current position of the buffer.
584
585	unread int // The number of unread characters in the buffer.
586
587	newlines int // The number of line breaks since last non-break/non-blank character
588
589	raw_buffer     []byte // The raw buffer.
590	raw_buffer_pos int    // The current position of the buffer.
591
592	encoding yaml_encoding_t // The input encoding.
593
594	offset int         // The offset of the current position (in bytes).
595	mark   yaml_mark_t // The mark of the current position.
596
597	// Comments
598
599	head_comment []byte // The current head comments
600	line_comment []byte // The current line comments
601	foot_comment []byte // The current foot comments
602	tail_comment []byte // Foot comment that happens at the end of a block.
603	stem_comment []byte // Comment in item preceding a nested structure (list inside list item, etc)
604
605	comments      []yaml_comment_t // The folded comments for all parsed tokens
606	comments_head int
607
608	// Scanner stuff
609
610	stream_start_produced bool // Have we started to scan the input stream?
611	stream_end_produced   bool // Have we reached the end of the input stream?
612
613	flow_level int // The number of unclosed '[' and '{' indicators.
614
615	tokens          []yaml_token_t // The tokens queue.
616	tokens_head     int            // The head of the tokens queue.
617	tokens_parsed   int            // The number of tokens fetched from the queue.
618	token_available bool           // Does the tokens queue contain a token ready for dequeueing.
619
620	indent  int   // The current indentation level.
621	indents []int // The indentation levels stack.
622
623	simple_key_allowed bool                // May a simple key occur at the current position?
624	simple_keys        []yaml_simple_key_t // The stack of simple keys.
625	simple_keys_by_tok map[int]int         // possible simple_key indexes indexed by token_number
626
627	// Parser stuff
628
629	state          yaml_parser_state_t    // The current parser state.
630	states         []yaml_parser_state_t  // The parser states stack.
631	marks          []yaml_mark_t          // The stack of marks.
632	tag_directives []yaml_tag_directive_t // The list of TAG directives.
633
634	// Dumper stuff
635
636	aliases []yaml_alias_data_t // The alias data.
637
638	document *yaml_document_t // The currently parsed document.
639}
640
641type yaml_comment_t struct {
642
643	scan_mark  yaml_mark_t // Position where scanning for comments started
644	token_mark yaml_mark_t // Position after which tokens will be associated with this comment
645	start_mark yaml_mark_t // Position of '#' comment mark
646	end_mark   yaml_mark_t // Position where comment terminated
647
648	head []byte
649	line []byte
650	foot []byte
651}
652
653// Emitter Definitions
654
655// The prototype of a write handler.
656//
657// The write handler is called when the emitter needs to flush the accumulated
658// characters to the output.  The handler should write @a size bytes of the
659// @a buffer to the output.
660//
661// @param[in,out]   data        A pointer to an application data specified by
662//                              yaml_emitter_set_output().
663// @param[in]       buffer      The buffer with bytes to be written.
664// @param[in]       size        The size of the buffer.
665//
666// @returns On success, the handler should return @c 1.  If the handler failed,
667// the returned value should be @c 0.
668//
669type yaml_write_handler_t func(emitter *yaml_emitter_t, buffer []byte) error
670
671type yaml_emitter_state_t int
672
673// The emitter states.
674const (
675	// Expect STREAM-START.
676	yaml_EMIT_STREAM_START_STATE yaml_emitter_state_t = iota
677
678	yaml_EMIT_FIRST_DOCUMENT_START_STATE       // Expect the first DOCUMENT-START or STREAM-END.
679	yaml_EMIT_DOCUMENT_START_STATE             // Expect DOCUMENT-START or STREAM-END.
680	yaml_EMIT_DOCUMENT_CONTENT_STATE           // Expect the content of a document.
681	yaml_EMIT_DOCUMENT_END_STATE               // Expect DOCUMENT-END.
682	yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE   // Expect the first item of a flow sequence.
683	yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE   // Expect the next item of a flow sequence, with the comma already written out
684	yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE         // Expect an item of a flow sequence.
685	yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE     // Expect the first key of a flow mapping.
686	yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE     // Expect the next key of a flow mapping, with the comma already written out
687	yaml_EMIT_FLOW_MAPPING_KEY_STATE           // Expect a key of a flow mapping.
688	yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE  // Expect a value for a simple key of a flow mapping.
689	yaml_EMIT_FLOW_MAPPING_VALUE_STATE         // Expect a value of a flow mapping.
690	yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE  // Expect the first item of a block sequence.
691	yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE        // Expect an item of a block sequence.
692	yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE    // Expect the first key of a block mapping.
693	yaml_EMIT_BLOCK_MAPPING_KEY_STATE          // Expect the key of a block mapping.
694	yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE // Expect a value for a simple key of a block mapping.
695	yaml_EMIT_BLOCK_MAPPING_VALUE_STATE        // Expect a value of a block mapping.
696	yaml_EMIT_END_STATE                        // Expect nothing.
697)
698
699// The emitter structure.
700//
701// All members are internal.  Manage the structure using the @c yaml_emitter_
702// family of functions.
703type yaml_emitter_t struct {
704
705	// Error handling
706
707	error   yaml_error_type_t // Error type.
708	problem string            // Error description.
709
710	// Writer stuff
711
712	write_handler yaml_write_handler_t // Write handler.
713
714	output_buffer *[]byte   // String output data.
715	output_writer io.Writer // File output data.
716
717	buffer     []byte // The working buffer.
718	buffer_pos int    // The current position of the buffer.
719
720	raw_buffer     []byte // The raw buffer.
721	raw_buffer_pos int    // The current position of the buffer.
722
723	encoding yaml_encoding_t // The stream encoding.
724
725	// Emitter stuff
726
727	canonical   bool         // If the output is in the canonical style?
728	best_indent int          // The number of indentation spaces.
729	best_width  int          // The preferred width of the output lines.
730	unicode     bool         // Allow unescaped non-ASCII characters?
731	line_break  yaml_break_t // The preferred line break.
732
733	state  yaml_emitter_state_t   // The current emitter state.
734	states []yaml_emitter_state_t // The stack of states.
735
736	events      []yaml_event_t // The event queue.
737	events_head int            // The head of the event queue.
738
739	indents []int // The stack of indentation levels.
740
741	tag_directives []yaml_tag_directive_t // The list of tag directives.
742
743	indent int // The current indentation level.
744
745	flow_level int // The current flow level.
746
747	root_context       bool // Is it the document root context?
748	sequence_context   bool // Is it a sequence context?
749	mapping_context    bool // Is it a mapping context?
750	simple_key_context bool // Is it a simple mapping key context?
751
752	line       int  // The current line.
753	column     int  // The current column.
754	whitespace bool // If the last character was a whitespace?
755	indention  bool // If the last character was an indentation character (' ', '-', '?', ':')?
756	open_ended bool // If an explicit document end is required?
757
758	space_above bool // Is there's an empty line above?
759	foot_indent int  // The indent used to write the foot comment above, or -1 if none.
760
761	// Anchor analysis.
762	anchor_data struct {
763		anchor []byte // The anchor value.
764		alias  bool   // Is it an alias?
765	}
766
767	// Tag analysis.
768	tag_data struct {
769		handle []byte // The tag handle.
770		suffix []byte // The tag suffix.
771	}
772
773	// Scalar analysis.
774	scalar_data struct {
775		value                 []byte              // The scalar value.
776		multiline             bool                // Does the scalar contain line breaks?
777		flow_plain_allowed    bool                // Can the scalar be expessed in the flow plain style?
778		block_plain_allowed   bool                // Can the scalar be expressed in the block plain style?
779		single_quoted_allowed bool                // Can the scalar be expressed in the single quoted style?
780		block_allowed         bool                // Can the scalar be expressed in the literal or folded styles?
781		style                 yaml_scalar_style_t // The output style.
782	}
783
784	// Comments
785	head_comment []byte
786	line_comment []byte
787	foot_comment []byte
788	tail_comment []byte
789
790	key_line_comment []byte
791
792	// Dumper stuff
793
794	opened bool // If the stream was already opened?
795	closed bool // If the stream was already closed?
796
797	// The information associated with the document nodes.
798	anchors *struct {
799		references int  // The number of references.
800		anchor     int  // The anchor id.
801		serialized bool // If the node has been emitted?
802	}
803
804	last_anchor_id int // The last assigned anchor id.
805
806	document *yaml_document_t // The currently emitted document.
807}
808