yaml.h 53 KB

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