yamlh.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. //
  2. // Copyright (c) 2011-2019 Canonical Ltd
  3. // Copyright (c) 2006-2010 Kirill Simonov
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. // of the Software, and to permit persons to whom the Software is furnished to do
  10. // so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. package yaml
  23. import (
  24. "fmt"
  25. "io"
  26. )
  27. // The version directive data.
  28. type yaml_version_directive_t struct {
  29. major int8 // The major version number.
  30. minor int8 // The minor version number.
  31. }
  32. // The tag directive data.
  33. type yaml_tag_directive_t struct {
  34. handle []byte // The tag handle.
  35. prefix []byte // The tag prefix.
  36. }
  37. type yaml_encoding_t int
  38. // The stream encoding.
  39. const (
  40. // Let the parser choose the encoding.
  41. yaml_ANY_ENCODING yaml_encoding_t = iota
  42. yaml_UTF8_ENCODING // The default UTF-8 encoding.
  43. yaml_UTF16LE_ENCODING // The UTF-16-LE encoding with BOM.
  44. yaml_UTF16BE_ENCODING // The UTF-16-BE encoding with BOM.
  45. )
  46. type yaml_break_t int
  47. // Line break types.
  48. const (
  49. // Let the parser choose the break type.
  50. yaml_ANY_BREAK yaml_break_t = iota
  51. yaml_CR_BREAK // Use CR for line breaks (Mac style).
  52. yaml_LN_BREAK // Use LN for line breaks (Unix style).
  53. yaml_CRLN_BREAK // Use CR LN for line breaks (DOS style).
  54. )
  55. type yaml_error_type_t int
  56. // Many bad things could happen with the parser and emitter.
  57. const (
  58. // No error is produced.
  59. yaml_NO_ERROR yaml_error_type_t = iota
  60. yaml_MEMORY_ERROR // Cannot allocate or reallocate a block of memory.
  61. yaml_READER_ERROR // Cannot read or decode the input stream.
  62. yaml_SCANNER_ERROR // Cannot scan the input stream.
  63. yaml_PARSER_ERROR // Cannot parse the input stream.
  64. yaml_COMPOSER_ERROR // Cannot compose a YAML document.
  65. yaml_WRITER_ERROR // Cannot write to the output stream.
  66. yaml_EMITTER_ERROR // Cannot emit a YAML stream.
  67. )
  68. // The pointer position.
  69. type yaml_mark_t struct {
  70. index int // The position index.
  71. line int // The position line.
  72. column int // The position column.
  73. }
  74. // Node Styles
  75. type yaml_style_t int8
  76. type yaml_scalar_style_t yaml_style_t
  77. // Scalar styles.
  78. const (
  79. // Let the emitter choose the style.
  80. yaml_ANY_SCALAR_STYLE yaml_scalar_style_t = 0
  81. yaml_PLAIN_SCALAR_STYLE yaml_scalar_style_t = 1 << iota // The plain scalar style.
  82. yaml_SINGLE_QUOTED_SCALAR_STYLE // The single-quoted scalar style.
  83. yaml_DOUBLE_QUOTED_SCALAR_STYLE // The double-quoted scalar style.
  84. yaml_LITERAL_SCALAR_STYLE // The literal scalar style.
  85. yaml_FOLDED_SCALAR_STYLE // The folded scalar style.
  86. )
  87. type yaml_sequence_style_t yaml_style_t
  88. // Sequence styles.
  89. const (
  90. // Let the emitter choose the style.
  91. yaml_ANY_SEQUENCE_STYLE yaml_sequence_style_t = iota
  92. yaml_BLOCK_SEQUENCE_STYLE // The block sequence style.
  93. yaml_FLOW_SEQUENCE_STYLE // The flow sequence style.
  94. )
  95. type yaml_mapping_style_t yaml_style_t
  96. // Mapping styles.
  97. const (
  98. // Let the emitter choose the style.
  99. yaml_ANY_MAPPING_STYLE yaml_mapping_style_t = iota
  100. yaml_BLOCK_MAPPING_STYLE // The block mapping style.
  101. yaml_FLOW_MAPPING_STYLE // The flow mapping style.
  102. )
  103. // Tokens
  104. type yaml_token_type_t int
  105. // Token types.
  106. const (
  107. // An empty token.
  108. yaml_NO_TOKEN yaml_token_type_t = iota
  109. yaml_STREAM_START_TOKEN // A STREAM-START token.
  110. yaml_STREAM_END_TOKEN // A STREAM-END token.
  111. yaml_VERSION_DIRECTIVE_TOKEN // A VERSION-DIRECTIVE token.
  112. yaml_TAG_DIRECTIVE_TOKEN // A TAG-DIRECTIVE token.
  113. yaml_DOCUMENT_START_TOKEN // A DOCUMENT-START token.
  114. yaml_DOCUMENT_END_TOKEN // A DOCUMENT-END token.
  115. yaml_BLOCK_SEQUENCE_START_TOKEN // A BLOCK-SEQUENCE-START token.
  116. yaml_BLOCK_MAPPING_START_TOKEN // A BLOCK-SEQUENCE-END token.
  117. yaml_BLOCK_END_TOKEN // A BLOCK-END token.
  118. yaml_FLOW_SEQUENCE_START_TOKEN // A FLOW-SEQUENCE-START token.
  119. yaml_FLOW_SEQUENCE_END_TOKEN // A FLOW-SEQUENCE-END token.
  120. yaml_FLOW_MAPPING_START_TOKEN // A FLOW-MAPPING-START token.
  121. yaml_FLOW_MAPPING_END_TOKEN // A FLOW-MAPPING-END token.
  122. yaml_BLOCK_ENTRY_TOKEN // A BLOCK-ENTRY token.
  123. yaml_FLOW_ENTRY_TOKEN // A FLOW-ENTRY token.
  124. yaml_KEY_TOKEN // A KEY token.
  125. yaml_VALUE_TOKEN // A VALUE token.
  126. yaml_ALIAS_TOKEN // An ALIAS token.
  127. yaml_ANCHOR_TOKEN // An ANCHOR token.
  128. yaml_TAG_TOKEN // A TAG token.
  129. yaml_SCALAR_TOKEN // A SCALAR token.
  130. )
  131. func (tt yaml_token_type_t) String() string {
  132. switch tt {
  133. case yaml_NO_TOKEN:
  134. return "yaml_NO_TOKEN"
  135. case yaml_STREAM_START_TOKEN:
  136. return "yaml_STREAM_START_TOKEN"
  137. case yaml_STREAM_END_TOKEN:
  138. return "yaml_STREAM_END_TOKEN"
  139. case yaml_VERSION_DIRECTIVE_TOKEN:
  140. return "yaml_VERSION_DIRECTIVE_TOKEN"
  141. case yaml_TAG_DIRECTIVE_TOKEN:
  142. return "yaml_TAG_DIRECTIVE_TOKEN"
  143. case yaml_DOCUMENT_START_TOKEN:
  144. return "yaml_DOCUMENT_START_TOKEN"
  145. case yaml_DOCUMENT_END_TOKEN:
  146. return "yaml_DOCUMENT_END_TOKEN"
  147. case yaml_BLOCK_SEQUENCE_START_TOKEN:
  148. return "yaml_BLOCK_SEQUENCE_START_TOKEN"
  149. case yaml_BLOCK_MAPPING_START_TOKEN:
  150. return "yaml_BLOCK_MAPPING_START_TOKEN"
  151. case yaml_BLOCK_END_TOKEN:
  152. return "yaml_BLOCK_END_TOKEN"
  153. case yaml_FLOW_SEQUENCE_START_TOKEN:
  154. return "yaml_FLOW_SEQUENCE_START_TOKEN"
  155. case yaml_FLOW_SEQUENCE_END_TOKEN:
  156. return "yaml_FLOW_SEQUENCE_END_TOKEN"
  157. case yaml_FLOW_MAPPING_START_TOKEN:
  158. return "yaml_FLOW_MAPPING_START_TOKEN"
  159. case yaml_FLOW_MAPPING_END_TOKEN:
  160. return "yaml_FLOW_MAPPING_END_TOKEN"
  161. case yaml_BLOCK_ENTRY_TOKEN:
  162. return "yaml_BLOCK_ENTRY_TOKEN"
  163. case yaml_FLOW_ENTRY_TOKEN:
  164. return "yaml_FLOW_ENTRY_TOKEN"
  165. case yaml_KEY_TOKEN:
  166. return "yaml_KEY_TOKEN"
  167. case yaml_VALUE_TOKEN:
  168. return "yaml_VALUE_TOKEN"
  169. case yaml_ALIAS_TOKEN:
  170. return "yaml_ALIAS_TOKEN"
  171. case yaml_ANCHOR_TOKEN:
  172. return "yaml_ANCHOR_TOKEN"
  173. case yaml_TAG_TOKEN:
  174. return "yaml_TAG_TOKEN"
  175. case yaml_SCALAR_TOKEN:
  176. return "yaml_SCALAR_TOKEN"
  177. }
  178. return "<unknown token>"
  179. }
  180. // The token structure.
  181. type yaml_token_t struct {
  182. // The token type.
  183. typ yaml_token_type_t
  184. // The start/end of the token.
  185. start_mark, end_mark yaml_mark_t
  186. // The stream encoding (for yaml_STREAM_START_TOKEN).
  187. encoding yaml_encoding_t
  188. // The alias/anchor/scalar value or tag/tag directive handle
  189. // (for yaml_ALIAS_TOKEN, yaml_ANCHOR_TOKEN, yaml_SCALAR_TOKEN, yaml_TAG_TOKEN, yaml_TAG_DIRECTIVE_TOKEN).
  190. value []byte
  191. // The tag suffix (for yaml_TAG_TOKEN).
  192. suffix []byte
  193. // The tag directive prefix (for yaml_TAG_DIRECTIVE_TOKEN).
  194. prefix []byte
  195. // The scalar style (for yaml_SCALAR_TOKEN).
  196. style yaml_scalar_style_t
  197. // The version directive major/minor (for yaml_VERSION_DIRECTIVE_TOKEN).
  198. major, minor int8
  199. }
  200. // Events
  201. type yaml_event_type_t int8
  202. // Event types.
  203. const (
  204. // An empty event.
  205. yaml_NO_EVENT yaml_event_type_t = iota
  206. yaml_STREAM_START_EVENT // A STREAM-START event.
  207. yaml_STREAM_END_EVENT // A STREAM-END event.
  208. yaml_DOCUMENT_START_EVENT // A DOCUMENT-START event.
  209. yaml_DOCUMENT_END_EVENT // A DOCUMENT-END event.
  210. yaml_ALIAS_EVENT // An ALIAS event.
  211. yaml_SCALAR_EVENT // A SCALAR event.
  212. yaml_SEQUENCE_START_EVENT // A SEQUENCE-START event.
  213. yaml_SEQUENCE_END_EVENT // A SEQUENCE-END event.
  214. yaml_MAPPING_START_EVENT // A MAPPING-START event.
  215. yaml_MAPPING_END_EVENT // A MAPPING-END event.
  216. yaml_TAIL_COMMENT_EVENT
  217. )
  218. var eventStrings = []string{
  219. yaml_NO_EVENT: "none",
  220. yaml_STREAM_START_EVENT: "stream start",
  221. yaml_STREAM_END_EVENT: "stream end",
  222. yaml_DOCUMENT_START_EVENT: "document start",
  223. yaml_DOCUMENT_END_EVENT: "document end",
  224. yaml_ALIAS_EVENT: "alias",
  225. yaml_SCALAR_EVENT: "scalar",
  226. yaml_SEQUENCE_START_EVENT: "sequence start",
  227. yaml_SEQUENCE_END_EVENT: "sequence end",
  228. yaml_MAPPING_START_EVENT: "mapping start",
  229. yaml_MAPPING_END_EVENT: "mapping end",
  230. yaml_TAIL_COMMENT_EVENT: "tail comment",
  231. }
  232. func (e yaml_event_type_t) String() string {
  233. if e < 0 || int(e) >= len(eventStrings) {
  234. return fmt.Sprintf("unknown event %d", e)
  235. }
  236. return eventStrings[e]
  237. }
  238. // The event structure.
  239. type yaml_event_t struct {
  240. // The event type.
  241. typ yaml_event_type_t
  242. // The start and end of the event.
  243. start_mark, end_mark yaml_mark_t
  244. // The document encoding (for yaml_STREAM_START_EVENT).
  245. encoding yaml_encoding_t
  246. // The version directive (for yaml_DOCUMENT_START_EVENT).
  247. version_directive *yaml_version_directive_t
  248. // The list of tag directives (for yaml_DOCUMENT_START_EVENT).
  249. tag_directives []yaml_tag_directive_t
  250. // The comments
  251. head_comment []byte
  252. line_comment []byte
  253. foot_comment []byte
  254. tail_comment []byte
  255. // The anchor (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_ALIAS_EVENT).
  256. anchor []byte
  257. // The tag (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT).
  258. tag []byte
  259. // The scalar value (for yaml_SCALAR_EVENT).
  260. value []byte
  261. // Is the document start/end indicator implicit, or the tag optional?
  262. // (for yaml_DOCUMENT_START_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_SCALAR_EVENT).
  263. implicit bool
  264. // Is the tag optional for any non-plain style? (for yaml_SCALAR_EVENT).
  265. quoted_implicit bool
  266. // The style (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT).
  267. style yaml_style_t
  268. }
  269. func (e *yaml_event_t) scalar_style() yaml_scalar_style_t { return yaml_scalar_style_t(e.style) }
  270. func (e *yaml_event_t) sequence_style() yaml_sequence_style_t { return yaml_sequence_style_t(e.style) }
  271. func (e *yaml_event_t) mapping_style() yaml_mapping_style_t { return yaml_mapping_style_t(e.style) }
  272. // Nodes
  273. const (
  274. yaml_NULL_TAG = "tag:yaml.org,2002:null" // The tag !!null with the only possible value: null.
  275. yaml_BOOL_TAG = "tag:yaml.org,2002:bool" // The tag !!bool with the values: true and false.
  276. yaml_STR_TAG = "tag:yaml.org,2002:str" // The tag !!str for string values.
  277. yaml_INT_TAG = "tag:yaml.org,2002:int" // The tag !!int for integer values.
  278. yaml_FLOAT_TAG = "tag:yaml.org,2002:float" // The tag !!float for float values.
  279. yaml_TIMESTAMP_TAG = "tag:yaml.org,2002:timestamp" // The tag !!timestamp for date and time values.
  280. yaml_SEQ_TAG = "tag:yaml.org,2002:seq" // The tag !!seq is used to denote sequences.
  281. yaml_MAP_TAG = "tag:yaml.org,2002:map" // The tag !!map is used to denote mapping.
  282. // Not in original libyaml.
  283. yaml_BINARY_TAG = "tag:yaml.org,2002:binary"
  284. yaml_MERGE_TAG = "tag:yaml.org,2002:merge"
  285. yaml_DEFAULT_SCALAR_TAG = yaml_STR_TAG // The default scalar tag is !!str.
  286. yaml_DEFAULT_SEQUENCE_TAG = yaml_SEQ_TAG // The default sequence tag is !!seq.
  287. yaml_DEFAULT_MAPPING_TAG = yaml_MAP_TAG // The default mapping tag is !!map.
  288. )
  289. type yaml_node_type_t int
  290. // Node types.
  291. const (
  292. // An empty node.
  293. yaml_NO_NODE yaml_node_type_t = iota
  294. yaml_SCALAR_NODE // A scalar node.
  295. yaml_SEQUENCE_NODE // A sequence node.
  296. yaml_MAPPING_NODE // A mapping node.
  297. )
  298. // An element of a sequence node.
  299. type yaml_node_item_t int
  300. // An element of a mapping node.
  301. type yaml_node_pair_t struct {
  302. key int // The key of the element.
  303. value int // The value of the element.
  304. }
  305. // The node structure.
  306. type yaml_node_t struct {
  307. typ yaml_node_type_t // The node type.
  308. tag []byte // The node tag.
  309. // The node data.
  310. // The scalar parameters (for yaml_SCALAR_NODE).
  311. scalar struct {
  312. value []byte // The scalar value.
  313. length int // The length of the scalar value.
  314. style yaml_scalar_style_t // The scalar style.
  315. }
  316. // The sequence parameters (for YAML_SEQUENCE_NODE).
  317. sequence struct {
  318. items_data []yaml_node_item_t // The stack of sequence items.
  319. style yaml_sequence_style_t // The sequence style.
  320. }
  321. // The mapping parameters (for yaml_MAPPING_NODE).
  322. mapping struct {
  323. pairs_data []yaml_node_pair_t // The stack of mapping pairs (key, value).
  324. pairs_start *yaml_node_pair_t // The beginning of the stack.
  325. pairs_end *yaml_node_pair_t // The end of the stack.
  326. pairs_top *yaml_node_pair_t // The top of the stack.
  327. style yaml_mapping_style_t // The mapping style.
  328. }
  329. start_mark yaml_mark_t // The beginning of the node.
  330. end_mark yaml_mark_t // The end of the node.
  331. }
  332. // The document structure.
  333. type yaml_document_t struct {
  334. // The document nodes.
  335. nodes []yaml_node_t
  336. // The version directive.
  337. version_directive *yaml_version_directive_t
  338. // The list of tag directives.
  339. tag_directives_data []yaml_tag_directive_t
  340. tag_directives_start int // The beginning of the tag directives list.
  341. tag_directives_end int // The end of the tag directives list.
  342. start_implicit int // Is the document start indicator implicit?
  343. end_implicit int // Is the document end indicator implicit?
  344. // The start/end of the document.
  345. start_mark, end_mark yaml_mark_t
  346. }
  347. // The prototype of a read handler.
  348. //
  349. // The read handler is called when the parser needs to read more bytes from the
  350. // source. The handler should write not more than size bytes to the buffer.
  351. // The number of written bytes should be set to the size_read variable.
  352. //
  353. // [in,out] data A pointer to an application data specified by
  354. // yaml_parser_set_input().
  355. // [out] buffer The buffer to write the data from the source.
  356. // [in] size The size of the buffer.
  357. // [out] size_read The actual number of bytes read from the source.
  358. //
  359. // On success, the handler should return 1. If the handler failed,
  360. // the returned value should be 0. On EOF, the handler should set the
  361. // size_read to 0 and return 1.
  362. type yaml_read_handler_t func(parser *yaml_parser_t, buffer []byte) (n int, err error)
  363. // This structure holds information about a potential simple key.
  364. type yaml_simple_key_t struct {
  365. possible bool // Is a simple key possible?
  366. required bool // Is a simple key required?
  367. token_number int // The number of the token.
  368. mark yaml_mark_t // The position mark.
  369. }
  370. // The states of the parser.
  371. type yaml_parser_state_t int
  372. const (
  373. yaml_PARSE_STREAM_START_STATE yaml_parser_state_t = iota
  374. yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE // Expect the beginning of an implicit document.
  375. yaml_PARSE_DOCUMENT_START_STATE // Expect DOCUMENT-START.
  376. yaml_PARSE_DOCUMENT_CONTENT_STATE // Expect the content of a document.
  377. yaml_PARSE_DOCUMENT_END_STATE // Expect DOCUMENT-END.
  378. yaml_PARSE_BLOCK_NODE_STATE // Expect a block node.
  379. yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE // Expect a block node or indentless sequence.
  380. yaml_PARSE_FLOW_NODE_STATE // Expect a flow node.
  381. yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE // Expect the first entry of a block sequence.
  382. yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE // Expect an entry of a block sequence.
  383. yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE // Expect an entry of an indentless sequence.
  384. yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE // Expect the first key of a block mapping.
  385. yaml_PARSE_BLOCK_MAPPING_KEY_STATE // Expect a block mapping key.
  386. yaml_PARSE_BLOCK_MAPPING_VALUE_STATE // Expect a block mapping value.
  387. yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE // Expect the first entry of a flow sequence.
  388. yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE // Expect an entry of a flow sequence.
  389. yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE // Expect a key of an ordered mapping.
  390. yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE // Expect a value of an ordered mapping.
  391. yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE // Expect the and of an ordered mapping entry.
  392. yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE // Expect the first key of a flow mapping.
  393. yaml_PARSE_FLOW_MAPPING_KEY_STATE // Expect a key of a flow mapping.
  394. yaml_PARSE_FLOW_MAPPING_VALUE_STATE // Expect a value of a flow mapping.
  395. yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE // Expect an empty value of a flow mapping.
  396. yaml_PARSE_END_STATE // Expect nothing.
  397. )
  398. func (ps yaml_parser_state_t) String() string {
  399. switch ps {
  400. case yaml_PARSE_STREAM_START_STATE:
  401. return "yaml_PARSE_STREAM_START_STATE"
  402. case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE:
  403. return "yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE"
  404. case yaml_PARSE_DOCUMENT_START_STATE:
  405. return "yaml_PARSE_DOCUMENT_START_STATE"
  406. case yaml_PARSE_DOCUMENT_CONTENT_STATE:
  407. return "yaml_PARSE_DOCUMENT_CONTENT_STATE"
  408. case yaml_PARSE_DOCUMENT_END_STATE:
  409. return "yaml_PARSE_DOCUMENT_END_STATE"
  410. case yaml_PARSE_BLOCK_NODE_STATE:
  411. return "yaml_PARSE_BLOCK_NODE_STATE"
  412. case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE:
  413. return "yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE"
  414. case yaml_PARSE_FLOW_NODE_STATE:
  415. return "yaml_PARSE_FLOW_NODE_STATE"
  416. case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE:
  417. return "yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE"
  418. case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE:
  419. return "yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE"
  420. case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE:
  421. return "yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE"
  422. case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE:
  423. return "yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE"
  424. case yaml_PARSE_BLOCK_MAPPING_KEY_STATE:
  425. return "yaml_PARSE_BLOCK_MAPPING_KEY_STATE"
  426. case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE:
  427. return "yaml_PARSE_BLOCK_MAPPING_VALUE_STATE"
  428. case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE:
  429. return "yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE"
  430. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE:
  431. return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE"
  432. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE:
  433. return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE"
  434. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE:
  435. return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE"
  436. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE:
  437. return "yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE"
  438. case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE:
  439. return "yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE"
  440. case yaml_PARSE_FLOW_MAPPING_KEY_STATE:
  441. return "yaml_PARSE_FLOW_MAPPING_KEY_STATE"
  442. case yaml_PARSE_FLOW_MAPPING_VALUE_STATE:
  443. return "yaml_PARSE_FLOW_MAPPING_VALUE_STATE"
  444. case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE:
  445. return "yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE"
  446. case yaml_PARSE_END_STATE:
  447. return "yaml_PARSE_END_STATE"
  448. }
  449. return "<unknown parser state>"
  450. }
  451. // This structure holds aliases data.
  452. type yaml_alias_data_t struct {
  453. anchor []byte // The anchor.
  454. index int // The node id.
  455. mark yaml_mark_t // The anchor mark.
  456. }
  457. // The parser structure.
  458. //
  459. // All members are internal. Manage the structure using the
  460. // yaml_parser_ family of functions.
  461. type yaml_parser_t struct {
  462. // Error handling
  463. error yaml_error_type_t // Error type.
  464. problem string // Error description.
  465. // The byte about which the problem occurred.
  466. problem_offset int
  467. problem_value int
  468. problem_mark yaml_mark_t
  469. // The error context.
  470. context string
  471. context_mark yaml_mark_t
  472. // Reader stuff
  473. read_handler yaml_read_handler_t // Read handler.
  474. input_reader io.Reader // File input data.
  475. input []byte // String input data.
  476. input_pos int
  477. eof bool // EOF flag
  478. buffer []byte // The working buffer.
  479. buffer_pos int // The current position of the buffer.
  480. unread int // The number of unread characters in the buffer.
  481. newlines int // The number of line breaks since last non-break/non-blank character
  482. raw_buffer []byte // The raw buffer.
  483. raw_buffer_pos int // The current position of the buffer.
  484. encoding yaml_encoding_t // The input encoding.
  485. offset int // The offset of the current position (in bytes).
  486. mark yaml_mark_t // The mark of the current position.
  487. // Comments
  488. head_comment []byte // The current head comments
  489. line_comment []byte // The current line comments
  490. foot_comment []byte // The current foot comments
  491. tail_comment []byte // Foot comment that happens at the end of a block.
  492. stem_comment []byte // Comment in item preceding a nested structure (list inside list item, etc)
  493. comments []yaml_comment_t // The folded comments for all parsed tokens
  494. comments_head int
  495. // Scanner stuff
  496. stream_start_produced bool // Have we started to scan the input stream?
  497. stream_end_produced bool // Have we reached the end of the input stream?
  498. flow_level int // The number of unclosed '[' and '{' indicators.
  499. tokens []yaml_token_t // The tokens queue.
  500. tokens_head int // The head of the tokens queue.
  501. tokens_parsed int // The number of tokens fetched from the queue.
  502. token_available bool // Does the tokens queue contain a token ready for dequeueing.
  503. indent int // The current indentation level.
  504. indents []int // The indentation levels stack.
  505. simple_key_allowed bool // May a simple key occur at the current position?
  506. simple_keys []yaml_simple_key_t // The stack of simple keys.
  507. simple_keys_by_tok map[int]int // possible simple_key indexes indexed by token_number
  508. // Parser stuff
  509. state yaml_parser_state_t // The current parser state.
  510. states []yaml_parser_state_t // The parser states stack.
  511. marks []yaml_mark_t // The stack of marks.
  512. tag_directives []yaml_tag_directive_t // The list of TAG directives.
  513. // Dumper stuff
  514. aliases []yaml_alias_data_t // The alias data.
  515. document *yaml_document_t // The currently parsed document.
  516. }
  517. type yaml_comment_t struct {
  518. scan_mark yaml_mark_t // Position where scanning for comments started
  519. token_mark yaml_mark_t // Position after which tokens will be associated with this comment
  520. start_mark yaml_mark_t // Position of '#' comment mark
  521. end_mark yaml_mark_t // Position where comment terminated
  522. head []byte
  523. line []byte
  524. foot []byte
  525. }
  526. // Emitter Definitions
  527. // The prototype of a write handler.
  528. //
  529. // The write handler is called when the emitter needs to flush the accumulated
  530. // characters to the output. The handler should write @a size bytes of the
  531. // @a buffer to the output.
  532. //
  533. // @param[in,out] data A pointer to an application data specified by
  534. // yaml_emitter_set_output().
  535. // @param[in] buffer The buffer with bytes to be written.
  536. // @param[in] size The size of the buffer.
  537. //
  538. // @returns On success, the handler should return @c 1. If the handler failed,
  539. // the returned value should be @c 0.
  540. //
  541. type yaml_write_handler_t func(emitter *yaml_emitter_t, buffer []byte) error
  542. type yaml_emitter_state_t int
  543. // The emitter states.
  544. const (
  545. // Expect STREAM-START.
  546. yaml_EMIT_STREAM_START_STATE yaml_emitter_state_t = iota
  547. yaml_EMIT_FIRST_DOCUMENT_START_STATE // Expect the first DOCUMENT-START or STREAM-END.
  548. yaml_EMIT_DOCUMENT_START_STATE // Expect DOCUMENT-START or STREAM-END.
  549. yaml_EMIT_DOCUMENT_CONTENT_STATE // Expect the content of a document.
  550. yaml_EMIT_DOCUMENT_END_STATE // Expect DOCUMENT-END.
  551. yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE // Expect the first item of a flow sequence.
  552. yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE // Expect the next item of a flow sequence, with the comma already written out
  553. yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE // Expect an item of a flow sequence.
  554. yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE // Expect the first key of a flow mapping.
  555. yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE // Expect the next key of a flow mapping, with the comma already written out
  556. yaml_EMIT_FLOW_MAPPING_KEY_STATE // Expect a key of a flow mapping.
  557. yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE // Expect a value for a simple key of a flow mapping.
  558. yaml_EMIT_FLOW_MAPPING_VALUE_STATE // Expect a value of a flow mapping.
  559. yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE // Expect the first item of a block sequence.
  560. yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE // Expect an item of a block sequence.
  561. yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE // Expect the first key of a block mapping.
  562. yaml_EMIT_BLOCK_MAPPING_KEY_STATE // Expect the key of a block mapping.
  563. yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE // Expect a value for a simple key of a block mapping.
  564. yaml_EMIT_BLOCK_MAPPING_VALUE_STATE // Expect a value of a block mapping.
  565. yaml_EMIT_END_STATE // Expect nothing.
  566. )
  567. // The emitter structure.
  568. //
  569. // All members are internal. Manage the structure using the @c yaml_emitter_
  570. // family of functions.
  571. type yaml_emitter_t struct {
  572. // Error handling
  573. error yaml_error_type_t // Error type.
  574. problem string // Error description.
  575. // Writer stuff
  576. write_handler yaml_write_handler_t // Write handler.
  577. output_buffer *[]byte // String output data.
  578. output_writer io.Writer // File output data.
  579. buffer []byte // The working buffer.
  580. buffer_pos int // The current position of the buffer.
  581. raw_buffer []byte // The raw buffer.
  582. raw_buffer_pos int // The current position of the buffer.
  583. encoding yaml_encoding_t // The stream encoding.
  584. // Emitter stuff
  585. canonical bool // If the output is in the canonical style?
  586. best_indent int // The number of indentation spaces.
  587. best_width int // The preferred width of the output lines.
  588. unicode bool // Allow unescaped non-ASCII characters?
  589. line_break yaml_break_t // The preferred line break.
  590. state yaml_emitter_state_t // The current emitter state.
  591. states []yaml_emitter_state_t // The stack of states.
  592. events []yaml_event_t // The event queue.
  593. events_head int // The head of the event queue.
  594. indents []int // The stack of indentation levels.
  595. tag_directives []yaml_tag_directive_t // The list of tag directives.
  596. indent int // The current indentation level.
  597. flow_level int // The current flow level.
  598. root_context bool // Is it the document root context?
  599. sequence_context bool // Is it a sequence context?
  600. mapping_context bool // Is it a mapping context?
  601. simple_key_context bool // Is it a simple mapping key context?
  602. line int // The current line.
  603. column int // The current column.
  604. whitespace bool // If the last character was a whitespace?
  605. indention bool // If the last character was an indentation character (' ', '-', '?', ':')?
  606. open_ended bool // If an explicit document end is required?
  607. space_above bool // Is there's an empty line above?
  608. foot_indent int // The indent used to write the foot comment above, or -1 if none.
  609. // Anchor analysis.
  610. anchor_data struct {
  611. anchor []byte // The anchor value.
  612. alias bool // Is it an alias?
  613. }
  614. // Tag analysis.
  615. tag_data struct {
  616. handle []byte // The tag handle.
  617. suffix []byte // The tag suffix.
  618. }
  619. // Scalar analysis.
  620. scalar_data struct {
  621. value []byte // The scalar value.
  622. multiline bool // Does the scalar contain line breaks?
  623. flow_plain_allowed bool // Can the scalar be expessed in the flow plain style?
  624. block_plain_allowed bool // Can the scalar be expressed in the block plain style?
  625. single_quoted_allowed bool // Can the scalar be expressed in the single quoted style?
  626. block_allowed bool // Can the scalar be expressed in the literal or folded styles?
  627. style yaml_scalar_style_t // The output style.
  628. }
  629. // Comments
  630. head_comment []byte
  631. line_comment []byte
  632. foot_comment []byte
  633. tail_comment []byte
  634. key_line_comment []byte
  635. // Dumper stuff
  636. opened bool // If the stream was already opened?
  637. closed bool // If the stream was already closed?
  638. // The information associated with the document nodes.
  639. anchors *struct {
  640. references int // The number of references.
  641. anchor int // The anchor id.
  642. serialized bool // If the node has been emitted?
  643. }
  644. last_anchor_id int // The last assigned anchor id.
  645. document *yaml_document_t // The currently emitted document.
  646. }