summaryrefslogtreecommitdiff
path: root/src/yaml_private.h
blob: 3ae24a23862729f7700a788c0cdf6872ac9ce830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <yaml.h>

#include <assert.h>
#include <limits.h>

/*
 * Memory management.
 */

YAML_DECLARE(void *)
yaml_malloc(size_t size);

YAML_DECLARE(void *)
yaml_realloc(void *ptr, size_t size);

YAML_DECLARE(void)
yaml_free(void *ptr);

YAML_DECLARE(yaml_char_t *)
yaml_strdup(const yaml_char_t *);

/*
 * Error management.
 */

/*
 * Generic error initializers; not to be used directly.
 */

#define ERROR_INIT(error, _type)                                                \
    (memset(&(error), 0, sizeof(yaml_error_t)),                                 \
     (error).type = (_type),                                                    \
     0)

#define READING_ERROR_INIT(error, _type, _problem, _offset, _value)             \
    (ERROR_INIT(error, _type),                                                  \
     (error).data.reading.problem = (_problem),                                 \
     (error).data.reading.offset = (_offset),                                   \
     (error).data.reading.value = (_value),                                     \
     0)

#define LOADING_ERROR_INIT(error, _type, _problem, _problem_mark)               \
    (ERROR_INIT(error, _type),                                                  \
     (error).data.loading.context = NULL,                                       \
     (error).data.loading.context_mark.index = 0,                               \
     (error).data.loading.context_mark.line = 0,                                \
     (error).data.loading.context_mark.column = 0,                              \
     (error).data.loading.problem = (_problem),                                 \
     (error).data.loading.problem_mark = (_problem_mark),                       \
     0)

#define LOADING_ERROR_WITH_CONTEXT_INIT(error, _type, _context, _context_mark,  \
        _problem, _problem_mark)                                                \
    (ERROR_INIT(error, _type),                                                  \
     (error).data.loading.context = (_context),                                 \
     (error).data.loading.context_mark = (_context_mark),                       \
     (error).data.loading.problem = (_problem),                                 \
     (error).data.loading.problem_mark = (_problem_mark),                       \
     0)

#define WRITING_ERROR_INIT(error, _type, _problem, _offset)                     \
    (ERROR_INIT(error, _type),                                                  \
     (error).data.writing.problem = (_problem),                                 \
     (error).data.writing.offset = (_offset),                                   \
     0)

#define DUMPING_ERROR_INIT(error, _type, _problem)                              \
    (ERROR_INIT(error, _type),                                                  \
     (error).data.dumping.problem = (_problem),                                 \
     0)

#define RESOLVING_ERROR_INIT(error, _type, _problem)                            \
    (ERROR_INIT(error, _type),                                                  \
     (error).data.resolving.problem = (_problem),                               \
     0)

/*
 * Specific error initializers.
 */

#define MEMORY_ERROR_INIT(self)                                                 \
    ERROR_INIT((self)->error, YAML_MEMORY_ERROR)

#define READER_ERROR_INIT(self, _problem, _offset)                              \
    READING_ERROR_INIT((self)->error, YAML_READER_ERROR, _problem, _offset, -1)

#define DECODER_ERROR_INIT(self, _problem, _offset, _value)                     \
    READING_ERROR_INIT((self)->error, YAML_DECODER_ERROR, _problem, _offset, _value)

#define SCANNER_ERROR_INIT(self, _problem, _problem_mark)                       \
    LOADING_ERROR_INIT((self)->error, YAML_SCANNER_ERROR, _problem, _problem_mark)

#define SCANNER_ERROR_WITH_CONTEXT_INIT(self, _context, _context_mark,          \
        _problem, _problem_mark)                                                \
    LOADING_ERROR_WITH_CONTEXT_INIT((self)->error, YAML_SCANNER_ERROR,          \
            _context, _context_mark, _problem, _problem_mark)

#define PARSER_ERROR_INIT(self, _problem, _problem_mark)                        \
    LOADING_ERROR_INIT((self)->error, YAML_PARSER_ERROR, _problem, _problem_mark)

#define PARSER_ERROR_WITH_CONTEXT_INIT(self, _context, _context_mark,           \
        _problem, _problem_mark)                                                \
    LOADING_ERROR_WITH_CONTEXT_INIT((self)->error, YAML_PARSER_ERROR,           \
            _context, _context_mark, _problem, _problem_mark)

#define COMPOSER_ERROR_INIT(self, _problem, _problem_mark)                      \
    LOADING_ERROR_INIT((self)->error, YAML_COMPOSER_ERROR, _problem, _problem_mark)

#define COMPOSER_ERROR_WITH_CONTEXT_INIT(self, _context, _context_mark,         \
        _problem, _problem_mark)                                                \
    LOADING_ERROR_WITH_CONTEXT_INIT((self)->error, YAML_COMPOSER_ERROR,         \
            _context, _context_mark, _problem, _problem_mark)

#define WRITER_ERROR_INIT(self, _problem, _offset)                              \
    WRITING_ERROR_INIT((self)->error, YAML_WRITER_ERROR, _problem, _offset)

#define EMITTER_ERROR_INIT(self, _problem)                                      \
    DUMPING_ERROR_INIT((self)->error, YAML_EMITTER_ERROR, _problem)

#define SERIALIZER_ERROR_INIT(self, _problem)                                   \
    DUMPING_ERROR_INIT((self)->error, YAML_SERIALIZER_ERROR, _problem)

#define RESOLVER_ERROR_INIT(self, _problem)                                     \
    RESOLVER_ERROR_INIT((self)->error, YAML_RESOLVER_ERROR, _problem)

/*
 * The size of the input raw buffer.
 */

#define RAW_INPUT_BUFFER_CAPACITY   16384

/*
 * The size of the input buffer.
 *
 * The input buffer should be large enough to hold the content of the raw
 * buffer after it is decoded.
 */

#define INPUT_BUFFER_CAPACITY   (RAW_INPUT_BUFFER_CAPACITY*3)

/*
 * The size of the output buffer.
 */

#define OUTPUT_BUFFER_CAPACITY  16384

/*
 * The size of the output raw buffer.
 *
 * The raw buffer should be able to hold the content of the output buffer
 * after it is encoded.
 */

#define RAW_OUTPUT_BUFFER_CAPACITY  (OUTPUT_BUFFER_CAPACITY*2+2)

/*
 * The size of other stacks and queues.
 */

#define INITIAL_STACK_CAPACITY  16
#define INITIAL_QUEUE_CAPACITY  16
#define INITIAL_STRING_CAPACITY 16

/*
 * String management.
 */

/*
 * An immutable string used as an input buffer.
 */

typedef struct yaml_istring_s {
    const yaml_char_t *buffer;
    size_t pointer;
    size_t length;
} yaml_istring_t;

/*
 * A string that is used as an output buffer.
 */

typedef struct yaml_ostring_s {
    yaml_char_t *buffer;
    size_t pointer;
    size_t capacity;
} yaml_ostring_t;

/*
 * A string that could be used both as an input and an output buffer.
 */

typedef struct yaml_iostring_s {
    yaml_char_t *buffer;
    size_t pointer;
    size_t length;
    size_t capacity;
} yaml_iostring_t;

/*
 * Separate type for non-UTF-8 i/o strings.
 */

typedef struct yaml_raw_iostring_s {
    unsigned char *buffer;
    size_t pointer;
    size_t length;
    size_t capacity;
} yaml_raw_iostring_t;

YAML_DECLARE(int)
yaml_ostring_extend(yaml_char_t **buffer, size_t *capacity);

YAML_DECLARE(int)
yaml_ostring_join(
        yaml_char_t **base_buffer, size_t *base_pointer, size_t *base_capacity,
        yaml_char_t *adj_buffer, size_t adj_pointer);

#define ISTRING(buffer, length) { (buffer), 0, (length) }

#define NULL_OSTRING { NULL, 0, 0 }

#define IOSTRING_INIT(self, string, _capacity)                                  \
    (((string).buffer = yaml_malloc(_capacity)) ?                               \
        ((string).pointer = (string).length = 0,                                \
         (string).capacity = (_capacity),                                       \
         memset((string).buffer, 0, (_capacity)),                               \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

#define IOSTRING_DEL(self, string)                                              \
    (yaml_free((string).buffer),                                                \
     (string).buffer = NULL,                                                    \
     ((string).pointer = (string).length = (string).capacity = 0))

#define OSTRING_INIT(self, string, _capacity)                                   \
    (((string).buffer = yaml_malloc(_capacity)) ?                               \
        ((string).pointer = 0,                                                  \
         (string).capacity = (_capacity),                                       \
         memset((string).buffer, 0, (_capacity)),                               \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

#define OSTRING_DEL(self, string)                                               \
    (yaml_free((string).buffer),                                                \
     (string).buffer = NULL,                                                    \
     ((string).pointer = (string).capacity = 0))

#define OSTRING_EXTEND(self, string)                                            \
    ((((string).pointer+5 < (string).capacity)                                  \
        || yaml_ostring_extend(&(string).buffer, &(string).capacity)) ?         \
     1 :                                                                        \
     ((self)->error.type = YAML_MEMORY_ERROR,                                   \
      0))

#define CLEAR(self, string)                                                     \
    ((string).pointer = 0,                                                      \
     memset((string).buffer, 0, (string).capacity))

#define JOIN(self, base_string, adj_string)                                     \
    ((yaml_ostring_join(&(base_string).buffer, &(base_string).pointer,          \
                       &(base_string).capacity,                                 \
                       (adj_string).buffer, (adj_string).pointer)) ?            \
        ((adj_string).pointer = 0,                                              \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

/*
 * String check operations.
 */

/*
 * Get the octet at the specified position.
 */

#define OCTET_AT(string, offset)                                                \
    ((string).buffer[(string).pointer+(offset)])

/*
 * Get the current offset.
 */

#define OCTET(string)   OCTET_AT((string), 0)

/*
 * Check the octet at the specified position.
 */

#define CHECK_AT(string, octet, offset)                                         \
    (OCTET_AT((string), (offset)) == (yaml_char_t)(octet))

/*
 * Check the current octet in the buffer.
 */

#define CHECK(string, octet)    CHECK_AT((string), (octet), 0)

/*
 * Check if the character at the specified position is an alphabetical
 * character, a digit, '_', or '-'.
 */

#define IS_ALPHA_AT(string, offset)                                             \
     ((OCTET_AT((string), (offset)) >= (yaml_char_t) '0' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) '9') ||                    \
      (OCTET_AT((string), (offset)) >= (yaml_char_t) 'A' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) 'Z') ||                    \
      (OCTET_AT((string), (offset)) >= (yaml_char_t) 'a' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) 'z') ||                    \
      OCTET_AT((string), (offset)) == '_' ||                                    \
      OCTET_AT((string), (offset)) == '-')

#define IS_ALPHA(string)    IS_ALPHA_AT((string), 0)

/*
 * Check if the character at the specified position is a digit.
 */

#define IS_DIGIT_AT(string, offset)                                             \
     ((OCTET_AT((string), (offset)) >= (yaml_char_t) '0' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) '9'))

#define IS_DIGIT(string)    IS_DIGIT_AT((string), 0)

/*
 * Get the value of a digit.
 */

#define AS_DIGIT_AT(string, offset)                                             \
     (OCTET_AT((string), (offset)) - (yaml_char_t) '0')

#define AS_DIGIT(string)    AS_DIGIT_AT((string), 0)

/*
 * Check if the character at the specified position is a hex-digit.
 */

#define IS_HEX_AT(string, offset)                                               \
     ((OCTET_AT((string), (offset)) >= (yaml_char_t) '0' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) '9') ||                    \
      (OCTET_AT((string), (offset)) >= (yaml_char_t) 'A' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) 'F') ||                    \
      (OCTET_AT((string), (offset)) >= (yaml_char_t) 'a' &&                     \
       OCTET_AT((string), (offset)) <= (yaml_char_t) 'f'))

#define IS_HEX(string)    IS_HEX_AT((string), 0)

/*
 * Get the value of a hex-digit.
 */

#define AS_HEX_AT(string, offset)                                               \
      ((OCTET_AT((string), (offset)) >= (yaml_char_t) 'A' &&                    \
        OCTET_AT((string), (offset)) <= (yaml_char_t) 'F') ?                    \
       (OCTET_AT((string), (offset)) - (yaml_char_t) 'A' + 10) :                \
       (OCTET_AT((string), (offset)) >= (yaml_char_t) 'a' &&                    \
        OCTET_AT((string), (offset)) <= (yaml_char_t) 'f') ?                    \
       (OCTET_AT((string), (offset)) - (yaml_char_t) 'a' + 10) :                \
       (OCTET_AT((string), (offset)) - (yaml_char_t) '0'))
 
#define AS_HEX(string)  AS_HEX_AT((string), 0)
 
/*
 * Check if the character is ASCII.
 */

#define IS_ASCII_AT(string, offset)                                             \
    (OCTET_AT((string), (offset)) <= (yaml_char_t) '\x7F')

#define IS_ASCII(string)    IS_ASCII_AT((string), 0)

/*
 * Check if the character can be printed unescaped.
 */

#define IS_PRINTABLE_AT(string, offset)                                         \
    ((OCTET_AT((string), (offset)) == 0x0A)         /* . == #x0A */             \
     || (OCTET_AT((string), (offset)) >= 0x20       /* #x20 <= . <= #x7E */     \
         && OCTET_AT((string), (offset)) <= 0x7E)                               \
     || (OCTET_AT((string), (offset)) == 0xC2       /* #0xA0 <= . <= #xD7FF */  \
         && OCTET_AT((string), (offset)+1) >= 0xA0)                             \
     || (OCTET_AT((string), (offset)) > 0xC2                                    \
         && OCTET_AT((string), (offset)) < 0xED)                                \
     || (OCTET_AT((string), (offset)) == 0xED                                   \
         && OCTET_AT((string), (offset)+1) < 0xA0)                              \
     || (OCTET_AT((string), (offset)) == 0xEE)                                  \
     || (OCTET_AT((string), (offset)) == 0xEF       /* #xE000 <= . <= #xFFFD */ \
         && !(OCTET_AT((string), (offset)+1) == 0xBB       /* && . != #xFEFF */ \
             && OCTET_AT((string), (offset)+2) == 0xBF)                         \
         && !(OCTET_AT((string), (offset)+1) == 0xBF                            \
             && (OCTET_AT((string), (offset)+2) == 0xBE                         \
                 || OCTET_AT((string), (offset)+2) == 0xBF))))

#define IS_PRINTABLE(string)    IS_PRINTABLE_AT((string), 0)

/*
 * Check if the character at the specified position is NUL.
 */

#define IS_Z_AT(string, offset)   CHECK_AT((string), '\0', (offset))

#define IS_Z(string)    IS_Z_AT((string), 0)

/*
 * Check if the character at the specified position is BOM.
 */

#define IS_BOM_AT(string, offset)                                               \
     (CHECK_AT((string), '\xEF', (offset))                                      \
      && CHECK_AT((string), '\xBB', (offset)+1)                                 \
      && CHECK_AT((string), '\xBF', (offset)+2))    /* BOM (#xFEFF) */

#define IS_BOM(string)  IS_BOM_AT(string, 0)

/*
 * Check if the character at the specified position is space.
 */

#define IS_SPACE_AT(string, offset) CHECK_AT((string), ' ', (offset))

#define IS_SPACE(string)    IS_SPACE_AT((string), 0)

/*
 * Check if the character at the specified position is tab.
 */

#define IS_TAB_AT(string, offset)   CHECK_AT((string), '\t', (offset))

#define IS_TAB(string)  IS_TAB_AT((string), 0)

/*
 * Check if the character at the specified position is blank (space or tab).
 */

#define IS_BLANK_AT(string, offset)                                             \
    (IS_SPACE_AT((string), (offset)) || IS_TAB_AT((string), (offset)))

#define IS_BLANK(string)    IS_BLANK_AT((string), 0)

/*
 * Check if the character at the specified position is a line break.
 */

#define IS_BREAK_AT(string, offset)                                             \
    (CHECK_AT((string), '\r', (offset))                 /* CR (#xD)*/           \
     || CHECK_AT((string), '\n', (offset))              /* LF (#xA) */          \
     || (CHECK_AT((string), '\xC2', (offset))                                   \
         && CHECK_AT((string), '\x85', (offset)+1))     /* NEL (#x85) */        \
     || (CHECK_AT((string), '\xE2', (offset))                                   \
         && CHECK_AT((string), '\x80', (offset)+1)                              \
         && CHECK_AT((string), '\xA8', (offset)+2))     /* LS (#x2028) */       \
     || (CHECK_AT((string), '\xE2', (offset))                                   \
         && CHECK_AT((string), '\x80', (offset)+1)                              \
         && CHECK_AT((string), '\xA9', (offset)+2)))    /* PS (#x2029) */

#define IS_BREAK(string)    IS_BREAK_AT((string), 0)

#define IS_CRLF_AT(string, offset)                                              \
     (CHECK_AT((string), '\r', (offset)) && CHECK_AT((string), '\n', (offset)+1))

#define IS_CRLF(string) IS_CRLF_AT((string), 0)

/*
 * Check if the character is a line break or NUL.
 */

#define IS_BREAKZ_AT(string, offset)                                            \
    (IS_BREAK_AT((string), (offset)) || IS_Z_AT((string), (offset)))

#define IS_BREAKZ(string)   IS_BREAKZ_AT((string), 0)

/*
 * Check if the character is a line break, space, or NUL.
 */

#define IS_SPACEZ_AT(string, offset)                                            \
    (IS_SPACE_AT((string), (offset)) || IS_BREAKZ_AT((string), (offset)))

#define IS_SPACEZ(string)   IS_SPACEZ_AT((string), 0)

/*
 * Check if the character is a line break, space, tab, or NUL.
 */

#define IS_BLANKZ_AT(string, offset)                                            \
    (IS_BLANK_AT((string), (offset)) || IS_BREAKZ_AT((string), (offset)))

#define IS_BLANKZ(string)   IS_BLANKZ_AT((string), 0)

/*
 * Determine the width of the character.
 */

#define WIDTH_AT(string, offset)                                                \
     ((OCTET_AT((string), (offset)) & 0x80) == 0x00 ? 1 :                       \
      (OCTET_AT((string), (offset)) & 0xE0) == 0xC0 ? 2 :                       \
      (OCTET_AT((string), (offset)) & 0xF0) == 0xE0 ? 3 :                       \
      (OCTET_AT((string), (offset)) & 0xF8) == 0xF0 ? 4 : 0)

#define WIDTH(string)   WIDTH_AT((string), 0)

/*
 * Move the string pointer to the next character.
 */

#define MOVE(string)    ((string).pointer += WIDTH((string)))

/*
 * Write a single octet and bump the pointer.
 */

#define JOIN_OCTET(string, octet)                                               \
    ((string).buffer[(string).pointer++] = (octet))

/*
 * Copy a single octet and bump the pointers.
 */

#define COPY_OCTET(target_string, source_string)                                \
    ((target_string).buffer[(target_string).pointer++]                          \
     = (source_string).buffer[(source_string).pointer++])

/*
 * Copy a character and move the pointers of both strings.
 */

#define COPY(target_string, source_string)                                      \
    ((OCTET(source_string) & 0x80) == 0x00 ?                                    \
     COPY_OCTET((target_string), (source_string)) :                             \
     (OCTET(source_string) & 0xE0) == 0xC0 ?                                    \
     (COPY_OCTET((target_string), (source_string)),                             \
      COPY_OCTET((target_string), (source_string))) :                           \
     (OCTET(source_string) & 0xF0) == 0xE0 ?                                    \
     (COPY_OCTET((target_string), (source_string)),                             \
      COPY_OCTET((target_string), (source_string)),                             \
      COPY_OCTET((target_string), (source_string))) :                           \
     (OCTET(source_string) & 0xF8) == 0xF0 ?                                    \
     (COPY_OCTET((target_string), (source_string)),                             \
      COPY_OCTET((target_string), (source_string)),                             \
      COPY_OCTET((target_string), (source_string)),                             \
      COPY_OCTET((target_string), (source_string))) : 0)

/*
 * Stack and queue management.
 */

YAML_DECLARE(int)
yaml_stack_extend(void **list, size_t size, size_t *length, size_t *capacity);

YAML_DECLARE(int)
yaml_queue_extend(void **list, size_t size,
        size_t *head, size_t *tail, size_t *capacity);

#define STACK_INIT(self, stack, _capacity)                                      \
    (((stack).list = yaml_malloc((_capacity)*sizeof(*(stack).list))) ?          \
        ((stack).length = 0,                                                    \
         (stack).capacity = (_capacity),                                        \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

#define STACK_DEL(self, stack)                                                  \
    (yaml_free((stack).list),                                                   \
     (stack).list = NULL,                                                       \
     (stack).length = (stack).capacity = 0)

#define STACK_EMPTY(self, stack)                                                \
    ((stack).length == 0)

#define STACK_ITER(self, stack, index)                                          \
    ((stack).list + index)

#define PUSH(self, stack, value)                                                \
    (((stack).length < (stack).capacity                                         \
      || yaml_stack_extend((void **)&(stack).list, sizeof(*(stack).list),       \
              &(stack).length, &(stack).capacity)) ?                            \
        ((stack).list[(stack).length++] = (value),                              \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

#define POP(self, stack)                                                        \
    ((stack).list[--(stack).length])

#define QUEUE_INIT(self, queue, _capacity)                                      \
    (((queue).list = yaml_malloc((_capacity)*sizeof(*(queue).list))) ?          \
        ((queue).head = (queue).tail = 0,                                       \
         (queue).capacity = (_capacity),                                        \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

#define QUEUE_DEL(self, queue)                                                  \
    (yaml_free((queue).list),                                                   \
     (queue).list = NULL,                                                       \
     (queue).head = (queue).tail = (queue).capacity = 0)

#define QUEUE_EMPTY(self, queue)                                                \
    ((queue).head == (queue).tail)

#define QUEUE_ITER(self, queue, index)                                          \
    ((queue).list + (queue).head + index)

#define ENQUEUE(self, queue, value)                                             \
    (((queue).tail != (queue).capacity                                          \
      || yaml_queue_extend((void **)&(queue).list, sizeof(*(queue).list),       \
          &(queue).head, &(queue).tail, &(queue).capacity)) ?                   \
        ((queue).list[(queue).tail++] = (value),                                \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

#define DEQUEUE(self, queue)                                                    \
    ((queue).list[(queue).head++])

#define QUEUE_INSERT(self, queue, index, value)                                 \
    (((queue).tail != (queue).capacity                                          \
      || yaml_queue_extend((void **)&(queue).list, sizeof(*(queue).list),       \
          &(queue).head, &(queue).tail, &(queue).capacity)) ?                   \
        (memmove((queue).list+(queue).head+(index)+1,                           \
                 (queue).list+(queue).head+(index),                             \
            ((queue).tail-(queue).head-(index))*sizeof(*(queue).list)),         \
         (queue).list[(queue).head+(index)] = (value),                          \
         (queue).tail++,                                                        \
         1) :                                                                   \
        ((self)->error.type = YAML_MEMORY_ERROR,                                \
         0))

/*
 * Token initializers.
 */

#define TOKEN_INIT(token, _type, _start_mark, _end_mark)                        \
    (memset(&(token), 0, sizeof(yaml_token_t)),                                 \
     (token).type = (_type),                                                    \
     (token).start_mark = (_start_mark),                                        \
     (token).end_mark = (_end_mark))

#define STREAM_START_TOKEN_INIT(token, _encoding, _start_mark, _end_mark)       \
    (TOKEN_INIT((token), YAML_STREAM_START_TOKEN, (_start_mark), (_end_mark)),  \
     (token).data.stream_start.encoding = (_encoding))

#define STREAM_END_TOKEN_INIT(token, _start_mark, _end_mark)                    \
    (TOKEN_INIT((token), YAML_STREAM_END_TOKEN, (_start_mark), (_end_mark)))

#define ALIAS_TOKEN_INIT(token, _value, _start_mark, _end_mark)                 \
    (TOKEN_INIT((token), YAML_ALIAS_TOKEN, (_start_mark), (_end_mark)),         \
     (token).data.alias.value = (_value))

#define ANCHOR_TOKEN_INIT(token, _value, _start_mark, _end_mark)                \
    (TOKEN_INIT((token), YAML_ANCHOR_TOKEN, (_start_mark), (_end_mark)),        \
     (token).data.anchor.value = (_value))

#define TAG_TOKEN_INIT(token, _handle, _suffix, _start_mark, _end_mark)         \
    (TOKEN_INIT((token), YAML_TAG_TOKEN, (_start_mark), (_end_mark)),           \
     (token).data.tag.handle = (_handle),                                       \
     (token).data.tag.suffix = (_suffix))

#define SCALAR_TOKEN_INIT(token, _value, _length, _style, _start_mark, _end_mark)   \
    (TOKEN_INIT((token), YAML_SCALAR_TOKEN, (_start_mark), (_end_mark)),        \
     (token).data.scalar.value = (_value),                                      \
     (token).data.scalar.length = (_length),                                    \
     (token).data.scalar.style = (_style))

#define VERSION_DIRECTIVE_TOKEN_INIT(token, _major, _minor, _start_mark, _end_mark) \
    (TOKEN_INIT((token), YAML_VERSION_DIRECTIVE_TOKEN, (_start_mark), (_end_mark)), \
     (token).data.version_directive.major = (_major),                           \
     (token).data.version_directive.minor = (_minor))

#define TAG_DIRECTIVE_TOKEN_INIT(token, _handle, _prefix, _start_mark, _end_mark)   \
    (TOKEN_INIT((token), YAML_TAG_DIRECTIVE_TOKEN, (_start_mark), (_end_mark)), \
     (token).data.tag_directive.handle = (_handle),                             \
     (token).data.tag_directive.prefix = (_prefix))

/*
 * Event initializers.
 */

#define EVENT_INIT(event, _type, _start_mark, _end_mark)                        \
    (memset(&(event), 0, sizeof(yaml_event_t)),                                 \
     (event).type = (_type),                                                    \
     (event).start_mark = (_start_mark),                                        \
     (event).end_mark = (_end_mark))

#define STREAM_START_EVENT_INIT(event, _encoding, _start_mark, _end_mark)       \
    (EVENT_INIT((event), YAML_STREAM_START_EVENT, (_start_mark), (_end_mark)),  \
     (event).data.stream_start.encoding = (_encoding))

#define STREAM_END_EVENT_INIT(event, _start_mark, _end_mark)                    \
    (EVENT_INIT((event), YAML_STREAM_END_EVENT, (_start_mark), (_end_mark)))

#define DOCUMENT_START_EVENT_INIT(event, _version_directive,                    \
        _tag_directives_list, _tag_directives_length, _tag_directives_capacity, \
        _is_implicit, _start_mark, _end_mark)                                   \
    (EVENT_INIT((event), YAML_DOCUMENT_START_EVENT, (_start_mark),(_end_mark)), \
     (event).data.document_start.version_directive = (_version_directive),      \
     (event).data.document_start.tag_directives.list = (_tag_directives_list),  \
     (event).data.document_start.tag_directives.length = (_tag_directives_length),  \
     (event).data.document_start.tag_directives.capacity = (_tag_directives_capacity),  \
     (event).data.document_start.is_implicit = (_is_implicit))

#define DOCUMENT_END_EVENT_INIT(event, _is_implicit, _start_mark, _end_mark)    \
    (EVENT_INIT((event), YAML_DOCUMENT_END_EVENT, (_start_mark), (_end_mark)),  \
     (event).data.document_end.is_implicit = (_is_implicit))

#define ALIAS_EVENT_INIT(event, _anchor, _start_mark, _end_mark)                \
    (EVENT_INIT((event), YAML_ALIAS_EVENT, (_start_mark), (_end_mark)),         \
     (event).data.alias.anchor = (_anchor))

#define SCALAR_EVENT_INIT(event, _anchor, _tag, _value, _length,                \
        _is_plain_implicit, _is_quoted_implicit, _style, _start_mark, _end_mark)    \
    (EVENT_INIT((event), YAML_SCALAR_EVENT, (_start_mark), (_end_mark)),        \
     (event).data.scalar.anchor = (_anchor),                                    \
     (event).data.scalar.tag = (_tag),                                          \
     (event).data.scalar.value = (_value),                                      \
     (event).data.scalar.length = (_length),                                    \
     (event).data.scalar.is_plain_implicit = (_is_plain_implicit),              \
     (event).data.scalar.is_quoted_implicit = (_is_quoted_implicit),            \
     (event).data.scalar.style = (_style))

#define SEQUENCE_START_EVENT_INIT(event, _anchor, _tag, _is_implicit, _style,   \
        _start_mark, _end_mark)                                                 \
    (EVENT_INIT((event), YAML_SEQUENCE_START_EVENT, (_start_mark), (_end_mark)),    \
     (event).data.sequence_start.anchor = (_anchor),                            \
     (event).data.sequence_start.tag = (_tag),                                  \
     (event).data.sequence_start.is_implicit = (_is_implicit),                  \
     (event).data.sequence_start.style = (_style))

#define SEQUENCE_END_EVENT_INIT(event, _start_mark, _end_mark)                  \
    (EVENT_INIT((event), YAML_SEQUENCE_END_EVENT, (_start_mark), (_end_mark)))

#define MAPPING_START_EVENT_INIT(event, _anchor, _tag, _is_implicit, _style,    \
        _start_mark, _end_mark)                                                 \
    (EVENT_INIT((event), YAML_MAPPING_START_EVENT, (_start_mark), (_end_mark)), \
     (event).data.mapping_start.anchor = (_anchor),                             \
     (event).data.mapping_start.tag = (_tag),                                   \
     (event).data.mapping_start.is_implicit = (_is_implicit),                   \
     (event).data.mapping_start.style = (_style))

#define MAPPING_END_EVENT_INIT(event, _start_mark, _end_mark)                   \
    (EVENT_INIT((event), YAML_MAPPING_END_EVENT, (_start_mark), (_end_mark)))

#if 0

/*
 * Document initializer.
 */

#define DOCUMENT_INIT(document,document_nodes_start,document_nodes_end,         \
        document_version_directive,document_tag_directives_start,               \
        document_tag_directives_end,document_start_implicit,                    \
        document_end_implicit,document_start_mark,document_end_mark)            \
    (memset(&(document), 0, sizeof(yaml_document_t)),                           \
     (document).nodes.start = (document_nodes_start),                           \
     (document).nodes.end = (document_nodes_end),                               \
     (document).nodes.top = (document_nodes_start),                             \
     (document).version_directive = (document_version_directive),               \
     (document).tag_directives.start = (document_tag_directives_start),         \
     (document).tag_directives.end = (document_tag_directives_end),             \
     (document).start_implicit = (document_start_implicit),                     \
     (document).end_implicit = (document_end_implicit),                         \
     (document).start_mark = (document_start_mark),                             \
     (document).end_mark = (document_end_mark))

/*
 * Node initializers.
 */

#define NODE_INIT(node,node_type,node_tag,node_start_mark,node_end_mark)        \
    (memset(&(node), 0, sizeof(yaml_node_t)),                                   \
     (node).type = (node_type),                                                 \
     (node).tag = (node_tag),                                                   \
     (node).start_mark = (node_start_mark),                                     \
     (node).end_mark = (node_end_mark))

#define SCALAR_NODE_INIT(node,node_tag,node_value,node_length,                  \
        node_style,start_mark,end_mark)                                         \
    (NODE_INIT((node),YAML_SCALAR_NODE,(node_tag),(start_mark),(end_mark)),     \
     (node).data.scalar.value = (node_value),                                   \
     (node).data.scalar.length = (node_length),                                 \
     (node).data.scalar.style = (node_style))

#define SEQUENCE_NODE_INIT(node,node_tag,node_items_start,node_items_end,       \
        node_style,start_mark,end_mark)                                         \
    (NODE_INIT((node),YAML_SEQUENCE_NODE,(node_tag),(start_mark),(end_mark)),   \
     (node).data.sequence.items.start = (node_items_start),                     \
     (node).data.sequence.items.end = (node_items_end),                         \
     (node).data.sequence.items.top = (node_items_start),                       \
     (node).data.sequence.style = (node_style))

#define MAPPING_NODE_INIT(node,node_tag,node_pairs_start,node_pairs_end,        \
        node_style,start_mark,end_mark)                                         \
    (NODE_INIT((node),YAML_MAPPING_NODE,(node_tag),(start_mark),(end_mark)),    \
     (node).data.mapping.pairs.start = (node_pairs_start),                      \
     (node).data.mapping.pairs.end = (node_pairs_end),                          \
     (node).data.mapping.pairs.top = (node_pairs_start),                        \
     (node).data.mapping.style = (node_style))

#endif

/*
 * This structure holds information about a potential simple key.
 */

typedef struct yaml_simple_key_s {
    /* Is a simple key possible? */
    int is_possible;
    /* Is a simple key required? */
    int is_required;
    /* The number of the token. */
    size_t token_number;
    /* The position mark. */
    yaml_mark_t mark;
} yaml_simple_key_t;

/*
 * The states of the parser.
 */

typedef enum yaml_parser_state_e {
    /* Expect STREAM-START. */
    YAML_PARSE_STREAM_START_STATE,
    /* Expect the beginning of an implicit document. */
    YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE,
    /* Expect DOCUMENT-START. */
    YAML_PARSE_DOCUMENT_START_STATE,
    /* Expect the content of a document. */
    YAML_PARSE_DOCUMENT_CONTENT_STATE,
    /* Expect DOCUMENT-END. */
    YAML_PARSE_DOCUMENT_END_STATE,
    /* Expect a block node. */
    YAML_PARSE_BLOCK_NODE_STATE,
    /* Expect a block node or indentless sequence. */
    YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE,
    /* Expect a flow node. */
    YAML_PARSE_FLOW_NODE_STATE,
    /* Expect the first entry of a block sequence. */
    YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE,
    /* Expect an entry of a block sequence. */
    YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE,
    /* Expect an entry of an indentless sequence. */
    YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE,
    /* Expect the first key of a block mapping. */
    YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE,
    /* Expect a block mapping key. */
    YAML_PARSE_BLOCK_MAPPING_KEY_STATE,
    /* Expect a block mapping value. */
    YAML_PARSE_BLOCK_MAPPING_VALUE_STATE,
    /* Expect the first entry of a flow sequence. */
    YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE,
    /* Expect an entry of a flow sequence. */
    YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE,
    /* Expect a key of an ordered mapping. */
    YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE,
    /* Expect a value of an ordered mapping. */
    YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE,
    /* Expect the and of an ordered mapping entry. */
    YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE,
    /* Expect the first key of a flow mapping. */
    YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE,
    /* Expect a key of a flow mapping. */
    YAML_PARSE_FLOW_MAPPING_KEY_STATE,
    /* Expect a value of a flow mapping. */
    YAML_PARSE_FLOW_MAPPING_VALUE_STATE,
    /* Expect an empty value of a flow mapping. */
    YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE,
    /* Expect nothing. */
    YAML_PARSE_END_STATE
} yaml_parser_state_t;

/*
 * This structure holds aliases data.
 */

typedef struct yaml_alias_data_s {
    /* The anchor. */
    yaml_char_t *anchor;
    /* The node id. */
    int index;
    /* The anchor mark. */
    yaml_mark_t mark;
} yaml_alias_data_t;

/*
 * The structure that holds data used by the file and string readers.
 */

typedef struct yaml_standard_reader_data_t {
    /* String input data. */
    yaml_istring_t string;
    /* File input data. */
    FILE *file;
} yaml_standard_reader_data_t;

/*
 * The internal parser structure.
 */

struct yaml_parser_s {

    /*
     * Error stuff.
     */

    yaml_error_t error;

    /*
     * Reader stuff.
     */

    /* The read handler. */
    yaml_reader_t *reader;

    /* The application data to be passed to the reader. */
    void *reader_data;

    /* Standard (string or file) input data. */
    yaml_standard_reader_data_t standard_reader_data;

    /* EOF flag. */
    int is_eof;

    /* The working buffer. */
    yaml_iostring_t input;

    /* The number of unread characters in the buffer. */
    size_t unread;

    /* The raw buffer. */
    yaml_raw_iostring_t raw_input;

    /* The input encoding. */
    yaml_encoding_t encoding;

    /* The offset of the current position (in bytes). */
    size_t offset;

    /* The mark of the current position. */
    yaml_mark_t mark;

    /*
     * Scanner stuff.
     */

    /* Have we started to scan the input stream? */
    int is_stream_start_produced;

    /* Have we reached the end of the input stream? */
    int is_stream_end_produced;

    /* The number of unclosed '[' and '{' indicators. */
    int flow_level;

    /* The tokens queue. */
    struct {
        yaml_token_t *list;
        size_t head;
        size_t tail;
        size_t capacity;
    } tokens;

    /* The number of tokens fetched from the queue. */
    size_t tokens_parsed;

    /* Does the tokens queue contain a token ready for dequeueing. */
    int is_token_available;

    /* The indentation levels stack. */
    struct {
        int *list;
        size_t length;
        size_t capacity;
    } indents;

    /* The current indentation level. */
    int indent;

    /* May a simple key occur at the current position? */
    int is_simple_key_allowed;

    /* The stack of simple keys. */
    struct {
        yaml_simple_key_t *list;
        size_t length;
        size_t capacity;
    } simple_keys;

    /*
     * Parser stuff.
     */

    /* The parser states stack. */
    struct {
        yaml_parser_state_t *list;
        size_t length;
        size_t capacity;
    } states;

    /* The current parser state. */
    yaml_parser_state_t state;

    /* The stack of marks. */
    struct {
        yaml_mark_t *list;
        size_t length;
        size_t capacity;
    } marks;

    /* The list of TAG directives. */
    struct {
        yaml_tag_directive_t *list;
        size_t length;
        size_t capacity;
    } tag_directives;

    /*
     * Dumper stuff.
     */

    /* The resolve handler. */
    yaml_resolver_t *resolver;

    /* The application data to be passed to the resolver. */
    void *resolver_data;

    /* The alias data. */
    struct {
        yaml_alias_data_t *list;
        size_t length;
        size_t capacity;
    } aliases;

    /* The currently parsed document. */
    yaml_document_t *document;

};

/*
 * The emitter states.
 */

typedef enum yaml_emitter_state_e {
    /** Expect STREAM-START. */
    YAML_EMIT_STREAM_START_STATE,
    /** Expect the first DOCUMENT-START or STREAM-END. */
    YAML_EMIT_FIRST_DOCUMENT_START_STATE,
    /** Expect DOCUMENT-START or STREAM-END. */
    YAML_EMIT_DOCUMENT_START_STATE,
    /** Expect the content of a document. */
    YAML_EMIT_DOCUMENT_CONTENT_STATE,
    /** Expect DOCUMENT-END. */
    YAML_EMIT_DOCUMENT_END_STATE,
    /** Expect the first item of a flow sequence. */
    YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE,
    /** Expect an item of a flow sequence. */
    YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE,
    /** Expect the first key of a flow mapping. */
    YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE,
    /** Expect a key of a flow mapping. */
    YAML_EMIT_FLOW_MAPPING_KEY_STATE,
    /** Expect a value for a simple key of a flow mapping. */
    YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE,
    /** Expect a value of a flow mapping. */
    YAML_EMIT_FLOW_MAPPING_VALUE_STATE,
    /** Expect the first item of a block sequence. */
    YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE,
    /** Expect an item of a block sequence. */
    YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE,
    /** Expect the first key of a block mapping. */
    YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE,
    /** Expect the key of a block mapping. */
    YAML_EMIT_BLOCK_MAPPING_KEY_STATE,
    /** Expect a value for a simple key of a block mapping. */
    YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE,
    /** Expect a value of a block mapping. */
    YAML_EMIT_BLOCK_MAPPING_VALUE_STATE,
    /** Expect nothing. */
    YAML_EMIT_END_STATE
} yaml_emitter_state_t;

/*
 * The structure that holds data used by the file and string readers.
 */

typedef struct yaml_standard_writer_data_t {
    /* String output data. */
    yaml_ostring_t string;
    size_t *length;
    /* File output data. */
    FILE *file;
} yaml_standard_writer_data_t;

/*
 * The internals emitter structure.
 */

struct yaml_emitter_s {

    /*
     * Error stuff.
     */

    yaml_error_t error;

    /*
     * Writer stuff.
     */

    /* Write handler. */
    yaml_writer_t *writer;

    /* A pointer for passing to the white handler. */
    void *writer_data;

    /* Standard (string or file) output data. */
    yaml_standard_writer_data_t standard_writer_data;

    /* The working buffer. */
    yaml_iostring_t output;

    /* The raw buffer. */
    yaml_raw_iostring_t raw_output;

    /* The offset of the current position (in bytes). */
    size_t offset;

    /* The stream encoding. */
    yaml_encoding_t encoding;

    /*
     * Emitter stuff.
     */

    /* If the output is in the canonical style? */
    int is_canonical;
    /* The number of indentation spaces. */
    int best_indent;
    /* The preferred width of the output lines. */
    int best_width;
    /* Allow unescaped non-ASCII characters? */
    int is_unicode;
    /* The preferred line break. */
    yaml_break_t line_break;

    /* The stack of states. */
    struct {
        yaml_emitter_state_t *list;
        size_t length;
        size_t capacity;
    } states;

    /* The current emitter state. */
    yaml_emitter_state_t state;

    /* The event queue. */
    struct {
        yaml_event_t *list;
        size_t head;
        size_t tail;
        size_t capacity;
    } events;

    /* The stack of indentation levels. */
    struct {
        int *list;
        size_t length;
        size_t capacity;
    } indents;

    /* The list of tag directives. */
    struct {
        yaml_tag_directive_t *list;
        size_t length;
        size_t capacity;
    } tag_directives;

    /* The current indentation level. */
    int indent;

    /* The current flow level. */
    int flow_level;

    /* Is it the document root context? */
    int is_root_context;
    /* Is it a sequence context? */
    int is_sequence_context;
    /* Is it a mapping context? */
    int is_mapping_context;
    /* Is it a simple mapping key context? */
    int is_simple_key_context;

    /* The current line. */
    int line;
    /* The current column. */
    int column;
    /* If the last character was a whitespace? */
    int is_whitespace;
    /* If the last character was an indentation character (' ', '-', '?', ':')? */
    int is_indention;

    /* Anchor analysis. */
    struct {
        /* The anchor value. */
        const yaml_char_t *anchor;
        /* The anchor length. */
        size_t anchor_length;
        /* Is it an alias? */
        int is_alias;
    } anchor_data;

    /* Tag analysis. */
    struct {
        /* The tag handle. */
        const yaml_char_t *handle;
        /* The tag handle length. */
        size_t handle_length;
        /* The tag suffix. */
        const yaml_char_t *suffix;
        /* The tag suffix length. */
        size_t suffix_length;
    } tag_data;

    /* Scalar analysis. */
    struct {
        /* The scalar value. */
        const yaml_char_t *value;
        /* The scalar length. */
        size_t length;
        /* Does the scalar contain line breaks? */
        int is_multiline;
        /* Can the scalar be expessed in the flow plain style? */
        int is_flow_plain_allowed;
        /* Can the scalar be expressed in the block plain style? */
        int is_block_plain_allowed;
        /* Can the scalar be expressed in the single quoted style? */
        int is_single_quoted_allowed;
        /* Can the scalar be expressed in the literal or folded styles? */
        int is_block_allowed;
        /* The output style. */
        yaml_scalar_style_t style;
    } scalar_data;

    /*
     * Dumper stuff.
     */

    /* If the stream was already opened? */
    int is_opened;
    /* If the stream was already closed? */
    int is_closed;

    /* The information associated with the document nodes. */
    struct {
        /* The number of references. */
        size_t references;
        /* The anchor id. */
        int anchor;
        /* If the node has been emitted? */
        int is_serialized;
    } *anchors;

    /* The last assigned anchor id. */
    int last_anchor_id;

    /* The currently emitted document. */
    yaml_document_t *document;

};

/*
 * Reader: Ensure that the buffer contains at least `length` characters.
 */

YAML_DECLARE(int)
yaml_parser_update_buffer(yaml_parser_t *parser, size_t length);

/*
 * Scanner: Ensure that the token stack contains at least one token ready.
 */

YAML_DECLARE(int)
yaml_parser_fetch_more_tokens(yaml_parser_t *parser);