emitterc.go 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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. }