root/libyaml/trunk/src/yaml_private.h

Revision 272, 55.1 kB (checked in by xi, 5 months ago)

Tagged the 0.1.1 release (better later than never).

Line 
1 /*****************************************************************************
2  * Private Definitions 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 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <yaml.h>
15
16 #include <assert.h>
17 #include <limits.h>
18 #include <errno.h>
19 #include <ctype.h>
20 #include <locale.h>
21
22 /*****************************************************************************
23  * Memory Management
24  *****************************************************************************/
25
26 YAML_DECLARE(void *)
27 yaml_malloc(size_t size);
28
29 YAML_DECLARE(void *)
30 yaml_realloc(void *ptr, size_t size);
31
32 YAML_DECLARE(void)
33 yaml_free(void *ptr);
34
35 YAML_DECLARE(yaml_char_t *)
36 yaml_strdup(const yaml_char_t *);
37
38 /*****************************************************************************
39  * Error Management
40  *****************************************************************************/
41
42 /*
43  * Generic error initializers; not to be used directly.
44  */
45
46 #define ERROR_INIT(error, _type)                                                \
47     (memset(&(error), 0, sizeof(yaml_error_t)),                                 \
48      (error).type = (_type),                                                    \
49      0)
50
51 #define READING_ERROR_INIT(error, _type, _problem, _offset, _value)             \
52     (ERROR_INIT(error, _type),                                                  \
53      (error).data.reading.problem = (_problem),                                 \
54      (error).data.reading.offset = (_offset),                                   \
55      (error).data.reading.value = (_value),                                     \
56      0)
57
58 #define LOADING_ERROR_INIT(error, _type, _problem, _problem_mark)               \
59     (ERROR_INIT(error, _type),                                                  \
60      (error).data.loading.context = NULL,                                       \
61      (error).data.loading.context_mark.index = 0,                               \
62      (error).data.loading.context_mark.line = 0,                                \
63      (error).data.loading.context_mark.column = 0,                              \
64      (error).data.loading.problem = (_problem),                                 \
65      (error).data.loading.problem_mark = (_problem_mark),                       \
66      0)
67
68 #define LOADING_ERROR_WITH_CONTEXT_INIT(error, _type, _context, _context_mark,  \
69         _problem, _problem_mark)                                                \
70     (ERROR_INIT(error, _type),                                                  \
71      (error).data.loading.context = (_context),                                 \
72      (error).data.loading.context_mark = (_context_mark),                       \
73      (error).data.loading.problem = (_problem),                                 \
74      (error).data.loading.problem_mark = (_problem_mark),                       \
75      0)
76
77 #define WRITING_ERROR_INIT(error, _type, _problem, _offset)                     \
78     (ERROR_INIT(error, _type),                                                  \
79      (error).data.writing.problem = (_problem),                                 \
80      (error).data.writing.offset = (_offset),                                   \
81      0)
82
83 #define DUMPING_ERROR_INIT(error, _type, _problem)                              \
84     (ERROR_INIT(error, _type),                                                  \
85      (error).data.dumping.problem = (_problem),                                 \
86      0)
87
88 #define RESOLVING_ERROR_INIT(error, _type, _problem)                            \
89     (ERROR_INIT(error, _type),                                                  \
90      (error).data.resolving.problem = (_problem),                               \
91      0)
92
93 /*
94  * Specific error initializers.
95  */
96
97 #define MEMORY_ERROR_INIT(self)                                                 \
98     ERROR_INIT((self)->error, YAML_MEMORY_ERROR)
99
100 #define READER_ERROR_INIT(self, _problem, _offset)                              \
101     READING_ERROR_INIT((self)->error, YAML_READER_ERROR, _problem, _offset, -1)
102
103 #define DECODER_ERROR_INIT(self, _problem, _offset, _value)                     \
104     READING_ERROR_INIT((self)->error, YAML_DECODER_ERROR, _problem, _offset, _value)
105
106 #define SCANNER_ERROR_INIT(self, _problem, _problem_mark)                       \
107     LOADING_ERROR_INIT((self)->error, YAML_SCANNER_ERROR, _problem, _problem_mark)
108
109 #define SCANNER_ERROR_WITH_CONTEXT_INIT(self, _context, _context_mark,          \
110         _problem, _problem_mark)                                                \
111     LOADING_ERROR_WITH_CONTEXT_INIT((self)->error, YAML_SCANNER_ERROR,          \
112             _context, _context_mark, _problem, _problem_mark)
113
114 #define PARSER_ERROR_INIT(self, _problem, _problem_mark)                        \
115     LOADING_ERROR_INIT((self)->error, YAML_PARSER_ERROR, _problem, _problem_mark)
116
117 #define PARSER_ERROR_WITH_CONTEXT_INIT(self, _context, _context_mark,           \
118         _problem, _problem_mark)                                                \
119     LOADING_ERROR_WITH_CONTEXT_INIT((self)->error, YAML_PARSER_ERROR,           \
120             _context, _context_mark, _problem, _problem_mark)
121
122 #define COMPOSER_ERROR_INIT(self, _problem, _problem_mark)                      \
123     LOADING_ERROR_INIT((self)->error, YAML_COMPOSER_ERROR, _problem, _problem_mark)
124
125 #define COMPOSER_ERROR_WITH_CONTEXT_INIT(self, _context, _context_mark,         \
126         _problem, _problem_mark)                                                \
127     LOADING_ERROR_WITH_CONTEXT_INIT((self)->error, YAML_COMPOSER_ERROR,         \
128             _context, _context_mark, _problem, _problem_mark)
129
130 #define WRITER_ERROR_INIT(self, _problem, _offset)                              \
131     WRITING_ERROR_INIT((self)->error, YAML_WRITER_ERROR, _problem, _offset)
132
133 #define EMITTER_ERROR_INIT(self, _problem)                                      \
134     DUMPING_ERROR_INIT((self)->error, YAML_EMITTER_ERROR, _problem)
135
136 #define SERIALIZER_ERROR_INIT(self, _problem)                                   \
137     DUMPING_ERROR_INIT((self)->error, YAML_SERIALIZER_ERROR, _problem)
138
139 #define RESOLVER_ERROR_INIT(self, _problem)                                     \
140     RESOLVER_ERROR_INIT((self)->error, YAML_RESOLVER_ERROR, _problem)
141
142 /*****************************************************************************
143  * Buffer Sizes
144  *****************************************************************************/
145
146 /*
147  * The size of the input raw buffer.
148  */
149
150 #define RAW_INPUT_BUFFER_CAPACITY   16384
151
152 /*
153  * The size of the input buffer.
154  *
155  * The input buffer should be large enough to hold the content of the raw
156  * buffer after it is decoded.  The raw input buffer could be encoded in UTF-8
157  * or UTF-16 while the input buffer is always encoded in UTF-8.  A UTF-16
158  * character may take 2 or 4 bytes, and a UTF-8 character takes up to 4 bytes.
159  * We use the *3 multiplier just to be safe.
160  */
161
162 #define INPUT_BUFFER_CAPACITY   (RAW_INPUT_BUFFER_CAPACITY*3)
163
164 /*
165  * The size of the output buffer.
166  */
167
168 #define OUTPUT_BUFFER_CAPACITY  16384
169
170 /*
171  * The size of the output raw buffer.
172  *
173  * The raw buffer should be able to hold the content of the output buffer
174  * after it is encoded.
175  */
176
177 #define RAW_OUTPUT_BUFFER_CAPACITY  (OUTPUT_BUFFER_CAPACITY*2+2)
178
179 /*
180  * The size of other stacks and queues.
181  */
182
183 #define INITIAL_STACK_CAPACITY  16
184 #define INITIAL_QUEUE_CAPACITY  16
185 #define INITIAL_STRING_CAPACITY 16
186
187 /*****************************************************************************
188  * String Management
189  *****************************************************************************/
190
191 /*
192  * An immutable string used as an input buffer.
193  */
194
195 typedef struct yaml_istring_s {
196     const yaml_char_t *buffer;
197     size_t pointer;
198     size_t length;
199 } yaml_istring_t;
200
201 /*
202  * A string that is used as an output buffer.
203  */
204
205 typedef struct yaml_ostring_s {
206     yaml_char_t *buffer;
207     size_t pointer;
208     size_t capacity;
209 } yaml_ostring_t;
210
211 /*
212  * A string that could be used both as an input and an output buffer.
213  */
214
215 typedef struct yaml_iostring_s {
216     yaml_char_t *buffer;
217     size_t pointer;
218     size_t length;
219     size_t capacity;
220 } yaml_iostring_t;
221
222 /*
223  * A separate type for non-UTF-8 i/o strings.
224  */
225
226 typedef struct yaml_raw_iostring_s {
227     unsigned char *buffer;
228     size_t pointer;
229     size_t length;
230     size_t capacity;
231 } yaml_raw_iostring_t;
232
233 /*
234  * Double the string capacity.
235  */
236
237 YAML_DECLARE(int)
238 yaml_ostring_extend(yaml_char_t **buffer, size_t *capacity);
239
240 /*
241  * Append a string to the end of the base string expanding it if needed.
242  */
243
244 YAML_DECLARE(int)
245 yaml_ostring_join(
246         yaml_char_t **base_buffer, size_t *base_pointer, size_t *base_capacity,
247         yaml_char_t *adj_buffer, size_t adj_pointer);
248
249 /*
250  * Basic string operations.
251  */
252
253 #define ISTRING(buffer, length) { (buffer), 0, (length) }
254
255 #define NULL_OSTRING { NULL, 0, 0 }
256
257 #define IOSTRING_INIT(self, string, _capacity)                                  \
258     (((string).buffer = yaml_malloc(_capacity)) ?                               \
259         ((string).pointer = (string).length = 0,                                \
260          (string).capacity = (_capacity),                                       \
261          memset((string).buffer, 0, (_capacity)),                               \
262          1) :                                                                   \
263         ((self)->error.type = YAML_MEMORY_ERROR,                                \
264          0))
265
266 #define IOSTRING_DEL(self, string)                                              \
267     (yaml_free((string).buffer),                                                \
268      (string).buffer = NULL,                                                    \
269      ((string).pointer = (string).length = (string).capacity = 0))
270
271 #define IOSTRING_SET(self, string, _buffer, _capacity)                          \
272     (memset((_buffer), 0, (_capacity)),                                         \
273      (string).buffer = (_buffer),                                               \
274      (string).pointer = (string).length = 0,                                    \
275      (string).capacity = (_capacity))
276
277 #define OSTRING_INIT(self, string, _capacity)                                   \
278     (((string).buffer = yaml_malloc(_capacity)) ?                               \
279         ((string).pointer = 0,                                                  \
280          (string).capacity = (_capacity),                                       \
281          memset((string).buffer, 0, (_capacity)),                               \
282          1) :                                                                   \
283         ((self)->error.type = YAML_MEMORY_ERROR,                                \
284          0))
285
286 #define OSTRING_DEL(self, string)                                               \
287     (yaml_free((string).buffer),                                                \
288      (string).buffer = NULL,                                                    \
289      ((string).pointer = (string).capacity = 0))
290
291 #define OSTRING_EXTEND(self, string)                                            \
292     ((((string).pointer+5 < (string).capacity)                                  \
293         || yaml_ostring_extend(&(string).buffer, &(string).capacity)) ?         \
294      1 :                                                                        \
295      ((self)->error.type = YAML_MEMORY_ERROR,                                   \
296       0))
297
298 #define CLEAR(self, string)                                                     \
299     ((string).pointer = 0,                                                      \
300      memset((string).buffer, 0, (string).capacity))
301
302 #define JOIN(self, base_string, adj_string)                                     \
303     ((yaml_ostring_join(&(base_string).buffer, &(base_string).pointer,          \
304                        &(base_string).capacity,                                 \
305                        (adj_string).buffer, (adj_string).pointer)) ?            \
306         ((adj_string).pointer = 0,                                              \
307          1) :                                                                   \
308         ((self)->error.type = YAML_MEMORY_ERROR,                                \
309          0))
310
311 /*****************************************************************************
312  * String Tests
313  *****************************************************************************/
314
315 /*
316  * Get the octet at the specified position.
317  */
318
319 #define OCTET_AT(string, offset)                                                \
320     ((string).buffer[(string).pointer+(offset)])
321
322 /*
323  * Get the current offset.
324  */
325
326 #define OCTET(string)   OCTET_AT((string), 0)
327
328 /*
329  * Check the octet at the specified position.
330  */
331
332 #define CHECK_AT(string, octet, offset)                                         \
333     (OCTET_AT((string), (offset)) == (yaml_char_t)(octet))
334
335 /*
336  * Check the current octet in the buffer.
337  */
338
339 #define CHECK(string, octet)    CHECK_AT((string), (octet), 0)
340
341 /*
342  * Check if the character at the specified position is an alphabetical
343  * character, a digit, '_', or '-'.
344  */
345
346 #define IS_ALPHA_AT(string, offset)                                             \
347      ((OCTET_AT((string), (offset)) >= (yaml_char_t) '0' &&                     \
348        OCTET_AT((string), (offset)) <= (yaml_char_t) '9') ||                    \
349       (OCTET_AT((string), (offset)) >= (yaml_char_t) 'A' &&                     \
350        OCTET_AT((string), (offset)) <= (yaml_char_t) 'Z') ||                    \
351       (OCTET_AT((string), (offset)) >= (yaml_char_t) 'a' &&                     \
352        OCTET_AT((string), (offset)) <= (yaml_char_t) 'z') ||                    \
353       OCTET_AT((string), (offset)) == '_' ||                                    \
354       OCTET_AT((string), (offset)) == '-')
355
356 #define IS_ALPHA(string)    IS_ALPHA_AT((string), 0)
357
358 /*
359  * Check if the character at the specified position is a digit.
360  */
361
362 #define IS_DIGIT_AT(string, offset)                                             \
363      ((OCTET_AT((string), (offset)) >= (yaml_char_t) '0' &&                     \
364        OCTET_AT((string), (offset)) <= (yaml_char_t) '9'))
365
366 #define IS_DIGIT(string)    IS_DIGIT_AT((string), 0)
367
368 /*
369  * Get the value of a digit.
370  */
371
372 #define AS_DIGIT_AT(string, offset)                                             \
373      (OCTET_AT((string), (offset)) - (yaml_char_t) '0')
374
375 #define AS_DIGIT(string)    AS_DIGIT_AT((string), 0)
376
377 /*
378  * Check if the character at the specified position is a hex-digit.
379  */
380
381 #define IS_HEX_AT(string, offset)                                               \
382      ((OCTET_AT((string), (offset)) >= (yaml_char_t) '0' &&                     \
383        OCTET_AT((string), (offset)) <= (yaml_char_t) '9') ||                    \
384       (OCTET_AT((string), (offset)) >= (yaml_char_t) 'A' &&                     \
385        OCTET_AT((string), (offset)) <= (yaml_char_t) 'F') ||                    \
386       (OCTET_AT((string), (offset)) >= (yaml_char_t) 'a' &&                     \
387        OCTET_AT((string), (offset)) <= (yaml_char_t) 'f'))
388
389 #define IS_HEX(string)    IS_HEX_AT((string), 0)
390
391 /*
392  * Get the value of a hex-digit.
393  */
394
395 #define AS_HEX_AT(string, offset)                                               \
396       ((OCTET_AT((string), (offset)) >= (yaml_char_t) 'A' &&                    \
397         OCTET_AT((string), (offset)) <= (yaml_char_t) 'F') ?                    \
398        (OCTET_AT((string), (offset)) - (yaml_char_t) 'A' + 10) :                \
399        (OCTET_AT((string), (offset)) >= (yaml_char_t) 'a' &&                    \
400         OCTET_AT((string), (offset)) <= (yaml_char_t) 'f') ?                    \
401        (OCTET_AT((string), (offset)) - (yaml_char_t) 'a' + 10) :                \
402        (OCTET_AT((string), (offset)) - (yaml_char_t) '0'))
403  
404 #define AS_HEX(string)  AS_HEX_AT((string), 0)
405  
406 /*
407  * Check if the character is ASCII.
408  */
409
410 #define IS_ASCII_AT(string, offset)                                             \
411     (OCTET_AT((string), (offset)) <= (yaml_char_t) '\x7F')
412
413 #define IS_ASCII(string)    IS_ASCII_AT((string), 0)
414
415 /*
416  * Check if the character can be printed unescaped.
417  */
418
419 #define IS_PRINTABLE_AT(string, offset)                                         \
420     ((OCTET_AT((string), (offset)) == 0x0A)         /* . == #x0A */             \
421      || (OCTET_AT((string), (offset)) >= 0x20       /* #x20 <= . <= #x7E */     \
422          && OCTET_AT((string), (offset)) <= 0x7E)                               \
423      || (OCTET_AT((string), (offset)) == 0xC2       /* #0xA0 <= . <= #xD7FF */  \
424          && OCTET_AT((string), (offset)+1) >= 0xA0)                             \
425      || (OCTET_AT((string), (offset)) > 0xC2                                    \
426          && OCTET_AT((string), (offset)) < 0xED)                                \
427      || (OCTET_AT((string), (offset)) == 0xED                                   \
428          && OCTET_AT((string), (offset)+1) < 0xA0)                              \
429      || (OCTET_AT((string), (offset)) == 0xEE)                                  \
430      || (OCTET_AT((string), (offset)) == 0xEF       /* #xE000 <= . <= #xFFFD */ \
431          && !(OCTET_AT((string), (offset)+1) == 0xBB       /* && . != #xFEFF */ \
432              && OCTET_AT((string), (offset)+2) == 0xBF)                         \
433          && !(OCTET_AT((string), (offset)+1) == 0xBF                            \
434              && (OCTET_AT((string), (offset)+2) == 0xBE                         \
435                  || OCTET_AT((string), (offset)+2) == 0xBF))))
436
437 #define IS_PRINTABLE(string)    IS_PRINTABLE_AT((string), 0)
438
439 /*
440  * Check if the character at the specified position is NUL.
441  */
442
443 #define IS_Z_AT(string, offset)   CHECK_AT((string), '\0', (offset))
444
445 #define IS_Z(string)    IS_Z_AT((string), 0)
446
447 /*
448  * Check if the character at the specified position is BOM.
449  */
450
451 #define IS_BOM_AT(string, offset)                                               \
452      (CHECK_AT((string), '\xEF', (offset))                                      \
453       && CHECK_AT((string), '\xBB', (offset)+1)                                 \
454       && CHECK_AT((string), '\xBF', (offset)+2))    /* BOM (#xFEFF) */
455
456 #define IS_BOM(string)  IS_BOM_AT(string, 0)
457
458 /*
459  * Check if the character at the specified position is space.
460  */
461
462 #define IS_SPACE_AT(string, offset) CHECK_AT((string), ' ', (offset))
463
464 #define IS_SPACE(string)    IS_SPACE_AT((string), 0)
465
466 /*
467  * Check if the character at the specified position is tab.
468  */
469
470 #define IS_TAB_AT(string, offset)   CHECK_AT((string), '\t', (offset))
471
472 #define IS_TAB(string)  IS_TAB_AT((string), 0)
473
474 /*
475  * Check if the character at the specified position is blank (space or tab).
476  */
477
478 #define IS_BLANK_AT(string, offset)                                             \
479     (IS_SPACE_AT((string), (offset)) || IS_TAB_AT((string), (offset)))
480
481 #define IS_BLANK(string)    IS_BLANK_AT((string), 0)
482
483 /*
484  * Check if the character at the specified position is a line break.
485  */
486
487 #define IS_BREAK_AT(string, offset)                                             \
488     (CHECK_AT((string), '\r', (offset))                 /* CR (#xD)*/           \
489      || CHECK_AT((string), '\n', (offset))              /* LF (#xA) */          \
490      || (CHECK_AT((string), '\xC2', (offset))                                   \
491          && CHECK_AT((string), '\x85', (offset)+1))     /* NEL (#x85) */        \
492      || (CHECK_AT((string), '\xE2', (offset))                                   \
493          && CHECK_AT((string), '\x80', (offset)+1)                              \
494          && CHECK_AT((string), '\xA8', (offset)+2))     /* LS (#x2028) */       \
495      || (CHECK_AT((string), '\xE2', (offset))                                   \
496          && CHECK_AT((string), '\x80', (offset)+1)                              \
497          && CHECK_AT((string), '\xA9', (offset)+2)))    /* PS (#x2029) */
498
499 #define IS_BREAK(string)    IS_BREAK_AT((string), 0)
500
501 #define IS_CRLF_AT(string, offset)                                              \
502      (CHECK_AT((string), '\r', (offset)) && CHECK_AT((string), '\n', (offset)+1))
503
504 #define IS_CRLF(string) IS_CRLF_AT((string), 0)
505
506 /*
507  * Check if the character is a line break or NUL.
508  */
509
510 #define IS_BREAKZ_AT(string, offset)                                            \
511     (IS_BREAK_AT((string), (offset)) || IS_Z_AT((string), (offset)))
512
513 #define IS_BREAKZ(string)   IS_BREAKZ_AT((string), 0)
514
515 /*
516  * Check if the character is a line break, space, or NUL.
517  */
518
519 #define IS_SPACEZ_AT(string, offset)                                            \
520     (IS_SPACE_AT((string), (offset)) || IS_BREAKZ_AT((string), (offset)))
521
522 #define IS_SPACEZ(string)   IS_SPACEZ_AT((string), 0)
523
524 /*
525  * Check if the character is a line break, space, tab, or NUL.
526  */
527
528 #define IS_BLANKZ_AT(string, offset)                                            \
529     (IS_BLANK_AT((string), (offset)) || IS_BREAKZ_AT((string), (offset)))
530
531 #define IS_BLANKZ(string)   IS_BLANKZ_AT((string), 0)
532
533 /*
534  * Determine the width of the character.
535  */
536
537 #define WIDTH_AT(string, offset)                                                \
538      ((OCTET_AT((string), (offset)) & 0x80) == 0x00 ? 1 :                       \
539       (OCTET_AT((string), (offset)) & 0xE0) == 0xC0 ? 2 :                       \
540       (OCTET_AT((string), (offset)) & 0xF0) == 0xE0 ? 3 :                       \
541       (OCTET_AT((string), (offset)) & 0xF8) == 0xF0 ? 4 : 0)
542
543 #define WIDTH(string)   WIDTH_AT((string), 0)
544
545 /*
546  * Move the string pointer to the next character.
547  */
548
549 #define MOVE(string)    ((string).pointer += WIDTH((string)))
550
551 /*
552  * Write a single octet and bump the pointer.
553  */
554
555 #define JOIN_OCTET(string, octet)                                               \
556     ((string).buffer[(string).pointer++] = (octet))
557
558 /*
559  * Copy a single octet and bump the pointers.
560  */
561
562 #define COPY_OCTET(target_string, source_string)                                \
563     ((target_string).buffer[(target_string).pointer++]                          \
564      = (source_string).buffer[(source_string).pointer++])
565
566 /*
567  * Copy a character and move the pointers of both strings.
568  */
569
570 #define COPY(target_string, source_string)                                      \
571     ((OCTET(source_string) & 0x80) == 0x00 ?                                    \
572      COPY_OCTET((target_string), (source_string)) :                             \
573      (OCTET(source_string) & 0xE0) == 0xC0 ?                                    \
574      (COPY_OCTET((target_string), (source_string)),                             \
575       COPY_OCTET((target_string), (source_string))) :                           \
576      (OCTET(source_string) & 0xF0) == 0xE0 ?                                    \
577      (COPY_OCTET((target_string), (source_string)),                             \
578       COPY_OCTET((target_string), (source_string)),                             \
579       COPY_OCTET((target_string), (source_string))) :                           \
580      (OCTET(source_string) & 0xF8) == 0xF0 ?                                    \
581      (COPY_OCTET((target_string), (source_string)),                             \
582       COPY_OCTET((target_string), (source_string)),                             \
583       COPY_OCTET((target_string), (source_string)),                             \
584       COPY_OCTET((target_string), (source_string))) : 0)
585
586 /*****************************************************************************
587  * Stack and Queue Management
588  *****************************************************************************/
589
590 /*
591  * Double the stack capacity.
592  */
593
594 YAML_DECLARE(int)
595 yaml_stack_extend(void **list, size_t size, size_t *length, size_t *capacity);
596
597 /*
598  * Double the queue capacity.
599  */
600
601 YAML_DECLARE(int)
602 yaml_queue_extend(void **list, size_t size,
603         size_t *head, size_t *tail, size_t *capacity);
604
605 /*
606  * Basic stack operations.
607  */
608
609 #define STACK_INIT(self, stack, _capacity)                                      \
610     (((stack).list = yaml_malloc((_capacity)*sizeof(*(stack).list))) ?          \
611         ((stack).length = 0,                                                    \
612          (stack).capacity = (_capacity),                                        \
613          1) :                                                                   \
614         ((self)->error.type = YAML_MEMORY_ERROR,                                \
615          0))
616
617 #define STACK_DEL(self, stack)                                                  \
618     (yaml_free((stack).list),                                                   \
619      (stack).list = NULL,                                                       \
620      (stack).length = (stack).capacity = 0)
621
622 #define STACK_SET(self, stack, _list, _capacity)                                \
623     ((stack).list = (_list),                                                    \
624      (stack).length = 0,                                                        \
625      (stack).capacity = (_capacity))
626
627 #define STACK_EMPTY(self, stack)                                                \
628     ((stack).length == 0)
629
630 #define STACK_ITER(self, stack, index)                                          \
631     ((stack).list + index)
632
633 #define PUSH(self, stack, value)                                                \
634     (((stack).length < (stack).capacity                                         \
635       || yaml_stack_extend((void **)&(stack).list, sizeof(*(stack).list),       \
636               &(stack).length, &(stack).capacity)) ?                            \
637         ((stack).list[(stack).length++] = (value),                              \
638          1) :                                                                   \
639         ((self)->error.type = YAML_MEMORY_ERROR,                                \
640          0))
641
642 #define POP(self, stack)                                                        \
643     ((stack).list[--(stack).length])
644
645 /*
646  * Basic queue operations.
647  */
648
649 #define QUEUE_INIT(self, queue, _capacity)                                      \
650     (((queue).list = yaml_malloc((_capacity)*sizeof(*(queue).list))) ?          \
651         ((queue).head = (queue).tail = 0,                                       \
652          (queue).capacity = (_capacity),                                        \
653          1) :                                                                   \
654         ((self)->error.type = YAML_MEMORY_ERROR,                                \
655          0))
656
657 #define QUEUE_DEL(self, queue)                                                  \
658     (yaml_free((queue).list),                                                   \
659      (queue).list = NULL,                                                       \
660      (queue).head = (queue).tail = (queue).capacity = 0)
661
662 #define QUEUE_SET(self, queue, _list, _capacity)                                \
663     ((queue).list = (_list),                                                    \
664      (queue).head = (queue).tail = 0,                                           \
665      (queue).capacity = (_capacity))
666
667 #define QUEUE_EMPTY(self, queue)                                                \
668     ((queue).head == (queue).tail)
669
670 #define QUEUE_ITER(self, queue, index)                                          \
671     ((queue).list + (queue).head + index)
672
673 #define ENQUEUE(self, queue, value)                                             \
674     (((queue).tail != (queue).capacity                                          \
675       || yaml_queue_extend((void **)&(queue).list, sizeof(*(queue).list),       \
676           &(queue).head, &(queue).tail, &(queue).capacity)) ?                   \
677         ((queue).list[(queue).tail++] = (value),                                \
678          1) :                                                                   \
679         ((self)->error.type = YAML_MEMORY_ERROR,                                \
680          0))
681
682 #define DEQUEUE(self, queue)                                                    \
683     ((queue).list[(queue).head++])
684
685 #define QUEUE_INSERT(self, queue, index, value)                                 \
686     (((queue).tail != (queue).capacity                                          \
687       || yaml_queue_extend((void **)&(queue).list, sizeof(*(queue).list),       \
688           &(queue).head, &(queue).tail, &(queue).capacity)) ?                   \
689         (memmove((queue).list+(queue).head+(index)+1,                           \
690                  (queue).list+(queue).head+(index),                             \
691             ((queue).tail-(queue).head-(index))*sizeof(*(queue).list)),         \
692          (queue).list[(queue).head+(index)] = (value),                          \
693          (queue).tail++,                                                        \
694          1) :                                                                   \
695         ((self)->error.type = YAML_MEMORY_ERROR,                                \
696          0))
697
698 /*****************************************************************************
699  * Token Initializers
700  *****************************************************************************/
701
702 #define TOKEN_INIT(token, _type, _start_mark, _end_mark)                        \
703     (memset(&(token), 0, sizeof(yaml_token_t)),                                 \
704      (token).type = (_type),                                                    \
705      (token).start_mark = (_start_mark),                                        \
706      (token).end_mark = (_end_mark))
707
708 #define STREAM_START_TOKEN_INIT(token, _encoding, _start_mark, _end_mark)       \
709     (TOKEN_INIT((token), YAML_STREAM_START_TOKEN, (_start_mark), (_end_mark)),  \
710      (token).data.stream_start.encoding = (_encoding))
711
712 #define STREAM_END_TOKEN_INIT(token, _start_mark, _end_mark)                    \
713     (TOKEN_INIT((token), YAML_STREAM_END_TOKEN, (_start_mark), (_end_mark)))
714
715 #define ALIAS_TOKEN_INIT(token, _value, _start_mark, _end_mark)                 \
716     (TOKEN_INIT((token), YAML_ALIAS_TOKEN, (_start_mark), (_end_mark)),         \
717      (token).data.alias.value = (_value))
718
719 #define ANCHOR_TOKEN_INIT(token, _value, _start_mark, _end_mark)                \
720     (TOKEN_INIT((token), YAML_ANCHOR_TOKEN, (_start_mark), (_end_mark)),        \
721      (token).data.anchor.value = (_value))
722
723 #define TAG_TOKEN_INIT(token, _handle, _suffix, _start_mark, _end_mark)         \
724     (TOKEN_INIT((token), YAML_TAG_TOKEN, (_start_mark), (_end_mark)),           \
725      (token).data.tag.handle = (_handle),                                       \
726      (token).data.tag.suffix = (_suffix))
727
728 #define SCALAR_TOKEN_INIT(token, _value, _length, _style, _start_mark, _end_mark)   \
729     (TOKEN_INIT((token), YAML_SCALAR_TOKEN, (_start_mark), (_end_mark)),        \
730      (token).data.scalar.value = (_value),                                      \
731      (token).data.scalar.length = (_length),                                    \
732      (token).data.scalar.style = (_style))
733
734 #define VERSION_DIRECTIVE_TOKEN_INIT(token, _major, _minor, _start_mark, _end_mark) \
735     (TOKEN_INIT((token), YAML_VERSION_DIRECTIVE_TOKEN, (_start_mark), (_end_mark)), \
736      (token).data.version_directive.major = (_major),                           \
737      (token).data.version_directive.minor = (_minor))
738
739 #define TAG_DIRECTIVE_TOKEN_INIT(token, _handle, _prefix, _start_mark, _end_mark)   \
740     (TOKEN_INIT((token), YAML_TAG_DIRECTIVE_TOKEN, (_start_mark), (_end_mark)), \
741      (token).data.tag_directive.handle = (_handle),                             \
742      (token).data.tag_directive.prefix = (_prefix))
743
744 /*****************************************************************************
745  * Event Initializers
746  *****************************************************************************/
747
748 #define EVENT_INIT(event, _type, _start_mark, _end_mark)                        \
749     (memset(&(event), 0, sizeof(yaml_event_t)),                                 \
750      (event).type = (_type),                                                    \
751      (event).start_mark = (_start_mark),                                        \
752      (event).end_mark = (_end_mark))
753
754 #define STREAM_START_EVENT_INIT(event, _encoding, _start_mark, _end_mark)       \
755     (EVENT_INIT((event), YAML_STREAM_START_EVENT, (_start_mark), (_end_mark)),  \
756      (event).data.stream_start.encoding = (_encoding))
757
758 #define STREAM_END_EVENT_INIT(event, _start_mark, _end_mark)                    \
759     (EVENT_INIT((event), YAML_STREAM_END_EVENT, (_start_mark), (_end_mark)))
760
761 #define DOCUMENT_START_EVENT_INIT(event, _version_directive,                    \
762         _tag_directives_list, _tag_directives_length, _tag_directives_capacity, \
763         _is_implicit, _start_mark, _end_mark)                                   \
764     (EVENT_INIT((event), YAML_DOCUMENT_START_EVENT, (_start_mark),(_end_mark)), \
765      (event).data.document_start.version_directive = (_version_directive),      \
766      (event).data.document_start.tag_directives.list = (_tag_directives_list),  \
767      (event).data.document_start.tag_directives.length = (_tag_directives_length),  \
768      (event).data.document_start.tag_directives.capacity = (_tag_directives_capacity),  \
769      (event).data.document_start.is_implicit = (_is_implicit))
770
771 #define DOCUMENT_END_EVENT_INIT(event, _is_implicit, _start_mark, _end_mark)    \
772     (EVENT_INIT((event), YAML_DOCUMENT_END_EVENT, (_start_mark), (_end_mark)),  \
773      (event).data.document_end.is_implicit = (_is_implicit))
774
775 #define ALIAS_EVENT_INIT(event, _anchor, _start_mark, _end_mark)                \
776     (EVENT_INIT((event), YAML_ALIAS_EVENT, (_start_mark), (_end_mark)),         \
777      (event).data.alias.anchor = (_anchor))
778
779 #define SCALAR_EVENT_INIT(event, _anchor, _tag, _value, _length,                \
780         _is_plain_nonspecific, _is_quoted_nonspecific, _style, _start_mark, _end_mark)  \
781     (EVENT_INIT((event), YAML_SCALAR_EVENT, (_start_mark), (_end_mark)),        \
782      (event).data.scalar.anchor = (_anchor),                                    \
783      (event).data.scalar.tag = (_tag),                                          \
784      (event).data.scalar.value = (_value),                                      \
785      (event).data.scalar.length = (_length),                                    \
786      (event).data.scalar.is_plain_nonspecific = (_is_plain_nonspecific),        \
787      (event).data.scalar.is_quoted_nonspecific = (_is_quoted_nonspecific),      \
788      (event).data.scalar.style = (_style))
789
790 #define SEQUENCE_START_EVENT_INIT(event, _anchor, _tag, _is_nonspecific, _style,    \
791         _start_mark, _end_mark)                                                 \
792     (EVENT_INIT((event), YAML_SEQUENCE_START_EVENT, (_start_mark), (_end_mark)),    \
793      (event).data.sequence_start.anchor = (_anchor),                            \
794      (event).data.sequence_start.tag = (_tag),                                  \
795      (event).data.sequence_start.is_nonspecific = (_is_nonspecific),            \
796      (event).data.sequence_start.style = (_style))
797
798 #define SEQUENCE_END_EVENT_INIT(event, _start_mark, _end_mark)                  \
799     (EVENT_INIT((event), YAML_SEQUENCE_END_EVENT, (_start_mark), (_end_mark)))
800
801 #define MAPPING_START_EVENT_INIT(event, _anchor, _tag, _is_nonspecific, _style, \
802         _start_mark, _end_mark)                                                 \
803     (EVENT_INIT((event), YAML_MAPPING_START_EVENT, (_start_mark), (_end_mark)), \
804      (event).data.mapping_start.anchor = (_anchor),                             \
805      (event).data.mapping_start.tag = (_tag),                                   \
806      (event).data.mapping_start.is_nonspecific = (_is_nonspecific),             \
807      (event).data.mapping_start.style = (_style))
808
809 #define MAPPING_END_EVENT_INIT(event, _start_mark, _end_mark)                   \
810     (EVENT_INIT((event), YAML_MAPPING_END_EVENT, (_start_mark), (_end_mark)))
811
812 /*****************************************************************************
813  * Document, Node and Arc Initializers
814  *****************************************************************************/
815
816 #define DOCUMENT_INIT(document, _nodes_list, _nodes_length, _nodes_capacity,    \
817         _version_directive, _tag_directives_list, _tag_directives_length,       \
818         _tag_directives_capacity, _is_start_implicit, _is_end_implicit,         \
819         _start_mark, _end_mark)                                                 \
820     (memset(&(document), 0, sizeof(yaml_document_t)),                           \
821      (document).type = YAML_DOCUMENT,                                           \
822      (document).nodes.list = (_nodes_list),                                     \
823      (document).nodes.length = (_nodes_length),                                 \
824      (document).nodes.capacity = (_nodes_capacity),                             \
825      (document).version_directive = (_version_directive),                       \
826      (document).tag_directives.list = (_tag_directives_list),                   \
827      (document).tag_directives.length = (_tag_directives_length),               \
828      (document).tag_directives.capacity = (_tag_directives_capacity),           \
829      (document).is_start_implicit = (_is_start_implicit),                       \
830      (document).is_end_implicit = (_is_end_implicit),                           \
831      (document).start_mark = (_start_mark),                                     \
832      (document).end_mark = (_end_mark))
833
834 #define NODE_INIT(node, _type, _anchor, _tag, _start_mark, _end_mark)           \
835     (memset(&(node), 0, sizeof(yaml_node_t)),                                   \
836      (node).type = (_type),                                                     \
837      (node).anchor = (_anchor),                                                 \
838      (node).tag = (_tag),                                                       \
839      (node).start_mark = (_start_mark),                                         \
840      (node).end_mark = (_end_mark))
841
842 #define SCALAR_NODE_INIT(node, _anchor, _tag, _value, _length, _style,          \
843         _start_mark, _end_mark)                                                 \
844     (NODE_INIT((node), YAML_SCALAR_NODE, (_anchor), (_tag),                     \
845                (_start_mark), (_end_mark)),                                     \
846      (node).data.scalar.value = (_value),                                       \
847      (node).data.scalar.length = (_length),                                     \
848      (node).data.scalar.style = (_style))
849
850 #define SEQUENCE_NODE_INIT(node, _anchor, _tag, _items_list, _items_length,     \
851         _items_capacity, _style, _start_mark, _end_mark)                        \
852     (NODE_INIT((node), YAML_SEQUENCE_NODE, (_anchor), (_tag),                   \
853                (_start_mark), (_end_mark)),                                     \
854      (node).data.sequence.items.list = (_items_list),                           \
855      (node).data.sequence.items.length = (_items_length),                       \
856      (node).data.sequence.items.capacity = (_items_capacity),                   \
857      (node).data.sequence.style = (_style))
858
859 #define MAPPING_NODE_INIT(node, _anchor, _tag, _pairs_list, _pairs_length,      \
860         _pairs_capacity, _style, _start_mark, _end_mark)                        \
861     (NODE_INIT((node), YAML_MAPPING_NODE, (_anchor), (_tag),                    \
862                (_start_mark), (_end_mark)),                                     \
863      (node).data.mapping.pairs.list = (_pairs_list),                            \
864      (node).data.mapping.pairs.length = (_pairs_length),                        \
865      (node).data.mapping.pairs.capacity = (_pairs_capacity),                    \
866      (node).data.mapping.style = (_style))
867
868 #define ARC_INIT(arc, _type, _tag)                                              \
869     (memset(&(arc), 0, sizeof(yaml_arc_t)),                                     \
870      (arc).type = (_type),                                                      \
871      (arc).tag = (_tag))
872
873 #define SEQUENCE_ITEM_ARC_INIT(arc, _tag, _index)                               \
874     (ARC_INIT((arc), YAML_SEQUENCE_ITEM_ARC, (_tag)),                           \</