root/libyaml/trunk/include/yaml.h

Revision 267, 95.9 kB (checked in by xi, 6 months ago)

Minor API updates.

Line 
1 /*****************************************************************************
2  * Public Interface for LibYAML
3  *
4  * Copyright (c) 2006 Kirill Simonov
5  *
6  * LibYAML is free software; you can use, modify and/or redistribute it under
7  * the terms of the MIT license; see the file LICENCE for more details.
8  *****************************************************************************/
9
10 /*****************************************************************************
11  * General guidelines.
12  *****************************************************************************/
13
14 /*
15  * Basic conventions.
16  *
17  * All functions exported by exported by LibYAML starts with the the prefix
18  * `yaml_`; types starts with `yaml_` and ends with `_t`; macros and
19  * enumeration values start with `YAML_`.
20  *
21  * A function may serve as method for an object or a particular type; such
22  * functions start with `yaml_<type>`.  A type constructor is named
23  * `yaml_<type>_new()`, a type destructor is named `yaml_<type>_delete()`.
24  *
25  * A function signifies whether it succeeded or failed with its return value;
26  * typically it is `1` for success, `0` for failure.  Functions that return a
27  * pointer may indicate an error condition by returning `NULL`.  Functions that
28  * never fail commonly do not return any value.  For most of the functions, an
29  * error value means that the function is unable to allocate some memory
30  * buffer.  For parsing and emitting functions, detailed information on the
31  * nature of the error could be obtained.
32  *
33  * LibYAML provides two active objects: parsers and emitters and three passive
34  * objects: tokens, events and documents.
35  *
36  *
37  *
38  *
39  * FIXME: Calling conventions.
40  * FIXME: Memory allocation.
41  * FIXME: Errors and exceptions.
42  * FIXME: And so on, and so forth.
43  *
44  *
45  *
46  *
47  */
48
49
50 #ifndef YAML_H
51 #define YAML_H
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60
61 /*****************************************************************************
62  * Export Definitions
63  *****************************************************************************/
64
65 /*
66  * Public API declarations.
67  *
68  * The following definitions are relevant only for the Win32 platform.  If you
69  * are building LibYAML as a static library or linking your application against
70  * LibYAML compiled as a static library, define the macro
71  * `YAML_DECLARE_STATIC`.  If you are building LibYAML as a dynamic library
72  * (DLL), you need to define `YAML_DECLARE_EXPORT`.  You don't need to define
73  * any macros in case you are linking your application against LibYAML compiled
74  * as DLL.
75  *
76  * There is no need to define any macros if you use LibYAML on non-Win32
77  * platform.
78  */
79
80 #ifdef WIN32
81 #   if defined(YAML_DECLARE_STATIC)
82 #       define  YAML_DECLARE(type)  type
83 #   elif defined(YAML_DECLARE_EXPORT)
84 #       define  YAML_DECLARE(type)  __declspec(dllexport) type
85 #   else
86 #       define  YAML_DECLARE(type)  __declspec(dllimport) type
87 #   endif
88 #else
89 #   define  YAML_DECLARE(type)  type
90 #endif
91
92 /*****************************************************************************
93  * Version Information
94  *****************************************************************************/
95
96 /*
97  * The major, minor and patch version numbers of LibYAML.
98  */
99
100 #define YAML_VERSION_MAJOR  0
101 #define YAML_VERSION_MINOR  2
102 #define YAML_VERSION_PATCH  0
103
104 /*
105  * The version of LibYAML as a string.
106  */
107
108 #define YAML_VERSION_STRING "0.2.0"
109
110 /*
111  * Get the library version numbers at runtime.
112  *
113  * Arguments:
114  *
115  * - `major`: a pointer to store the major version number.
116  *
117  * - `minor`: a pointer to store the minor version number.
118  *
119  * - `patch`: a pointer to store the patch version number.
120  */
121
122 YAML_DECLARE(void)
123 yaml_get_version(int *major, int *minor, int *patch);
124
125 /*
126  * Get the library version as a string at runtime.
127  *
128  * Returns: the version of LibYAML as a static string.
129  */
130
131 YAML_DECLARE(const char *)
132 yaml_get_version_string(void);
133
134 /*****************************************************************************
135  * Error Handling
136  *****************************************************************************/
137
138 /*
139  * The type of the error.
140  *
141  * The YAML parser and emitter reports any error details using the
142  * `yaml_error_t` structure.  The error type shows what subsystem generated the
143  * error and what additional information about the error is available.
144  */
145
146 typedef enum yaml_error_type_e {
147     /* No error was produced. */
148     YAML_NO_ERROR,
149
150     /* Cannot allocate or reallocate a block of memory. */
151     YAML_MEMORY_ERROR,
152
153     /* Cannot read from the input stream. */
154     YAML_READER_ERROR,
155     /* Cannot decode a character in the input stream. */
156     YAML_DECODER_ERROR,
157     /* Cannot scan a YAML token. */
158     YAML_SCANNER_ERROR,
159     /* Cannot parse a YAML event. */
160     YAML_PARSER_ERROR,
161     /* Cannot compose a YAML document. */
162     YAML_COMPOSER_ERROR,
163
164     /* Cannot write into the output stream. */
165     YAML_WRITER_ERROR,
166     /* Cannot emit a YAML event. */
167     YAML_EMITTER_ERROR,
168     /* Cannot serialize a YAML document. */
169     YAML_SERIALIZER_ERROR,
170
171     /* Cannot resolve an implicit YAML node tag. */
172     YAML_RESOLVER_ERROR
173 } yaml_error_type_t;
174
175 /*
176  * Position in the input stream.
177  *
178  * Marks are used to indicate the position of YAML tokens, events and documents
179  * in the input stream as well as to point to the place where a parser error has
180  * occured.
181  */
182
183 typedef struct yaml_mark_s {
184     /* The number of the character in the input stream (starting from zero). */
185     size_t index;
186     /* The line in the input stream (starting from zero). */
187     size_t line;
188     /* The column in the input stream (starting from zero). */
189     size_t column;
190 } yaml_mark_t;
191
192 /*
193  * Error details.
194  *
195  * The structure gives detailed information on any problem that occured during
196  * parsing or emitting.
197  */
198
199 typedef struct yaml_error_s {
200
201     /* The error type. */
202     yaml_error_type_t type;
203
204     /* The specific information for each error type. */
205     union {
206
207         /*
208          * A problem occured while reading the input stream (relevant for
209          * `YAML_READER_ERROR` and `YAML_DECODER_ERROR`).
210          */
211         struct {
212             /* The problem description. */
213             const char *problem;
214             /* The position in the input stream, in bytes. */
215             size_t offset;
216             /* The problematic octet or character (`-1` if not applicable). */
217             int value;
218         } reading;
219
220         /*
221          * A problem occured while loading YAML data from the input stream
222          * (relevant for `YAML_SCANNER_ERROR`, `YAML_PARSER_ERROR`, and
223          * `YAML_COMPOSER_ERROR`).
224          */
225         struct {
226             /* The description of the context in which the problem occured
227                (`NULL` if not applicable). */
228             const char *context;
229             /* The context mark (if `context` is not `NULL`). */
230             yaml_mark_t context_mark;
231             /* The problem description. */
232             const char *problem;
233             /* The problem mark. */
234             yaml_mark_t problem_mark;
235         } loading;
236
237         /*
238          * A problem occured while writing into the output stream (relevant for
239          * `YAML_WRITER_ERROR`).
240          */
241         struct {
242             /* The problem description. */
243             const char *problem;
244             /* The position in the output stream, in bytes. */
245             size_t offset;
246         } writing;
247
248         /*
249          * A problem while dumping YAML data into the output stream (relevant
250          * for `YAML_EMITTER_ERROR` and `YAML_SERIALIZER_ERROR`).
251          */
252         struct {
253             /* The problem description. */
254             const char *problem;
255         } dumping;
256
257         /*
258          * A problem occured while resolving an implicit YAML node tag
259          * (relevant for `YAML_RESOLVER_ERROR`).
260          */
261         struct {
262             /* The problem description. */
263             const char *problem;
264         } resolving;
265
266     } data;
267
268 } yaml_error_t;
269
270 /*
271  * Generate an error message.
272  *
273  * Given an instance of the `yaml_error_t` structure and a buffer, the function
274  * fills the buffer with a message describing the error.  The generated message
275  * follows the pattern: `"Error type: error details"`.  If the buffer is not
276  * large enough to hold the whole message, the function fails.
277  *
278  * Arguments:
279  *
280  * - `error`: an error object obtained using `yaml_parser_get_error()` or
281  *   `yaml_emitter_get_error()`.
282  *
283  * - `buffer`: a pointer to a character buffer to be filled with a generated
284  *   error message.
285  *
286  * - `capacity`: the size of the buffer.
287  *
288  * Returns: `1` if the function succeeded, `0` on error.  The function may fail
289  * if the provided buffer is not large enough to hold the whole buffer, in
290  * which case an application may increase the size of the buffer and call the
291  * function again.  If the function fails, the content of the buffer is
292  * undefined.
293  */
294
295 YAML_DECLARE(int)
296 yaml_error_message(yaml_error_t *error, char *buffer, size_t capacity);
297
298 /*****************************************************************************
299  * Basic Types
300  *****************************************************************************/
301
302 /*
303  * The character type (UTF-8 octet).
304  *
305  * Usage of the string type `(yaml_char_t *)` in the LibYAML API indicates that
306  * the string is encoded in UTF-8.
307  */
308
309 typedef unsigned char yaml_char_t;
310
311 /*
312  * The version directive information.
313  *
314  * Note that LibYAML only supports YAML 1.1.
315  */
316
317 typedef struct yaml_version_directive_s {
318     /* The major version number. */
319     int major;
320     /* The minor version number. */
321     int minor;
322 } yaml_version_directive_t;
323
324 /*
325  * The tag directive information.
326  */
327
328 typedef struct yaml_tag_directive_s {
329     /* The tag handle. */
330     yaml_char_t *handle;
331     /* The tag prefix. */
332     yaml_char_t *prefix;
333 } yaml_tag_directive_t;
334
335 /*
336  * The stream encoding.
337  *
338  * An application may explicitly specify the encoding in the input stream or
339  * let the parser to detect the input stream encoding from a BOM mark.  If the
340  * stream does not start with a BOM mark, UTF-8 is assumed.
341  *
342  * An application may specify the encoding of the output stream or let the
343  * emitter to choose a suitable encoding, in which case, UTF-8 is used.
344  */
345
346 typedef enum yaml_encoding_e {
347     /* The default/autodetected encoding. */
348     YAML_ANY_ENCODING,
349     /* The UTF-8 encoding. */
350     YAML_UTF8_ENCODING,
351     /* The UTF-16-LE encoding. */
352     YAML_UTF16LE_ENCODING,
353     /* The UTF-16-BE encoding. */
354     YAML_UTF16BE_ENCODING
355 } yaml_encoding_t;
356
357 /*
358  * Line break types.
359  *
360  * An application may specify the line break type the emitter should use or
361  * leave it to the emitter discretion.  In the latter case, LN (Unix style) is
362  * used.
363  */
364
365 typedef enum yaml_break_e {
366     /* Let the parser choose the break type. */
367     YAML_ANY_BREAK,
368     /* Use CR for line breaks (Mac style). */
369     YAML_CR_BREAK,
370     /* Use LN for line breaks (Unix style). */
371     YAML_LN_BREAK,
372     /* Use CR LN for line breaks (DOS style). */
373     YAML_CRLN_BREAK
374 } yaml_break_t;
375
376 /*****************************************************************************
377  * Node Styles
378  *****************************************************************************/
379
380 /*
381  * Scalar styles.
382  *
383  * There are two groups of scalar types in YAML: flow and block.  Flow scalars
384  * are divided into three styles: plain, single-quoted, and double-quoted;
385  * block scalars are divided into two styles: literal and folded.
386  *
387  * The parser reports the style in which a scalar node is represented, however
388  * it is a purely presentation details that must not be used in interpreting
389  * the node content.
390  *
391  * An application may suggest a preferred node style or leave it completely
392  * to the emitter discretion.  Note that the emitter may ignore any stylistic
393  * suggestions.
394  */
395
396 typedef enum yaml_scalar_style_e {
397     /* Let the emitter choose the style. */
398     YAML_ANY_SCALAR_STYLE,
399
400     /* The plain flow scalar style. */
401     YAML_PLAIN_SCALAR_STYLE,
402
403     /* The single-quoted flow scalar style. */
404     YAML_SINGLE_QUOTED_SCALAR_STYLE,
405     /* The double-quoted flow scalar style. */
406     YAML_DOUBLE_QUOTED_SCALAR_STYLE,
407
408     /* The literal block scalar style. */
409     YAML_LITERAL_SCALAR_STYLE,
410     /* The folded block scalar style. */
411     YAML_FOLDED_SCALAR_STYLE
412 } yaml_scalar_style_t;
413
414 /*
415  * Sequence styles.
416  *
417  * YAML supports two sequence styles: flow and block.
418  *
419  * The parser reports the style of a sequence node, but this information should
420  * not be used in interpreting the sequence content.
421  *
422  * An application may suggest a preferred sequence style or leave it completely
423  * to the emitter discretion.  Note that the emitter may ignore any stylistic
424  * hints.
425  */
426 typedef enum yaml_sequence_style_e {
427     /* Let the emitter choose the style. */
428     YAML_ANY_SEQUENCE_STYLE,
429
430     /* The flow sequence style. */
431     YAML_FLOW_SEQUENCE_STYLE,
432     /* The block sequence style. */
433     YAML_BLOCK_SEQUENCE_STYLE
434 } yaml_sequence_style_t;
435
436 /*
437  * Mapping styles.
438  *
439  * YAML supports two mapping styles: flow and block.
440  *
441  * The parser reports the style of a mapping node, but this information should
442  * not be used in interpreting the mapping content.
443  *
444  * An application may suggest a preferred mapping style or leave it completely
445  * to the emitter discretion.  Note that the emitter may ignore any stylistic
446  * hints.
447  */
448
449 typedef enum yaml_mapping_style_e {
450     /* Let the emitter choose the style. */
451     YAML_ANY_MAPPING_STYLE,
452
453     /* The block mapping style. */
454     YAML_BLOCK_MAPPING_STYLE,
455     /* The flow mapping style. */
456     YAML_FLOW_MAPPING_STYLE
457 } yaml_mapping_style_t;
458
459 /*****************************************************************************
460  * Tokens
461  *****************************************************************************/
462
463 /*
464  * Token types.
465  *
466  * The LibYAML scanner generates the following types of tokens:
467  *
468  * - STREAM-START: indicates the beginning of the stream.
469  *
470  * - STREAM-END: indicates the end of the stream.
471  *
472  * - VERSION-DIRECTIVE: describes the `%YAML` directive.
473  *
474  * - TAG-DIRECTIVE: describes the `%TAG` directive.
475  *
476  * - DOCUMENT-START: the indicator `---`.
477  *
478  * - DOCUMENT-END: the indicator `...`.
479  *
480  * - BLOCK-SEQUENCE-START: indentation increase indicating the beginning of a
481  *   block sequence node.
482  *
483  * - BLOCK-MAPPING-START: indentation increase indicating the beginning of a
484  *   block mapping node.
485  *
486  * - BLOCK-END: indentation decrease indicating the end of a block collection
487  *   node.
488  *
489  * - FLOW-SEQUENCE-START: the indicator `[`.
490  *
491  * - FLOW-SEQUENCE-END: the indicator `]`.
492  *
493  * - FLOW-MAPPING-START: the indicator `{`.
494  *
495  * - FLOW-MAPPING-END: the indicator `}`.
496  *
497  * - BLOCK-ENTRY: the beginning of an item of a block sequence.
498  *
499  * - FLOW-ENTRY: the beginning of an item of a flow sequence.
500  *
501  * - KEY: the beginning of a simple key, or the indicator `?`.
502  *
503  * - VALUE: the indicator `:`.
504  *
505  * - ALIAS: an alias of a node.
506  *
507  * - ANCHOR: a node anchor.
508  *
509  * - TAG: a node tag.
510  *
511  * - SCALAR: the content of a scalar node.
512  */
513
514 typedef enum yaml_token_type_e {
515     /* An empty unitialized token. */
516     YAML_NO_TOKEN,
517
518     /* A STREAM-START token. */
519     YAML_STREAM_START_TOKEN,
520     /* A STREAM-END token. */
521     YAML_STREAM_END_TOKEN,
522
523     /* A VERSION-DIRECTIVE token. */
524     YAML_VERSION_DIRECTIVE_TOKEN,
525     /* A TAG-DIRECTIVE token. */
526     YAML_TAG_DIRECTIVE_TOKEN,
527     /* A DOCUMENT-START token. */
528     YAML_DOCUMENT_START_TOKEN,
529     /* A DOCUMENT-END token. */
530     YAML_DOCUMENT_END_TOKEN,
531
532     /* A BLOCK-SEQUENCE-START token. */
533     YAML_BLOCK_SEQUENCE_START_TOKEN,
534     /* A BLOCK-SEQUENCE-END token. */
535     YAML_BLOCK_MAPPING_START_TOKEN,
536     /* A BLOCK-END token. */
537     YAML_BLOCK_END_TOKEN,
538
539     /* A FLOW-SEQUENCE-START token. */
540     YAML_FLOW_SEQUENCE_START_TOKEN,
541     /* A FLOW-SEQUENCE-END token. */
542     YAML_FLOW_SEQUENCE_END_TOKEN,
543     /* A FLOW-MAPPING-START token. */
544     YAML_FLOW_MAPPING_START_TOKEN,
545     /* A FLOW-MAPPING-END token. */
546     YAML_FLOW_MAPPING_END_TOKEN,
547
548     /* A BLOCK-ENTRY token. */
549     YAML_BLOCK_ENTRY_TOKEN,
550     /* A FLOW-ENTRY token. */
551     YAML_FLOW_ENTRY_TOKEN,
552     /* A KEY token. */
553     YAML_KEY_TOKEN,
554     /* A VALUE token. */
555     YAML_VALUE_TOKEN,
556
557     /* An ALIAS token. */
558     YAML_ALIAS_TOKEN,
559     /* An ANCHOR token. */
560     YAML_ANCHOR_TOKEN,
561     /* A TAG token. */
562     YAML_TAG_TOKEN,
563     /* A SCALAR token. */
564     YAML_SCALAR_TOKEN
565 } yaml_token_type_t;
566
567 /*
568  * The token object.
569  *
570  * Typically the token API is too low-level to be used directly by
571  * applications.  A possible user of the token API is a syntax highlighting
572  * application.
573  */
574
575 typedef struct yaml_token_s {
576
577     /* The token type. */
578     yaml_token_type_t type;
579
580     /* The token data. */
581     union {
582
583         /* Extra data associated with a STREAM-START token. */
584         struct {
585             /* The stream encoding. */
586             yaml_encoding_t encoding;
587         } stream_start;
588
589         /* Extra data associated with a VERSION-DIRECTIVE token. */
590         struct {
591             /* The major version number. */
592             int major;
593             /* The minor version number. */
594             int minor;
595         } version_directive;
596
597         /* Extra data associated with a TAG-DIRECTIVE token. */
598         struct {
599             /* The tag handle. */
600             yaml_char_t *handle;
601             /* The tag prefix. */
602             yaml_char_t *prefix;
603         } tag_directive;
604
605         /* Extra data associated with an ALIAS token. */
606         struct {
607             /* The alias value. */
608             yaml_char_t *value;
609         } alias;
610
611         /* Extra data associated with an ANCHOR token. */
612         struct {
613             /* The anchor value. */
614             yaml_char_t *value;
615         } anchor;
616
617         /* Extra data associated with a TAG token. */
618         struct {
619             /* The tag handle. */
620             yaml_char_t *handle;
621             /* The tag suffix. */
622             yaml_char_t *suffix;
623         } tag;
624
625         /* Extra data associated with a SCALAR token. */
626         struct {
627             /* The scalar value. */
628             yaml_char_t *value;
629             /* The length of the scalar value. */
630             size_t length;
631             /* The scalar style. */
632             yaml_scalar_style_t style;
633         } scalar;
634
635     } data;
636
637     /* The beginning of the token. */
638     yaml_mark_t start_mark;
639     /* The end of the token. */
640     yaml_mark_t end_mark;
641
642 } yaml_token_t;
643
644 /*
645  * Allocate a new empty token object.
646  *
647  * A token object allocated using this function must be deleted with
648  * `yaml_token_delete()`.
649  *
650  * Returns: a new empty token object or `NULL` on error.  The function may fail
651  * if it cannot allocate memory for a new token object.
652  */
653
654 YAML_DECLARE(yaml_token_t *)
655 yaml_token_new(void);
656
657 /*
658  * Deallocate a token object and free the associated data.
659  *
660  * A token object must be previously allocated with `yaml_token_new()`.
661  *
662  * Arguments:
663  *
664  * - `token`: a token object to be deallocated.
665  */
666
667 YAML_DECLARE(void)
668 yaml_token_delete(yaml_token_t *token);
669
670 /*
671  * Duplicate a token object.
672  *
673  * This function creates a deep copy of an existing token object.  It accepts
674  * two token objects: an empty token and a model token.  The latter is supposed
675  * to be initialized with `yaml_parser_parse_token()`.  The function assigns
676  * the type of the model to the empty token as well as duplicates and copies
677  * the internal state associated with the model token.
678  *
679  * Arguments:
680  *
681  * - `token`: an empty token object.
682  *
683  * - `model`: a token to be copied.
684  *
685  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
686  * allocate memory for duplicating the state of the model token.  In that case,
687  * the token remains empty.
688  */
689
690 YAML_DECLARE(int)
691 yaml_token_duplicate(yaml_token_t *token, const yaml_token_t *model);
692
693 /*
694  * Clear the token state.
695  *
696  * This function clears the type and the internal state of a token object
697  * freeing any associated data.  After applying this function to a token, it
698  * becomes empty.  It is supposed that the token was previously initialized
699  * using `yaml_parser_parse_token()` or `yaml_token_duplicate()`.
700  *
701  * Arguments:
702  *
703  * - `token`: a token object.
704  */
705
706 YAML_DECLARE(void)
707 yaml_token_clear(yaml_token_t *token);
708
709 /*****************************************************************************
710  * Events
711  *****************************************************************************/
712
713 /*
714  * Event types.
715  *
716  * The LibYAML parser generates, while the LibYAML emitter accepts, YAML events
717  * of the following types:
718  *
719  * - STREAM-START: indicates the beginning of the stream.
720  *
721  * - STREAM-END: indicates the end of the stream.
722  *
723  * - DOCUMENT-START: indicates the beginning of the document.
724  *
725  * - DOCUMENT-END: indicates the end of the document.
726  *
727  * - ALIAS: an alias to an already produced node.
728  *
729  * - SCALAR: a scalar node.
730  *
731  * - SEQUENCE-START: indicates the beginning of a sequence node.
732  *
733  * - SEQUENCE-END: indicates the end of a sequence node.
734  *
735  * - MAPPING-START: indicates the beginning of a mapping node.
736  *
737  * - MAPPING-END: indicates the end of a mapping node.
738  *
739  * A valid sequence of events obeys the grammar:
740  *
741  *      stream ::= STREAM-START document* STREAM-END
742  *      document ::= DOCUMENT-START node DOCUMENT-END
743  *      node ::= ALIAS | SCALAR | sequence | mapping
744  *      sequence ::= SEQUENCE-START node* SEQUENCE-END
745  *      mapping ::= MAPPING-START (node node)* MAPPING-END
746  */
747
748 typedef enum yaml_event_type_e {
749     /* An empty unitialized event. */
750     YAML_NO_EVENT,
751
752     /* A STREAM-START event. */
753     YAML_STREAM_START_EVENT,
754     /* A STREAM-END event. */
755     YAML_STREAM_END_EVENT,
756
757     /* A DOCUMENT-START event. */
758     YAML_DOCUMENT_START_EVENT,
759     /* A DOCUMENT-END event. */
760     YAML_DOCUMENT_END_EVENT,
761
762     /* An ALIAS event. */
763     YAML_ALIAS_EVENT,
764     /* A SCALAR event. */
765     YAML_SCALAR_EVENT,
766
767     /* A SEQUENCE-START event. */
768     YAML_SEQUENCE_START_EVENT,
769     /* A SEQUENCE-END event. */
770     YAML_SEQUENCE_END_EVENT,
771
772     /* A MAPPING-START event. */
773     YAML_MAPPING_START_EVENT,
774     /* A MAPPING-END event. */
775     YAML_MAPPING_END_EVENT
776 } yaml_event_type_t;
777
778 /*
779  * The event object.
780  *
781  * The event-level API of LibYAML should be used for streaming applications.
782  */
783
784 typedef struct yaml_event_s {
785
786     /* The event type. */
787     yaml_event_type_t type;
788
789     /* The event data. */
790     union {
791        
792         /* The stream parameters (for `YAML_STREAM_START_EVENT`). */
793         struct {
794             /* The document encoding. */
795             yaml_encoding_t encoding;
796         } stream_start;
797
798         /* The document parameters (for `YAML_DOCUMENT_START_EVENT`). */
799         struct {
800             /* The version directive or `NULL` if not present. */
801             yaml_version_directive_t *version_directive;
802
803             /* The list of tag directives. */
804             struct {
805                 /* The beginning of the list or `NULL`. */
806                 yaml_tag_directive_t *list;
807                 /* The length of the list. */
808                 size_t length;
809                 /* The capacity of the list (used internally). */
810                 size_t capacity;
811             } tag_directives;
812
813             /* Set if the document indicator is omitted. */
814             int is_implicit;
815         } document_start;
816
817         /* The document end parameters (for `YAML_DOCUMENT_END_EVENT`). */
818         struct {
819             /* Set if the document end indicator is omitted. */
820             int is_implicit;
821         } document_end;
822
823         /* The alias parameters (for `YAML_ALIAS_EVENT`). */
824         struct {
825             /* The anchor. */
826             yaml_char_t *anchor;
827         } alias;
828
829         /* The scalar parameters (for `YAML_SCALAR_EVENT`). */
830         struct {
831             /* The node anchor or `NULL`. */
832             yaml_char_t *anchor;
833             /* The node tag or `NULL`. */
834             yaml_char_t *tag;
835             /* The scalar value. */
836             yaml_char_t *value;
837             /* The length of the scalar value (in bytes). */
838             size_t length;
839             /* Set if the tag is optional for the plain style. */
840             int is_plain_nonspecific;
841             /* Set if the tag is optional for any non-plain style. */
842             int is_quoted_nonspecific;
843             /* The scalar style. */
844             yaml_scalar_style_t style;
845         } scalar;
846
847         /* The sequence parameters (for `YAML_SEQUENCE_START_EVENT`). */
848         struct {
849             /* The node anchor or `NULL`. */
850             yaml_char_t *anchor;
851             /* The node tag or `NULL`. */
852             yaml_char_t *tag;
853             /* Set if the tag is optional. */
854             int is_nonspecific;
855             /* The sequence style. */
856             yaml_sequence_style_t style;
857         } sequence_start;
858
859         /* The mapping parameters (for `YAML_MAPPING_START_EVENT`). */
860         struct {
861             /* The node anchor or `NULL`. */
862             yaml_char_t *anchor;
863             /* The node tag or `NULL`. */
864             yaml_char_t *tag;
865             /* Set if the tag is optional. */
866             int is_nonspecific;
867             /* The mapping style. */
868             yaml_mapping_style_t style;
869         } mapping_start;
870
871     } data;
872
873     /* The beginning of the event. */
874     yaml_mark_t start_mark;
875     /* The end of the event. */
876     yaml_mark_t end_mark;
877
878 } yaml_event_t;
879
880 /*
881  * Allocate a new empty event object.
882  *
883  * An event object allocated using this function must be deleted with
884  * `yaml_event_delete()`.
885  *
886  * Returns: a new empty event object or `NULL` on error.  The function may fail
887  * if it cannot allocate memory for a new event object.
888  */
889
890 YAML_DECLARE(yaml_event_t *)
891 yaml_event_new(void);
892
893 /*
894  * Deallocate an event object and free the associated data.
895  *
896  * An event object must be previously allocated with `yaml_event_new()`.
897  *
898  * Arguments:
899  *
900  * - `event`: an event object to be deallocated.
901  */
902
903 YAML_DECLARE(void)
904 yaml_event_delete(yaml_event_t *event);
905
906 /*
907  * Duplicate an event object.
908  *
909  * This function creates a deep copy of an existing event object.  It accepts
910  * two objects: an empty event and a model event.  The model event is supposed
911  * to be initialized either with `yaml_parser_parse_event()` or using one of
912  * the functions `yaml_event_create_*()`.  The function assigns the type of the
913  * model to the empty event and copies the internal state associated with the
914  * model event.
915  *
916  * Arguments:
917  *
918  * - `event`: an empty event object.
919  *
920  * - `model`: an event to be copied.
921  *
922  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
923  * allocate memory for duplicating the state of the model event.  In that case,
924  * the event remains empty.
925  */
926
927 YAML_DECLARE(int)
928 yaml_event_duplicate(yaml_event_t *event, const yaml_event_t *model);
929
930 /*
931  * Clear the event state.
932  *
933  * This function clears the type and the internal state of an event object
934  * freeing any associated data.  After applying this function to an event, it
935  * becomes empty.  It is supposed that the event was previously initialized
936  * using `yaml_parser_parse_event()` or `yaml_event_duplicate()`.  Note that
937  * the function `yaml_emitter_emit_event()` also clears the given event.
938  *
939  * Arguments:
940  *
941  * - `event`: an event object.
942  */
943
944 YAML_DECLARE(void)
945 yaml_event_clear(yaml_event_t *event);
946
947 /*
948  * Create a STREAM-START event.
949  *
950  * This function initializes an empty event object allocated with
951  * `yaml_event_new()`.  The initialized event could be fed to
952  * `yaml_emitter_emit_event()`.
953  *
954  * Arguments:
955  *
956  * - `event`: an empty event object.
957  *
958  * - `encoding`: the stream encoding.
959  *
960  * Returns: `1`.  The function never fails.
961  */
962
963 YAML_DECLARE(int)
964 yaml_event_create_stream_start(yaml_event_t *event,
965         yaml_encoding_t encoding);
966
967 /*
968  * Create a STREAM-END event.
969  *
970  * This function initializes an empty event object allocated with
971  * `yaml_event_new()`.  The initialized event could be fed to
972  * `yaml_emitter_emit_event()`.
973  *
974  * Arguments:
975  *
976  * - `event`: an empty event object.
977  *
978  * Returns: `1`.  The function never fails.
979  */
980
981 YAML_DECLARE(int)
982 yaml_event_create_stream_end(yaml_event_t *event);
983
984 /*
985  * Create a DOCUMENT-START event.
986  *
987  * This function initializes an empty event object allocated with
988  * `yaml_event_new()`.  The initialized event could be fed to
989  * `yaml_emitter_emit_event()`.
990  *
991  * Arguments:
992  *
993  * - `event`: an empty event object.
994  *
995  * - `version_directive`: a structure specifying the content of the `%YAML`
996  *   directive or `NULL` if the directive could be omitted.  Note that LibYAML
997  *   supports YAML 1.1 only.  The function does not check if the supplied
998  *   version equals to 1.1, but the emitter will fail to emit the event if it
999  *   is not so.
1000  *
1001  * - `tag_directives_list`: a pointer to a list specifying the content of the
1002  *   `%TAG` directives associated with the document or `NULL` if the document
1003  *   does not contain `%TAG` directives.  The content of a tag directive is a
1004  *   pair (handle, prefix) of non-empty NUL-terminated UTF-8 strings.  The tag
1005  *   handle is one of `!`, `!!` or a sequence of alphanumeric characters, `_`
1006  *   and `-` surrounded by `!`.  The tag prefix is a prefix of any valid tag,
1007  *   that is, it is a non-empty prefix of either a global tag (a valid URI) or
1008  *   a local tag (an arbitrary string starting with `!`).  The function does
1009  *   not check if the given directive values satisfy these requirements, but
1010  *   the emitter will fail to emit the event if they are not met.
1011  *
1012  * - `tag_directives_length`: the length of `tag_directives_list`; `0` if
1013  *   `tag_directives_list` is `NULL`.
1014  *
1015  * - `is_implicit`: `1` if the document indicator `---` is omitted, `0`
1016  *   otherwise.  Note that this attribute is only a stylistic hint and could be
1017  *   ignored by the emitter.
1018  *
1019  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
1020  * allocate memory for duplicating the event parameters.  In this case, the
1021  * event remains empty.
1022  */
1023
1024 YAML_DECLARE(int)
1025 yaml_event_create_document_start(yaml_event_t *event,
1026         const yaml_version_directive_t *version_directive,
1027         const yaml_tag_directive_t *tag_directives_list,
1028         size_t tag_directives_length,
1029         int is_implicit);
1030
1031 /*
1032  * Create a DOCUMENT-END event.
1033  *
1034  * This function initializes an empty event object allocated with
1035  * `yaml_event_new()`.  The initialized event could be fed to
1036  * `yaml_emitter_emit_event()`.
1037  *
1038  * Arguments:
1039  *
1040  * - `event`: an empty event object.
1041  *
1042  * - `is_implicit`: `1` if the document end indicator `...` is omitted, `0`
1043  *   otherwise.  Note that this attribute is only a stylistic hint and could be
1044  *   ignored by the emitter.
1045  *
1046  * Returns: `1`.  The function never fails.
1047  */
1048
1049 YAML_DECLARE(int)
1050 yaml_event_create_document_end(yaml_event_t *event, int is_implicit);
1051
1052 /*
1053  * Create an ANCHOR event.
1054  *
1055  * This function initializes an empty event object allocated with
1056  * `yaml_event_new()`.  The initialized event could be fed to
1057  * `yaml_emitter_emit_event()`.
1058  *
1059  * Arguments:
1060  *
1061  * - `event`: an empty event object.
1062  *
1063  * - `anchor`: the anchor value.  The anchor should be a non-empty
1064  *   NUL-terminated string containing only alphanumerical characters, `_`, and
1065  *   `-`.  The function does not check if this requirement is satisfied, but if
1066  *   it is not so, the emitter will fail to emit the generated event.
1067  *
1068  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
1069  * allocate memory for duplicating `anchor`.  In this case, the event remains
1070  * empty.
1071  */
1072
1073 YAML_DECLARE(int)
1074 yaml_event_create_alias(yaml_event_t *event, const yaml_char_t *anchor);
1075
1076 /*
1077  * Create a SCALAR event.
1078  *
1079  * This function initializes an empty event object allocated with
1080  * `yaml_event_new()`.  The initialized event could be fed to
1081  * `yaml_emitter_emit_event()`.
1082  *
1083  * Arguments:
1084  *
1085  * - `event`: an empty event object.
1086  *
1087  * - `anchor`: the anchor value or `NULL`.  The anchor should be a non-empty
1088  *   NUL-terminated string containing only alphanumerical characters, `_`, and
1089  *   `-`.
1090  *
1091  * - `tag`: the tag value or `NULL`.  The tag should be a non-empty UTF-8
1092  *   NUL-terminated string.  The tag could be global (a valid URI) or local (an
1093  *   arbitrary string starting with `!`).  If `NULL` is provided, at least one
1094  *   of the flags `is_plain_nonspecific` and `is_quoted_nonspecific` must be
1095  *   set.  The function does not check whether these requirements are
1096  *   satisfied, but the emitter will fail to emit the event if it is not so.
1097  *
1098  * - `value`: the value of the scalar node.  The value should be a UTF-8
1099  *   string.  It could contain any valid UTF-8 character including NUL.  The
1100  *   function does not check if `value` is a valid UTF-8 string, but the
1101  *   emitter will fail to emit the event if it is not so.
1102  *
1103  * - `length`: the length of the scalar value (in bytes) or `-1`.  If `length`
1104  *   is set to `-1`, the `value` is assumed to be NUL-terminated.
1105  *
1106  * - `is_plain_nonspecific`: `1` if the node tag could be omitted while
1107  *   emitting the node provided that the the plain style is used for
1108  *   representing the node value; `0` otherwise.  That this flag is set assumes
1109  *   that the tag could be correctly determined by the parser using the node
1110  *   position and content.
1111  *
1112  * - `is_quoted_nonspecific`: `1` if the node tag could be omitted while
1113  *   emitting the node provided that the node value is represented using any
1114  *   non-plain style; `0` otherwise.  That this flag is set assumes that the
1115  *   tag could be correctly determined by the parser using the node position
1116  *   and content.
1117  *
1118  * - `style`: the node style.  Note that this attribute only serves as a hint
1119  *   and may be ignored by the emitter.
1120  *
1121  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
1122  * allocate memory for duplicating the given string buffers.  In this case, the
1123  * event remains empty.
1124  */
1125
1126 YAML_DECLARE(int)
1127 yaml_event_create_scalar(yaml_event_t *event,
1128         const yaml_char_t *anchor, const yaml_char_t *tag,
1129         const yaml_char_t *value, int length,
1130         int is_plain_nonspecific, int is_quoted_nonspecific,
1131         yaml_scalar_style_t style);
1132
1133 /*
1134  * Create a SEQUENCE-START event.
1135  *
1136  * This function initializes an empty event object allocated with
1137  * `yaml_event_new()`.  The initialized event could be fed to
1138  * `yaml_emitter_emit_event()`.
1139  *
1140  * Arguments:
1141  *
1142  * - `event`: an empty event object.
1143  *
1144  * - `anchor`: the anchor value or `NULL`.  The anchor should be a non-empty
1145  *   NUL-terminated string containing only alphanumerical characters, `_`, and
1146  *   `-`.  The function does not check if this requirement is satisfied, but if
1147  *   it is not so, the emitter will fail to emit the generated event.
1148  *
1149  * - `tag`: the tag value or `NULL`.  The tag should be a non-empty UTF-8
1150  *   NUL-terminated string.  The tag could be global (a valid URI) or local (an
1151  *   arbitrary string starting with `!`).  If `NULL` is provided, the flag
1152  *   `is_nonspecific` must be set.  The function does not check whether these
1153  *   requirements are satisfied, but if it is not so, the emitter will fail to
1154  *   emit the generated event.
1155  *
1156  * - `is_nonspecific`: `1` if the node tag could be omitted while
1157  *   emitting the node; `0` otherwise.  This flag should only be set if the
1158  *   node tag could be correctly determined by the parser using the node
1159  *   position in the document graph.
1160  *
1161  * - `style`: the node style.  Note that this attribute only serves as a hint
1162  *   and may be ignored by the emitter.
1163  *
1164  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
1165  * allocate memory for duplicating the given string buffers.  In this case, the
1166  * event remains empty.
1167  */
1168
1169 YAML_DECLARE(int)
1170 yaml_event_create_sequence_start(yaml_event_t *event,
1171         const yaml_char_t *anchor, const yaml_char_t *tag,
1172         int is_nonspecific, yaml_sequence_style_t style);
1173
1174 /*
1175  * Create a SEQUENCE-END event.
1176  *
1177  * This function initializes an empty event object allocated with
1178  * `yaml_event_new()`.  The initialized event could be fed to
1179  * `yaml_emitter_emit_event()`.
1180  *
1181  * Arguments:
1182  *
1183  * - `event`: an empty event object.
1184  *
1185  * Returns: `1`.  This function never fails.
1186  */
1187
1188 YAML_DECLARE(int)
1189 yaml_event_create_sequence_end(yaml_event_t *event);
1190
1191 /*
1192  * Create a MAPPING-START event.
1193  *
1194  * This function initializes an empty event object allocated with
1195  * `yaml_event_new()`.  The initialized event could be fed to
1196  * `yaml_emitter_emit_event()`.
1197  *
1198  * Arguments:
1199  *
1200  * - `event`: an empty event object.
1201  *
1202  * - `anchor`: the anchor value or `NULL`.  The anchor should be a non-empty
1203  *   NUL-terminated string containing only alphanumerical characters, `_`, and
1204  *   `-`.  The function does not check if this requirement is satisfied, but if
1205  *   it is not so, the emitter will fail to emit the generated event.
1206  *
1207  * - `tag`: the tag value or `NULL`.  The tag should be a non-empty UTF-8
1208  *   NUL-terminated string.  The tag could be global (a valid URI) or local (an
1209  *   arbitrary string starting with `!`).  If `NULL` is provided, the flag
1210  *   `is_nonspecific` must be set.  The function does not check whether these
1211  *   requirements are satisfied, but if it is not so, the emitter will fail to
1212  *   emit the generated event.
1213  *
1214  * - `is_nonspecific`: `1` if the node tag could be omitted while
1215  *   emitting the node; `0` otherwise.  This flag should only be set if the
1216  *   node tag could be correctly determined by the parser using the node
1217  *   position in the document graph.
1218  *
1219  * - `style`: the node style.  Note that this attribute only serves as a hint
1220  *   and may be ignored by the emitter.
1221  *
1222  * Returns: `1` on success, `0` on error.  The function may fail if it cannot
1223  * allocate memory for duplicating the given string buffers.  In this case, the
1224  * event remains empty.
1225  */
1226
1227 YAML_DECLARE(int)
1228 yaml_event_create_mapping_start(yaml_event_t *event,
1229         const yaml_char_t *anchor, const yaml_char_t *tag,
1230         int is_nonspecific, yaml_mapping_style_t style);
1231
1232 /*
1233  * Create a MAPPING-END event.
1234  *
1235  * This function initializes an empty event object allocated with
1236  * `yaml_event_new()`.  The initialized event could be fed to
1237  * `yaml_emitter_emit_event()`.
1238  *
1239  * Arguments:
1240  *
1241  * - `event`: an empty event object.
1242  *
1243  * Returns: `1`.  This function never fails.
1244  */
1245
1246 YAML_DECLARE(int)
1247 yaml_event_create_mapping_end(yaml_event_t *event);
1248
1249 /*****************************************************************************
1250  * Documents and Nodes
1251  *****************************************************************************/
1252
1253 /*
1254  * Well-known scalar tags.
1255  */
1256
1257<