yaml.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. /**
  2. * @file yaml.h
  3. * @brief Public interface for libyaml.
  4. *
  5. * Include the header file with the code:
  6. * @code
  7. * #include <yaml.h>
  8. * @endcode
  9. */
  10. #ifndef YAML_H
  11. #define YAML_H
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. /**
  19. * @defgroup export Export Definitions
  20. * @{
  21. */
  22. /** The public API declaration. */
  23. #define YAML_DECLARE_STATIC
  24. #if defined(__MINGW32__)
  25. # define YAML_DECLARE(type) type
  26. #elif defined(_WIN32)
  27. # if defined(YAML_DECLARE_STATIC)
  28. # define YAML_DECLARE(type) type
  29. # elif defined(YAML_DECLARE_EXPORT)
  30. # define YAML_DECLARE(type) __declspec(dllexport) type
  31. # else
  32. # define YAML_DECLARE(type) __declspec(dllimport) type
  33. # endif
  34. #else
  35. # define YAML_DECLARE(type) type
  36. #endif
  37. /** @} */
  38. /**
  39. * @defgroup version Version Information
  40. * @{
  41. */
  42. /**
  43. * Get the library version as a string.
  44. *
  45. * @returns The function returns the pointer to a static string of the form
  46. * @c "X.Y.Z", where @c X is the major version number, @c Y is a minor version
  47. * number, and @c Z is the patch version number.
  48. */
  49. YAML_DECLARE(const char *)
  50. yaml_get_version_string(void);
  51. /**
  52. * Get the library version numbers.
  53. *
  54. * @param[out] major Major version number.
  55. * @param[out] minor Minor version number.
  56. * @param[out] patch Patch version number.
  57. */
  58. YAML_DECLARE(void)
  59. yaml_get_version(int *major, int *minor, int *patch);
  60. /** @} */
  61. /**
  62. * @defgroup basic Basic Types
  63. * @{
  64. */
  65. /** The character type (UTF-8 octet). */
  66. typedef unsigned char yaml_char_t;
  67. /** The version directive data. */
  68. typedef struct yaml_version_directive_s {
  69. /** The major version number. */
  70. int major;
  71. /** The minor version number. */
  72. int minor;
  73. } yaml_version_directive_t;
  74. /** The tag directive data. */
  75. typedef struct yaml_tag_directive_s {
  76. /** The tag handle. */
  77. yaml_char_t *handle;
  78. /** The tag prefix. */
  79. yaml_char_t *prefix;
  80. } yaml_tag_directive_t;
  81. /** The stream encoding. */
  82. typedef enum yaml_encoding_e {
  83. /** Let the parser choose the encoding. */
  84. YAML_ANY_ENCODING,
  85. /** The default UTF-8 encoding. */
  86. YAML_UTF8_ENCODING,
  87. /** The UTF-16-LE encoding with BOM. */
  88. YAML_UTF16LE_ENCODING,
  89. /** The UTF-16-BE encoding with BOM. */
  90. YAML_UTF16BE_ENCODING
  91. } yaml_encoding_t;
  92. /** Line break types. */
  93. typedef enum yaml_break_e {
  94. /** Let the parser choose the break type. */
  95. YAML_ANY_BREAK,
  96. /** Use CR for line breaks (Mac style). */
  97. YAML_CR_BREAK,
  98. /** Use LN for line breaks (Unix style). */
  99. YAML_LN_BREAK,
  100. /** Use CR LN for line breaks (DOS style). */
  101. YAML_CRLN_BREAK
  102. } yaml_break_t;
  103. /** Many bad things could happen with the parser and emitter. */
  104. typedef enum yaml_error_type_e {
  105. /** No error is produced. */
  106. YAML_NO_ERROR,
  107. /** Cannot allocate or reallocate a block of memory. */
  108. YAML_MEMORY_ERROR,
  109. /** Cannot read or decode the input stream. */
  110. YAML_READER_ERROR,
  111. /** Cannot scan the input stream. */
  112. YAML_SCANNER_ERROR,
  113. /** Cannot parse the input stream. */
  114. YAML_PARSER_ERROR,
  115. /** Cannot compose a YAML document. */
  116. YAML_COMPOSER_ERROR,
  117. /** Cannot write to the output stream. */
  118. YAML_WRITER_ERROR,
  119. /** Cannot emit a YAML stream. */
  120. YAML_EMITTER_ERROR
  121. } yaml_error_type_t;
  122. /** The pointer position. */
  123. typedef struct yaml_mark_s {
  124. /** The position index. */
  125. size_t index;
  126. /** The position line. */
  127. size_t line;
  128. /** The position column. */
  129. size_t column;
  130. } yaml_mark_t;
  131. /** @} */
  132. /**
  133. * @defgroup styles Node Styles
  134. * @{
  135. */
  136. /** Scalar styles. */
  137. typedef enum yaml_scalar_style_e {
  138. /** Let the emitter choose the style. */
  139. YAML_ANY_SCALAR_STYLE,
  140. /** The plain scalar style. */
  141. YAML_PLAIN_SCALAR_STYLE,
  142. /** The single-quoted scalar style. */
  143. YAML_SINGLE_QUOTED_SCALAR_STYLE,
  144. /** The double-quoted scalar style. */
  145. YAML_DOUBLE_QUOTED_SCALAR_STYLE,
  146. /** The literal scalar style. */
  147. YAML_LITERAL_SCALAR_STYLE,
  148. /** The folded scalar style. */
  149. YAML_FOLDED_SCALAR_STYLE
  150. } yaml_scalar_style_t;
  151. /** Sequence styles. */
  152. typedef enum yaml_sequence_style_e {
  153. /** Let the emitter choose the style. */
  154. YAML_ANY_SEQUENCE_STYLE,
  155. /** The block sequence style. */
  156. YAML_BLOCK_SEQUENCE_STYLE,
  157. /** The flow sequence style. */
  158. YAML_FLOW_SEQUENCE_STYLE
  159. } yaml_sequence_style_t;
  160. /** Mapping styles. */
  161. typedef enum yaml_mapping_style_e {
  162. /** Let the emitter choose the style. */
  163. YAML_ANY_MAPPING_STYLE,
  164. /** The block mapping style. */
  165. YAML_BLOCK_MAPPING_STYLE,
  166. /** The flow mapping style. */
  167. YAML_FLOW_MAPPING_STYLE
  168. /* YAML_FLOW_SET_MAPPING_STYLE */
  169. } yaml_mapping_style_t;
  170. /** @} */
  171. /**
  172. * @defgroup tokens Tokens
  173. * @{
  174. */
  175. /** Token types. */
  176. typedef enum yaml_token_type_e {
  177. /** An empty token. */
  178. YAML_NO_TOKEN,
  179. /** A STREAM-START token. */
  180. YAML_STREAM_START_TOKEN,
  181. /** A STREAM-END token. */
  182. YAML_STREAM_END_TOKEN,
  183. /** A VERSION-DIRECTIVE token. */
  184. YAML_VERSION_DIRECTIVE_TOKEN,
  185. /** A TAG-DIRECTIVE token. */
  186. YAML_TAG_DIRECTIVE_TOKEN,
  187. /** A DOCUMENT-START token. */
  188. YAML_DOCUMENT_START_TOKEN,
  189. /** A DOCUMENT-END token. */
  190. YAML_DOCUMENT_END_TOKEN,
  191. /** A BLOCK-SEQUENCE-START token. */
  192. YAML_BLOCK_SEQUENCE_START_TOKEN,
  193. /** A BLOCK-MAPPING-START token. */
  194. YAML_BLOCK_MAPPING_START_TOKEN,
  195. /** A BLOCK-END token. */
  196. YAML_BLOCK_END_TOKEN,
  197. /** A FLOW-SEQUENCE-START token. */
  198. YAML_FLOW_SEQUENCE_START_TOKEN,
  199. /** A FLOW-SEQUENCE-END token. */
  200. YAML_FLOW_SEQUENCE_END_TOKEN,
  201. /** A FLOW-MAPPING-START token. */
  202. YAML_FLOW_MAPPING_START_TOKEN,
  203. /** A FLOW-MAPPING-END token. */
  204. YAML_FLOW_MAPPING_END_TOKEN,
  205. /** A BLOCK-ENTRY token. */
  206. YAML_BLOCK_ENTRY_TOKEN,
  207. /** A FLOW-ENTRY token. */
  208. YAML_FLOW_ENTRY_TOKEN,
  209. /** A KEY token. */
  210. YAML_KEY_TOKEN,
  211. /** A VALUE token. */
  212. YAML_VALUE_TOKEN,
  213. /** An ALIAS token. */
  214. YAML_ALIAS_TOKEN,
  215. /** An ANCHOR token. */
  216. YAML_ANCHOR_TOKEN,
  217. /** A TAG token. */
  218. YAML_TAG_TOKEN,
  219. /** A SCALAR token. */
  220. YAML_SCALAR_TOKEN
  221. } yaml_token_type_t;
  222. /** The token structure. */
  223. typedef struct yaml_token_s {
  224. /** The token type. */
  225. yaml_token_type_t type;
  226. /** The token data. */
  227. union {
  228. /** The stream start (for @c YAML_STREAM_START_TOKEN). */
  229. struct {
  230. /** The stream encoding. */
  231. yaml_encoding_t encoding;
  232. } stream_start;
  233. /** The alias (for @c YAML_ALIAS_TOKEN). */
  234. struct {
  235. /** The alias value. */
  236. yaml_char_t *value;
  237. } alias;
  238. /** The anchor (for @c YAML_ANCHOR_TOKEN). */
  239. struct {
  240. /** The anchor value. */
  241. yaml_char_t *value;
  242. } anchor;
  243. /** The tag (for @c YAML_TAG_TOKEN). */
  244. struct {
  245. /** The tag handle. */
  246. yaml_char_t *handle;
  247. /** The tag suffix. */
  248. yaml_char_t *suffix;
  249. } tag;
  250. /** The scalar value (for @c YAML_SCALAR_TOKEN). */
  251. struct {
  252. /** The scalar value. */
  253. yaml_char_t *value;
  254. /** The length of the scalar value. */
  255. size_t length;
  256. /** The scalar style. */
  257. yaml_scalar_style_t style;
  258. } scalar;
  259. /** The version directive (for @c YAML_VERSION_DIRECTIVE_TOKEN). */
  260. struct {
  261. /** The major version number. */
  262. int major;
  263. /** The minor version number. */
  264. int minor;
  265. } version_directive;
  266. /** The tag directive (for @c YAML_TAG_DIRECTIVE_TOKEN). */
  267. struct {
  268. /** The tag handle. */
  269. yaml_char_t *handle;
  270. /** The tag prefix. */
  271. yaml_char_t *prefix;
  272. } tag_directive;
  273. } data;
  274. /** The beginning of the token. */
  275. yaml_mark_t start_mark;
  276. /** The end of the token. */
  277. yaml_mark_t end_mark;
  278. } yaml_token_t;
  279. /**
  280. * Free any memory allocated for a token object.
  281. *
  282. * @param[in,out] token A token object.
  283. */
  284. YAML_DECLARE(void)
  285. yaml_token_delete(yaml_token_t *token);
  286. /** @} */
  287. /**
  288. * @defgroup events Events
  289. * @{
  290. */
  291. /** Event types. */
  292. typedef enum yaml_event_type_e {
  293. /** An empty event. */
  294. YAML_NO_EVENT,
  295. /** A STREAM-START event. */
  296. YAML_STREAM_START_EVENT,
  297. /** A STREAM-END event. */
  298. YAML_STREAM_END_EVENT,
  299. /** A DOCUMENT-START event. */
  300. YAML_DOCUMENT_START_EVENT,
  301. /** A DOCUMENT-END event. */
  302. YAML_DOCUMENT_END_EVENT,
  303. /** An ALIAS event. */
  304. YAML_ALIAS_EVENT,
  305. /** A SCALAR event. */
  306. YAML_SCALAR_EVENT,
  307. /** A SEQUENCE-START event. */
  308. YAML_SEQUENCE_START_EVENT,
  309. /** A SEQUENCE-END event. */
  310. YAML_SEQUENCE_END_EVENT,
  311. /** A MAPPING-START event. */
  312. YAML_MAPPING_START_EVENT,
  313. /** A MAPPING-END event. */
  314. YAML_MAPPING_END_EVENT
  315. } yaml_event_type_t;
  316. /** The event structure. */
  317. typedef struct yaml_event_s {
  318. /** The event type. */
  319. yaml_event_type_t type;
  320. /** The event data. */
  321. union {
  322. /** The stream parameters (for @c YAML_STREAM_START_EVENT). */
  323. struct {
  324. /** The document encoding. */
  325. yaml_encoding_t encoding;
  326. } stream_start;
  327. /** The document parameters (for @c YAML_DOCUMENT_START_EVENT). */
  328. struct {
  329. /** The version directive. */
  330. yaml_version_directive_t *version_directive;
  331. /** The list of tag directives. */
  332. struct {
  333. /** The beginning of the tag directives list. */
  334. yaml_tag_directive_t *start;
  335. /** The end of the tag directives list. */
  336. yaml_tag_directive_t *end;
  337. } tag_directives;
  338. /** Is the document indicator implicit? */
  339. int implicit;
  340. } document_start;
  341. /** The document end parameters (for @c YAML_DOCUMENT_END_EVENT). */
  342. struct {
  343. /** Is the document end indicator implicit? */
  344. int implicit;
  345. } document_end;
  346. /** The alias parameters (for @c YAML_ALIAS_EVENT). */
  347. struct {
  348. /** The anchor. */
  349. yaml_char_t *anchor;
  350. } alias;
  351. /** The scalar parameters (for @c YAML_SCALAR_EVENT). */
  352. struct {
  353. /** The anchor. */
  354. yaml_char_t *anchor;
  355. /** The tag. */
  356. yaml_char_t *tag;
  357. /** The scalar value. */
  358. yaml_char_t *value;
  359. /** The length of the scalar value. */
  360. size_t length;
  361. /** Is the tag optional for the plain style? */
  362. int plain_implicit;
  363. /** Is the tag optional for any non-plain style? */
  364. int quoted_implicit;
  365. /** The scalar style. */
  366. yaml_scalar_style_t style;
  367. } scalar;
  368. /** The sequence parameters (for @c YAML_SEQUENCE_START_EVENT). */
  369. struct {
  370. /** The anchor. */
  371. yaml_char_t *anchor;
  372. /** The tag. */
  373. yaml_char_t *tag;
  374. /** Is the tag optional? */
  375. int implicit;
  376. /** The sequence style. */
  377. yaml_sequence_style_t style;
  378. } sequence_start;
  379. /** The mapping parameters (for @c YAML_MAPPING_START_EVENT). */
  380. struct {
  381. /** The anchor. */
  382. yaml_char_t *anchor;
  383. /** The tag. */
  384. yaml_char_t *tag;
  385. /** Is the tag optional? */
  386. int implicit;
  387. /** The mapping style. */
  388. yaml_mapping_style_t style;
  389. } mapping_start;
  390. } data;
  391. /** The beginning of the event. */
  392. yaml_mark_t start_mark;
  393. /** The end of the event. */
  394. yaml_mark_t end_mark;
  395. } yaml_event_t;
  396. /**
  397. * Create the STREAM-START event.
  398. *
  399. * @param[out] event An empty event object.
  400. * @param[in] encoding The stream encoding.
  401. *
  402. * @returns @c 1 if the function succeeded, @c 0 on error.
  403. */
  404. YAML_DECLARE(int)
  405. yaml_stream_start_event_initialize(yaml_event_t *event,
  406. yaml_encoding_t encoding);
  407. /**
  408. * Create the STREAM-END event.
  409. *
  410. * @param[out] event An empty event object.
  411. *
  412. * @returns @c 1 if the function succeeded, @c 0 on error.
  413. */
  414. YAML_DECLARE(int)
  415. yaml_stream_end_event_initialize(yaml_event_t *event);
  416. /**
  417. * Create the DOCUMENT-START event.
  418. *
  419. * The @a implicit argument is considered as a stylistic parameter and may be
  420. * ignored by the emitter.
  421. *
  422. * @param[out] event An empty event object.
  423. * @param[in] version_directive The %YAML directive value or
  424. * @c NULL.
  425. * @param[in] tag_directives_start The beginning of the %TAG
  426. * directives list.
  427. * @param[in] tag_directives_end The end of the %TAG directives
  428. * list.
  429. * @param[in] implicit If the document start indicator is
  430. * implicit.
  431. *
  432. * @returns @c 1 if the function succeeded, @c 0 on error.
  433. */
  434. YAML_DECLARE(int)
  435. yaml_document_start_event_initialize(yaml_event_t *event,
  436. yaml_version_directive_t *version_directive,
  437. yaml_tag_directive_t *tag_directives_start,
  438. yaml_tag_directive_t *tag_directives_end,
  439. int implicit);
  440. /**
  441. * Create the DOCUMENT-END event.
  442. *
  443. * The @a implicit argument is considered as a stylistic parameter and may be
  444. * ignored by the emitter.
  445. *
  446. * @param[out] event An empty event object.
  447. * @param[in] implicit If the document end indicator is implicit.
  448. *
  449. * @returns @c 1 if the function succeeded, @c 0 on error.
  450. */
  451. YAML_DECLARE(int)
  452. yaml_document_end_event_initialize(yaml_event_t *event, int implicit);
  453. /**
  454. * Create an ALIAS event.
  455. *
  456. * @param[out] event An empty event object.
  457. * @param[in] anchor The anchor value.
  458. *
  459. * @returns @c 1 if the function succeeded, @c 0 on error.
  460. */
  461. YAML_DECLARE(int)
  462. yaml_alias_event_initialize(yaml_event_t *event, const yaml_char_t *anchor);
  463. /**
  464. * Create a SCALAR event.
  465. *
  466. * The @a style argument may be ignored by the emitter.
  467. *
  468. * Either the @a tag attribute or one of the @a plain_implicit and
  469. * @a quoted_implicit flags must be set.
  470. *
  471. * @param[out] event An empty event object.
  472. * @param[in] anchor The scalar anchor or @c NULL.
  473. * @param[in] tag The scalar tag or @c NULL.
  474. * @param[in] value The scalar value.
  475. * @param[in] length The length of the scalar value.
  476. * @param[in] plain_implicit If the tag may be omitted for the plain
  477. * style.
  478. * @param[in] quoted_implicit If the tag may be omitted for any
  479. * non-plain style.
  480. * @param[in] style The scalar style.
  481. *
  482. * @returns @c 1 if the function succeeded, @c 0 on error.
  483. */
  484. YAML_DECLARE(int)
  485. yaml_scalar_event_initialize(yaml_event_t *event,
  486. const yaml_char_t *anchor, const yaml_char_t *tag,
  487. const yaml_char_t *value, int length,
  488. int plain_implicit, int quoted_implicit,
  489. yaml_scalar_style_t style);
  490. /**
  491. * Create a SEQUENCE-START event.
  492. *
  493. * The @a style argument may be ignored by the emitter.
  494. *
  495. * Either the @a tag attribute or the @a implicit flag must be set.
  496. *
  497. * @param[out] event An empty event object.
  498. * @param[in] anchor The sequence anchor or @c NULL.
  499. * @param[in] tag The sequence tag or @c NULL.
  500. * @param[in] implicit If the tag may be omitted.
  501. * @param[in] style The sequence style.
  502. *
  503. * @returns @c 1 if the function succeeded, @c 0 on error.
  504. */
  505. YAML_DECLARE(int)
  506. yaml_sequence_start_event_initialize(yaml_event_t *event,
  507. const yaml_char_t *anchor, const yaml_char_t *tag, int implicit,
  508. yaml_sequence_style_t style);
  509. /**
  510. * Create a SEQUENCE-END event.
  511. *
  512. * @param[out] event An empty event object.
  513. *
  514. * @returns @c 1 if the function succeeded, @c 0 on error.
  515. */
  516. YAML_DECLARE(int)
  517. yaml_sequence_end_event_initialize(yaml_event_t *event);
  518. /**
  519. * Create a MAPPING-START event.
  520. *
  521. * The @a style argument may be ignored by the emitter.
  522. *
  523. * Either the @a tag attribute or the @a implicit flag must be set.
  524. *
  525. * @param[out] event An empty event object.
  526. * @param[in] anchor The mapping anchor or @c NULL.
  527. * @param[in] tag The mapping tag or @c NULL.
  528. * @param[in] implicit If the tag may be omitted.
  529. * @param[in] style The mapping style.
  530. *
  531. * @returns @c 1 if the function succeeded, @c 0 on error.
  532. */
  533. YAML_DECLARE(int)
  534. yaml_mapping_start_event_initialize(yaml_event_t *event,
  535. const yaml_char_t *anchor, const yaml_char_t *tag, int implicit,
  536. yaml_mapping_style_t style);
  537. /**
  538. * Create a MAPPING-END event.
  539. *
  540. * @param[out] event An empty event object.
  541. *
  542. * @returns @c 1 if the function succeeded, @c 0 on error.
  543. */
  544. YAML_DECLARE(int)
  545. yaml_mapping_end_event_initialize(yaml_event_t *event);
  546. /**
  547. * Free any memory allocated for an event object.
  548. *
  549. * @param[in,out] event An event object.
  550. */
  551. YAML_DECLARE(void)
  552. yaml_event_delete(yaml_event_t *event);
  553. /** @} */
  554. /**
  555. * @defgroup nodes Nodes
  556. * @{
  557. */
  558. /** The tag @c !!null with the only possible value: @c null. */
  559. #define YAML_NULL_TAG "tag:yaml.org,2002:null"
  560. /** The tag @c !!bool with the values: @c true and @c false. */
  561. #define YAML_BOOL_TAG "tag:yaml.org,2002:bool"
  562. /** The tag @c !!str for string values. */
  563. #define YAML_STR_TAG "tag:yaml.org,2002:str"
  564. /** The tag @c !!int for integer values. */
  565. #define YAML_INT_TAG "tag:yaml.org,2002:int"
  566. /** The tag @c !!float for float values. */
  567. #define YAML_FLOAT_TAG "tag:yaml.org,2002:float"
  568. /** The tag @c !!timestamp for date and time values. */
  569. #define YAML_TIMESTAMP_TAG "tag:yaml.org,2002:timestamp"
  570. /** The tag @c !!seq is used to denote sequences. */
  571. #define YAML_SEQ_TAG "tag:yaml.org,2002:seq"
  572. /** The tag @c !!map is used to denote mapping. */
  573. #define YAML_MAP_TAG "tag:yaml.org,2002:map"
  574. /** The default scalar tag is @c !!str. */
  575. #define YAML_DEFAULT_SCALAR_TAG YAML_STR_TAG
  576. /** The default sequence tag is @c !!seq. */
  577. #define YAML_DEFAULT_SEQUENCE_TAG YAML_SEQ_TAG
  578. /** The default mapping tag is @c !!map. */
  579. #define YAML_DEFAULT_MAPPING_TAG YAML_MAP_TAG
  580. /** Node types. */
  581. typedef enum yaml_node_type_e {
  582. /** An empty node. */
  583. YAML_NO_NODE,
  584. /** A scalar node. */
  585. YAML_SCALAR_NODE,
  586. /** A sequence node. */
  587. YAML_SEQUENCE_NODE,
  588. /** A mapping node. */
  589. YAML_MAPPING_NODE
  590. } yaml_node_type_t;
  591. /** The forward definition of a document node structure. */
  592. typedef struct yaml_node_s yaml_node_t;
  593. /** An element of a sequence node. */
  594. typedef int yaml_node_item_t;
  595. /** An element of a mapping node. */
  596. typedef struct yaml_node_pair_s {
  597. /** The key of the element. */
  598. int key;
  599. /** The value of the element. */
  600. int value;
  601. } yaml_node_pair_t;
  602. /** The node structure. */
  603. struct yaml_node_s {
  604. /** The node type. */
  605. yaml_node_type_t type;
  606. /** The node tag. */
  607. yaml_char_t *tag;
  608. /** The node data. */
  609. union {
  610. /** The scalar parameters (for @c YAML_SCALAR_NODE). */
  611. struct {
  612. /** The scalar value. */
  613. yaml_char_t *value;
  614. /** The length of the scalar value. */
  615. size_t length;
  616. /** The scalar style. */
  617. yaml_scalar_style_t style;
  618. } scalar;
  619. /** The sequence parameters (for @c YAML_SEQUENCE_NODE). */
  620. struct {
  621. /** The stack of sequence items. */
  622. struct {
  623. /** The beginning of the stack. */
  624. yaml_node_item_t *start;
  625. /** The end of the stack. */
  626. yaml_node_item_t *end;
  627. /** The top of the stack. */
  628. yaml_node_item_t *top;
  629. } items;
  630. /** The sequence style. */
  631. yaml_sequence_style_t style;
  632. } sequence;
  633. /** The mapping parameters (for @c YAML_MAPPING_NODE). */
  634. struct {
  635. /** The stack of mapping pairs (key, value). */
  636. struct {
  637. /** The beginning of the stack. */
  638. yaml_node_pair_t *start;
  639. /** The end of the stack. */
  640. yaml_node_pair_t *end;
  641. /** The top of the stack. */
  642. yaml_node_pair_t *top;
  643. } pairs;
  644. /** The mapping style. */
  645. yaml_mapping_style_t style;
  646. } mapping;
  647. } data;
  648. /** The beginning of the node. */
  649. yaml_mark_t start_mark;
  650. /** The end of the node. */
  651. yaml_mark_t end_mark;
  652. };
  653. /** The document structure. */
  654. typedef struct yaml_document_s {
  655. /** The document nodes. */
  656. struct {
  657. /** The beginning of the stack. */
  658. yaml_node_t *start;
  659. /** The end of the stack. */
  660. yaml_node_t *end;
  661. /** The top of the stack. */
  662. yaml_node_t *top;
  663. } nodes;
  664. /** The version directive. */
  665. yaml_version_directive_t *version_directive;
  666. /** The list of tag directives. */
  667. struct {
  668. /** The beginning of the tag directives list. */
  669. yaml_tag_directive_t *start;
  670. /** The end of the tag directives list. */
  671. yaml_tag_directive_t *end;
  672. } tag_directives;
  673. /** Is the document start indicator implicit? */
  674. int start_implicit;
  675. /** Is the document end indicator implicit? */
  676. int end_implicit;
  677. /** The beginning of the document. */
  678. yaml_mark_t start_mark;
  679. /** The end of the document. */
  680. yaml_mark_t end_mark;
  681. } yaml_document_t;
  682. /**
  683. * Create a YAML document.
  684. *
  685. * @param[out] document An empty document object.
  686. * @param[in] version_directive The %YAML directive value or
  687. * @c NULL.
  688. * @param[in] tag_directives_start The beginning of the %TAG
  689. * directives list.
  690. * @param[in] tag_directives_end The end of the %TAG directives
  691. * list.
  692. * @param[in] start_implicit If the document start indicator is
  693. * implicit.
  694. * @param[in] end_implicit If the document end indicator is
  695. * implicit.
  696. *
  697. * @returns @c 1 if the function succeeded, @c 0 on error.
  698. */
  699. YAML_DECLARE(int)
  700. yaml_document_initialize(yaml_document_t *document,
  701. yaml_version_directive_t *version_directive,
  702. yaml_tag_directive_t *tag_directives_start,
  703. yaml_tag_directive_t *tag_directives_end,
  704. int start_implicit, int end_implicit);
  705. /**
  706. * Delete a YAML document and all its nodes.
  707. *
  708. * @param[in,out] document A document object.
  709. */
  710. YAML_DECLARE(void)
  711. yaml_document_delete(yaml_document_t *document);
  712. /**
  713. * Get a node of a YAML document.
  714. *
  715. * The pointer returned by this function is valid until any of the functions
  716. * modifying the documents are called.
  717. *
  718. * @param[in] document A document object.
  719. * @param[in] index The node id.
  720. *
  721. * @returns the node objct or @c NULL if @c node_id is out of range.
  722. */
  723. YAML_DECLARE(yaml_node_t *)
  724. yaml_document_get_node(yaml_document_t *document, int index);
  725. /**
  726. * Get the root of a YAML document node.
  727. *
  728. * The root object is the first object added to the document.
  729. *
  730. * The pointer returned by this function is valid until any of the functions
  731. * modifying the documents are called.
  732. *
  733. * An empty document produced by the parser signifies the end of a YAML
  734. * stream.
  735. *
  736. * @param[in] document A document object.
  737. *
  738. * @returns the node object or @c NULL if the document is empty.
  739. */
  740. YAML_DECLARE(yaml_node_t *)
  741. yaml_document_get_root_node(yaml_document_t *document);
  742. /**
  743. * Create a SCALAR node and attach it to the document.
  744. *
  745. * The @a style argument may be ignored by the emitter.
  746. *
  747. * @param[in,out] document A document object.
  748. * @param[in] tag The scalar tag.
  749. * @param[in] value The scalar value.
  750. * @param[in] length The length of the scalar value.
  751. * @param[in] style The scalar style.
  752. *
  753. * @returns the node id or @c 0 on error.
  754. */
  755. YAML_DECLARE(int)
  756. yaml_document_add_scalar(yaml_document_t *document,
  757. const yaml_char_t *tag, const yaml_char_t *value, int length,
  758. yaml_scalar_style_t style);
  759. /**
  760. * Create a SEQUENCE node and attach it to the document.
  761. *
  762. * The @a style argument may be ignored by the emitter.
  763. *
  764. * @param[in,out] document A document object.
  765. * @param[in] tag The sequence tag.
  766. * @param[in] style The sequence style.
  767. *
  768. * @returns the node id or @c 0 on error.
  769. */
  770. YAML_DECLARE(int)
  771. yaml_document_add_sequence(yaml_document_t *document,
  772. const yaml_char_t *tag, yaml_sequence_style_t style);
  773. /**
  774. * Create a MAPPING node and attach it to the document.
  775. *
  776. * The @a style argument may be ignored by the emitter.
  777. *
  778. * @param[in,out] document A document object.
  779. * @param[in] tag The sequence tag.
  780. * @param[in] style The sequence style.
  781. *
  782. * @returns the node id or @c 0 on error.
  783. */
  784. YAML_DECLARE(int)
  785. yaml_document_add_mapping(yaml_document_t *document,
  786. const yaml_char_t *tag, yaml_mapping_style_t style);
  787. /**
  788. * Add an item to a SEQUENCE node.
  789. *
  790. * @param[in,out] document A document object.
  791. * @param[in] sequence The sequence node id.
  792. * @param[in] item The item node id.
  793. *
  794. * @returns @c 1 if the function succeeded, @c 0 on error.
  795. */
  796. YAML_DECLARE(int)
  797. yaml_document_append_sequence_item(yaml_document_t *document,
  798. int sequence, int item);
  799. /**
  800. * Add a pair of a key and a value to a MAPPING node.
  801. *
  802. * @param[in,out] document A document object.
  803. * @param[in] mapping The mapping node id.
  804. * @param[in] key The key node id.
  805. * @param[in] value The value node id.
  806. *
  807. * @returns @c 1 if the function succeeded, @c 0 on error.
  808. */
  809. YAML_DECLARE(int)
  810. yaml_document_append_mapping_pair(yaml_document_t *document,
  811. int mapping, int key, int value);
  812. /** @} */
  813. /**
  814. * @defgroup parser Parser Definitions
  815. * @{
  816. */
  817. /**
  818. * The prototype of a read handler.
  819. *
  820. * The read handler is called when the parser needs to read more bytes from the
  821. * source. The handler should write not more than @a size bytes to the @a
  822. * buffer. The number of written bytes should be set to the @a length variable.
  823. *
  824. * @param[in,out] data A pointer to an application data specified by
  825. * yaml_parser_set_input().
  826. * @param[out] buffer The buffer to write the data from the source.
  827. * @param[in] size The size of the buffer.
  828. * @param[out] size_read The actual number of bytes read from the source.
  829. *
  830. * @returns On success, the handler should return @c 1. If the handler failed,
  831. * the returned value should be @c 0. On EOF, the handler should set the
  832. * @a size_read to @c 0 and return @c 1.
  833. */
  834. typedef int yaml_read_handler_t(void *data, unsigned char *buffer, size_t size,
  835. size_t *size_read);
  836. /**
  837. * This structure holds information about a potential simple key.
  838. */
  839. typedef struct yaml_simple_key_s {
  840. /** Is a simple key possible? */
  841. int possible;
  842. /** Is a simple key required? */
  843. int required;
  844. /** The number of the token. */
  845. size_t token_number;
  846. /** The position mark. */
  847. yaml_mark_t mark;
  848. } yaml_simple_key_t;
  849. /**
  850. * The states of the parser.
  851. */
  852. typedef enum yaml_parser_state_e {
  853. /** Expect STREAM-START. */
  854. YAML_PARSE_STREAM_START_STATE,
  855. /** Expect the beginning of an implicit document. */
  856. YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE,
  857. /** Expect DOCUMENT-START. */
  858. YAML_PARSE_DOCUMENT_START_STATE,
  859. /** Expect the content of a document. */
  860. YAML_PARSE_DOCUMENT_CONTENT_STATE,
  861. /** Expect DOCUMENT-END. */
  862. YAML_PARSE_DOCUMENT_END_STATE,
  863. /** Expect a block node. */
  864. YAML_PARSE_BLOCK_NODE_STATE,
  865. /** Expect a block node or indentless sequence. */
  866. YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE,
  867. /** Expect a flow node. */
  868. YAML_PARSE_FLOW_NODE_STATE,
  869. /** Expect the first entry of a block sequence. */
  870. YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE,
  871. /** Expect an entry of a block sequence. */
  872. YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE,
  873. /** Expect an entry of an indentless sequence. */
  874. YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE,
  875. /** Expect the first key of a block mapping. */
  876. YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE,
  877. /** Expect a block mapping key. */
  878. YAML_PARSE_BLOCK_MAPPING_KEY_STATE,
  879. /** Expect a block mapping value. */
  880. YAML_PARSE_BLOCK_MAPPING_VALUE_STATE,
  881. /** Expect the first entry of a flow sequence. */
  882. YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE,
  883. /** Expect an entry of a flow sequence. */
  884. YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE,
  885. /** Expect a key of an ordered mapping. */
  886. YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE,
  887. /** Expect a value of an ordered mapping. */
  888. YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE,
  889. /** Expect the and of an ordered mapping entry. */
  890. YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE,
  891. /** Expect the first key of a flow mapping. */
  892. YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE,
  893. /** Expect a key of a flow mapping. */
  894. YAML_PARSE_FLOW_MAPPING_KEY_STATE,
  895. /** Expect a value of a flow mapping. */
  896. YAML_PARSE_FLOW_MAPPING_VALUE_STATE,
  897. /** Expect an empty value of a flow mapping. */
  898. YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE,
  899. /** Expect nothing. */
  900. YAML_PARSE_END_STATE
  901. } yaml_parser_state_t;
  902. /**
  903. * This structure holds aliases data.
  904. */
  905. typedef struct yaml_alias_data_s {
  906. /** The anchor. */
  907. yaml_char_t *anchor;
  908. /** The node id. */
  909. int index;
  910. /** The anchor mark. */
  911. yaml_mark_t mark;
  912. } yaml_alias_data_t;
  913. /**
  914. * The parser structure.
  915. *
  916. * All members are internal. Manage the structure using the @c yaml_parser_
  917. * family of functions.
  918. */
  919. typedef struct yaml_parser_s {
  920. /**
  921. * @name Error handling
  922. * @{
  923. */
  924. /** Error type. */
  925. yaml_error_type_t error;
  926. /** Error description. */
  927. const char *problem;
  928. /** The byte about which the problem occured. */
  929. size_t problem_offset;
  930. /** The problematic value (@c -1 is none). */
  931. int problem_value;
  932. /** The problem position. */
  933. yaml_mark_t problem_mark;
  934. /** The error context. */
  935. const char *context;
  936. /** The context position. */
  937. yaml_mark_t context_mark;
  938. /**
  939. * @}
  940. */
  941. /**
  942. * @name Reader stuff
  943. * @{
  944. */
  945. /** Read handler. */
  946. yaml_read_handler_t *read_handler;
  947. /** A pointer for passing to the read handler. */
  948. void *read_handler_data;
  949. /** Standard (string or file) input data. */
  950. union {
  951. /** String input data. */
  952. struct {
  953. /** The string start pointer. */
  954. const unsigned char *start;
  955. /** The string end pointer. */
  956. const unsigned char *end;
  957. /** The string current position. */
  958. const unsigned char *current;
  959. } string;
  960. /** File input data. */
  961. FILE *file;
  962. } input;
  963. /** EOF flag */
  964. int eof;
  965. /** The working buffer. */
  966. struct {
  967. /** The beginning of the buffer. */
  968. yaml_char_t *start;
  969. /** The end of the buffer. */
  970. yaml_char_t *end;
  971. /** The current position of the buffer. */
  972. yaml_char_t *pointer;
  973. /** The last filled position of the buffer. */
  974. yaml_char_t *last;
  975. } buffer;
  976. /* The number of unread characters in the buffer. */
  977. size_t unread;
  978. /** The raw buffer. */
  979. struct {
  980. /** The beginning of the buffer. */
  981. unsigned char *start;
  982. /** The end of the buffer. */
  983. unsigned char *end;
  984. /** The current position of the buffer. */
  985. unsigned char *pointer;
  986. /** The last filled position of the buffer. */
  987. unsigned char *last;
  988. } raw_buffer;
  989. /** The input encoding. */
  990. yaml_encoding_t encoding;
  991. /** The offset of the current position (in bytes). */
  992. size_t offset;
  993. /** The mark of the current position. */
  994. yaml_mark_t mark;
  995. /**
  996. * @}
  997. */
  998. /**
  999. * @name Scanner stuff
  1000. * @{
  1001. */
  1002. /** Have we started to scan the input stream? */
  1003. int stream_start_produced;
  1004. /** Have we reached the end of the input stream? */
  1005. int stream_end_produced;
  1006. /** The number of unclosed '[' and '{' indicators. */
  1007. int flow_level;
  1008. /** The tokens queue. */
  1009. struct {
  1010. /** The beginning of the tokens queue. */
  1011. yaml_token_t *start;
  1012. /** The end of the tokens queue. */
  1013. yaml_token_t *end;
  1014. /** The head of the tokens queue. */
  1015. yaml_token_t *head;
  1016. /** The tail of the tokens queue. */
  1017. yaml_token_t *tail;
  1018. } tokens;
  1019. /** The number of tokens fetched from the queue. */
  1020. size_t tokens_parsed;
  1021. /** Does the tokens queue contain a token ready for dequeueing. */
  1022. int token_available;
  1023. /** The indentation levels stack. */
  1024. struct {
  1025. /** The beginning of the stack. */
  1026. int *start;
  1027. /** The end of the stack. */
  1028. int *end;
  1029. /** The top of the stack. */
  1030. int *top;
  1031. } indents;
  1032. /** The current indentation level. */
  1033. int indent;
  1034. /** May a simple key occur at the current position? */
  1035. int simple_key_allowed;
  1036. /** The stack of simple keys. */
  1037. struct {
  1038. /** The beginning of the stack. */
  1039. yaml_simple_key_t *start;
  1040. /** The end of the stack. */
  1041. yaml_simple_key_t *end;
  1042. /** The top of the stack. */
  1043. yaml_simple_key_t *top;
  1044. } simple_keys;
  1045. /**
  1046. * @}
  1047. */
  1048. /**
  1049. * @name Parser stuff
  1050. * @{
  1051. */
  1052. /** The parser states stack. */
  1053. struct {
  1054. /** The beginning of the stack. */
  1055. yaml_parser_state_t *start;
  1056. /** The end of the stack. */
  1057. yaml_parser_state_t *end;
  1058. /** The top of the stack. */
  1059. yaml_parser_state_t *top;
  1060. } states;
  1061. /** The current parser state. */
  1062. yaml_parser_state_t state;
  1063. /** The stack of marks. */
  1064. struct {
  1065. /** The beginning of the stack. */
  1066. yaml_mark_t *start;
  1067. /** The end of the stack. */
  1068. yaml_mark_t *end;
  1069. /** The top of the stack. */
  1070. yaml_mark_t *top;
  1071. } marks;
  1072. /** The list of TAG directives. */
  1073. struct {
  1074. /** The beginning of the list. */
  1075. yaml_tag_directive_t *start;
  1076. /** The end of the list. */
  1077. yaml_tag_directive_t *end;
  1078. /** The top of the list. */
  1079. yaml_tag_directive_t *top;
  1080. } tag_directives;
  1081. /**
  1082. * @}
  1083. */
  1084. /**
  1085. * @name Dumper stuff
  1086. * @{
  1087. */
  1088. /** The alias data. */
  1089. struct {
  1090. /** The beginning of the list. */
  1091. yaml_alias_data_t *start;
  1092. /** The end of the list. */
  1093. yaml_alias_data_t *end;
  1094. /** The top of the list. */
  1095. yaml_alias_data_t *top;
  1096. } aliases;
  1097. /** The currently parsed document. */
  1098. yaml_document_t *document;
  1099. /**
  1100. * @}
  1101. */
  1102. } yaml_parser_t;
  1103. /**
  1104. * Initialize a parser.
  1105. *
  1106. * This function creates a new parser object. An application is responsible
  1107. * for destroying the object using the yaml_parser_delete() function.
  1108. *
  1109. * @param[out] parser An empty parser object.
  1110. *
  1111. * @returns @c 1 if the function succeeded, @c 0 on error.
  1112. */
  1113. YAML_DECLARE(int)
  1114. yaml_parser_initialize(yaml_parser_t *parser);
  1115. /**
  1116. * Destroy a parser.
  1117. *
  1118. * @param[in,out] parser A parser object.
  1119. */
  1120. YAML_DECLARE(void)
  1121. yaml_parser_delete(yaml_parser_t *parser);
  1122. /**
  1123. * Set a string input.
  1124. *
  1125. * Note that the @a input pointer must be valid while the @a parser object
  1126. * exists. The application is responsible for destroing @a input after
  1127. * destroying the @a parser.
  1128. *
  1129. * @param[in,out] parser A parser object.
  1130. * @param[in] input A source data.
  1131. * @param[in] size The length of the source data in bytes.
  1132. */
  1133. YAML_DECLARE(void)
  1134. yaml_parser_set_input_string(yaml_parser_t *parser,
  1135. const unsigned char *input, size_t size);
  1136. /**
  1137. * Set a file input.
  1138. *
  1139. * @a file should be a file object open for reading. The application is
  1140. * responsible for closing the @a file.
  1141. *
  1142. * @param[in,out] parser A parser object.
  1143. * @param[in] file An open file.
  1144. */
  1145. YAML_DECLARE(void)
  1146. yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file);
  1147. /**
  1148. * Set a generic input handler.
  1149. *
  1150. * @param[in,out] parser A parser object.
  1151. * @param[in] handler A read handler.
  1152. * @param[in] data Any application data for passing to the read
  1153. * handler.
  1154. */
  1155. YAML_DECLARE(void)
  1156. yaml_parser_set_input(yaml_parser_t *parser,
  1157. yaml_read_handler_t *handler, void *data);
  1158. /**
  1159. * Set the source encoding.
  1160. *
  1161. * @param[in,out] parser A parser object.
  1162. * @param[in] encoding The source encoding.
  1163. */
  1164. YAML_DECLARE(void)
  1165. yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding);
  1166. /**
  1167. * Scan the input stream and produce the next token.
  1168. *
  1169. * Call the function subsequently to produce a sequence of tokens corresponding
  1170. * to the input stream. The initial token has the type
  1171. * @c YAML_STREAM_START_TOKEN while the ending token has the type
  1172. * @c YAML_STREAM_END_TOKEN.
  1173. *
  1174. * An application is responsible for freeing any buffers associated with the
  1175. * produced token object using the @c yaml_token_delete function.
  1176. *
  1177. * An application must not alternate the calls of yaml_parser_scan() with the
  1178. * calls of yaml_parser_parse() or yaml_parser_load(). Doing this will break
  1179. * the parser.
  1180. *
  1181. * @param[in,out] parser A parser object.
  1182. * @param[out] token An empty token object.
  1183. *
  1184. * @returns @c 1 if the function succeeded, @c 0 on error.
  1185. */
  1186. YAML_DECLARE(int)
  1187. yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
  1188. /**
  1189. * Parse the input stream and produce the next parsing event.
  1190. *
  1191. * Call the function subsequently to produce a sequence of events corresponding
  1192. * to the input stream. The initial event has the type
  1193. * @c YAML_STREAM_START_EVENT while the ending event has the type
  1194. * @c YAML_STREAM_END_EVENT.
  1195. *
  1196. * An application is responsible for freeing any buffers associated with the
  1197. * produced event object using the yaml_event_delete() function.
  1198. *
  1199. * An application must not alternate the calls of yaml_parser_parse() with the
  1200. * calls of yaml_parser_scan() or yaml_parser_load(). Doing this will break the
  1201. * parser.
  1202. *
  1203. * @param[in,out] parser A parser object.
  1204. * @param[out] event An empty event object.
  1205. *
  1206. * @returns @c 1 if the function succeeded, @c 0 on error.
  1207. */
  1208. YAML_DECLARE(int)
  1209. yaml_parser_parse(yaml_parser_t *parser, yaml_event_t *event);
  1210. /**
  1211. * Parse the input stream and produce the next YAML document.
  1212. *
  1213. * Call this function subsequently to produce a sequence of documents
  1214. * constituting the input stream.
  1215. *
  1216. * If the produced document has no root node, it means that the document
  1217. * end has been reached.
  1218. *
  1219. * An application is responsible for freeing any data associated with the
  1220. * produced document object using the yaml_document_delete() function.
  1221. *
  1222. * An application must not alternate the calls of yaml_parser_load() with the
  1223. * calls of yaml_parser_scan() or yaml_parser_parse(). Doing this will break
  1224. * the parser.
  1225. *
  1226. * @param[in,out] parser A parser object.
  1227. * @param[out] document An empty document object.
  1228. *
  1229. * @returns @c 1 if the function succeeded, @c 0 on error.
  1230. */
  1231. YAML_DECLARE(int)
  1232. yaml_parser_load(yaml_parser_t *parser, yaml_document_t *document);
  1233. /** @} */
  1234. /**
  1235. * @defgroup emitter Emitter Definitions
  1236. * @{
  1237. */
  1238. /**
  1239. * The prototype of a write handler.
  1240. *
  1241. * The write handler is called when the emitter needs to flush the accumulated
  1242. * characters to the output. The handler should write @a size bytes of the
  1243. * @a buffer to the output.
  1244. *
  1245. * @param[in,out] data A pointer to an application data specified by
  1246. * yaml_emitter_set_output().
  1247. * @param[in] buffer The buffer with bytes to be written.
  1248. * @param[in] size The size of the buffer.
  1249. *
  1250. * @returns On success, the handler should return @c 1. If the handler failed,
  1251. * the returned value should be @c 0.
  1252. */
  1253. typedef int yaml_write_handler_t(void *data, unsigned char *buffer, size_t size);
  1254. /** The emitter states. */
  1255. typedef enum yaml_emitter_state_e {
  1256. /** Expect STREAM-START. */
  1257. YAML_EMIT_STREAM_START_STATE,
  1258. /** Expect the first DOCUMENT-START or STREAM-END. */
  1259. YAML_EMIT_FIRST_DOCUMENT_START_STATE,
  1260. /** Expect DOCUMENT-START or STREAM-END. */
  1261. YAML_EMIT_DOCUMENT_START_STATE,
  1262. /** Expect the content of a document. */
  1263. YAML_EMIT_DOCUMENT_CONTENT_STATE,
  1264. /** Expect DOCUMENT-END. */
  1265. YAML_EMIT_DOCUMENT_END_STATE,
  1266. /** Expect the first item of a flow sequence. */
  1267. YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE,
  1268. /** Expect an item of a flow sequence. */
  1269. YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE,
  1270. /** Expect the first key of a flow mapping. */
  1271. YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE,
  1272. /** Expect a key of a flow mapping. */
  1273. YAML_EMIT_FLOW_MAPPING_KEY_STATE,
  1274. /** Expect a value for a simple key of a flow mapping. */
  1275. YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE,
  1276. /** Expect a value of a flow mapping. */
  1277. YAML_EMIT_FLOW_MAPPING_VALUE_STATE,
  1278. /** Expect the first item of a block sequence. */
  1279. YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE,
  1280. /** Expect an item of a block sequence. */
  1281. YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE,
  1282. /** Expect the first key of a block mapping. */
  1283. YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE,
  1284. /** Expect the key of a block mapping. */
  1285. YAML_EMIT_BLOCK_MAPPING_KEY_STATE,
  1286. /** Expect a value for a simple key of a block mapping. */
  1287. YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE,
  1288. /** Expect a value of a block mapping. */
  1289. YAML_EMIT_BLOCK_MAPPING_VALUE_STATE,
  1290. /** Expect nothing. */
  1291. YAML_EMIT_END_STATE
  1292. } yaml_emitter_state_t;
  1293. /* This is needed for C++ */
  1294. typedef struct yaml_anchors_s {
  1295. /** The number of references. */
  1296. int references;
  1297. /** The anchor id. */
  1298. int anchor;
  1299. /** If the node has been emitted? */
  1300. int serialized;
  1301. } yaml_anchors_t;
  1302. /**
  1303. * The emitter structure.
  1304. *
  1305. * All members are internal. Manage the structure using the @c yaml_emitter_
  1306. * family of functions.
  1307. */
  1308. typedef struct yaml_emitter_s {
  1309. /**
  1310. * @name Error handling
  1311. * @{
  1312. */
  1313. /** Error type. */
  1314. yaml_error_type_t error;
  1315. /** Error description. */
  1316. const char *problem;
  1317. /**
  1318. * @}
  1319. */
  1320. /**
  1321. * @name Writer stuff
  1322. * @{
  1323. */
  1324. /** Write handler. */
  1325. yaml_write_handler_t *write_handler;
  1326. /** A pointer for passing to the write handler. */
  1327. void *write_handler_data;
  1328. /** Standard (string or file) output data. */
  1329. union {
  1330. /** String output data. */
  1331. struct {
  1332. /** The buffer pointer. */
  1333. unsigned char *buffer;
  1334. /** The buffer size. */
  1335. size_t size;
  1336. /** The number of written bytes. */
  1337. size_t *size_written;
  1338. } string;
  1339. /** File output data. */
  1340. FILE *file;
  1341. } output;
  1342. /** The working buffer. */
  1343. struct {
  1344. /** The beginning of the buffer. */
  1345. yaml_char_t *start;
  1346. /** The end of the buffer. */
  1347. yaml_char_t *end;
  1348. /** The current position of the buffer. */
  1349. yaml_char_t *pointer;
  1350. /** The last filled position of the buffer. */
  1351. yaml_char_t *last;
  1352. } buffer;
  1353. /** The raw buffer. */
  1354. struct {
  1355. /** The beginning of the buffer. */
  1356. unsigned char *start;
  1357. /** The end of the buffer. */
  1358. unsigned char *end;
  1359. /** The current position of the buffer. */
  1360. unsigned char *pointer;
  1361. /** The last filled position of the buffer. */
  1362. unsigned char *last;
  1363. } raw_buffer;
  1364. /** The stream encoding. */
  1365. yaml_encoding_t encoding;
  1366. /**
  1367. * @}
  1368. */
  1369. /**
  1370. * @name Emitter stuff
  1371. * @{
  1372. */
  1373. /** If the output is in the canonical style? */
  1374. int canonical;
  1375. /** The number of indentation spaces. */
  1376. int best_indent;
  1377. /** The preferred width of the output lines. */
  1378. int best_width;
  1379. /** Allow unescaped non-ASCII characters? */
  1380. int unicode;
  1381. /** The preferred line break. */
  1382. yaml_break_t line_break;
  1383. /** The stack of states. */
  1384. struct {
  1385. /** The beginning of the stack. */
  1386. yaml_emitter_state_t *start;
  1387. /** The end of the stack. */
  1388. yaml_emitter_state_t *end;
  1389. /** The top of the stack. */
  1390. yaml_emitter_state_t *top;
  1391. } states;
  1392. /** The current emitter state. */
  1393. yaml_emitter_state_t state;
  1394. /** The event queue. */
  1395. struct {
  1396. /** The beginning of the event queue. */
  1397. yaml_event_t *start;
  1398. /** The end of the event queue. */
  1399. yaml_event_t *end;
  1400. /** The head of the event queue. */
  1401. yaml_event_t *head;
  1402. /** The tail of the event queue. */
  1403. yaml_event_t *tail;
  1404. } events;
  1405. /** The stack of indentation levels. */
  1406. struct {
  1407. /** The beginning of the stack. */
  1408. int *start;
  1409. /** The end of the stack. */
  1410. int *end;
  1411. /** The top of the stack. */
  1412. int *top;
  1413. } indents;
  1414. /** The list of tag directives. */
  1415. struct {
  1416. /** The beginning of the list. */
  1417. yaml_tag_directive_t *start;
  1418. /** The end of the list. */
  1419. yaml_tag_directive_t *end;
  1420. /** The top of the list. */
  1421. yaml_tag_directive_t *top;
  1422. } tag_directives;
  1423. /** The current indentation level. */
  1424. int indent;
  1425. /** The current flow level. */
  1426. int flow_level;
  1427. /** Is it the document root context? */
  1428. int root_context;
  1429. /** Is it a sequence context? */
  1430. int sequence_context;
  1431. /** Is it a mapping context? */
  1432. int mapping_context;
  1433. /** Is it a simple mapping key context? */
  1434. int simple_key_context;
  1435. /** The current line. */
  1436. int line;
  1437. /** The current column. */
  1438. int column;
  1439. /** If the last character was a whitespace? */
  1440. int whitespace;
  1441. /** If the last character was an indentation character (' ', '-', '?', ':')? */
  1442. int indention;
  1443. /** If an explicit document end is required? */
  1444. int open_ended;
  1445. /** Anchor analysis. */
  1446. struct {
  1447. /** The anchor value. */
  1448. yaml_char_t *anchor;
  1449. /** The anchor length. */
  1450. size_t anchor_length;
  1451. /** Is it an alias? */
  1452. int alias;
  1453. } anchor_data;
  1454. /** Tag analysis. */
  1455. struct {
  1456. /** The tag handle. */
  1457. yaml_char_t *handle;
  1458. /** The tag handle length. */
  1459. size_t handle_length;
  1460. /** The tag suffix. */
  1461. yaml_char_t *suffix;
  1462. /** The tag suffix length. */
  1463. size_t suffix_length;
  1464. } tag_data;
  1465. /** Scalar analysis. */
  1466. struct {
  1467. /** The scalar value. */
  1468. yaml_char_t *value;
  1469. /** The scalar length. */
  1470. size_t length;
  1471. /** Does the scalar contain line breaks? */
  1472. int multiline;
  1473. /** Can the scalar be expessed in the flow plain style? */
  1474. int flow_plain_allowed;
  1475. /** Can the scalar be expressed in the block plain style? */
  1476. int block_plain_allowed;
  1477. /** Can the scalar be expressed in the single quoted style? */
  1478. int single_quoted_allowed;
  1479. /** Can the scalar be expressed in the literal or folded styles? */
  1480. int block_allowed;
  1481. /** The output style. */
  1482. yaml_scalar_style_t style;
  1483. } scalar_data;
  1484. /**
  1485. * @}
  1486. */
  1487. /**
  1488. * @name Dumper stuff
  1489. * @{
  1490. */
  1491. /** If the stream was already opened? */
  1492. int opened;
  1493. /** If the stream was already closed? */
  1494. int closed;
  1495. /** The information associated with the document nodes. */
  1496. yaml_anchors_t *anchors;
  1497. /** The last assigned anchor id. */
  1498. int last_anchor_id;
  1499. /** The currently emitted document. */
  1500. yaml_document_t *document;
  1501. /**
  1502. * @}
  1503. */
  1504. } yaml_emitter_t;
  1505. /**
  1506. * Initialize an emitter.
  1507. *
  1508. * This function creates a new emitter object. An application is responsible
  1509. * for destroying the object using the yaml_emitter_delete() function.
  1510. *
  1511. * @param[out] emitter An empty parser object.
  1512. *
  1513. * @returns @c 1 if the function succeeded, @c 0 on error.
  1514. */
  1515. YAML_DECLARE(int)
  1516. yaml_emitter_initialize(yaml_emitter_t *emitter);
  1517. /**
  1518. * Destroy an emitter.
  1519. *
  1520. * @param[in,out] emitter An emitter object.
  1521. */
  1522. YAML_DECLARE(void)
  1523. yaml_emitter_delete(yaml_emitter_t *emitter);
  1524. /**
  1525. * Set a string output.
  1526. *
  1527. * The emitter will write the output characters to the @a output buffer of the
  1528. * size @a size. The emitter will set @a size_written to the number of written
  1529. * bytes. If the buffer is smaller than required, the emitter produces the
  1530. * YAML_WRITE_ERROR error.
  1531. *
  1532. * @param[in,out] emitter An emitter object.
  1533. * @param[in] output An output buffer.
  1534. * @param[in] size The buffer size.
  1535. * @param[in] size_written The pointer to save the number of written
  1536. * bytes.
  1537. */
  1538. YAML_DECLARE(void)
  1539. yaml_emitter_set_output_string(yaml_emitter_t *emitter,
  1540. unsigned char *output, size_t size, size_t *size_written);
  1541. /**
  1542. * Set a file output.
  1543. *
  1544. * @a file should be a file object open for writing. The application is
  1545. * responsible for closing the @a file.
  1546. *
  1547. * @param[in,out] emitter An emitter object.
  1548. * @param[in] file An open file.
  1549. */
  1550. YAML_DECLARE(void)
  1551. yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file);
  1552. /**
  1553. * Set a generic output handler.
  1554. *
  1555. * @param[in,out] emitter An emitter object.
  1556. * @param[in] handler A write handler.
  1557. * @param[in] data Any application data for passing to the write
  1558. * handler.
  1559. */
  1560. YAML_DECLARE(void)
  1561. yaml_emitter_set_output(yaml_emitter_t *emitter,
  1562. yaml_write_handler_t *handler, void *data);
  1563. /**
  1564. * Set the output encoding.
  1565. *
  1566. * @param[in,out] emitter An emitter object.
  1567. * @param[in] encoding The output encoding.
  1568. */
  1569. YAML_DECLARE(void)
  1570. yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding);
  1571. /**
  1572. * Set if the output should be in the "canonical" format as in the YAML
  1573. * specification.
  1574. *
  1575. * @param[in,out] emitter An emitter object.
  1576. * @param[in] canonical If the output is canonical.
  1577. */
  1578. YAML_DECLARE(void)
  1579. yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical);
  1580. /**
  1581. * Set the indentation increment.
  1582. *
  1583. * @param[in,out] emitter An emitter object.
  1584. * @param[in] indent The indentation increment (1 < . < 10).
  1585. */
  1586. YAML_DECLARE(void)
  1587. yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent);
  1588. /**
  1589. * Set the preferred line width. @c -1 means unlimited.
  1590. *
  1591. * @param[in,out] emitter An emitter object.
  1592. * @param[in] width The preferred line width.
  1593. */
  1594. YAML_DECLARE(void)
  1595. yaml_emitter_set_width(yaml_emitter_t *emitter, int width);
  1596. /**
  1597. * Set if unescaped non-ASCII characters are allowed.
  1598. *
  1599. * @param[in,out] emitter An emitter object.
  1600. * @param[in] unicode If unescaped Unicode characters are allowed.
  1601. */
  1602. YAML_DECLARE(void)
  1603. yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode);
  1604. /**
  1605. * Set the preferred line break.
  1606. *
  1607. * @param[in,out] emitter An emitter object.
  1608. * @param[in] line_break The preferred line break.
  1609. */
  1610. YAML_DECLARE(void)
  1611. yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break);
  1612. /**
  1613. * Emit an event.
  1614. *
  1615. * The event object may be generated using the yaml_parser_parse() function.
  1616. * The emitter takes the responsibility for the event object and destroys its
  1617. * content after it is emitted. The event object is destroyed even if the
  1618. * function fails.
  1619. *
  1620. * @param[in,out] emitter An emitter object.
  1621. * @param[in,out] event An event object.
  1622. *
  1623. * @returns @c 1 if the function succeeded, @c 0 on error.
  1624. */
  1625. YAML_DECLARE(int)
  1626. yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event);
  1627. /**
  1628. * Start a YAML stream.
  1629. *
  1630. * This function should be used before yaml_emitter_dump() is called.
  1631. *
  1632. * @param[in,out] emitter An emitter object.
  1633. *
  1634. * @returns @c 1 if the function succeeded, @c 0 on error.
  1635. */
  1636. YAML_DECLARE(int)
  1637. yaml_emitter_open(yaml_emitter_t *emitter);
  1638. /**
  1639. * Finish a YAML stream.
  1640. *
  1641. * This function should be used after yaml_emitter_dump() is called.
  1642. *
  1643. * @param[in,out] emitter An emitter object.
  1644. *
  1645. * @returns @c 1 if the function succeeded, @c 0 on error.
  1646. */
  1647. YAML_DECLARE(int)
  1648. yaml_emitter_close(yaml_emitter_t *emitter);
  1649. /**
  1650. * Emit a YAML document.
  1651. *
  1652. * The documen object may be generated using the yaml_parser_load() function
  1653. * or the yaml_document_initialize() function. The emitter takes the
  1654. * responsibility for the document object and destroys its content after
  1655. * it is emitted. The document object is destroyed even if the function fails.
  1656. *
  1657. * @param[in,out] emitter An emitter object.
  1658. * @param[in,out] document A document object.
  1659. *
  1660. * @returns @c 1 if the function succeeded, @c 0 on error.
  1661. */
  1662. YAML_DECLARE(int)
  1663. yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document);
  1664. /**
  1665. * Flush the accumulated characters to the output.
  1666. *
  1667. * @param[in,out] emitter An emitter object.
  1668. *
  1669. * @returns @c 1 if the function succeeded, @c 0 on error.
  1670. */
  1671. YAML_DECLARE(int)
  1672. yaml_emitter_flush(yaml_emitter_t *emitter);
  1673. /** @} */
  1674. #ifdef __cplusplus
  1675. }
  1676. #endif
  1677. #endif /* #ifndef YAML_H */