emitterc.go 44 KB

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