emitterc.go 54 KB

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