You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

emitterc.go 54 kB

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