emitterc.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. package yaml
  2. import (
  3. "bytes"
  4. )
  5. // Flush the buffer if needed.
  6. func flush(emitter *yaml_emitter_t) bool {
  7. if emitter.buffer_pos+5 >= len(emitter.buffer) {
  8. return yaml_emitter_flush(emitter)
  9. }
  10. return true
  11. }
  12. // Put a character to the output buffer.
  13. func put(emitter *yaml_emitter_t, value byte) bool {
  14. if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
  15. return false
  16. }
  17. emitter.buffer[emitter.buffer_pos] = value
  18. emitter.buffer_pos++
  19. emitter.column++
  20. return true
  21. }
  22. // Put a line break to the output buffer.
  23. func put_break(emitter *yaml_emitter_t) bool {
  24. if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
  25. return false
  26. }
  27. switch emitter.line_break {
  28. case yaml_CR_BREAK:
  29. emitter.buffer[emitter.buffer_pos] = '\r'
  30. emitter.buffer_pos += 1
  31. case yaml_LN_BREAK:
  32. emitter.buffer[emitter.buffer_pos] = '\n'
  33. emitter.buffer_pos += 1
  34. case yaml_CRLN_BREAK:
  35. emitter.buffer[emitter.buffer_pos+0] = '\r'
  36. emitter.buffer[emitter.buffer_pos+1] = '\n'
  37. emitter.buffer_pos += 2
  38. default:
  39. panic("unknown line break setting")
  40. }
  41. emitter.column = 0
  42. emitter.line++
  43. return true
  44. }
  45. // Copy a character from a string into buffer.
  46. func write(emitter *yaml_emitter_t, s []byte, i *int) bool {
  47. if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
  48. return false
  49. }
  50. p := emitter.buffer_pos
  51. w := width(s[*i])
  52. switch w {
  53. case 4:
  54. emitter.buffer[p+3] = s[*i+3]
  55. fallthrough
  56. case 3:
  57. emitter.buffer[p+2] = s[*i+2]
  58. fallthrough
  59. case 2:
  60. emitter.buffer[p+1] = s[*i+1]
  61. fallthrough
  62. case 1:
  63. emitter.buffer[p+0] = s[*i+0]
  64. default:
  65. panic("unknown character width")
  66. }
  67. emitter.column++
  68. emitter.buffer_pos += w
  69. *i += w
  70. return true
  71. }
  72. // Write a whole string into buffer.
  73. func write_all(emitter *yaml_emitter_t, s []byte) bool {
  74. for i := 0; i < len(s); {
  75. if !write(emitter, s, &i) {
  76. return false
  77. }
  78. }
  79. return true
  80. }
  81. // Copy a line break character from a string into buffer.
  82. func write_break(emitter *yaml_emitter_t, s []byte, i *int) bool {
  83. if s[*i] == '\n' {
  84. if !put_break(emitter) {
  85. return false
  86. }
  87. *i++
  88. } else {
  89. if !write(emitter, s, i) {
  90. return false
  91. }
  92. emitter.column = 0
  93. emitter.line++
  94. }
  95. return true
  96. }
  97. // Set an emitter error and return false.
  98. func yaml_emitter_set_emitter_error(emitter *yaml_emitter_t, problem string) bool {
  99. emitter.error = yaml_EMITTER_ERROR
  100. emitter.problem = problem
  101. return false
  102. }
  103. // Emit an event.
  104. func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  105. emitter.events = append(emitter.events, *event)
  106. for !yaml_emitter_need_more_events(emitter) {
  107. event := &emitter.events[emitter.events_head]
  108. if !yaml_emitter_analyze_event(emitter, event) {
  109. return false
  110. }
  111. if !yaml_emitter_state_machine(emitter, event) {
  112. return false
  113. }
  114. yaml_event_delete(event)
  115. emitter.events_head++
  116. }
  117. return true
  118. }
  119. // Check if we need to accumulate more events before emitting.
  120. //
  121. // We accumulate extra
  122. // - 1 event for DOCUMENT-START
  123. // - 2 events for SEQUENCE-START
  124. // - 3 events for MAPPING-START
  125. //
  126. func yaml_emitter_need_more_events(emitter *yaml_emitter_t) bool {
  127. if emitter.events_head == len(emitter.events) {
  128. return true
  129. }
  130. var accumulate int
  131. switch emitter.events[emitter.events_head].typ {
  132. case yaml_DOCUMENT_START_EVENT:
  133. accumulate = 1
  134. break
  135. case yaml_SEQUENCE_START_EVENT:
  136. accumulate = 2
  137. break
  138. case yaml_MAPPING_START_EVENT:
  139. accumulate = 3
  140. break
  141. default:
  142. return false
  143. }
  144. if len(emitter.events)-emitter.events_head > accumulate {
  145. return false
  146. }
  147. var level int
  148. for i := emitter.events_head; i < len(emitter.events); i++ {
  149. switch emitter.events[i].typ {
  150. case yaml_STREAM_START_EVENT, yaml_DOCUMENT_START_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT:
  151. level++
  152. case yaml_STREAM_END_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT:
  153. level--
  154. }
  155. if level == 0 {
  156. return false
  157. }
  158. }
  159. return true
  160. }
  161. // Append a directive to the directives stack.
  162. func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_directive_t, allow_duplicates bool) bool {
  163. for i := 0; i < len(emitter.tag_directives); i++ {
  164. if bytes.Equal(value.handle, emitter.tag_directives[i].handle) {
  165. if allow_duplicates {
  166. return true
  167. }
  168. return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive")
  169. }
  170. }
  171. // [Go] Do we actually need to copy this given garbage collection
  172. // and the lack of deallocating destructors?
  173. tag_copy := yaml_tag_directive_t{
  174. handle: make([]byte, len(value.handle)),
  175. prefix: make([]byte, len(value.prefix)),
  176. }
  177. copy(tag_copy.handle, value.handle)
  178. copy(tag_copy.prefix, value.prefix)
  179. emitter.tag_directives = append(emitter.tag_directives, tag_copy)
  180. return true
  181. }
  182. // Increase the indentation level.
  183. func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
  184. emitter.indents = append(emitter.indents, emitter.indent)
  185. if emitter.indent < 0 {
  186. if flow {
  187. emitter.indent = emitter.best_indent
  188. } else {
  189. emitter.indent = 0
  190. }
  191. } else if !indentless {
  192. emitter.indent += emitter.best_indent
  193. }
  194. return true
  195. }
  196. // State dispatcher.
  197. func yaml_emitter_state_machine(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  198. switch emitter.state {
  199. default:
  200. case yaml_EMIT_STREAM_START_STATE:
  201. return yaml_emitter_emit_stream_start(emitter, event)
  202. case yaml_EMIT_FIRST_DOCUMENT_START_STATE:
  203. return yaml_emitter_emit_document_start(emitter, event, true)
  204. case yaml_EMIT_DOCUMENT_START_STATE:
  205. return yaml_emitter_emit_document_start(emitter, event, false)
  206. case yaml_EMIT_DOCUMENT_CONTENT_STATE:
  207. return yaml_emitter_emit_document_content(emitter, event)
  208. case yaml_EMIT_DOCUMENT_END_STATE:
  209. return yaml_emitter_emit_document_end(emitter, event)
  210. case yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
  211. return yaml_emitter_emit_flow_sequence_item(emitter, event, true)
  212. case yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE:
  213. return yaml_emitter_emit_flow_sequence_item(emitter, event, false)
  214. case yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
  215. return yaml_emitter_emit_flow_mapping_key(emitter, event, true)
  216. case yaml_EMIT_FLOW_MAPPING_KEY_STATE:
  217. return yaml_emitter_emit_flow_mapping_key(emitter, event, false)
  218. case yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
  219. return yaml_emitter_emit_flow_mapping_value(emitter, event, true)
  220. case yaml_EMIT_FLOW_MAPPING_VALUE_STATE:
  221. return yaml_emitter_emit_flow_mapping_value(emitter, event, false)
  222. case yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
  223. return yaml_emitter_emit_block_sequence_item(emitter, event, true)
  224. case yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
  225. return yaml_emitter_emit_block_sequence_item(emitter, event, false)
  226. case yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
  227. return yaml_emitter_emit_block_mapping_key(emitter, event, true)
  228. case yaml_EMIT_BLOCK_MAPPING_KEY_STATE:
  229. return yaml_emitter_emit_block_mapping_key(emitter, event, false)
  230. case yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
  231. return yaml_emitter_emit_block_mapping_value(emitter, event, true)
  232. case yaml_EMIT_BLOCK_MAPPING_VALUE_STATE:
  233. return yaml_emitter_emit_block_mapping_value(emitter, event, false)
  234. case yaml_EMIT_END_STATE:
  235. return yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END")
  236. }
  237. panic("invalid emitter state")
  238. }
  239. // Expect STREAM-START.
  240. func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  241. if event.typ != yaml_STREAM_START_EVENT {
  242. return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START")
  243. }
  244. if emitter.encoding == yaml_ANY_ENCODING {
  245. emitter.encoding = event.encoding
  246. if emitter.encoding == yaml_ANY_ENCODING {
  247. emitter.encoding = yaml_UTF8_ENCODING
  248. }
  249. }
  250. if emitter.best_indent < 2 || emitter.best_indent > 9 {
  251. emitter.best_indent = 2
  252. }
  253. if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 {
  254. emitter.best_width = 80
  255. }
  256. if emitter.best_width < 0 {
  257. emitter.best_width = 1<<31 - 1
  258. }
  259. if emitter.line_break == yaml_ANY_BREAK {
  260. emitter.line_break = yaml_LN_BREAK
  261. }
  262. emitter.indent = -1
  263. emitter.line = 0
  264. emitter.column = 0
  265. emitter.whitespace = true
  266. emitter.indention = true
  267. if emitter.encoding != yaml_UTF8_ENCODING {
  268. if !yaml_emitter_write_bom(emitter) {
  269. return false
  270. }
  271. }
  272. emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE
  273. return true
  274. }
  275. // Expect DOCUMENT-START or STREAM-END.
  276. func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  277. if event.typ == yaml_DOCUMENT_START_EVENT {
  278. if event.version_directive != nil {
  279. if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) {
  280. return false
  281. }
  282. }
  283. for i := 0; i < len(event.tag_directives); i++ {
  284. tag_directive := &event.tag_directives[i]
  285. if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) {
  286. return false
  287. }
  288. if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) {
  289. return false
  290. }
  291. }
  292. for i := 0; i < len(default_tag_directives); i++ {
  293. tag_directive := &default_tag_directives[i]
  294. if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) {
  295. return false
  296. }
  297. }
  298. implicit := event.implicit
  299. if !first || emitter.canonical {
  300. implicit = false
  301. }
  302. if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) {
  303. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
  304. return false
  305. }
  306. if !yaml_emitter_write_indent(emitter) {
  307. return false
  308. }
  309. }
  310. if event.version_directive != nil {
  311. implicit = false
  312. if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) {
  313. return false
  314. }
  315. if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) {
  316. return false
  317. }
  318. if !yaml_emitter_write_indent(emitter) {
  319. return false
  320. }
  321. }
  322. if len(event.tag_directives) > 0 {
  323. implicit = false
  324. for i := 0; i < len(event.tag_directives); i++ {
  325. tag_directive := &event.tag_directives[i]
  326. if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) {
  327. return false
  328. }
  329. if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) {
  330. return false
  331. }
  332. if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) {
  333. return false
  334. }
  335. if !yaml_emitter_write_indent(emitter) {
  336. return false
  337. }
  338. }
  339. }
  340. if yaml_emitter_check_empty_document(emitter) {
  341. implicit = false
  342. }
  343. if !implicit {
  344. if !yaml_emitter_write_indent(emitter) {
  345. return false
  346. }
  347. if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) {
  348. return false
  349. }
  350. if emitter.canonical {
  351. if !yaml_emitter_write_indent(emitter) {
  352. return false
  353. }
  354. }
  355. }
  356. emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE
  357. return true
  358. }
  359. if event.typ == yaml_STREAM_END_EVENT {
  360. if emitter.open_ended {
  361. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
  362. return false
  363. }
  364. if !yaml_emitter_write_indent(emitter) {
  365. return false
  366. }
  367. }
  368. if !yaml_emitter_flush(emitter) {
  369. return false
  370. }
  371. emitter.state = yaml_EMIT_END_STATE
  372. return true
  373. }
  374. return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END")
  375. }
  376. // Expect the root node.
  377. func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  378. emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE)
  379. return yaml_emitter_emit_node(emitter, event, true, false, false, false)
  380. }
  381. // Expect DOCUMENT-END.
  382. func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  383. if event.typ != yaml_DOCUMENT_END_EVENT {
  384. return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END")
  385. }
  386. if !yaml_emitter_write_indent(emitter) {
  387. return false
  388. }
  389. if !event.implicit {
  390. // [Go] Allocate the slice elsewhere.
  391. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
  392. return false
  393. }
  394. if !yaml_emitter_write_indent(emitter) {
  395. return false
  396. }
  397. }
  398. if !yaml_emitter_flush(emitter) {
  399. return false
  400. }
  401. emitter.state = yaml_EMIT_DOCUMENT_START_STATE
  402. emitter.tag_directives = emitter.tag_directives[:0]
  403. return true
  404. }
  405. // Expect a flow item node.
  406. func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  407. if first {
  408. if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
  409. return false
  410. }
  411. if !yaml_emitter_increase_indent(emitter, true, false) {
  412. return false
  413. }
  414. emitter.flow_level++
  415. }
  416. if event.typ == yaml_SEQUENCE_END_EVENT {
  417. emitter.flow_level--
  418. emitter.indent = emitter.indents[len(emitter.indents)-1]
  419. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  420. if emitter.canonical && !first {
  421. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  422. return false
  423. }
  424. if !yaml_emitter_write_indent(emitter) {
  425. return false
  426. }
  427. }
  428. if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) {
  429. return false
  430. }
  431. emitter.state = emitter.states[len(emitter.states)-1]
  432. emitter.states = emitter.states[:len(emitter.states)-1]
  433. return true
  434. }
  435. if !first {
  436. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  437. return false
  438. }
  439. }
  440. if emitter.canonical || emitter.column > emitter.best_width {
  441. if !yaml_emitter_write_indent(emitter) {
  442. return false
  443. }
  444. }
  445. emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE)
  446. return yaml_emitter_emit_node(emitter, event, false, true, false, false)
  447. }
  448. // Expect a flow key node.
  449. func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  450. if first {
  451. if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
  452. return false
  453. }
  454. if !yaml_emitter_increase_indent(emitter, true, false) {
  455. return false
  456. }
  457. emitter.flow_level++
  458. }
  459. if event.typ == yaml_MAPPING_END_EVENT {
  460. emitter.flow_level--
  461. emitter.indent = emitter.indents[len(emitter.indents)-1]
  462. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  463. if emitter.canonical && !first {
  464. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  465. return false
  466. }
  467. if !yaml_emitter_write_indent(emitter) {
  468. return false
  469. }
  470. }
  471. if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) {
  472. return false
  473. }
  474. emitter.state = emitter.states[len(emitter.states)-1]
  475. emitter.states = emitter.states[:len(emitter.states)-1]
  476. return true
  477. }
  478. if !first {
  479. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  480. return false
  481. }
  482. }
  483. if emitter.canonical || emitter.column > emitter.best_width {
  484. if !yaml_emitter_write_indent(emitter) {
  485. return false
  486. }
  487. }
  488. if !emitter.canonical && yaml_emitter_check_simple_key(emitter) {
  489. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)
  490. return yaml_emitter_emit_node(emitter, event, false, false, true, true)
  491. }
  492. if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) {
  493. return false
  494. }
  495. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE)
  496. return yaml_emitter_emit_node(emitter, event, false, false, true, false)
  497. }
  498. // Expect a flow value node.
  499. func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
  500. if simple {
  501. if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
  502. return false
  503. }
  504. } else {
  505. if emitter.canonical || emitter.column > emitter.best_width {
  506. if !yaml_emitter_write_indent(emitter) {
  507. return false
  508. }
  509. }
  510. if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) {
  511. return false
  512. }
  513. }
  514. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE)
  515. return yaml_emitter_emit_node(emitter, event, false, false, true, false)
  516. }
  517. // Expect a block item node.
  518. func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  519. if first {
  520. if !yaml_emitter_increase_indent(emitter, false, emitter.mapping_context && !emitter.indention) {
  521. return false
  522. }
  523. }
  524. if event.typ == yaml_SEQUENCE_END_EVENT {
  525. emitter.indent = emitter.indents[len(emitter.indents)-1]
  526. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  527. emitter.state = emitter.states[len(emitter.states)-1]
  528. emitter.states = emitter.states[:len(emitter.states)-1]
  529. return true
  530. }
  531. if !yaml_emitter_write_indent(emitter) {
  532. return false
  533. }
  534. if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) {
  535. return false
  536. }
  537. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE)
  538. return yaml_emitter_emit_node(emitter, event, false, true, false, false)
  539. }
  540. // Expect a block key node.
  541. func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  542. if first {
  543. if !yaml_emitter_increase_indent(emitter, false, false) {
  544. return false
  545. }
  546. }
  547. if event.typ == yaml_MAPPING_END_EVENT {
  548. emitter.indent = emitter.indents[len(emitter.indents)-1]
  549. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  550. emitter.state = emitter.states[len(emitter.states)-1]
  551. emitter.states = emitter.states[:len(emitter.states)-1]
  552. return true
  553. }
  554. if !yaml_emitter_write_indent(emitter) {
  555. return false
  556. }
  557. if yaml_emitter_check_simple_key(emitter) {
  558. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)
  559. return yaml_emitter_emit_node(emitter, event, false, false, true, true)
  560. }
  561. if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) {
  562. return false
  563. }
  564. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE)
  565. return yaml_emitter_emit_node(emitter, event, false, false, true, false)
  566. }
  567. // Expect a block value node.
  568. func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
  569. if simple {
  570. if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
  571. return false
  572. }
  573. } else {
  574. if !yaml_emitter_write_indent(emitter) {
  575. return false
  576. }
  577. if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) {
  578. return false
  579. }
  580. }
  581. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE)
  582. return yaml_emitter_emit_node(emitter, event, false, false, true, false)
  583. }
  584. // Expect a node.
  585. func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t,
  586. root bool, sequence bool, mapping bool, simple_key bool) bool {
  587. emitter.root_context = root
  588. emitter.sequence_context = sequence
  589. emitter.mapping_context = mapping
  590. emitter.simple_key_context = simple_key
  591. switch event.typ {
  592. case yaml_ALIAS_EVENT:
  593. return yaml_emitter_emit_alias(emitter, event)
  594. case yaml_SCALAR_EVENT:
  595. return yaml_emitter_emit_scalar(emitter, event)
  596. case yaml_SEQUENCE_START_EVENT:
  597. return yaml_emitter_emit_sequence_start(emitter, event)
  598. case yaml_MAPPING_START_EVENT:
  599. return yaml_emitter_emit_mapping_start(emitter, event)
  600. default:
  601. return yaml_emitter_set_emitter_error(emitter,
  602. "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS")
  603. }
  604. return false
  605. }
  606. // Expect ALIAS.
  607. func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  608. if !yaml_emitter_process_anchor(emitter) {
  609. return false
  610. }
  611. emitter.state = emitter.states[len(emitter.states)-1]
  612. emitter.states = emitter.states[:len(emitter.states)-1]
  613. return true
  614. }
  615. // Expect SCALAR.
  616. func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  617. if !yaml_emitter_select_scalar_style(emitter, event) {
  618. return false
  619. }
  620. if !yaml_emitter_process_anchor(emitter) {
  621. return false
  622. }
  623. if !yaml_emitter_process_tag(emitter) {
  624. return false
  625. }
  626. if !yaml_emitter_increase_indent(emitter, true, false) {
  627. return false
  628. }
  629. if !yaml_emitter_process_scalar(emitter) {
  630. return false
  631. }
  632. emitter.indent = emitter.indents[len(emitter.indents)-1]
  633. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  634. emitter.state = emitter.states[len(emitter.states)-1]
  635. emitter.states = emitter.states[:len(emitter.states)-1]
  636. return true
  637. }
  638. // Expect SEQUENCE-START.
  639. func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  640. if !yaml_emitter_process_anchor(emitter) {
  641. return false
  642. }
  643. if !yaml_emitter_process_tag(emitter) {
  644. return false
  645. }
  646. if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE ||
  647. yaml_emitter_check_empty_sequence(emitter) {
  648. emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE
  649. } else {
  650. emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE
  651. }
  652. return true
  653. }
  654. // Expect MAPPING-START.
  655. func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  656. if !yaml_emitter_process_anchor(emitter) {
  657. return false
  658. }
  659. if !yaml_emitter_process_tag(emitter) {
  660. return false
  661. }
  662. if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE ||
  663. yaml_emitter_check_empty_mapping(emitter) {
  664. emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE
  665. } else {
  666. emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE
  667. }
  668. return true
  669. }
  670. // Check if the document content is an empty scalar.
  671. func yaml_emitter_check_empty_document(emitter *yaml_emitter_t) bool {
  672. return false // [Go] Huh?
  673. }
  674. // Check if the next events represent an empty sequence.
  675. func yaml_emitter_check_empty_sequence(emitter *yaml_emitter_t) bool {
  676. if len(emitter.events)-emitter.events_head < 2 {
  677. return false
  678. }
  679. return emitter.events[emitter.events_head].typ == yaml_SEQUENCE_START_EVENT &&
  680. emitter.events[emitter.events_head+1].typ == yaml_SEQUENCE_END_EVENT
  681. }
  682. // Check if the next events represent an empty mapping.
  683. func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool {
  684. if len(emitter.events)-emitter.events_head < 2 {
  685. return false
  686. }
  687. return emitter.events[emitter.events_head].typ == yaml_MAPPING_START_EVENT &&
  688. emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT
  689. }
  690. // Check if the next node can be expressed as a simple key.
  691. func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool {
  692. length := 0
  693. switch emitter.events[emitter.events_head].typ {
  694. case yaml_ALIAS_EVENT:
  695. length += len(emitter.anchor_data.anchor)
  696. case yaml_SCALAR_EVENT:
  697. if emitter.scalar_data.multiline {
  698. return false
  699. }
  700. length += len(emitter.anchor_data.anchor) +
  701. len(emitter.tag_data.handle) +
  702. len(emitter.tag_data.suffix) +
  703. len(emitter.scalar_data.value)
  704. case yaml_SEQUENCE_START_EVENT:
  705. if !yaml_emitter_check_empty_sequence(emitter) {
  706. return false
  707. }
  708. length += len(emitter.anchor_data.anchor) +
  709. len(emitter.tag_data.handle) +
  710. len(emitter.tag_data.suffix)
  711. case yaml_MAPPING_START_EVENT:
  712. if !yaml_emitter_check_empty_mapping(emitter) {
  713. return false
  714. }
  715. length += len(emitter.anchor_data.anchor) +
  716. len(emitter.tag_data.handle) +
  717. len(emitter.tag_data.suffix)
  718. default:
  719. return false
  720. }
  721. return length <= 128
  722. }
  723. // Determine an acceptable scalar style.
  724. func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  725. no_tag := len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0
  726. if no_tag && !event.implicit && !event.quoted_implicit {
  727. return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified")
  728. }
  729. style := event.scalar_style()
  730. if style == yaml_ANY_SCALAR_STYLE {
  731. style = yaml_PLAIN_SCALAR_STYLE
  732. }
  733. if emitter.canonical {
  734. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  735. }
  736. if emitter.simple_key_context && emitter.scalar_data.multiline {
  737. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  738. }
  739. if style == yaml_PLAIN_SCALAR_STYLE {
  740. if emitter.flow_level > 0 && !emitter.scalar_data.flow_plain_allowed ||
  741. emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed {
  742. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  743. }
  744. if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) {
  745. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  746. }
  747. if no_tag && !event.implicit {
  748. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  749. }
  750. }
  751. if style == yaml_SINGLE_QUOTED_SCALAR_STYLE {
  752. if !emitter.scalar_data.single_quoted_allowed {
  753. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  754. }
  755. }
  756. if style == yaml_LITERAL_SCALAR_STYLE || style == yaml_FOLDED_SCALAR_STYLE {
  757. if !emitter.scalar_data.block_allowed || emitter.flow_level > 0 || emitter.simple_key_context {
  758. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  759. }
  760. }
  761. if no_tag && !event.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE {
  762. emitter.tag_data.handle = []byte{'!'}
  763. }
  764. emitter.scalar_data.style = style
  765. return true
  766. }
  767. // Write an achor.
  768. func yaml_emitter_process_anchor(emitter *yaml_emitter_t) bool {
  769. if emitter.anchor_data.anchor == nil {
  770. return true
  771. }
  772. c := []byte{'&'}
  773. if emitter.anchor_data.alias {
  774. c[0] = '*'
  775. }
  776. if !yaml_emitter_write_indicator(emitter, c, true, false, false) {
  777. return false
  778. }
  779. return yaml_emitter_write_anchor(emitter, emitter.anchor_data.anchor)
  780. }
  781. // Write a tag.
  782. func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
  783. if len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 {
  784. return true
  785. }
  786. if len(emitter.tag_data.handle) > 0 {
  787. if !yaml_emitter_write_tag_handle(emitter, emitter.tag_data.handle) {
  788. return false
  789. }
  790. if len(emitter.tag_data.suffix) > 0 {
  791. if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
  792. return false
  793. }
  794. }
  795. } else {
  796. // [Go] Allocate these slices elsewhere.
  797. if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) {
  798. return false
  799. }
  800. if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
  801. return false
  802. }
  803. if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) {
  804. return false
  805. }
  806. }
  807. return true
  808. }
  809. // Write a scalar.
  810. func yaml_emitter_process_scalar(emitter *yaml_emitter_t) bool {
  811. switch emitter.scalar_data.style {
  812. case yaml_PLAIN_SCALAR_STYLE:
  813. return yaml_emitter_write_plain_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
  814. case yaml_SINGLE_QUOTED_SCALAR_STYLE:
  815. return yaml_emitter_write_single_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
  816. case yaml_DOUBLE_QUOTED_SCALAR_STYLE:
  817. return yaml_emitter_write_double_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
  818. case yaml_LITERAL_SCALAR_STYLE:
  819. return yaml_emitter_write_literal_scalar(emitter, emitter.scalar_data.value)
  820. case yaml_FOLDED_SCALAR_STYLE:
  821. return yaml_emitter_write_folded_scalar(emitter, emitter.scalar_data.value)
  822. }
  823. panic("unknown scalar style")
  824. }
  825. // Check if a %YAML directive is valid.
  826. func yaml_emitter_analyze_version_directive(emitter *yaml_emitter_t, version_directive *yaml_version_directive_t) bool {
  827. if version_directive.major != 1 || version_directive.minor != 1 {
  828. return yaml_emitter_set_emitter_error(emitter, "incompatible %YAML directive")
  829. }
  830. return true
  831. }
  832. // Check if a %TAG directive is valid.
  833. func yaml_emitter_analyze_tag_directive(emitter *yaml_emitter_t, tag_directive *yaml_tag_directive_t) bool {
  834. handle := tag_directive.handle
  835. prefix := tag_directive.prefix
  836. if len(handle) == 0 {
  837. return yaml_emitter_set_emitter_error(emitter, "tag handle must not be empty")
  838. }
  839. if handle[0] != '!' {
  840. return yaml_emitter_set_emitter_error(emitter, "tag handle must start with '!'")
  841. }
  842. if handle[len(handle)-1] != '!' {
  843. return yaml_emitter_set_emitter_error(emitter, "tag handle must end with '!'")
  844. }
  845. for i := 1; i < len(handle)-1; i += width(handle[i]) {
  846. if !is_alpha(handle, i) {
  847. return yaml_emitter_set_emitter_error(emitter, "tag handle must contain alphanumerical characters only")
  848. }
  849. }
  850. if len(prefix) == 0 {
  851. return yaml_emitter_set_emitter_error(emitter, "tag prefix must not be empty")
  852. }
  853. return true
  854. }
  855. // Check if an anchor is valid.
  856. func yaml_emitter_analyze_anchor(emitter *yaml_emitter_t, anchor []byte, alias bool) bool {
  857. if len(anchor) == 0 {
  858. problem := "anchor value must not be empty"
  859. if alias {
  860. problem = "alias value must not be empty"
  861. }
  862. return yaml_emitter_set_emitter_error(emitter, problem)
  863. }
  864. for i := 0; i < len(anchor); i += width(anchor[i]) {
  865. if !is_alpha(anchor, i) {
  866. problem := "anchor value must contain alphanumerical characters only"
  867. if alias {
  868. problem = "alias value must contain alphanumerical characters only"
  869. }
  870. return yaml_emitter_set_emitter_error(emitter, problem)
  871. }
  872. }
  873. emitter.anchor_data.anchor = anchor
  874. emitter.anchor_data.alias = alias
  875. return true
  876. }
  877. // Check if a tag is valid.
  878. func yaml_emitter_analyze_tag(emitter *yaml_emitter_t, tag []byte) bool {
  879. if len(tag) == 0 {
  880. return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty")
  881. }
  882. for i := 0; i < len(emitter.tag_directives); i++ {
  883. tag_directive := &emitter.tag_directives[i]
  884. if bytes.HasPrefix(tag, tag_directive.prefix) {
  885. emitter.tag_data.handle = tag_directive.handle
  886. emitter.tag_data.suffix = tag[len(tag_directive.prefix):]
  887. return true
  888. }
  889. }
  890. emitter.tag_data.suffix = tag
  891. return true
  892. }
  893. // Check if a scalar is valid.
  894. func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool {
  895. var (
  896. block_indicators = false
  897. flow_indicators = false
  898. line_breaks = false
  899. special_characters = false
  900. leading_space = false
  901. leading_break = false
  902. trailing_space = false
  903. trailing_break = false
  904. break_space = false
  905. space_break = false
  906. preceeded_by_whitespace = false
  907. followed_by_whitespace = false
  908. previous_space = false
  909. previous_break = false
  910. )
  911. emitter.scalar_data.value = value
  912. if len(value) == 0 {
  913. emitter.scalar_data.multiline = false
  914. emitter.scalar_data.flow_plain_allowed = false
  915. emitter.scalar_data.block_plain_allowed = true
  916. emitter.scalar_data.single_quoted_allowed = true
  917. emitter.scalar_data.block_allowed = false
  918. return true
  919. }
  920. if len(value) >= 3 && ((value[0] == '-' && value[1] == '-' && value[2] == '-') || (value[0] == '.' && value[1] == '.' && value[2] == '.')) {
  921. block_indicators = true
  922. flow_indicators = true
  923. }
  924. preceeded_by_whitespace = true
  925. for i, w := 0, 0; i < len(value); i += w {
  926. w = width(value[0])
  927. followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w)
  928. if i == 0 {
  929. switch value[i] {
  930. case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`':
  931. flow_indicators = true
  932. block_indicators = true
  933. case '?', ':':
  934. flow_indicators = true
  935. if followed_by_whitespace {
  936. block_indicators = true
  937. }
  938. case '-':
  939. if followed_by_whitespace {
  940. flow_indicators = true
  941. block_indicators = true
  942. }
  943. }
  944. } else {
  945. switch value[i] {
  946. case ',', '?', '[', ']', '{', '}':
  947. flow_indicators = true
  948. case ':':
  949. flow_indicators = true
  950. if followed_by_whitespace {
  951. block_indicators = true
  952. }
  953. case '#':
  954. if preceeded_by_whitespace {
  955. flow_indicators = true
  956. block_indicators = true
  957. }
  958. }
  959. }
  960. if !is_printable(value, i) || !is_ascii(value, i) && !emitter.unicode {
  961. special_characters = true
  962. }
  963. if is_space(value, i) {
  964. if i == 0 {
  965. leading_space = true
  966. }
  967. if i+width(value[i]) == len(value) {
  968. trailing_space = true
  969. }
  970. if previous_break {
  971. break_space = true
  972. }
  973. previous_space = true
  974. previous_break = false
  975. } else if is_break(value, i) {
  976. line_breaks = true
  977. if i == 0 {
  978. leading_break = true
  979. }
  980. if i+width(value[i]) == len(value) {
  981. trailing_break = true
  982. }
  983. if previous_space {
  984. space_break = true
  985. }
  986. previous_space = false
  987. previous_break = true
  988. } else {
  989. previous_space = false
  990. previous_break = false
  991. }
  992. // [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition.
  993. preceeded_by_whitespace = is_blankz(value, i)
  994. }
  995. emitter.scalar_data.multiline = line_breaks
  996. emitter.scalar_data.flow_plain_allowed = true
  997. emitter.scalar_data.block_plain_allowed = true
  998. emitter.scalar_data.single_quoted_allowed = true
  999. emitter.scalar_data.block_allowed = true
  1000. if leading_space || leading_break || trailing_space || trailing_break {
  1001. emitter.scalar_data.flow_plain_allowed = false
  1002. emitter.scalar_data.block_plain_allowed = false
  1003. }
  1004. if trailing_space {
  1005. emitter.scalar_data.block_allowed = false
  1006. }
  1007. if break_space {
  1008. emitter.scalar_data.flow_plain_allowed = false
  1009. emitter.scalar_data.block_plain_allowed = false
  1010. emitter.scalar_data.single_quoted_allowed = false
  1011. }
  1012. if space_break || special_characters {
  1013. emitter.scalar_data.flow_plain_allowed = false
  1014. emitter.scalar_data.block_plain_allowed = false
  1015. emitter.scalar_data.single_quoted_allowed = false
  1016. emitter.scalar_data.block_allowed = false
  1017. }
  1018. if line_breaks {
  1019. emitter.scalar_data.flow_plain_allowed = false
  1020. emitter.scalar_data.block_plain_allowed = false
  1021. }
  1022. if flow_indicators {
  1023. emitter.scalar_data.flow_plain_allowed = false
  1024. }
  1025. if block_indicators {
  1026. emitter.scalar_data.block_plain_allowed = false
  1027. }
  1028. return true
  1029. }
  1030. // Check if the event data is valid.
  1031. func yaml_emitter_analyze_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  1032. emitter.anchor_data.anchor = nil
  1033. emitter.tag_data.handle = nil
  1034. emitter.tag_data.suffix = nil
  1035. emitter.scalar_data.value = nil
  1036. switch event.typ {
  1037. case yaml_ALIAS_EVENT:
  1038. if !yaml_emitter_analyze_anchor(emitter, event.anchor, true) {
  1039. return false
  1040. }
  1041. case yaml_SCALAR_EVENT:
  1042. if len(event.anchor) > 0 {
  1043. if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
  1044. return false
  1045. }
  1046. }
  1047. if len(event.tag) > 0 && (emitter.canonical || (!event.implicit && !event.quoted_implicit)) {
  1048. if !yaml_emitter_analyze_tag(emitter, event.tag) {
  1049. return false
  1050. }
  1051. }
  1052. if !yaml_emitter_analyze_scalar(emitter, event.value) {
  1053. return false
  1054. }
  1055. case yaml_SEQUENCE_START_EVENT:
  1056. if len(event.anchor) > 0 {
  1057. if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
  1058. return false
  1059. }
  1060. }
  1061. if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
  1062. if !yaml_emitter_analyze_tag(emitter, event.tag) {
  1063. return false
  1064. }
  1065. }
  1066. case yaml_MAPPING_START_EVENT:
  1067. if len(event.anchor) > 0 {
  1068. if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
  1069. return false
  1070. }
  1071. }
  1072. if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
  1073. if !yaml_emitter_analyze_tag(emitter, event.tag) {
  1074. return false
  1075. }
  1076. }
  1077. }
  1078. return true
  1079. }
  1080. // Write the BOM character.
  1081. func yaml_emitter_write_bom(emitter *yaml_emitter_t) bool {
  1082. if !flush(emitter) {
  1083. return false
  1084. }
  1085. pos := emitter.buffer_pos
  1086. emitter.buffer[pos+0] = '\xEF'
  1087. emitter.buffer[pos+1] = '\xBB'
  1088. emitter.buffer[pos+2] = '\xBF'
  1089. emitter.buffer_pos += 3
  1090. return true
  1091. }
  1092. func yaml_emitter_write_indent(emitter *yaml_emitter_t) bool {
  1093. indent := emitter.indent
  1094. if indent < 0 {
  1095. indent = 0
  1096. }
  1097. if !emitter.indention || emitter.column > indent || (emitter.column == indent && !emitter.whitespace) {
  1098. if !put_break(emitter) {
  1099. return false
  1100. }
  1101. }
  1102. for emitter.column < indent {
  1103. if !put(emitter, ' ') {
  1104. return false
  1105. }
  1106. }
  1107. emitter.whitespace = true
  1108. emitter.indention = true
  1109. return true
  1110. }
  1111. func yaml_emitter_write_indicator(emitter *yaml_emitter_t, indicator []byte, need_whitespace, is_whitespace, is_indention bool) bool {
  1112. if need_whitespace && !emitter.whitespace {
  1113. if !put(emitter, ' ') {
  1114. return false
  1115. }
  1116. }
  1117. if !write_all(emitter, indicator) {
  1118. return false
  1119. }
  1120. emitter.whitespace = is_whitespace
  1121. emitter.indention = (emitter.indention && is_indention)
  1122. emitter.open_ended = false
  1123. return true
  1124. }
  1125. func yaml_emitter_write_anchor(emitter *yaml_emitter_t, value []byte) bool {
  1126. if !write_all(emitter, value) {
  1127. return false
  1128. }
  1129. emitter.whitespace = false
  1130. emitter.indention = false
  1131. return true
  1132. }
  1133. func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool {
  1134. if !emitter.whitespace {
  1135. if !put(emitter, ' ') {
  1136. return false
  1137. }
  1138. }
  1139. if !write_all(emitter, value) {
  1140. return false
  1141. }
  1142. emitter.whitespace = false
  1143. emitter.indention = false
  1144. return true
  1145. }
  1146. func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool {
  1147. if need_whitespace && !emitter.whitespace {
  1148. if !put(emitter, ' ') {
  1149. return false
  1150. }
  1151. }
  1152. for i := 0; i < len(value); {
  1153. var must_write bool
  1154. switch value[i] {
  1155. case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']':
  1156. must_write = true
  1157. default:
  1158. must_write = is_alpha(value, i)
  1159. }
  1160. if must_write {
  1161. if !write(emitter, value, &i) {
  1162. return false
  1163. }
  1164. } else {
  1165. w := width(value[i])
  1166. for k := 0; k < w; k++ {
  1167. octet := value[i]
  1168. i++
  1169. if !put(emitter, '%') {
  1170. return false
  1171. }
  1172. c := octet >> 4
  1173. if c < 10 {
  1174. c += '0'
  1175. } else {
  1176. c += 'A' - 10
  1177. }
  1178. if !put(emitter, c) {
  1179. return false
  1180. }
  1181. c = octet & 0x0f
  1182. if c < 10 {
  1183. c += '0'
  1184. } else {
  1185. c += 'A' - 10
  1186. }
  1187. if !put(emitter, c) {
  1188. return false
  1189. }
  1190. }
  1191. }
  1192. }
  1193. emitter.whitespace = false
  1194. emitter.indention = false
  1195. return true
  1196. }
  1197. func yaml_emitter_write_plain_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
  1198. if !emitter.whitespace {
  1199. if !put(emitter, ' ') {
  1200. return false
  1201. }
  1202. }
  1203. spaces := false
  1204. breaks := false
  1205. for i := 0; i < len(value); {
  1206. if is_space(value, i) {
  1207. if allow_breaks && !spaces && emitter.column > emitter.best_width && !is_space(value, i+1) {
  1208. if !yaml_emitter_write_indent(emitter) {
  1209. return false
  1210. }
  1211. i += width(value[i])
  1212. } else {
  1213. if !write(emitter, value, &i) {
  1214. return false
  1215. }
  1216. }
  1217. spaces = true
  1218. } else if is_break(value, i) {
  1219. if !breaks && value[i] == '\n' {
  1220. if !put_break(emitter) {
  1221. return false
  1222. }
  1223. }
  1224. if !write_break(emitter, value, &i) {
  1225. return false
  1226. }
  1227. emitter.indention = true
  1228. breaks = true
  1229. } else {
  1230. if breaks {
  1231. if !yaml_emitter_write_indent(emitter) {
  1232. return false
  1233. }
  1234. }
  1235. if !write(emitter, value, &i) {
  1236. return false
  1237. }
  1238. emitter.indention = false
  1239. spaces = false
  1240. breaks = false
  1241. }
  1242. }
  1243. emitter.whitespace = false
  1244. emitter.indention = false
  1245. if emitter.root_context {
  1246. emitter.open_ended = true
  1247. }
  1248. return true
  1249. }
  1250. func yaml_emitter_write_single_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
  1251. if !yaml_emitter_write_indicator(emitter, []byte{'\''}, true, false, false) {
  1252. return false
  1253. }
  1254. spaces := false
  1255. breaks := false
  1256. for i := 0; i < len(value); {
  1257. if is_space(value, i) {
  1258. if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 && !is_space(value, i+1) {
  1259. if !yaml_emitter_write_indent(emitter) {
  1260. return false
  1261. }
  1262. i += width(value[i])
  1263. } else {
  1264. if !write(emitter, value, &i) {
  1265. return false
  1266. }
  1267. }
  1268. spaces = true
  1269. } else if is_break(value, i) {
  1270. if !breaks && value[i] == '\n' {
  1271. if !put_break(emitter) {
  1272. return false
  1273. }
  1274. }
  1275. if !write_break(emitter, value, &i) {
  1276. return false
  1277. }
  1278. emitter.indention = true
  1279. breaks = true
  1280. } else {
  1281. if breaks {
  1282. if !yaml_emitter_write_indent(emitter) {
  1283. return false
  1284. }
  1285. }
  1286. if value[i] == '\'' {
  1287. if !put(emitter, '\'') {
  1288. return false
  1289. }
  1290. }
  1291. if !write(emitter, value, &i) {
  1292. return false
  1293. }
  1294. emitter.indention = false
  1295. spaces = false
  1296. breaks = false
  1297. }
  1298. }
  1299. if !yaml_emitter_write_indicator(emitter, []byte{'\''}, false, false, false) {
  1300. return false
  1301. }
  1302. emitter.whitespace = false
  1303. emitter.indention = false
  1304. return true
  1305. }
  1306. func yaml_emitter_write_double_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
  1307. spaces := false
  1308. if !yaml_emitter_write_indicator(emitter, []byte{'"'}, true, false, false) {
  1309. return false
  1310. }
  1311. for i := 0; i < len(value); {
  1312. if !is_printable(value, i) || (!emitter.unicode && !is_ascii(value, i)) ||
  1313. is_bom(value, i) || is_break(value, i) ||
  1314. value[i] == '"' || value[i] == '\\' {
  1315. octet := value[i]
  1316. var w int
  1317. var v rune
  1318. switch {
  1319. case octet&0x80 == 0x00:
  1320. w, v = 1, rune(octet&0x7F)
  1321. case octet&0xE0 == 0xC0:
  1322. w, v = 2, rune(octet&0x1F)
  1323. case octet&0xF0 == 0xE0:
  1324. w, v = 3, rune(octet&0x0F)
  1325. case octet&0xF8 == 0xF0:
  1326. w, v = 4, rune(octet&0x07)
  1327. }
  1328. for k := 1; k < w; k++ {
  1329. octet = value[i+k]
  1330. v = (v << 6) + (rune(octet) & 0x3F)
  1331. }
  1332. i += w
  1333. if !put(emitter, '\\') {
  1334. return false
  1335. }
  1336. var ok bool
  1337. switch v {
  1338. case 0x00:
  1339. ok = put(emitter, '0')
  1340. case 0x07:
  1341. ok = put(emitter, 'a')
  1342. case 0x08:
  1343. ok = put(emitter, 'b')
  1344. case 0x09:
  1345. ok = put(emitter, 't')
  1346. case 0x0A:
  1347. ok = put(emitter, 'n')
  1348. case 0x0b:
  1349. ok = put(emitter, 'v')
  1350. case 0x0c:
  1351. ok = put(emitter, 'f')
  1352. case 0x0d:
  1353. ok = put(emitter, 'r')
  1354. case 0x1b:
  1355. ok = put(emitter, 'e')
  1356. case 0x22:
  1357. ok = put(emitter, '"')
  1358. case 0x5c:
  1359. ok = put(emitter, '\\')
  1360. case 0x85:
  1361. ok = put(emitter, 'N')
  1362. case 0xA0:
  1363. ok = put(emitter, '_')
  1364. case 0x2028:
  1365. ok = put(emitter, 'L')
  1366. case 0x2029:
  1367. ok = put(emitter, 'P')
  1368. default:
  1369. if v <= 0xFF {
  1370. ok = put(emitter, 'x')
  1371. w = 2
  1372. } else if v <= 0xFFFF {
  1373. ok = put(emitter, 'u')
  1374. w = 4
  1375. } else {
  1376. ok = put(emitter, 'U')
  1377. w = 8
  1378. }
  1379. for k := (w - 1) * 4; ok && k >= 0; k -= 4 {
  1380. digit := byte((v >> uint(k)) & 0x0F)
  1381. if digit < 10 {
  1382. ok = put(emitter, digit+'0')
  1383. } else {
  1384. ok = put(emitter, digit+'A'-10)
  1385. }
  1386. }
  1387. }
  1388. if !ok {
  1389. return false
  1390. }
  1391. spaces = false
  1392. } else if is_space(value, i) {
  1393. if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 {
  1394. if !yaml_emitter_write_indent(emitter) {
  1395. return false
  1396. }
  1397. if is_space(value, i+1) {
  1398. if !put(emitter, '\\') {
  1399. return false
  1400. }
  1401. }
  1402. i += width(value[i])
  1403. } else if !write(emitter, value, &i) {
  1404. return false
  1405. }
  1406. spaces = true
  1407. } else {
  1408. if !write(emitter, value, &i) {
  1409. return false
  1410. }
  1411. spaces = false
  1412. }
  1413. }
  1414. if !yaml_emitter_write_indicator(emitter, []byte{'"'}, false, false, false) {
  1415. return false
  1416. }
  1417. emitter.whitespace = false
  1418. emitter.indention = false
  1419. return true
  1420. }
  1421. func yaml_emitter_write_block_scalar_hints(emitter *yaml_emitter_t, value []byte) bool {
  1422. if is_space(value, 0) || is_break(value, 0) {
  1423. indent_hint := []byte{'0' + byte(emitter.best_indent)}
  1424. if !yaml_emitter_write_indicator(emitter, indent_hint, false, false, false) {
  1425. return false
  1426. }
  1427. }
  1428. emitter.open_ended = false
  1429. var chomp_hint [1]byte
  1430. if len(value) == 0 {
  1431. chomp_hint[0] = '-'
  1432. } else {
  1433. i := len(value) - 1
  1434. for value[i]&0xC0 == 0x80 {
  1435. i--
  1436. }
  1437. if !is_break(value, i) {
  1438. chomp_hint[0] = '-'
  1439. } else if i == 0 {
  1440. chomp_hint[0] = '+'
  1441. emitter.open_ended = true
  1442. } else {
  1443. i--
  1444. for value[i]&0xC0 == 0x80 {
  1445. i--
  1446. }
  1447. if is_break(value, i) {
  1448. chomp_hint[0] = '+'
  1449. emitter.open_ended = true
  1450. }
  1451. }
  1452. }
  1453. if chomp_hint[0] != 0 {
  1454. if !yaml_emitter_write_indicator(emitter, chomp_hint[:], false, false, false) {
  1455. return false
  1456. }
  1457. }
  1458. return true
  1459. }
  1460. func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bool {
  1461. if !yaml_emitter_write_indicator(emitter, []byte{'|'}, true, false, false) {
  1462. return false
  1463. }
  1464. if !yaml_emitter_write_block_scalar_hints(emitter, value) {
  1465. return false
  1466. }
  1467. if !put_break(emitter) {
  1468. return false
  1469. }
  1470. emitter.indention = true
  1471. emitter.whitespace = true
  1472. breaks := true
  1473. for i := 0; i < len(value); {
  1474. if is_break(value, i) {
  1475. if !write_break(emitter, value, &i) {
  1476. return false
  1477. }
  1478. emitter.indention = true
  1479. breaks = true
  1480. } else {
  1481. if breaks {
  1482. if !yaml_emitter_write_indent(emitter) {
  1483. return false
  1484. }
  1485. }
  1486. if !write(emitter, value, &i) {
  1487. return false
  1488. }
  1489. emitter.indention = false
  1490. breaks = false
  1491. }
  1492. }
  1493. return true
  1494. }
  1495. func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) bool {
  1496. if !yaml_emitter_write_indicator(emitter, []byte{'>'}, true, false, false) {
  1497. return false
  1498. }
  1499. if !yaml_emitter_write_block_scalar_hints(emitter, value) {
  1500. return false
  1501. }
  1502. if !put_break(emitter) {
  1503. return false
  1504. }
  1505. emitter.indention = true
  1506. emitter.whitespace = true
  1507. breaks := true
  1508. leading_spaces := true
  1509. for i := 0; i < len(value); {
  1510. if is_break(value, i) {
  1511. if !breaks && !leading_spaces && value[i] == '\n' {
  1512. k := 0
  1513. for is_break(value, k) {
  1514. k += width(value[k])
  1515. }
  1516. if !is_blankz(value, k) {
  1517. if !put_break(emitter) {
  1518. return false
  1519. }
  1520. }
  1521. }
  1522. if !write_break(emitter, value, &i) {
  1523. return false
  1524. }
  1525. emitter.indention = true
  1526. breaks = true
  1527. } else {
  1528. if breaks {
  1529. if !yaml_emitter_write_indent(emitter) {
  1530. return false
  1531. }
  1532. leading_spaces = is_blank(value, i)
  1533. }
  1534. if !breaks && is_space(value, i) && !is_space(value, i+1) && emitter.column > emitter.best_width {
  1535. if !yaml_emitter_write_indent(emitter) {
  1536. return false
  1537. }
  1538. i += width(value[i])
  1539. } else {
  1540. if !write(emitter, value, &i) {
  1541. return false
  1542. }
  1543. }
  1544. emitter.indention = false
  1545. breaks = false
  1546. }
  1547. }
  1548. return true
  1549. }