emitter.go 45 KB

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