summaryrefslogtreecommitdiff
path: root/include/yaml.h
blob: 582d8227b6abac377c95d606bddc75ba08777f98 (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
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
/*****************************************************************************
 * Public Interface for LibYAML
 *
 * Copyright (c) 2006 Kirill Simonov
 *
 * LibYAML is free software; you can use, modify and/or redistribute it under
 * the terms of the MIT license; see the file LICENCE for more details.
 *****************************************************************************/

/*
 * General guidelines.
 *
 * Naming conventions: all functions exported by LibYAML starts with the `yaml_` prefix;
 * types starts with `yaml_` and ends with `_t`; macros and enumerations starts
 * with `YAML_`.
 *
 * FIXME: Calling conventions.
 * FIXME: Memory allocation.
 * FIXME: Errors and exceptions.
 * FIXME: And so on, and so forth.
 */


#ifndef YAML_H
#define YAML_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*****************************************************************************
 * Export Definitions
 *****************************************************************************/

/*
 * Public API declarations.
 *
 * The following definitions are relevant only for the Win32 platform.  If you
 * are building LibYAML as a static library or linking your application against
 * LibYAML compiled as a static library, define the macro
 * `YAML_DECLARE_STATIC`.  If you are building LibYAML as a dynamic library
 * (DLL), you need to define `YAML_DECLARE_EXPORT`.  You don't need to define
 * any macros in case you are linking your application against LibYAML compiled
 * as DLL.
 *
 * There is no need to define any macros if you use LibYAML on non-Win32
 * platform.
 */

#ifdef WIN32
#   if defined(YAML_DECLARE_STATIC)
#       define  YAML_DECLARE(type)  type
#   elif defined(YAML_DECLARE_EXPORT)
#       define  YAML_DECLARE(type)  __declspec(dllexport) type
#   else
#       define  YAML_DECLARE(type)  __declspec(dllimport) type
#   endif
#else
#   define  YAML_DECLARE(type)  type
#endif

/*****************************************************************************
 * Version Information
 *****************************************************************************/

/*
 * The major, minor and patch version numbers of LibYAML.
 */

#define YAML_VERSION_MAJOR  0
#define YAML_VERSION_MINOR  2
#define YAML_VERSION_PATCH  0

/*
 * The version of LibYAML as a string.
 */

#define YAML_VERSION_STRING "0.2.0"

/*
 * Get the library version numbers at runtime.
 *
 * Arguments:
 *
 * - `major`: a pointer to store the major version number.
 *
 * - `minor`: a pointer to store the minor version number.
 *
 * - `patch`: a pointer to store the patch version number.
 */

YAML_DECLARE(void)
yaml_get_version(int *major, int *minor, int *patch);

/*
 * Get the library version as a string at runtime.
 *
 * Returns: the version of LibYAML as a static string.
 */

YAML_DECLARE(const char *)
yaml_get_version_string(void);

/*****************************************************************************
 * Error Handling
 *****************************************************************************/

/*
 * The type of the error.
 *
 * The YAML parser and emitter reports any error details using the
 * `yaml_error_t` structure.  The error type shows what subsystem generated the
 * error and what additional information about the error is available.
 */

typedef enum yaml_error_type_e {
    /* No error was produced. */
    YAML_NO_ERROR,

    /* Cannot allocate or reallocate a block of memory. */
    YAML_MEMORY_ERROR,

    /* Cannot read from the input stream. */
    YAML_READER_ERROR,
    /* Cannot decode a character in the input stream. */
    YAML_DECODER_ERROR,
    /* Cannot scan a YAML token. */
    YAML_SCANNER_ERROR,
    /* Cannot parse a YAML event. */
    YAML_PARSER_ERROR,
    /* Cannot compose a YAML document. */
    YAML_COMPOSER_ERROR,

    /* Cannot write into the output stream. */
    YAML_WRITER_ERROR,
    /* Cannot emit a YAML event. */
    YAML_EMITTER_ERROR,
    /* Cannot serialize a YAML document. */
    YAML_SERIALIZER_ERROR,

    /* Cannot resolve an implicit YAML node tag. */
    YAML_RESOLVER_ERROR
} yaml_error_type_t;

/*
 * Position in the input stream.
 *
 * Marks are used to indicate the position of YAML tokens, events and documents
 * in the input stream as well as to point to the place where a parser error has
 * occured.
 */

typedef struct yaml_mark_s {
    /* The number of the character in the input stream (starting from zero). */
    size_t index;
    /* The line in the input stream (starting from zero). */
    size_t line;
    /* The column in the input stream (starting from zero). */
    size_t column;
} yaml_mark_t;

/*
 * Error details.
 *
 * The structure gives detailed information on any problem that occured during
 * parsing or emitting.
 */

typedef struct yaml_error_s {

    /* The error type. */
    yaml_error_type_t type;

    /* The specific information for each error type. */
    union {

        /*
         * A problem occured while reading the input stream (relevant for
         * `YAML_READER_ERROR` and `YAML_DECODER_ERROR`).
         */
        struct {
            /* The problem description. */
            const char *problem;
            /* The position in the input stream, in bytes. */
            size_t offset;
            /* The problematic octet or character (`-1` if not applicable). */
            int value;
        } reading;

        /*
         * A problem occured while loading YAML data from the input stream
         * (relevant for `YAML_SCANNER_ERROR`, `YAML_PARSER_ERROR`, and
         * `YAML_COMPOSER_ERROR`).
         */
        struct {
            /* The description of the context in which the problem occured
               (`NULL` if not applicable). */
            const char *context;
            /* The context mark (if `context` is not `NULL`). */
            yaml_mark_t context_mark;
            /* The problem description. */
            const char *problem;
            /* The problem mark. */
            yaml_mark_t problem_mark;
        } loading;

        /*
         * A problem occured while writing into the output stream (relevant for
         * `YAML_WRITER_ERROR`).
         */
        struct {
            /* The problem description. */
            const char *problem;
            /* The position in the output stream, in bytes. */
            size_t offset;
        } writing;

        /*
         * A problem while dumping YAML data into the output stream (relevant
         * for `YAML_EMITTER_ERROR` and `YAML_SERIALIZER_ERROR`).
         */
        struct {
            /* The problem description. */
            const char *problem;
        } dumping;

        /*
         * A problem occured while resolving an implicit YAML node tag
         * (relevant for `YAML_RESOLVER_ERROR`).
         */
        struct {
            /* The problem description. */
            const char *problem;
        } resolving;

    } data;

} yaml_error_t;

/*
 * Generate an error message.
 *
 * Given an instance of the `yaml_error_t` structure and a buffer, the function
 * fills the buffer with a message describing the error.  The generated message
 * follows the pattern: `"Error type: error details"`.  If the buffer is not
 * large enough to hold the whole message, the function fails.
 *
 * Arguments:
 *
 * - `error`: an error object obtained using `yaml_parser_get_error()` or
 *   `yaml_emitter_get_error()`.
 *
 * - `buffer`: a pointer to a character buffer to be filled with a generated
 *   error message.
 *
 * - `capacity`: the size of the buffer.
 *
 * Returns: `1` if the function succeeded, `0` on error.  The function may fail
 * if the provided buffer is not large enough to hold the whole buffer, in
 * which case an application may increase the size of the buffer and call the
 * function again.  If the function fails, the content of the buffer is
 * undefined.
 */

YAML_DECLARE(int)
yaml_error_message(yaml_error_t *error, char *buffer, size_t capacity);

/******************************************************************************
 * Basic Types
 ******************************************************************************/

/*
 * The character type (UTF-8 octet).
 *
 * Usage of the string type `(yaml_char_t *)` in the LibYAML API indicates that
 * the string is encoded in UTF-8.
 */

typedef unsigned char yaml_char_t;

/*
 * The version directive information.
 *
 * Note that LibYAML only supports YAML 1.1.
 */

typedef struct yaml_version_directive_s {
    /* The major version number. */
    int major;
    /* The minor version number. */
    int minor;
} yaml_version_directive_t;

/*
 * The tag directive information.
 */

typedef struct yaml_tag_directive_s {
    /* The tag handle. */
    yaml_char_t *handle;
    /* The tag prefix. */
    yaml_char_t *prefix;
} yaml_tag_directive_t;

/*
 * The stream encoding.
 *
 * An application may explicitly specify the encoding in the input stream or
 * let the parser to detect the input stream encoding from a BOM mark.  If the
 * stream does not start with a BOM mark, UTF-8 is assumed.
 *
 * An application may specify the encoding of the output stream or let the
 * emitter to choose a suitable encoding, in which case, UTF-8 is used.
 */

typedef enum yaml_encoding_e {
    /* The default/autodetected encoding. */
    YAML_ANY_ENCODING,
    /* The UTF-8 encoding. */
    YAML_UTF8_ENCODING,
    /* The UTF-16-LE encoding. */
    YAML_UTF16LE_ENCODING,
    /* The UTF-16-BE encoding. */
    YAML_UTF16BE_ENCODING
} yaml_encoding_t;

/*
 * Line break types.
 *
 * An application may specify the line break type the emitter should use or
 * leave it to the emitter discretion.  In the latter case, LN (Unix style) is
 * used.
 */

typedef enum yaml_break_e {
    /* Let the parser choose the break type. */
    YAML_ANY_BREAK,
    /* Use CR for line breaks (Mac style). */
    YAML_CR_BREAK,
    /* Use LN for line breaks (Unix style). */
    YAML_LN_BREAK,
    /* Use CR LN for line breaks (DOS style). */
    YAML_CRLN_BREAK
} yaml_break_t;

/******************************************************************************
 * Node Styles
 ******************************************************************************/

/*
 * Scalar styles.
 *
 * There are two groups of scalar types in YAML: flow and block.  Flow scalars
 * are divided into three styles: plain, single-quoted, and double-quoted;
 * block scalars are divided into two styles: literal and folded.
 *
 * The parser reports the style in which a scalar node is represented, however
 * it is a purely presentation details that must not be used in interpreting
 * the node content.
 *
 * An application may suggest a preferred node style or leave it completely
 * to the emitter discretion.  Note that the emitter may ignore any stylistic
 * suggestions.
 */

typedef enum yaml_scalar_style_e {
    /* Let the emitter choose the style. */
    YAML_ANY_SCALAR_STYLE,

    /* The plain flow scalar style. */
    YAML_PLAIN_SCALAR_STYLE,

    /* The single-quoted flow scalar style. */
    YAML_SINGLE_QUOTED_SCALAR_STYLE,
    /* The double-quoted flow scalar style. */
    YAML_DOUBLE_QUOTED_SCALAR_STYLE,

    /* The literal block scalar style. */
    YAML_LITERAL_SCALAR_STYLE,
    /* The folded block scalar style. */
    YAML_FOLDED_SCALAR_STYLE
} yaml_scalar_style_t;

/*
 * Sequence styles.
 *
 * YAML supports two sequence styles: flow and block.
 *
 * The parser reports the style of a sequence node, but this information should
 * not be used in interpreting the sequence content.
 *
 * An application may suggest a preferred sequence style or leave it completely
 * to the emitter discretion.  Note that the emitter may ignore any stylistic
 * hints.
 */
typedef enum yaml_sequence_style_e {
    /* Let the emitter choose the style. */
    YAML_ANY_SEQUENCE_STYLE,

    /* The flow sequence style. */
    YAML_FLOW_SEQUENCE_STYLE
    /* The block sequence style. */
    YAML_BLOCK_SEQUENCE_STYLE,
} yaml_sequence_style_t;

/*
 * Mapping styles.
 *
 * YAML supports two mapping styles: flow and block.
 *
 * The parser reports the style of a mapping node, but this information should
 * not be used in interpreting the mapping content.
 *
 * An application may suggest a preferred mapping style or leave it completely
 * to the emitter discretion.  Note that the emitter may ignore any stylistic
 * hints.
 */

typedef enum yaml_mapping_style_e {
    /* Let the emitter choose the style. */
    YAML_ANY_MAPPING_STYLE,

    /* The block mapping style. */
    YAML_BLOCK_MAPPING_STYLE,
    /* The flow mapping style. */
    YAML_FLOW_MAPPING_STYLE
} yaml_mapping_style_t;

/******************************************************************************
 * Tokens
 ******************************************************************************/

/*
 * Token types.
 *
 * The LibYAML scanner generates the following types of tokens:
 *
 * - STREAM-START: indicates the beginning of the stream.
 *
 * - STREAM-END: indicates the end of the stream.
 *
 * - VERSION-DIRECTIVE: describes the `%YAML` directive.
 *
 * - TAG-DIRECTIVE: describes the `%TAG` directive.
 *
 * - DOCUMENT-START: the indicator `---`.
 *
 * - DOCUMENT-END: the indicator `...`.
 *
 * - BLOCK-SEQUENCE-START: indentation increase indicating the beginning of a
 *   block sequence node.
 *
 * - BLOCK-MAPPING-START: indentation increase indicating the beginning of a
 *   block mapping node.
 *
 * - BLOCK-END: indentation decrease indicating the end of a block collection
 *   node.
 *
 * - FLOW-SEQUENCE-START: the indicator `[`.
 *
 * - FLOW-SEQUENCE-END: the indicator `]`.
 *
 * - FLOW-MAPPING-START: the indicator `{`.
 *
 * - FLOW-MAPPING-END: the indicator `}`.
 *
 * - BLOCK-ENTRY: the beginning of an item of a block sequence.
 *
 * - FLOW-ENTRY: the beginning of an item of a flow sequence.
 *
 * - KEY: the beginning of a simple key, or the indicator `?`.
 *
 * - VALUE: the indicator `:`.
 *
 * - ALIAS: an alias of a node.
 *
 * - ANCHOR: a node anchor.
 *
 * - TAG: a node tag.
 *
 * - SCALAR: the content of a scalar node.
 */

typedef enum yaml_token_type_e {
    /* An empty unitialized token. */
    YAML_NO_TOKEN,

    /* A STREAM-START token. */
    YAML_STREAM_START_TOKEN,
    /* A STREAM-END token. */
    YAML_STREAM_END_TOKEN,

    /* A VERSION-DIRECTIVE token. */
    YAML_VERSION_DIRECTIVE_TOKEN,
    /* A TAG-DIRECTIVE token. */
    YAML_TAG_DIRECTIVE_TOKEN,
    /* A DOCUMENT-START token. */
    YAML_DOCUMENT_START_TOKEN,
    /* A DOCUMENT-END token. */
    YAML_DOCUMENT_END_TOKEN,

    /* A BLOCK-SEQUENCE-START token. */
    YAML_BLOCK_SEQUENCE_START_TOKEN,
    /* A BLOCK-SEQUENCE-END token. */
    YAML_BLOCK_MAPPING_START_TOKEN,
    /* A BLOCK-END token. */
    YAML_BLOCK_END_TOKEN,

    /* A FLOW-SEQUENCE-START token. */
    YAML_FLOW_SEQUENCE_START_TOKEN,
    /* A FLOW-SEQUENCE-END token. */
    YAML_FLOW_SEQUENCE_END_TOKEN,
    /* A FLOW-MAPPING-START token. */
    YAML_FLOW_MAPPING_START_TOKEN,
    /* A FLOW-MAPPING-END token. */
    YAML_FLOW_MAPPING_END_TOKEN,

    /* A BLOCK-ENTRY token. */
    YAML_BLOCK_ENTRY_TOKEN,
    /* A FLOW-ENTRY token. */
    YAML_FLOW_ENTRY_TOKEN,
    /* A KEY token. */
    YAML_KEY_TOKEN,
    /* A VALUE token. */
    YAML_VALUE_TOKEN,

    /* An ALIAS token. */
    YAML_ALIAS_TOKEN,
    /* An ANCHOR token. */
    YAML_ANCHOR_TOKEN,
    /* A TAG token. */
    YAML_TAG_TOKEN,
    /* A SCALAR token. */
    YAML_SCALAR_TOKEN
} yaml_token_type_t;

/*
 * The token object.
 *
 * Typically the token API is too low-level to be used directly by
 * applications.  A possible user of the token API is a syntax highlighting
 * application.
 */

typedef struct yaml_token_s {

    /* The token type. */
    yaml_token_type_t type;

    /* The token data. */
    union {

        /* Extra data associated with a STREAM-START token. */
        struct {
            /* The stream encoding. */
            yaml_encoding_t encoding;
        } stream_start;

        /* Extra data associated with a VERSION-DIRECTIVE token. */
        struct {
            /* The major version number. */
            int major;
            /* The minor version number. */
            int minor;
        } version_directive;

        /* Extra data associated with a TAG-DIRECTIVE token. */
        struct {
            /* The tag handle. */
            yaml_char_t *handle;
            /* The tag prefix. */
            yaml_char_t *prefix;
        } tag_directive;

        /* Extra data associated with an ALIAS token. */
        struct {
            /* The alias value. */
            yaml_char_t *value;
        } alias;

        /* Extra data associated with an ANCHOR token. */
        struct {
            /* The anchor value. */
            yaml_char_t *value;
        } anchor;

        /* Extra data associated with a TAG token. */
        struct {
            /* The tag handle. */
            yaml_char_t *handle;
            /* The tag suffix. */
            yaml_char_t *suffix;
        } tag;

        /* Extra data associated with a SCALAR token. */
        struct {
            /* The scalar value. */
            yaml_char_t *value;
            /* The length of the scalar value. */
            size_t length;
            /* The scalar style. */
            yaml_scalar_style_t style;
        } scalar;

    } data;

    /* The beginning of the token. */
    yaml_mark_t start_mark;
    /* The end of the token. */
    yaml_mark_t end_mark;

} yaml_token_t;

/*
 * Allocate a new empty token object.
 *
 * A token object allocated using this function must be deleted with
 * `yaml_token_delete()`.
 *
 * Returns: a new empty token object or `NULL` on error.  The function may fail
 * if it cannot allocate memory for a new token object.
 */

YAML_DECLARE(yaml_token_t *)
yaml_token_new(void);

/*
 * Deallocate a token object and free the associated data.
 *
 * A token object must be previously allocated with `yaml_token_new()`.
 *
 * Arguments:
 *
 * - `token`: a token object to be deallocated.
 */

YAML_DECLARE(void)
yaml_token_delete(yaml_token_t *token);

/*
 * Duplicate a token object.
 *
 * This function creates a deep copy of an existing token object.  It accepts
 * two token objects: an empty token and a model token.  The latter is supposed
 * to be initialized with `yaml_parser_parse_token()`.  The function assigns
 * the type of the model to the empty token as well as duplicates and copies
 * the internal state associated with the model token.
 *
 * Arguments:
 *
 * - `token`: an empty token object.
 *
 * - `model`: a token to be copied.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the state of the model token.  In that case,
 * the token remains empty.
 */

YAML_DECLARE(int)
yaml_token_duplicate(yaml_token_t *token, const yaml_token_t *model);

/*
 * Clear the token state.
 *
 * This function clears the type and the internal state of a token object
 * freeing any associated data.  After applying this function to a token, it
 * becomes empty.  It is supposed that the token was previously initialized
 * using `yaml_parser_parse_token()` or `yaml_token_duplicate()`.
 *
 * Arguments:
 *
 * - `token`: a token object.
 */

YAML_DECLARE(void)
yaml_token_clear(yaml_token_t *token);

/******************************************************************************
 * Events
 ******************************************************************************/

/*
 * Event types.
 *
 * The LibYAML parser generates, while the LibYAML emitter accepts, YAML events
 * of the following types:
 *
 * - STREAM-START: indicates the beginning of the stream.
 *
 * - STREAM-END: indicates the end of the stream.
 *
 * - DOCUMENT-START: indicates the beginning of the document.
 *
 * - DOCUMENT-END: indicates the end of the document.
 *
 * - ALIAS: an alias to an already produced node.
 *
 * - SCALAR: a scalar node.
 *
 * - SEQUENCE-START: indicates the beginning of a sequence node.
 *
 * - SEQUENCE-END: indicates the end of a sequence node.
 *
 * - MAPPING-START: indicates the beginning of a mapping node.
 *
 * - MAPPING-END: indicates the end of a mapping node.
 *
 * A valid sequence of events obeys the grammar:
 *
 *      stream ::= STREAM-START document* STREAM-END
 *      document ::= DOCUMENT-START node DOCUMENT-END
 *      node ::= ALIAS | SCALAR | sequence | mapping
 *      sequence ::= SEQUENCE-START node* SEQUENCE-END
 *      mapping ::= MAPPING-START (node node)* MAPPING-END
 */

typedef enum yaml_event_type_e {
    /* An empty unitialized event. */
    YAML_NO_EVENT,

    /* A STREAM-START event. */
    YAML_STREAM_START_EVENT,
    /* A STREAM-END event. */
    YAML_STREAM_END_EVENT,

    /* A DOCUMENT-START event. */
    YAML_DOCUMENT_START_EVENT,
    /* A DOCUMENT-END event. */
    YAML_DOCUMENT_END_EVENT,

    /* An ALIAS event. */
    YAML_ALIAS_EVENT,
    /* A SCALAR event. */
    YAML_SCALAR_EVENT,

    /* A SEQUENCE-START event. */
    YAML_SEQUENCE_START_EVENT,
    /* A SEQUENCE-END event. */
    YAML_SEQUENCE_END_EVENT,

    /* A MAPPING-START event. */
    YAML_MAPPING_START_EVENT,
    /* A MAPPING-END event. */
    YAML_MAPPING_END_EVENT
} yaml_event_type_t;

/*
 * The event object.
 *
 * The event-level API of LibYAML should be used for streaming applications.
 */

typedef struct yaml_event_s {

    /* The event type. */
    yaml_event_type_t type;

    /* The event data. */
    union {
        
        /* The stream parameters (for `YAML_STREAM_START_EVENT`). */
        struct {
            /* The document encoding. */
            yaml_encoding_t encoding;
        } stream_start;

        /* The document parameters (for `YAML_DOCUMENT_START_EVENT`). */
        struct {
            /* The version directive or `NULL` if not present. */
            yaml_version_directive_t *version_directive;

            /* The list of tag directives. */
            struct {
                /* The beginning of the list or `NULL`. */
                yaml_tag_directive_t *list;
                /* The length of the list. */
                size_t length;
                /* The capacity of the list (used internally). */
                size_t capacity;
            } tag_directives;

            /* Set if the document indicator is omitted. */
            int is_implicit;
        } document_start;

        /* The document end parameters (for `YAML_DOCUMENT_END_EVENT`). */
        struct {
            /* Set if the document end indicator is omitted. */
            int is_implicit;
        } document_end;

        /* The alias parameters (for `YAML_ALIAS_EVENT`). */
        struct {
            /* The anchor. */
            yaml_char_t *anchor;
        } alias;

        /* The scalar parameters (for `YAML_SCALAR_EVENT`). */
        struct {
            /* The node anchor or `NULL`. */
            yaml_char_t *anchor;
            /* The node tag or `NULL`. */
            yaml_char_t *tag;
            /* The scalar value. */
            yaml_char_t *value;
            /* The length of the scalar value (in bytes). */
            size_t length;
            /* Set if the tag is optional for the plain style. */
            int is_plain_nonspecific;
            /* Set if the tag is optional for any non-plain style. */
            int is_quoted_nonspecific;
            /* The scalar style. */
            yaml_scalar_style_t style;
        } scalar;

        /* The sequence parameters (for `YAML_SEQUENCE_START_EVENT`). */
        struct {
            /* The node anchor or `NULL`. */
            yaml_char_t *anchor;
            /* The node tag or `NULL`. */
            yaml_char_t *tag;
            /* Set if the tag is optional. */
            int is_nonspecific;
            /* The sequence style. */
            yaml_sequence_style_t style;
        } sequence_start;

        /* The mapping parameters (for `YAML_MAPPING_START_EVENT`). */
        struct {
            /* The node anchor or `NULL`. */
            yaml_char_t *anchor;
            /* The node tag or `NULL`. */
            yaml_char_t *tag;
            /* Set if the tag is optional. */
            int is_nonspecific;
            /* The mapping style. */
            yaml_mapping_style_t style;
        } mapping_start;

    } data;

    /* The beginning of the event. */
    yaml_mark_t start_mark;
    /* The end of the event. */
    yaml_mark_t end_mark;

} yaml_event_t;

/*
 * Allocate a new empty event object.
 *
 * An event object allocated using this function must be deleted with
 * `yaml_event_delete()`.
 *
 * Returns: a new empty event object or `NULL` on error.  The function may fail
 * if it cannot allocate memory for a new event object.
 */

YAML_DECLARE(yaml_event_t *)
yaml_event_new(void);

/*
 * Deallocate an event object and free the associated data.
 *
 * An event object must be previously allocated with `yaml_event_new()`.
 *
 * Arguments:
 *
 * - `event`: an event object to be deallocated.
 */

YAML_DECLARE(void)
yaml_event_delete(yaml_event_t *event);

/*
 * Duplicate an event object.
 *
 * This function creates a deep copy of an existing event object.  It accepts
 * two objects: an empty event and a model event.  The model event is supposed
 * to be initialized either with `yaml_parser_parse_event()` or using one of
 * the functions `yaml_event_create_*()`.  The function assigns the type of the
 * model to the empty event and copies the internal state associated with the
 * model event.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `model`: an event to be copied.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the state of the model event.  In that case,
 * the event remains empty.
 */

YAML_DECLARE(int)
yaml_event_duplicate(yaml_event_t *event, const yaml_event_t *model);

/*
 * Clear the event state.
 *
 * This function clears the type and the internal state of an event object
 * freeing any associated data.  After applying this function to an event, it
 * becomes empty.  It is supposed that the event was previously initialized
 * using `yaml_parser_parse_event()` or `yaml_event_duplicate()`.  Note that
 * the function `yaml_emitter_emit_event()` also clears the given event.
 *
 * Arguments:
 *
 * - `event`: an event object.
 */

YAML_DECLARE(void)
yaml_event_clear(yaml_event_t *event);

/*
 * Create a STREAM-START event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `encoding`: the stream encoding.
 *
 * Returns: `1`.  The function never fails.
 */

YAML_DECLARE(int)
yaml_event_create_stream_start(yaml_event_t *event,
        yaml_encoding_t encoding);

/*
 * Create a STREAM-END event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * Returns: `1`.  The function never fails.
 */

YAML_DECLARE(int)
yaml_event_create_stream_end(yaml_event_t *event);

/*
 * Create a DOCUMENT-START event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `version_directive`: a structure specifying the content of the `%YAML`
 *   directive or `NULL` if the directive could be omitted.  Note that LibYAML
 *   supports YAML 1.1 only.  The function does not check if the supplied
 *   version equals to 1.1, but the emitter will fail to emit the event if it
 *   is not so.
 *
 * - `tag_directives_list`: a pointer to a list specifying the content of the
 *   `%TAG` directives associated with the document or `NULL` if the document
 *   does not contain `%TAG` directives.  The content of a tag directive is a
 *   pair (handle, prefix) of non-empty NUL-terminated UTF-8 strings.  The tag
 *   handle is one of `!`, `!!` or a sequence of alphanumeric characters, `_`
 *   and `-` surrounded by `!`.  The tag prefix is a prefix of any valid tag,
 *   that is, it is a non-empty prefix of either a global tag (a valid URI) or
 *   a local tag (an arbitrary string starting with `!`).  The function does
 *   not check if the given directive values satisfy these requirements, but
 *   the emitter will fail to emit the event if they are not met.
 *
 * - `tag_directives_length`: the length of `tag_directives_list`; `0` if
 *   `tag_directives_list` is `NULL`.
 *
 * - `is_implicit`: `1` if the document indicator `---` is omitted, `0`
 *   otherwise.  Note that this attribute is only a stylistic hint and could be
 *   ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the event parameters.  In this case, the
 * event remains empty.
 */

YAML_DECLARE(int)
yaml_event_create_document_start(yaml_event_t *event,
        const yaml_version_directive_t *version_directive,
        const yaml_tag_directive_t *tag_directives_list,
        size_t tag_directives_length,
        int is_implicit);

/*
 * Create a DOCUMENT-END event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `is_implicit`: `1` if the document end indicator `...` is omitted, `0`
 *   otherwise.  Note that this attribute is only a stylistic hint and could be
 *   ignored by the emitter.
 *
 * Returns: `1`.  The function never fails.
 */

YAML_DECLARE(int)
yaml_event_create_document_end(yaml_event_t *event, int is_implicit);

/*
 * Create an ANCHOR event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `anchor`: the anchor value.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.  The function does not check if this requirement is satisfied, but if
 *   it is not so, the emitter will fail to emit the generated event.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating `anchor`.  In this case, the event remains
 * empty.
 */

YAML_DECLARE(int)
yaml_event_create_alias(yaml_event_t *event, const yaml_char_t *anchor);

/*
 * Create a SCALAR event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `anchor`: the anchor value or `NULL`.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.
 *
 * - `tag`: the tag value or `NULL`.  The tag should be a non-empty UTF-8
 *   NUL-terminated string.  The tag could be global (a valid URI) or local (an
 *   arbitrary string starting with `!`).  If `NULL` is provided, at least one
 *   of the flags `is_plain_nonspecific` and `is_quoted_nonspecific` must be
 *   set.  The function does not check whether these requirements are
 *   satisfied, but the emitter will fail to emit the event if it is not so.
 *
 * - `value`: the value of the scalar node.  The value should be a UTF-8
 *   string.  It could contain any valid UTF-8 character including NUL.  The
 *   function does not check if `value` is a valid UTF-8 string, but the
 *   emitter will fail to emit the event if it is not so.
 *
 * - `length`: the length of the scalar value (in bytes) or `-1`.  If `length`
 *   is set to `-1`, the `value` is assumed to be NUL-terminated.
 *
 * - `is_plain_nonspecific`: `1` if the node tag could be omitted while
 *   emitting the node provided that the the plain style is used for
 *   representing the node value; `0` otherwise.  That this flag is set assumes
 *   that the tag could be correctly determined by the parser using the node
 *   position and content.
 *
 * - `is_quoted_nonspecific`: `1` if the node tag could be omitted while
 *   emitting the node provided that the node value is represented using any
 *   non-plain style; `0` otherwise.  That this flag is set assumes that the
 *   tag could be correctly determined by the parser using the node position
 *   and content.
 *
 * - `style`: the node style.  Note that this attribute only serves as a hint
 *   and may be ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the given string buffers.  In this case, the
 * event remains empty.
 */

YAML_DECLARE(int)
yaml_event_create_scalar(yaml_event_t *event,
        const yaml_char_t *anchor, const yaml_char_t *tag,
        const yaml_char_t *value, int length,
        int is_plain_nonspecific, int is_quoted_nonspecific,
        yaml_scalar_style_t style);

/*
 * Create a SEQUENCE-START event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `anchor`: the anchor value or `NULL`.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.  The function does not check if this requirement is satisfied, but if
 *   it is not so, the emitter will fail to emit the generated event.
 *
 * - `tag`: the tag value or `NULL`.  The tag should be a non-empty UTF-8
 *   NUL-terminated string.  The tag could be global (a valid URI) or local (an
 *   arbitrary string starting with `!`).  If `NULL` is provided, the flag
 *   `is_nonspecific` must be set.  The function does not check whether these
 *   requirements are satisfied, but if it is not so, the emitter will fail to
 *   emit the generated event.
 *
 * - `is_nonspecific`: `1` if the node tag could be omitted while
 *   emitting the node; `0` otherwise.  This flag should only be set if the
 *   node tag could be correctly determined by the parser using the node
 *   position in the document graph.
 *
 * - `style`: the node style.  Note that this attribute only serves as a hint
 *   and may be ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the given string buffers.  In this case, the
 * event remains empty.
 */

YAML_DECLARE(int)
yaml_event_create_sequence_start(yaml_event_t *event,
        const yaml_char_t *anchor, const yaml_char_t *tag,
        int is_nonspecific, yaml_sequence_style_t style);

/*
 * Create a SEQUENCE-END event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * Returns: `1`.  This function never fails.
 */

YAML_DECLARE(int)
yaml_event_create_sequence_end(yaml_event_t *event);

/*
 * Create a MAPPING-START event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * - `anchor`: the anchor value or `NULL`.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.  The function does not check if this requirement is satisfied, but if
 *   it is not so, the emitter will fail to emit the generated event.
 *
 * - `tag`: the tag value or `NULL`.  The tag should be a non-empty UTF-8
 *   NUL-terminated string.  The tag could be global (a valid URI) or local (an
 *   arbitrary string starting with `!`).  If `NULL` is provided, the flag
 *   `is_nonspecific` must be set.  The function does not check whether these
 *   requirements are satisfied, but if it is not so, the emitter will fail to
 *   emit the generated event.
 *
 * - `is_nonspecific`: `1` if the node tag could be omitted while
 *   emitting the node; `0` otherwise.  This flag should only be set if the
 *   node tag could be correctly determined by the parser using the node
 *   position in the document graph.
 *
 * - `style`: the node style.  Note that this attribute only serves as a hint
 *   and may be ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the given string buffers.  In this case, the
 * event remains empty.
 */

YAML_DECLARE(int)
yaml_event_create_mapping_start(yaml_event_t *event,
        const yaml_char_t *anchor, const yaml_char_t *tag,
        int is_nonspecific, yaml_mapping_style_t style);

/*
 * Create a MAPPING-END event.
 *
 * This function initializes an empty event object allocated with
 * `yaml_event_new()`.  The initialized event could be fed to
 * `yaml_emitter_emit_event()`.
 *
 * Arguments:
 *
 * - `event`: an empty event object.
 *
 * Returns: `1`.  This function never fails.
 */

YAML_DECLARE(int)
yaml_event_create_mapping_end(yaml_event_t *event);

/******************************************************************************
 * Documents and Nodes
 ******************************************************************************/

/*
 * Well-known scalar tags.
 */

#define YAML_NULL_TAG       ((const yaml_char_t *) "tag:yaml.org,2002:null")
#define YAML_BOOL_TAG       ((const yaml_char_t *) "tag:yaml.org,2002:bool")
#define YAML_STR_TAG        ((const yaml_char_t *) "tag:yaml.org,2002:str")
#define YAML_INT_TAG        ((const yaml_char_t *) "tag:yaml.org,2002:int")
#define YAML_FLOAT_TAG      ((const yaml_char_t *) "tag:yaml.org,2002:float")

/*
 * Basic collection tags.
 */

#define YAML_SEQ_TAG        ((const yaml_char_t *) "tag:yaml.org,2002:seq")
#define YAML_MAP_TAG        ((const yaml_char_t *) "tag:yaml.org,2002:map")

/*
 * The default tags for nodes lacking an explicit tag.
 */

#define YAML_DEFAULT_SCALAR_TAG     YAML_STR_TAG
#define YAML_DEFAULT_SEQUENCE_TAG   YAML_SEQ_TAG
#define YAML_DEFAULT_MAPPING_TAG    YAML_MAP_TAG

/*
 * Document types.
 *
 * There are no different document types in LibYAML: the document type field is
 * only used to distinguish between newly allocated documents and those that
 * are initialized with `yaml_parser_parse_document()` or
 * `yaml_document_create()`.
 */

typedef enum yaml_document_type_e {
    /* An empty uninitialized document. */
    YAML_NO_DOCUMENT.

    /* A YAML document. */
    YAML_DOCUMENT
} yaml_document_type_t;

/*
 * Node types.
 *
 * YAML recognizes three kinds of nodes: scalar, sequence and mapping.
 */

typedef enum yaml_node_type_e {
    /* An empty node. */
    YAML_NO_NODE,

    /* A scalar node. */
    YAML_SCALAR_NODE,
    /* A sequence node. */
    YAML_SEQUENCE_NODE,
    /* A mapping node. */
    YAML_MAPPING_NODE
} yaml_node_type_t;

/*
 * Arc types.
 *
 * Arcs are used to specify the path from the root node to a given node in the
 * document graph.  There are three kinds of arcs: an item in a sequence node,
 * a key in a mapping node, and a value in a mapping node.
 */

typedef enum yaml_arc_type_e {
    /* An empty arc. */
    YAML_NO_ARC,

    /* An item of a sequence. */
    YAML_SEQUENCE_ITEM_ARC,
    /* A key of a mapping. */
    YAML_MAPPING_KEY_ARC,
    /* A value of a mapping. */
    YAML_MAPPING_VALUE_ARC
} yaml_arc_type_t;

/*
 * An element of a sequence node.
 */

typedef int yaml_node_item_t;

/*
 * An element of a mapping node.
 */

typedef struct yaml_node_pair_s {
    /* A key in a mapping. */
    int key;
    /* A value in a mapping. */
    int value;
} yaml_node_pair_t;

/*
 * A path element.
 *
 * An arc is an element of a path from the root node to some other node in a
 * YAML document.  An arc consists of a collection node and information on how
 * it is connected to the next node in the path.  If the arc type is a sequence
 * item, then the collection node is a sequence and the arc refers to the index
 * in this sequence.  If the arc type is a mapping key, then the collection
 * node is a mapping.  If the arc type is a mapping value, then the collection
 * node is a mapping and the arc refers to the key associated to the value.
 */

typedef struct yaml_arc_s {

    /* The arc type. */
    yaml_arc_type_t type;

    /* The tag of the collection node. */
    yaml_char_t *tag;

    /* The connection information. */
    union {

        /* The sequence item data (for `YAML_SEQUENCE_ITEM_ARC`). */
        struct {
            /* The index of the item in the sequence (starting from `0`). */
            int index;
        } item;

        /* The mapping value data (for `YAML_MAPPING_VALUE_ARC`). */
        struct {
            /* The key associated with the value. */
            struct {
                /* The key node type. */
                yaml_node_type_t type;
                /* The key node tag. */
                yaml_char_t *tag;
                /* The key node details. */
                union {
                    /* The scalar data (for a scalar key). */
                    struct {
                        /* The scalar value. */
                        yaml_char_t *value;
                        /* The scalar length. */
                        size_t length;
                    } scalar;
                } data;
            } key;
        } value;
    } data;
} yaml_arc_t;

/*
 * The node object.
 *
 * A node object represents a node in a YAML document graph.  Node objects are
 * created using the family of functions `yaml_document_add_*()`.  Links
 * between nodes are created using the functions `yaml_document_append_*()`. A
 * node object is destroyed when the document containing it is destroyed.
 */

struct yaml_node_s {

    /* The node type. */
    yaml_node_type_t type;

    /* The node anchor or `NULL`. */
    yaml_char_t *anchor;
    /* The node tag. */
    yaml_char_t *tag;

    /* The node data. */
    union {
        
        /* The scalar parameters (for `YAML_SCALAR_NODE`). */
        struct {
            /* The scalar value. */
            yaml_char_t *value;
            /* The length of the scalar value. */
            size_t length;
            /* The scalar style. */
            yaml_scalar_style_t style;
        } scalar;

        /* The sequence parameters (for `YAML_SEQUENCE_NODE`). */
        struct {
            /* The list of sequence items. */
            struct {
                /* The pointer to the beginning of the list. */
                yaml_node_item_t *list;
                /* The length of the list. */
                size_t length;
                /* The capacity of the list (used internally). */
                size_t capacity;
            } items;
            /* The sequence style. */
            yaml_sequence_style_t style;
        } sequence;

        /* The mapping parameters (for `YAML_MAPPING_NODE`). */
        struct {
            /* The list of mapping pairs (key, value). */
            struct {
                /** The pointer to the beginning of the list. */
                yaml_node_pair_t *list;
                /* The length of the list. */
                size_t length;
                /* The capacity of the list (used internally). */
                size_t capacity;
            } pairs;
            /* The mapping style. */
            yaml_mapping_style_t style;
        } mapping;

    } data;

    /* The beginning of the node. */
    yaml_mark_t start_mark;
    /* The end of the node. */
    yaml_mark_t end_mark;

};

/*
 * The incomplete node object.
 *
 * This structure provides the information about a node that a tag resolver
 * could use to determine the specific node tag.  This information includes
 * the path from the root node and the node content for scalar nodes.
 */

typedef struct yaml_incomplete_node_s {

    /* The node type. */
    yaml_node_type_t type;

    /* The path to the new node. */
    struct {
        /* The pointer to the beginning of the list. */
        yaml_arc_t *list;
        /* The length of the list. */
        size_t length;
        /* The capacity of the list (used internally). */
        size_t capacity;
    } path;

    /* The node data. */
    union {

        /* The scalar parameters (for `YAML_SCALAR_NODE`). */
        struct {
            /* The scalar value. */
            yaml_char_t *value;
            /* The length of the scalar value. */
            size_t length;
            /* Set if the scalar is plain. */
            int is_plain;
        } scalar;

    } data;

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

} yaml_incomplete_node_t;

/*
 * The document object.
 *
 * A document object represents the main structure of the YAML object model:
 * the document graph consisting of nodes of three kinds: scalars, sequences,
 * and mappings with the selected root node.
 *
 * An empty document object is allocated using the function
 * `yaml_document_new()`.  Then the function `yaml_parser_parse_document()`
 * could be used to load a YAML document from the input stream.  Alternatively,
 * a document could be created with `yaml_document_create()` and its content
 * could be specified using the families of functions `yaml_document_add_*()`
 * and `yaml_document_append_*()`.  A document with all associated nodes could
 * be destroyed using the function `yaml_document_delete()`.
 */

typedef struct yaml_document_s {

    /* The document type. */
    yaml_document_type_t type;

    /* The document nodes (for internal use only). */
    struct {
        /* The pointer to the beginning of the list. */
        yaml_node_t *list;
        /* The length of the list. */
        size_t length;
        /* The capacity of the list. */
        size_t capacity;
    } nodes;

    /* The version directive or `NULL`. */
    yaml_version_directive_t *version_directive;

    /* The list of tag directives. */
    struct {
        /* The pointer to the beginning of the list or `NULL`. */
        yaml_tag_directive_t *list;
        /** The length of the list. */
        size_t length;
        /** The capacity of the list (used internally). */
        size_t capacity;
    } tag_directives;

    /** Set if the document start indicator is implicit. */
    int is_start_implicit;
    /** Set if the document end indicator is implicit. */
    int is_end_implicit;

    /** The beginning of the document. */
    yaml_mark_t start_mark;
    /** The end of the document. */
    yaml_mark_t end_mark;

} yaml_document_t;

/*
 * Allocate a new empty document object.
 *
 * A document object allocated using this function must be deleted with
 * `yaml_document_delete()`.
 *
 * Returns: a new empty document object or `NULL` on error.  The function may
 * fail if it cannot allocate memory for a new object.
 */

YAML_DECLARE(yaml_document_t *)
yaml_document_new(void);

/*
 * Deallocate a document object and free the associated data.
 *
 * A document object must be previously allocated with `yaml_document_new()`.
 *
 * Arguments:
 *
 * - `document`: a document object to be deallocated.
 */

YAML_DECLARE(void)
yaml_document_delete(yaml_document_t *document);

/*
 * Duplicate a document object.
 *
 * This function creates a complete copy of an existing document and all the
 * nodes it contains.  The function accepts two objects: an empty document and
 * a model document.  The model is supposed to be initialized either with
 * `yaml_parser_parse_document()` or constructed manually.  The functions
 * duplicate the content of the document and its nodes and assigns it to the
 * empty document.
 *
 * Arguments:
 *
 * - `document`: an empty document object.
 *
 * - `model`: a document to be copied.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the state of the model.  In that case, the
 * document remains empty.
 */

YAML_DECLARE(int)
yaml_document_duplicate(yaml_document_t *document, yaml_document_t *model);

/*
 * Clear the document.
 *
 * This function clears the type of the document and destroys all the document
 * nodes.  After applying this function to a document, it becomes empty.  It is
 * supposed that the document was previously initialized using
 * `yaml_parser_parse_document()` or created manually.  Note that the function
 * `yaml_emitter_emit_document()` also clears the given document.
 *
 * Arguments:
 *
 * - `document`: a document object.
 */

YAML_DECLARE(void)
yaml_document_clear(yaml_document_t *document);

/*
 * Create a YAML document.
 *
 * This function initializes an empty document object allocated with
 * `yaml_document_new()`.  Further, nodes could be added to the document using
 * the functions `yaml_document_add_*()` and `yaml_document_append_*()`.  The
 * initialized document could be fed to `yaml_emitter_emit_document()`.
 *
 * Arguments:
 *
 * - `document`: an empty document object.
 *
 * - `version_directive`: a structure specifying the content of the `%YAML`
 *   directive or `NULL` if the directive could be omitted.  Note that LibYAML
 *   supports YAML 1.1 only.  The constructor does not check if the supplied
 *   version equals to 1.1, but the emitter will fail to emit the document if
 *   it is not so.
 *
 * - `tag_directives_list`: a pointer to a list specifying the content of the
 *   `%TAG` directives associated with the document or `NULL` if the document
 *   does not contain `%TAG` directives.  The content of a tag directive is a
 *   pair (handle, prefix) of non-empty NUL-terminated UTF-8 strings.  The tag
 *   handle is one of `!`, `!!` or a sequence of alphanumeric characters, `_`
 *   and `-` surrounded by `!`.  The tag prefix is a prefix of any valid tag,
 *   that is, it is a non-empty prefix of either a global tag (a valid URI) or
 *   a local tag (an arbitrary string starting with `!`).  The constructor does
 *   not check if the given directive values satisfy these requirements, but
 *   the emitter will fail to emit the document if they are not met.
 *
 * - `tag_directives_length`: the length of `tag_directives_list`; `0` if
 *   `tag_directives_list` is `NULL`.
 *
 * - `is_start_implicit`: `1` if the document start indicator `---` is omitted;
 *   `0` otherwise.  Note that this attribute is only a stylistic hint and
 *   could be ignored by the emitter.
 *
 * - `is_end_implicit`: `1` if the document end indicator `...` is omitted; `0`
 *   otherwise.  Note that this attribute is only a stylistic hint and could be
 *   ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the document parameters.  In this case, the
 * document remains empty.
 */

YAML_DECLARE(int)
yaml_document_create(yaml_document_t *document,
        const yaml_version_directive_t *version_directive,
        const yaml_tag_directive_t *tag_directives_list,
        size_t tag_directives_length,
        int is_start_implicit, int is_end_implicit);

/**
 * Get a node of a YAML document.
 *
 * The root node of the document has the id `0`.
 *
 * The pointer returned by this function is valid until any of the functions
 * modifying the document is called.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id.  The node id starts from `0` and increases by `1`
 *   for each newly added node.  Specifying a negative `node_id` is possible,
 *   which is interpeted as the number (<total number of nodes> - `node_id`).
 *
 * Returns: a pointer to the node object or `NULL` if `node_id` is out of
 * range.
 */

YAML_DECLARE(yaml_node_t *)
yaml_document_get_node(yaml_document_t *document, int node_id);

/*
 * Create a SCALAR node and attach it to the document.
 *
 * Note that the first node attached to a document becomes the root node.
 * There must exist a path from the root node to any node added to the
 * document, otherwise the function `yaml_emitter_emit_document()` will fail.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `anchor`: the node anchor or `NULL`.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.  The function does not check whether this requirement is satisfied,
 *   but the emitter may fail to emit the node if it is not so.  This parameter
 *   is considered as a stylistic hint and could be ignored by the emitter.
 *   The emitter may automatically generate an anchor for a node that does not
 *   specify it or specifies a duplicate anchor.
 *
 * - `tag`: the node tag.  The tag should be a non-empty UTF-8 NUL-terminated
 *   string.  The tag could be global (a valid URI) or local (an arbitrary
 *   string starting with `!`).  The function does not check whether these
 *   requirements are satisfied, but the emitter will fail to emit the node if
 *   it is not so.
 *
 * - `value`: the scalar value.  The value should be a string containing any
 *   valid UTF-8 character including NUL.  The function does not check if
 *   `value` is a valid UTF-8 string, but the emitter will fail to emit the
 *   node if it is not so.
 *
 * - `length`: the length of the scalar value (in bytes) or `-1`.  If `length`
 *   is set to `-1`, the `value` is assumed to be NUL-terminated.
 *
 * - `style`: the node style.  Note that this attribute only serves as a hint
 *   and may be ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the given string buffers.  If the function
 * succeeds, the id of the added node is returned via the pointer `node_id`
 * if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_scalar(yaml_document_t *document, int *node_id,
        const yaml_char_t *anchor, const yaml_char_t *tag,
        const yaml_char_t *value, int length,
        yaml_scalar_style_t style);

/*
 * Create a SEQUENCE node and attach it to the document.
 *
 * Note that the first node attached to a document becomes the root node.
 * There must exist a path from the root node to any node added to the
 * document, otherwise the function `yaml_emitter_emit_document()` will fail.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `anchor`: the node anchor or `NULL`.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.  The function does not check whether this requirement is satisfied,
 *   but the emitter may fail to emit the node if it is not so.  This parameter
 *   is considered as a stylistic hint and could be ignored by the emitter.
 *   The emitter may automatically generate an anchor for a node that does not
 *   specify it or specifies a duplicate anchor.
 *
 * - `tag`: the node tag.  The tag should be a non-empty UTF-8 NUL-terminated
 *   string.  The tag could be global (a valid URI) or local (an arbitrary
 *   string starting with `!`).  The function does not check whether these
 *   requirements are satisfied, but the emitter will fail to emit the node if
 *   it is not so.
 *
 * - `style`: the node style.  Note that this attribute only serves as a hint
 *   and may be ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the given string buffers.  If the function
 * succeeds, the id of the added node is returned via the pointer `node_id`
 * if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_sequence(yaml_document_t *document, int *node_id,
        const yaml_char_t *anchor, const yaml_char_t *tag,
        yaml_sequence_style_t style);

/*
 * Create a MAPPING node and attach it to the document.
 *
 * Note that the first node attached to a document becomes the root node.
 * There must exist a path from the root node to any node added to the
 * document, otherwise the function `yaml_emitter_emit_document()` will fail.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `anchor`: the node anchor or `NULL`.  The anchor should be a non-empty
 *   NUL-terminated string containing only alphanumerical characters, `_`, and
 *   `-`.  The function does not check whether this requirement is satisfied,
 *   but the emitter may fail to emit the node if it is not so.  This parameter
 *   is considered as a stylistic hint and could be ignored by the emitter.
 *   The emitter may automatically generate an anchor for a node that does not
 *   specify it or specifies a duplicate anchor.
 *
 * - `tag`: the node tag.  The tag should be a non-empty UTF-8 NUL-terminated
 *   string.  The tag could be global (a valid URI) or local (an arbitrary
 *   string starting with `!`).  The function does not check whether these
 *   requirements are satisfied, but the emitter will fail to emit the node if
 *   it is not so.
 *
 * - `style`: the node style.  Note that this attribute only serves as a hint
 *   and may be ignored by the emitter.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for duplicating the given string buffers.  If the function
 * succeeds, the id of the added node is returned via the pointer `node_id`
 * if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_mapping(yaml_document_t *document, int *node_id,
        const yaml_char_t *anchor, const yaml_char_t *tag,
        yaml_mapping_style_t style);

/*
 * Get the value of a `!!null` SCALAR node.
 *
 * Use this function to ensure that the given node is a scalar, the node tag is
 * equal to `tag:yaml.org,2002:null` and the node value is a valid null value.
 * Given that the `!!null` tag admits only one valid value, the value is not
 * returned.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * Returns: `1` if the node is a valid `!!null` scalar, `0` otherwise.
 */

YAML_DECLARE(int)
yaml_document_get_null_node(yaml_document_t *document, int node_id);

/*
 * Get the value of a `!!bool` SCALAR node.
 *
 * Use this function to ensure that the given node is a scalar, the node tag is
 * `tag:yaml.org,2002:bool` and the node value is a valid boolean value.  The
 * function returns the true value as `1` and the false value as `0`.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * - `value`: a pointer to save the node value or `NULL`.
 *
 * Returns: `1` if the node is a valid `!!bool` scalar, `0` otherwise.  If the
 * function succeeds and `value` is not `NULL`, the node value is saved to
 * `value`.
 */

YAML_DECLARE(int)
yaml_document_get_bool_node(yaml_document_t *document, int node_id,
        int *value);

/*
 * Get the value of a `!!str` SCALAR node.
 *
 * Use this function to ensure that the given node is a scalar, the node tag is
 * `tag:yaml.org,2002:str` and the node value is a string that does not contain
 * the NUL character.  In this case, the function returns the node value.  The
 * produced value is valid until the document object is cleared or deleted.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * - `value`: a pointer to save the node value or `NULL`.
 *
 * Returns: `1` if the node is a valid `!!str` scalar, `0` otherwise.  If the
 * function succeeds and `value` is not `NULL`, the node value is saved to
 * `value`.
 */

YAML_DECLARE(int)
yaml_document_get_str_node(yaml_document_t *document, int node_id,
        char **value);

/*
 * Get the value of an `!!int` SCALAR node.
 *
 * Use this function to ensure that the given node is a scalar, the node tag is
 * `tag:yaml.org,2002:int` and the node value is a valid integer.  In this
 * case, the function parses the node value and returns an integer number.  The
 * function recognizes decimal, hexdecimal and octal numbers including negative
 * numbers.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * - `value`: a pointer to save the node value or `NULL`.
 *
 * Returns: `1` if the node is a valid `!!int` scalar, `0` otherwise.  If the
 * function succeeds and `value` is not `NULL`, the node value is saved to
 * `value`.
 */

YAML_DECLARE(int)
yaml_document_get_int_node(yaml_document_t *document, int node_id,
        int *value);

/*
 * Get the value of a `!!float` SCALAR node.
 *
 * Use this function to ensure that the given node is a scalar, the node tag is
 * `tag:yaml.org,2002:float` and the node value is a valid float value.  In
 * this case, the function parses the node value and returns a float number.
 * The function recognizes float values in exponential and fixed notation as
 * well as special values `.nan`, `.inf` and `-.inf`.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * - `value`: a pointer to save the node value or `NULL`.
 *
 * Returns: `1` if the node is a valid `!!float` scalar, `0` otherwise.  If the
 * function succeeds and `value` is not `NULL`, the node value is saved to
 * `value`.
 */

YAML_DECLARE(int)
yaml_document_get_float_node(yaml_document_t *document, int node_id,
        double *value);

/*
 * Get the value of a `!!seq` SEQUENCE node.
 *
 * Use this function to ensure that the given node is a sequence and the node
 * tag is `tag:yaml.org,2002:seq`.  In this case, the function returns the list
 * of nodes that belong to the sequence.  The produced list is valid until the
 * document object is modified.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * - `items`: a pointer to save the list of sequence items or `NULL`.
 *
 * - `length`: a pointer to save the length of the sequence or `NULL`.
 *   `length` must be equal to `NULL` if and only if `items` is also `NULL`.
 *
 * Returns: `1` if the node is a valid `!!seq` sequence, `0` otherwise.  If the
 * function succeeds and `items` is not `NULL`, the list of sequence items is
 * saved to `items` and the sequence length is saved to `length`.
 */

YAML_DECLARE(int)
yaml_document_get_seq_node(yaml_document_t *document, int node_id,
        yaml_node_item_t **items, size_t *length);

/*
 * Get the value of a `!!map` MAPPING node.
 *
 * Use this function to ensure that the given node is a mapping and the node
 * tag is `tag:yaml.org,2002:map`.  In this case, the function returns the list
 * of node pairs (key, value) that belong to the sequence.  The produced list
 * is valid until the document is modified.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: the node id; could be negative.
 *
 * - `pairs`: a pointer to save the list of mapping pairs or `NULL`.
 *
 * - `length`: a pointer to save the length of the mapping or `NULL`.
 *   `length` must be equal to `NULL` if and only if `pairs` is also `NULL`.
 *
 * Returns: `1` if the node is a valid `!!map` mapping, `0` otherwise.  If the
 * function succeeds and `pairs` is not `NULL`, the list of mapping pairs is
 * saved to `pairs` and the mapping length is saved to `length`.
 */

YAML_DECLARE(int)
yaml_document_get_map_node(yaml_document_t *document, int node_id,
        yaml_node_pair_t **pairs, size_t *length);

/*
 * Add a `!!null` SCALAR node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_scalar(document, node_id, NULL,
 *              YAML_NULL_TAG, "null", -1, YAML_ANY_SCALAR_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_null_node(yaml_document_t *document, int *node_id);

/*
 * Add a `!!bool` SCALAR node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_scalar(document, node_id, NULL,
 *              YAML_BOOL_TAG, (value ? "true" : "false"), -1,
 *              YAML_ANY_SCALAR_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `value`: a boolean value; any non-zero value is true, `0` is false.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_bool_node(yaml_document_t *document, int *node_id,
        int value);

/*
 * Add a `!!str` SCALAR node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_scalar(document, node_id, NULL,
 *              YAML_STR_TAG, (const yaml_char_t *) value, -1,
 *              YAML_ANY_SCALAR_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `value`: a NUL-terminated UTF-8 string.  The function does not check if
 *   `value` is a valid UTF-8 string, but if it is not so, the emitter will
 *   fail to emit the node.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_str_node(yaml_document_t *document, int *node_id,
        const char *value);

/*
 * Add an `!!int` SCALAR node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_scalar(document, node_id, NULL,
 *              YAML_INT_TAG, <string representation of the value>, -1,
 *              YAML_ANY_SCALAR_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `value`: an integer value.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_int_node(yaml_document_t *document, int *node_id,
        int value);

/*
 * Add a `!!float` SCALAR node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_scalar(document, node_id, NULL,
 *              YAML_FLOAT_TAG, <string representation of the value>, -1,
 *              YAML_ANY_SCALAR_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * - `value`: a float value.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_float_node(yaml_document_t *document, int *node_id,
        double value);

/*
 * Add a `!!seq` SEQUENCE node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_sequence(document, node_id, NULL,
 *              YAML_SEQ_TAG, YAML_ANY_SEQUENCE_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_seq_node(yaml_document_t *document, int *node_id);

/*
 * Add a `!!map` MAPPING node to the document.
 *
 * This function is a shorthand for the call:
 *
 *      yaml_document_add_mapping(document, node_id, NULL,
 *              YAML_MAP_TAG, YAML_ANY_MAPPING_STYLE)
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `node_id`: a pointer to save the id of the generated node or `NULL`.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for new buffers.  If the function succeeds, the id of the
 * added node is returned via the pointer `node_id` if it is not set to `NULL`.
 */

YAML_DECLARE(int)
yaml_document_add_map_node(yaml_document_t *document, int *node_id);

/*
 * Add an item to a SEQUENCE node.
 *
 * The order in which items are added to a sequence coincides with the order
 * they are emitted into the output stream.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `sequence_id`: the id of a sequence node; could be negative.  It is a
 *   fatal error if `sequence_id` does not refer to an existing sequence node.
 *
 * - `item_id`: the id of an item node; could be negative.  It is a fatal error
 *   if `item_id` does not refer to an existing node.  Note that it is possible
 *   for `item_id` to coincide with `sequence_id`, which means that the
 *   sequence recursively contains itself.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for internal buffers.
 */

YAML_DECLARE(int)
yaml_document_append_sequence_item(yaml_document_t *document,
        int sequence_id, int item_id);

/*
 * Add a pair of a key and a value to a MAPPING node.
 *
 * The order in which (key, value) pairs are added to a mapping coincides with
 * the order in which they are presented in the output stream.  Note that the
 * mapping key order is a presentation detail and should not used to convey any
 * information.  An ordered mapping could be represented as a sequence of
 * single-paired mappings.
 *
 * Arguments:
 *
 * - `document`: a document object.
 *
 * - `mapping_id`: the id of a mapping node; could be negative.  It is a
 *   fatal error if `mapping_id` does not refer to an existing mapping node.
 *
 * - `key_id`: the id of a key node; could be negative.  It is a fatal error
 *   if `key_id` does not refer to an existing node.
 *
 * - `value_id`: the id of a value node; could be negative.  It is a fatal
 *   error if `value_id` does not refer to an existing node.
 *
 * Returns: `1` on success, `0` on error.  The function may fail if it cannot
 * allocate memory for internal buffers.
 */

YAML_DECLARE(int)
yaml_document_append_mapping_pair(yaml_document_t *document,
        int mapping_id, int key_id, int value_id);

/******************************************************************************
 * Callback Definitions
 ******************************************************************************/

/*
 * The prototype of a read handler.
 *
 * The reader is called when the parser needs to read more bytes from the input
 * stream.  The reader is given a buffer to fill and should returns the number
 * of bytes read.  The reader should block if no data from the input stream is
 * available, but it should return immediately if it could produce least one
 * byte of the input stream.  If the reader reaches the stream end, it should
 * return immediately setting the number of bytes read to `0`.
 *
 * Arguments:
 *
 * - `data`: a pointer to an application data specified with
 *   `yaml_parser_set_reader()`.
 *
 * - `buffer`: a pointer to a buffer which should be filled with the bytes
 *   from the input stream.
 *
 * - `capacity`: the maximum capacity of the buffer.
 *
 * - `length`: a pointer to save the actual number of bytes read from the input
 *   stream; `length` equals `0` signifies that the reader reached the end of
 *   the stream.
 *
 * Return: on success, the reader should return `1`.  If the reader fails for
 * any reason, it should return `0`.  On the end of the input stream, the
 * reader should set `length` to `0` and return `1`.
 */

typedef int yaml_reader_t(void *data, unsigned char *buffer, size_t capacity,
        size_t *length);

/*
 * The prototype of a write handler.
 *
 * The writer is called when the emitter needs to flush the accumulated bytes
 * into the output stream.
 *
 * Arguments:
 *
 * - `data`: a pointer to an application data specified with
 *   `yaml_emitter_set_writer()`.
 *
 * - `buffer`: a pointer to a buffer with bytes to be written to the output
 *   stream.
 *
 * - `length`: the number of bytes to be written.
 *
 * Returns: on success, the writer should return `1`.  If the writer fails for
 * any reason, it should return `0`.
 */

typedef int yaml_writer_t(void *data, const unsigned char *buffer,
        size_t length);

/**
 * The prototype of a nonspecific tag resolver.
 *
 * The resolver is called when the parser encounters a node without an explicit
 * tag.  The resolver should determine the correct tag of the node from the
 * path to the node from the root node and, in case of the scalar node, the
 * node value.  The resolver is also called by the emitter to determine whether
 * the node tag could be omitted.
 *
 * Arguments:
 *
 * - `data`: a pointer to an application data specified with
 *   `yaml_parser_set_resolver()` or `yaml_emitter_set_resolver()`.
 *
 * - `node`: information about the new node.
 *
 * - `tag`: A pointer to save the guessed node tag.  The value returned by the
 *   resolved is immediately copied.
 *
 * Returns: on success, the resolver should return `1`.  If the resolver fails
 * for any reason, it should return `0`.
 */

typedef int yaml_resolver_t(void *data, yaml_incomplete_node_t *node,
        yaml_char_t **tag);

/******************************************************************************
 * Parser Definitions
 ******************************************************************************/

/*
 * An opaque definition of the parser object.
 *
 * A parser object is used to parse an input YAML stream producing a sequence
 * of YAML tokens, events or documents.
 */

typedef struct yaml_parser_s yaml_parser_t;

/*
 * Allocate a new parser object.
 *
 * An allocated parser object should be deleted with `yaml_parser_delete()`
 *
 * Returns: a new parser object or `NULL` on error.  The function may fail if
 * it cannot allocate memory for internal buffers.
 */

YAML_DECLARE(yaml_parser_t *)
yaml_parser_new(void);

/*
 * Deallocate a parser and free the internal parser data.
 * 
 * A parser object must be previously allocated with `yaml_parser_new()`.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 */

YAML_DECLARE(void)
yaml_parser_delete(yaml_parser_t *parser);

/*
 * Clear and reinitialize the internal state of the parser.
 *
 * This function could be used for cleaning up a parser object after an error
 * condition or after the end of the input stream is reached.  A cleaned parser
 * object may be reused to parse another YAML stream.  Note that all the parser
 * parameters including the read handler must be reset.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 */

YAML_DECLARE(void)
yaml_parser_clear(yaml_parser_t *parser);

/*
 * Get the parser error.
 *
 * Use this function to get a detailed error information after failure of one
 * of the following functions: `yaml_parser_parse_token()`,
 * `yaml_parser_parse_event()`, `yaml_parser_parse_document()`,
 * `yaml_parser_parse_single_document()`.
 *
 * The pointer returned by the function is only valid until the parser object
 * is not modified.  However the error object could be safely copied.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * Returns: a pointer to an error object.  The returned pointer is only valid
 * until the parser object is not modified or deleted.  However the error
 * object could be safely copied.
 */

YAML_DECLARE(yaml_error_t *)
yaml_parser_get_error(yaml_parser_t *parser);

/*
 * Set the parser to read the input stream from a character buffer.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `buffer`: a pointer to character buffer containing the input stream.  The
 *   buffer must be valid until the parser object is cleared or deleted.
 *
 * - `length`: the length of the buffer in bytes.
 */

YAML_DECLARE(void)
yaml_parser_set_string_reader(yaml_parser_t *parser,
        const unsigned char *buffer, size_t length);

/*
 * Set the parser to read the input stream from a file.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `file`: a pointer to an open file object.  The pointer must be valid until
 *   the parser object is cleared or deleted.
 */

YAML_DECLARE(void)
yaml_parser_set_file_reader(yaml_parser_t *parser, FILE *file);

/*
 * Set an input stream reader for a parser.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `reader`: a read handler.
 *
 * - `data`: application data for passing to the reader.
 */

YAML_DECLARE(void)
yaml_parser_set_reader(yaml_parser_t *parser,
        yaml_reader_t *reader, void *data);

/*
 * Set the standard nonspecific tag resolver for a parser.
 *
 * The standard resolver recognizes the following scalar tags: `!!null`,
 * `!!bool`, `!!str`, `!!int`, and `!!float`.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 */

YAML_DECLARE(void)
yaml_parser_set_standard_resolver(yaml_parser_t *parser);

/*
 * Set a nonspecific tag resolver for a parser.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `resolver`: a resolve handler.
 *
 * - `data`: application data for passing to the resolver.
 */

YAML_DECLARE(void)
yaml_parser_set_resolver(yaml_parser_t *parser,
        yaml_resolver_t *resolver, void *data);

/*
 * Set the input stream encoding.
 *
 * Typically the parser guesses the input stream encoding by the BOM mark.  If
 * the BOM mark is not present, the UTF-8 encoding is assumed.  An application
 * could override the detection mechanism by specifying the the encoding
 * explicitly.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `encoding`: the input stream encoding.
 */

YAML_DECLARE(void)
yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding);

/*
 * Parse the input stream and produce the next token.
 *
 * An application may call this function subsequently to produce a sequence of
 * tokens corresponding to the input stream.  The first token in the sequence
 * is STREAM-START and the last token is STREAM-END.  When the stream ends, the
 * parser produces empty tokens.
 *
 * An application must not alternate calls of the functions
 * `yaml_parser_parse_token()`, `yaml_parser_parse_event()`,
 * `yaml_parser_parse_document()` and `yaml_parser_parse_single_document()` on
 * the same parser object.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `token`: an empty token object to save the token produced by the parser.
 *   An application is responsible for clearing or deleting the produced token.
 *   If the parser fails or the stream end is reached, the object is kept
 *   empty.
 *
 * Returns: `1` on success, `0` on error.  If the function succeeds and the
 * stream end is not reached, the token data is saved to the given token
 * object.  If the function fails, the error details could be obtained with
 * `yaml_parser_get_error()`.  In case of error, the parser is non-functional
 * until it is cleared.
 */

YAML_DECLARE(int)
yaml_parser_parse_token(yaml_parser_t *parser, yaml_token_t *token);

/*
 * Parse the input stream and produce the next parsing event.
 *
 * An application may call this function subsequently to produce a sequence of
 * events corresponding to the input stream.  The produced events satisfy
 * the grammar:
 *
 *      stream ::= STREAM-START document* STREAM-END
 *      document ::= DOCUMENT-START node DOCUMENT-END
 *      node ::= ALIAS | SCALAR | sequence | mapping
 *      sequence ::= SEQUENCE-START node* SEQUENCE-END
 *      mapping ::= MAPPING-START (node node)* MAPPING-END
 *
 * When the stream ends, the parser produces empty tokens.
 *
 * An application must not alternate calls of the functions
 * `yaml_parser_parse_token()`, `yaml_parser_parse_event()`,
 * `yaml_parser_parse_document()` and `yaml_parser_parse_single_document()` on
 * the same parser object.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `event`: an empty event object to save the event produced by the parser.
 *   An application is responsible for clearing or deleting the produced event.
 *   Alternatively the produced event could be fed to the emitter.  If the
 *   parser fails or the stream end is reached, the object is kept empty.
 *
 * Returns: `1` on success, `0` on error.  If the function succeeds and the
 * stream end is not reached, the event data is saved to the given event
 * object.  If the function fails, the error details could be obtained with
 * `yaml_parser_get_error()`.  In case of error, the parser is non-functional
 * until it is cleared.
 */

YAML_DECLARE(int)
yaml_parser_parse_event(yaml_parser_t *parser, yaml_event_t *event);

/*
 * Parse the input stream and produce the next YAML document.
 *
 * An application may call this function subsequently to produce a sequence of
 * documents constituting the input stream.  When the stream ends, the parser
 * produces empty documents.
 *
 * An application must not alternate calls of the functions
 * `yaml_parser_parse_token()`, `yaml_parser_parse_event()`,
 * `yaml_parser_parse_document()` and `yaml_parser_parse_single_document()` on
 * the same parser object.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `document`: an empty document object to save the document produced by the
 *   parser.  An application is responsible for clearing or deleting the
 *   produced document.  Alternatively the produced document could be fed to
 *   the emitter.  If the parser fails or the stream end is reached, the object
 *   is kept empty.
 *
 * Returns: `1` on success, `0` on error.  If the function succeeds and the
 * stream end is not reached, the document data is saved to the given document
 * object.  If the function fails, the error details could be obtained with
 * `yaml_parser_get_error()`.  In case of error, the parser is non-functional
 * until it is cleared.
 */

YAML_DECLARE(int)
yaml_parser_parse_document(yaml_parser_t *parser, yaml_document_t *document);

/*
 * Parse the input stream containing a single YAML document.
 *
 * An application may call this function to ensure that the input stream contain
 * no more that one document.  If the stream is empty, the parser produces an
 * empty document.  If the stream contains a single document, the parser returns
 * it.  If the stream contains more than one document, the parser produces an
 * error.
 *
 * An application must not alternate calls of the functions
 * `yaml_parser_parse_token()`, `yaml_parser_parse_event()`,
 * `yaml_parser_parse_document()` and `yaml_parser_parse_single_document()` on
 * the same parser object.
 *
 * Arguments:
 *
 * - `parser`: a parser object.
 *
 * - `document`: an empty document object to save the document produced by the
 *   parser.  An application is responsible for clearing or deleting the
 *   produced document.  Alternatively the produced document could be fed to
 *   the emitter.  If the parser fails or the stream is empty, the object is
 *   kept empty.
 *
 * Returns: `1` on success, `0` on error.  If the function succeeds and the
 * stream is not empty, the document data is saved to the given document
 * object.  If the function fails, the error details could be obtained with
 * `yaml_parser_get_error()`.  In case of error, the parser is non-functional
 * until it is cleared.
 */

YAML_DECLARE(int)
yaml_parser_parse_single_document(yaml_parser_t *parser,
        yaml_document_t *document);

/******************************************************************************
 * Emitter Definitions
 ******************************************************************************/

/*
 * An opaque definition of the emitter object.
 *
 * An emitter object is used to emit YAML events or documents into an output
 * YAML stream.
 */

typedef struct yaml_emitter_s yaml_emitter_t;

/*
 * Allocate a new emitter object.
 *
 * An allocated emitter object should be deleted with `yaml_emitter_delete()`.
 *
 * Returns: a new emitter or `NULL` on error.  The function mail fail if it
 * cannot allocate memory for internal buffers.
 */

YAML_DECLARE(yaml_emitter_t *)
yaml_emitter_new(void);

/*
 * Deallocate an emitter and free the internal emitter data.
 * 
 * An emitter object must be previously allocated with `yaml_emitter_new()`.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 */

YAML_DECLARE(void)
yaml_emitter_delete(yaml_emitter_t *emitter);

/*
 * Clear and reinitialize the internal state of the emitter.
 *
 * This function could be used for cleaning up an emitter object after an error
 * condition or after a complete YAML stream was produced.  A cleaned emitter
 * object may be reused to produce another YAML stream.  Note that all the
 * emitter parameters including the write handler must be reset.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 */

YAML_DECLARE(void)
yaml_emitter_clear(yaml_emitter_t *emitter);

/*
 * Get the emitter error.
 *
 * Use this function to get a detailed error information after failure of one
 * of the following functions: `yaml_emitter_emit_event()`,
 * `yaml_emitter_open()`, `yaml_emitter_close()`,
 * `yaml_emitter_emit_document()`, `yaml_emitter_emit_single_document()`.
 *
 * The pointer returned by the function is only valid until the emitter object
 * is not modified.  However the error object could be safely copied.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * Returns: a pointer to an error object.  The returned pointer is only valid
 * until the emitter object is not modified or deleted.  However the error
 * object could be safely copied.
 */

YAML_DECLARE(yaml_error_t *)
yaml_emitter_get_error(yaml_emitter_t *emitter);

/*
 * Set the emitter to dump the generated YAML stream into a string buffer.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `buffer`: a pointer to a buffer to store the generated YAML stream.  The
 *   pointer must be valid until the emitter object is cleared or deleted.
 *
 * - `capacity`: the size of the buffer.  The emitter will fail if the buffer
 *   is smaller than required to hold the whole stream.
 *
 * - `length`: a pointer to save the length of the produced stream (in bytes).
 */

YAML_DECLARE(void)
yaml_emitter_set_string_writer(yaml_emitter_t *emitter,
        unsigned char *buffer, size_t capacity, size_t *length);

/*
 * Set the emitter to dump the generated YAML stream into a file.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `file`: a pointer to a file open for writing.  The pointer must be valid
 *   until the emitter object is cleared or deleted.
 */

YAML_DECLARE(void)
yaml_emitter_set_file_writer(yaml_emitter_t *emitter, FILE *file);

/*
 * Set the output stream writer for an emitter.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `writer`: a write handler.
 *
 * - `data`: application data for passing to the writer.
 */

YAML_DECLARE(void)
yaml_emitter_set_writer(yaml_emitter_t *emitter,
        yaml_writer_t *writer, void *data);

/*
 * Set the standard nonspecific tag resolver for an emitter.
 *
 * The standard resolver recognizes the following scalar tags: `!!null`,
 * `!!bool`, `!!str`, `!!int`, and `!!float`.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 */

YAML_DECLARE(void)
yaml_emitter_set_standard_resolver(yaml_emitter_t *emitter);

/*
 * Set a nonspecific tag resolver for an emitter.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `resolver`: a resolve handler.
 *
 * - `data`: application data for passing to the resolver.
 */

YAML_DECLARE(void)
yaml_emitter_set_resolver(yaml_emitter_t *emitter,
        yaml_resolver_t *resolver, void *data);

/*
 * Set the output stream encoding.
 *
 * The emitter uses the UTF-8 encoding for the output stream unless another
 * encoding is specified explicitly.  The encoding could be specified using
 * this function or via the `encoding` parameter of the STREAM-START event.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `encoding`: the output stream encoding.
 */

YAML_DECLARE(void)
yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding);

/*
 * Specify if the emitter should use the "canonical" output format.
 *
 * The "canonical" format uses the flow style for collections and the
 * double-quoted style for scalars.  Node tags are always dumped explicitly.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `is_canonical`: `1` to set the "canonical" format, `0` otherwise.
 */

YAML_DECLARE(void)
yaml_emitter_set_canonical(yaml_emitter_t *emitter, int is_canonical);

/*
 * Set the intendation increment.
 *
 * The default intendation increment is `2`.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `indent`: the indentation increment; a number between `2` and `9`.
 */

YAML_DECLARE(void)
yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent);

/*
 * Set the preferred line width.
 *
 * The default preferred line width is `80`.  The given line width is only a
 * stylistic hint and could be violated by the emitter.  When the line width is
 * exceeded, the emitter seeks for a way to move to the next line.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `width`: the preferred line width; `-1` means unlimited.
 */

YAML_DECLARE(void)
yaml_emitter_set_width(yaml_emitter_t *emitter, int width);

/*
 * Specify if non-ASCII characters could be emitted unescaped.
 *
 * By default, the emitter always escapes non-ASCII characters using the `\u`
 * or `\U` format.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `is_unicode`: `1` if unescaped non-ASCII characters are allowed; `0`
 *   otherwise.
 */

YAML_DECLARE(void)
yaml_emitter_set_unicode(yaml_emitter_t *emitter, int is_unicode);

/*
 * Set the preferred line break.
 *
 * By default, the emitter uses the LN character for line breaks.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `line_break`: the preferred line break.
 */

YAML_DECLARE(void)
yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break);

/*
 * Emit an event to the output YAML stream.
 *
 * An application needs to call this function subsequently to produce a whole
 * YAML stream.  The event sequence must satisfy the following grammar:
 *
 *      stream ::= STREAM-START document* STREAM-END
 *      document ::= DOCUMENT-START node DOCUMENT-END
 *      node ::= ALIAS | SCALAR | sequence | mapping
 *      sequence ::= SEQUENCE-START node* SEQUENCE-END
 *      mapping ::= MAPPING-START (node node)* MAPPING-END
 *
 * An application must not alternate the calls of the functions
 * `yaml_emitter_emit_event()`, `yaml_emitter_emit_document()` and
 * `yaml_emitter_emit_single_document()` on the same emitter object.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `event`: an event to emit.  The event must be previously initialized using
 *   either one of the functions `yaml_event_create_*()` or with
 *   `yaml_parser_parse_event()`.  The emitter takes the responsibility for the
 *   event data and clears the event, so that it becomes empty.  The event is
 *   cleared even if the function fails.
 *
 * Returns: `1` on success, `0` on error.  Note that the emitter may not
 * immediately dump the given event, so that the function could indicate
 * success on an errorneous event.  In this case, some of the next calls of the
 * function will generate an error.  If the function fails, the error details
 * could be obtained with `yaml_emitter_get_error()`.  In case of error, the
 * emitter is non-functional until it is cleared.
 */

YAML_DECLARE(int)
yaml_emitter_emit_event(yaml_emitter_t *emitter, yaml_event_t *event);

/*
 * Start a YAML stream.
 *
 * This function should be used in conjunction with
 * `yaml_emitter_emit_document()` and `yaml_emitter_end()`.  This function must
 * be called once before any documents are emitted.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * Returns: `1` on success, `0` on error.  If the function fails, the error
 * details could be obtained with `yaml_emitter_get_error()`.  In case of
 * error, the emitter is non-functional until it is cleared.
 */

YAML_DECLARE(int)
yaml_emitter_start(yaml_emitter_t *emitter);

/*
 * Finish a YAML stream.
 *
 * This function should be used in conjunction with `yaml_emitter_start()` and
 * `yaml_emitter_emit_document()`.  This function must be called once after all
 * documents are emitted.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * Returns: `1` on success, `0` on error.  If the function fails, the error
 * details could be obtained with `yaml_emitter_get_error()`.  In case of
 * error, the emitter is non-functional until it is cleared.
 */

YAML_DECLARE(int)
yaml_emitter_end(yaml_emitter_t *emitter);

/*
 * Emit a YAML document.
 *
 * Before emitting any documents, the function `yaml_emitter_start()` must be
 * called.  After all documents are emitted, the function `yaml_emitter_end()`
 * must be called.
 *
 * An application must not alternate the calls of the functions
 * `yaml_emitter_emit_event()`, `yaml_emitter_emit_document()` and
 * `yaml_emitter_emit_single_document()` on the same emitter object.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `document`: a document to emit.  The document must have a root node with
 *   all the other nodes reachable from it.  The document may be prepared using
 *   the functions `yaml_document_create()`, `yaml_document_add_*()` and
 *   `yaml_document_append_*()` or obtained via `yaml_parser_parse_document()`.
 *   The emitter takes the responsibility for the document content and clears
 *   the document, so that it becomes empty.  The document is cleared even if
 *   the function fails.
 *
 * Returns: `1` on success, `0` on error.  If the function fails, the error
 * details could be obtained with `yaml_emitter_get_error()`.  In case of
 * error, the emitter is non-functional until it is cleared.
 */

YAML_DECLARE(int)
yaml_emitter_emit_document(yaml_emitter_t *emitter, yaml_document_t *document);

/*
 * Emit a YAML stream consisting of a single document.
 *
 * This function is a shorthand of the calls:
 *
 *      yaml_emitter_start(emitter)
 *      yaml_emitter_emit_document(emitter, document)
 *      yaml_emitter_end(emitter)
 *
 * An application must not alternate the calls of the functions
 * `yaml_emitter_emit_event()`, `yaml_emitter_emit_document()` and
 * `yaml_emitter_emit_single_document()` on the same emitter object.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * - `document`: a document to emit.  The document must have a root node with
 *   all the other nodes reachable from it.  The document may be prepared using
 *   the functions `yaml_document_create()`, `yaml_document_add_*()` and
 *   `yaml_document_append_*()` or obtained via `yaml_parser_parse_document()`.
 *   The emitter takes the responsibility for the document content and clears
 *   the document, so that it becomes empty.  The document is cleared even if
 *   the function fails.
 *
 * Returns: `1` on success, `0` on error.  If the function fails, the error
 * details could be obtained with `yaml_emitter_get_error()`.  In case of
 * error, the emitter is non-functional until it is cleared.
 */

YAML_DECLARE(int)
yaml_emitter_emit_single_document(yaml_emitter_t *emitter,
        yaml_document_t *document);

/*
 * Flush the accumulated characters.
 *
 * This function flushes the accumulated characters from the internal emitter
 * buffer to the output stream.  Note that the buffer is flushed automatically
 * when a complete document has emitted or a stream has ended.
 *
 * Arguments:
 *
 * - `emitter`: an emitter object.
 *
 * Returns: `1` on success, `0` on error.  If the function fails, the error
 * details could be obtained with `yaml_emitter_get_error()`.  In case of
 * error, the emitter is non-functional until it is cleared.
 */

YAML_DECLARE(int)
yaml_emitter_flush(yaml_emitter_t *emitter);


#ifdef __cplusplus
}
#endif

#endif /* #ifndef YAML_H */