另客网go项目公用的代码库

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