parserc.go 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. //
  2. // Copyright (c) 2011-2019 Canonical Ltd
  3. // Copyright (c) 2006-2010 Kirill Simonov
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. // of the Software, and to permit persons to whom the Software is furnished to do
  10. // so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. package yaml
  23. import (
  24. "bytes"
  25. )
  26. // The parser implements the following grammar:
  27. //
  28. // stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
  29. // implicit_document ::= block_node DOCUMENT-END*
  30. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  31. // block_node_or_indentless_sequence ::=
  32. // ALIAS
  33. // | properties (block_content | indentless_block_sequence)?
  34. // | block_content
  35. // | indentless_block_sequence
  36. // block_node ::= ALIAS
  37. // | properties block_content?
  38. // | block_content
  39. // flow_node ::= ALIAS
  40. // | properties flow_content?
  41. // | flow_content
  42. // properties ::= TAG ANCHOR? | ANCHOR TAG?
  43. // block_content ::= block_collection | flow_collection | SCALAR
  44. // flow_content ::= flow_collection | SCALAR
  45. // block_collection ::= block_sequence | block_mapping
  46. // flow_collection ::= flow_sequence | flow_mapping
  47. // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
  48. // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
  49. // block_mapping ::= BLOCK-MAPPING_START
  50. // ((KEY block_node_or_indentless_sequence?)?
  51. // (VALUE block_node_or_indentless_sequence?)?)*
  52. // BLOCK-END
  53. // flow_sequence ::= FLOW-SEQUENCE-START
  54. // (flow_sequence_entry FLOW-ENTRY)*
  55. // flow_sequence_entry?
  56. // FLOW-SEQUENCE-END
  57. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  58. // flow_mapping ::= FLOW-MAPPING-START
  59. // (flow_mapping_entry FLOW-ENTRY)*
  60. // flow_mapping_entry?
  61. // FLOW-MAPPING-END
  62. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  63. // Peek the next token in the token queue.
  64. func peek_token(parser *yaml_parser_t) *yaml_token_t {
  65. if parser.token_available || yaml_parser_fetch_more_tokens(parser) {
  66. token := &parser.tokens[parser.tokens_head]
  67. yaml_parser_unfold_comments(parser, token)
  68. return token
  69. }
  70. return nil
  71. }
  72. // yaml_parser_unfold_comments walks through the comments queue and joins all
  73. // comments behind the position of the provided token into the respective
  74. // top-level comment slices in the parser.
  75. func yaml_parser_unfold_comments(parser *yaml_parser_t, token *yaml_token_t) {
  76. for parser.comments_head < len(parser.comments) && token.start_mark.index >= parser.comments[parser.comments_head].token_mark.index {
  77. comment := &parser.comments[parser.comments_head]
  78. if len(comment.head) > 0 {
  79. if token.typ == yaml_BLOCK_END_TOKEN {
  80. // No heads on ends, so keep comment.head for a follow up token.
  81. break
  82. }
  83. if len(parser.head_comment) > 0 {
  84. parser.head_comment = append(parser.head_comment, '\n')
  85. }
  86. parser.head_comment = append(parser.head_comment, comment.head...)
  87. }
  88. if len(comment.foot) > 0 {
  89. if len(parser.foot_comment) > 0 {
  90. parser.foot_comment = append(parser.foot_comment, '\n')
  91. }
  92. parser.foot_comment = append(parser.foot_comment, comment.foot...)
  93. }
  94. if len(comment.line) > 0 {
  95. if len(parser.line_comment) > 0 {
  96. parser.line_comment = append(parser.line_comment, '\n')
  97. }
  98. parser.line_comment = append(parser.line_comment, comment.line...)
  99. }
  100. *comment = yaml_comment_t{}
  101. parser.comments_head++
  102. }
  103. }
  104. // Remove the next token from the queue (must be called after peek_token).
  105. func skip_token(parser *yaml_parser_t) {
  106. parser.token_available = false
  107. parser.tokens_parsed++
  108. parser.stream_end_produced = parser.tokens[parser.tokens_head].typ == yaml_STREAM_END_TOKEN
  109. parser.tokens_head++
  110. }
  111. // Get the next event.
  112. func yaml_parser_parse(parser *yaml_parser_t, event *yaml_event_t) bool {
  113. // Erase the event object.
  114. *event = yaml_event_t{}
  115. // No events after the end of the stream or error.
  116. if parser.stream_end_produced || parser.error != yaml_NO_ERROR || parser.state == yaml_PARSE_END_STATE {
  117. return true
  118. }
  119. // Generate the next event.
  120. return yaml_parser_state_machine(parser, event)
  121. }
  122. // Set parser error.
  123. func yaml_parser_set_parser_error(parser *yaml_parser_t, problem string, problem_mark yaml_mark_t) bool {
  124. parser.error = yaml_PARSER_ERROR
  125. parser.problem = problem
  126. parser.problem_mark = problem_mark
  127. return false
  128. }
  129. func yaml_parser_set_parser_error_context(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string, problem_mark yaml_mark_t) bool {
  130. parser.error = yaml_PARSER_ERROR
  131. parser.context = context
  132. parser.context_mark = context_mark
  133. parser.problem = problem
  134. parser.problem_mark = problem_mark
  135. return false
  136. }
  137. // State dispatcher.
  138. func yaml_parser_state_machine(parser *yaml_parser_t, event *yaml_event_t) bool {
  139. //trace("yaml_parser_state_machine", "state:", parser.state.String())
  140. switch parser.state {
  141. case yaml_PARSE_STREAM_START_STATE:
  142. return yaml_parser_parse_stream_start(parser, event)
  143. case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE:
  144. return yaml_parser_parse_document_start(parser, event, true)
  145. case yaml_PARSE_DOCUMENT_START_STATE:
  146. return yaml_parser_parse_document_start(parser, event, false)
  147. case yaml_PARSE_DOCUMENT_CONTENT_STATE:
  148. return yaml_parser_parse_document_content(parser, event)
  149. case yaml_PARSE_DOCUMENT_END_STATE:
  150. return yaml_parser_parse_document_end(parser, event)
  151. case yaml_PARSE_BLOCK_NODE_STATE:
  152. return yaml_parser_parse_node(parser, event, true, false)
  153. case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE:
  154. return yaml_parser_parse_node(parser, event, true, true)
  155. case yaml_PARSE_FLOW_NODE_STATE:
  156. return yaml_parser_parse_node(parser, event, false, false)
  157. case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE:
  158. return yaml_parser_parse_block_sequence_entry(parser, event, true)
  159. case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE:
  160. return yaml_parser_parse_block_sequence_entry(parser, event, false)
  161. case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE:
  162. return yaml_parser_parse_indentless_sequence_entry(parser, event)
  163. case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE:
  164. return yaml_parser_parse_block_mapping_key(parser, event, true)
  165. case yaml_PARSE_BLOCK_MAPPING_KEY_STATE:
  166. return yaml_parser_parse_block_mapping_key(parser, event, false)
  167. case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE:
  168. return yaml_parser_parse_block_mapping_value(parser, event)
  169. case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE:
  170. return yaml_parser_parse_flow_sequence_entry(parser, event, true)
  171. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE:
  172. return yaml_parser_parse_flow_sequence_entry(parser, event, false)
  173. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE:
  174. return yaml_parser_parse_flow_sequence_entry_mapping_key(parser, event)
  175. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE:
  176. return yaml_parser_parse_flow_sequence_entry_mapping_value(parser, event)
  177. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE:
  178. return yaml_parser_parse_flow_sequence_entry_mapping_end(parser, event)
  179. case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE:
  180. return yaml_parser_parse_flow_mapping_key(parser, event, true)
  181. case yaml_PARSE_FLOW_MAPPING_KEY_STATE:
  182. return yaml_parser_parse_flow_mapping_key(parser, event, false)
  183. case yaml_PARSE_FLOW_MAPPING_VALUE_STATE:
  184. return yaml_parser_parse_flow_mapping_value(parser, event, false)
  185. case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE:
  186. return yaml_parser_parse_flow_mapping_value(parser, event, true)
  187. default:
  188. panic("invalid parser state")
  189. }
  190. }
  191. // Parse the production:
  192. // stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
  193. // ************
  194. func yaml_parser_parse_stream_start(parser *yaml_parser_t, event *yaml_event_t) bool {
  195. token := peek_token(parser)
  196. if token == nil {
  197. return false
  198. }
  199. if token.typ != yaml_STREAM_START_TOKEN {
  200. return yaml_parser_set_parser_error(parser, "did not find expected <stream-start>", token.start_mark)
  201. }
  202. parser.state = yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE
  203. *event = yaml_event_t{
  204. typ: yaml_STREAM_START_EVENT,
  205. start_mark: token.start_mark,
  206. end_mark: token.end_mark,
  207. encoding: token.encoding,
  208. }
  209. skip_token(parser)
  210. return true
  211. }
  212. // Parse the productions:
  213. // implicit_document ::= block_node DOCUMENT-END*
  214. // *
  215. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  216. // *************************
  217. func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t, implicit bool) bool {
  218. token := peek_token(parser)
  219. if token == nil {
  220. return false
  221. }
  222. // Parse extra document end indicators.
  223. if !implicit {
  224. for token.typ == yaml_DOCUMENT_END_TOKEN {
  225. skip_token(parser)
  226. token = peek_token(parser)
  227. if token == nil {
  228. return false
  229. }
  230. }
  231. }
  232. if implicit && token.typ != yaml_VERSION_DIRECTIVE_TOKEN &&
  233. token.typ != yaml_TAG_DIRECTIVE_TOKEN &&
  234. token.typ != yaml_DOCUMENT_START_TOKEN &&
  235. token.typ != yaml_STREAM_END_TOKEN {
  236. // Parse an implicit document.
  237. if !yaml_parser_process_directives(parser, nil, nil) {
  238. return false
  239. }
  240. parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE)
  241. parser.state = yaml_PARSE_BLOCK_NODE_STATE
  242. var head_comment []byte
  243. if len(parser.head_comment) > 0 {
  244. // [Go] Scan the header comment backwards, and if an empty line is found, break
  245. // the header so the part before the last empty line goes into the
  246. // document header, while the bottom of it goes into a follow up event.
  247. for i := len(parser.head_comment) - 1; i > 0; i-- {
  248. if parser.head_comment[i] == '\n' {
  249. if i == len(parser.head_comment)-1 {
  250. head_comment = parser.head_comment[:i]
  251. parser.head_comment = parser.head_comment[i+1:]
  252. break
  253. } else if parser.head_comment[i-1] == '\n' {
  254. head_comment = parser.head_comment[:i-1]
  255. parser.head_comment = parser.head_comment[i+1:]
  256. break
  257. }
  258. }
  259. }
  260. }
  261. *event = yaml_event_t{
  262. typ: yaml_DOCUMENT_START_EVENT,
  263. start_mark: token.start_mark,
  264. end_mark: token.end_mark,
  265. head_comment: head_comment,
  266. }
  267. } else if token.typ != yaml_STREAM_END_TOKEN {
  268. // Parse an explicit document.
  269. var version_directive *yaml_version_directive_t
  270. var tag_directives []yaml_tag_directive_t
  271. start_mark := token.start_mark
  272. if !yaml_parser_process_directives(parser, &version_directive, &tag_directives) {
  273. return false
  274. }
  275. token = peek_token(parser)
  276. if token == nil {
  277. return false
  278. }
  279. if token.typ != yaml_DOCUMENT_START_TOKEN {
  280. yaml_parser_set_parser_error(parser,
  281. "did not find expected <document start>", token.start_mark)
  282. return false
  283. }
  284. parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE)
  285. parser.state = yaml_PARSE_DOCUMENT_CONTENT_STATE
  286. end_mark := token.end_mark
  287. *event = yaml_event_t{
  288. typ: yaml_DOCUMENT_START_EVENT,
  289. start_mark: start_mark,
  290. end_mark: end_mark,
  291. version_directive: version_directive,
  292. tag_directives: tag_directives,
  293. implicit: false,
  294. }
  295. skip_token(parser)
  296. } else {
  297. // Parse the stream end.
  298. parser.state = yaml_PARSE_END_STATE
  299. *event = yaml_event_t{
  300. typ: yaml_STREAM_END_EVENT,
  301. start_mark: token.start_mark,
  302. end_mark: token.end_mark,
  303. }
  304. skip_token(parser)
  305. }
  306. return true
  307. }
  308. // Parse the productions:
  309. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  310. // ***********
  311. //
  312. func yaml_parser_parse_document_content(parser *yaml_parser_t, event *yaml_event_t) bool {
  313. token := peek_token(parser)
  314. if token == nil {
  315. return false
  316. }
  317. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN ||
  318. token.typ == yaml_TAG_DIRECTIVE_TOKEN ||
  319. token.typ == yaml_DOCUMENT_START_TOKEN ||
  320. token.typ == yaml_DOCUMENT_END_TOKEN ||
  321. token.typ == yaml_STREAM_END_TOKEN {
  322. parser.state = parser.states[len(parser.states)-1]
  323. parser.states = parser.states[:len(parser.states)-1]
  324. return yaml_parser_process_empty_scalar(parser, event,
  325. token.start_mark)
  326. }
  327. return yaml_parser_parse_node(parser, event, true, false)
  328. }
  329. // Parse the productions:
  330. // implicit_document ::= block_node DOCUMENT-END*
  331. // *************
  332. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  333. //
  334. func yaml_parser_parse_document_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  335. token := peek_token(parser)
  336. if token == nil {
  337. return false
  338. }
  339. start_mark := token.start_mark
  340. end_mark := token.start_mark
  341. implicit := true
  342. if token.typ == yaml_DOCUMENT_END_TOKEN {
  343. end_mark = token.end_mark
  344. skip_token(parser)
  345. implicit = false
  346. }
  347. parser.tag_directives = parser.tag_directives[:0]
  348. parser.state = yaml_PARSE_DOCUMENT_START_STATE
  349. *event = yaml_event_t{
  350. typ: yaml_DOCUMENT_END_EVENT,
  351. start_mark: start_mark,
  352. end_mark: end_mark,
  353. implicit: implicit,
  354. }
  355. yaml_parser_set_event_comments(parser, event)
  356. if len(event.head_comment) > 0 && len(event.foot_comment) == 0 {
  357. event.foot_comment = event.head_comment
  358. event.head_comment = nil
  359. }
  360. return true
  361. }
  362. func yaml_parser_set_event_comments(parser *yaml_parser_t, event *yaml_event_t) {
  363. event.head_comment = parser.head_comment
  364. event.line_comment = parser.line_comment
  365. event.foot_comment = parser.foot_comment
  366. parser.head_comment = nil
  367. parser.line_comment = nil
  368. parser.foot_comment = nil
  369. parser.tail_comment = nil
  370. parser.stem_comment = nil
  371. }
  372. // Parse the productions:
  373. // block_node_or_indentless_sequence ::=
  374. // ALIAS
  375. // *****
  376. // | properties (block_content | indentless_block_sequence)?
  377. // ********** *
  378. // | block_content | indentless_block_sequence
  379. // *
  380. // block_node ::= ALIAS
  381. // *****
  382. // | properties block_content?
  383. // ********** *
  384. // | block_content
  385. // *
  386. // flow_node ::= ALIAS
  387. // *****
  388. // | properties flow_content?
  389. // ********** *
  390. // | flow_content
  391. // *
  392. // properties ::= TAG ANCHOR? | ANCHOR TAG?
  393. // *************************
  394. // block_content ::= block_collection | flow_collection | SCALAR
  395. // ******
  396. // flow_content ::= flow_collection | SCALAR
  397. // ******
  398. func yaml_parser_parse_node(parser *yaml_parser_t, event *yaml_event_t, block, indentless_sequence bool) bool {
  399. //defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)()
  400. token := peek_token(parser)
  401. if token == nil {
  402. return false
  403. }
  404. if token.typ == yaml_ALIAS_TOKEN {
  405. parser.state = parser.states[len(parser.states)-1]
  406. parser.states = parser.states[:len(parser.states)-1]
  407. *event = yaml_event_t{
  408. typ: yaml_ALIAS_EVENT,
  409. start_mark: token.start_mark,
  410. end_mark: token.end_mark,
  411. anchor: token.value,
  412. }
  413. yaml_parser_set_event_comments(parser, event)
  414. skip_token(parser)
  415. return true
  416. }
  417. start_mark := token.start_mark
  418. end_mark := token.start_mark
  419. var tag_token bool
  420. var tag_handle, tag_suffix, anchor []byte
  421. var tag_mark yaml_mark_t
  422. if token.typ == yaml_ANCHOR_TOKEN {
  423. anchor = token.value
  424. start_mark = token.start_mark
  425. end_mark = token.end_mark
  426. skip_token(parser)
  427. token = peek_token(parser)
  428. if token == nil {
  429. return false
  430. }
  431. if token.typ == yaml_TAG_TOKEN {
  432. tag_token = true
  433. tag_handle = token.value
  434. tag_suffix = token.suffix
  435. tag_mark = token.start_mark
  436. end_mark = token.end_mark
  437. skip_token(parser)
  438. token = peek_token(parser)
  439. if token == nil {
  440. return false
  441. }
  442. }
  443. } else if token.typ == yaml_TAG_TOKEN {
  444. tag_token = true
  445. tag_handle = token.value
  446. tag_suffix = token.suffix
  447. start_mark = token.start_mark
  448. tag_mark = token.start_mark
  449. end_mark = token.end_mark
  450. skip_token(parser)
  451. token = peek_token(parser)
  452. if token == nil {
  453. return false
  454. }
  455. if token.typ == yaml_ANCHOR_TOKEN {
  456. anchor = token.value
  457. end_mark = token.end_mark
  458. skip_token(parser)
  459. token = peek_token(parser)
  460. if token == nil {
  461. return false
  462. }
  463. }
  464. }
  465. var tag []byte
  466. if tag_token {
  467. if len(tag_handle) == 0 {
  468. tag = tag_suffix
  469. tag_suffix = nil
  470. } else {
  471. for i := range parser.tag_directives {
  472. if bytes.Equal(parser.tag_directives[i].handle, tag_handle) {
  473. tag = append([]byte(nil), parser.tag_directives[i].prefix...)
  474. tag = append(tag, tag_suffix...)
  475. break
  476. }
  477. }
  478. if len(tag) == 0 {
  479. yaml_parser_set_parser_error_context(parser,
  480. "while parsing a node", start_mark,
  481. "found undefined tag handle", tag_mark)
  482. return false
  483. }
  484. }
  485. }
  486. implicit := len(tag) == 0
  487. if indentless_sequence && token.typ == yaml_BLOCK_ENTRY_TOKEN {
  488. end_mark = token.end_mark
  489. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  490. *event = yaml_event_t{
  491. typ: yaml_SEQUENCE_START_EVENT,
  492. start_mark: start_mark,
  493. end_mark: end_mark,
  494. anchor: anchor,
  495. tag: tag,
  496. implicit: implicit,
  497. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  498. }
  499. return true
  500. }
  501. if token.typ == yaml_SCALAR_TOKEN {
  502. var plain_implicit, quoted_implicit bool
  503. end_mark = token.end_mark
  504. if (len(tag) == 0 && token.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') {
  505. plain_implicit = true
  506. } else if len(tag) == 0 {
  507. quoted_implicit = true
  508. }
  509. parser.state = parser.states[len(parser.states)-1]
  510. parser.states = parser.states[:len(parser.states)-1]
  511. *event = yaml_event_t{
  512. typ: yaml_SCALAR_EVENT,
  513. start_mark: start_mark,
  514. end_mark: end_mark,
  515. anchor: anchor,
  516. tag: tag,
  517. value: token.value,
  518. implicit: plain_implicit,
  519. quoted_implicit: quoted_implicit,
  520. style: yaml_style_t(token.style),
  521. }
  522. yaml_parser_set_event_comments(parser, event)
  523. skip_token(parser)
  524. return true
  525. }
  526. if token.typ == yaml_FLOW_SEQUENCE_START_TOKEN {
  527. // [Go] Some of the events below can be merged as they differ only on style.
  528. end_mark = token.end_mark
  529. parser.state = yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE
  530. *event = yaml_event_t{
  531. typ: yaml_SEQUENCE_START_EVENT,
  532. start_mark: start_mark,
  533. end_mark: end_mark,
  534. anchor: anchor,
  535. tag: tag,
  536. implicit: implicit,
  537. style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE),
  538. }
  539. yaml_parser_set_event_comments(parser, event)
  540. return true
  541. }
  542. if token.typ == yaml_FLOW_MAPPING_START_TOKEN {
  543. end_mark = token.end_mark
  544. parser.state = yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE
  545. *event = yaml_event_t{
  546. typ: yaml_MAPPING_START_EVENT,
  547. start_mark: start_mark,
  548. end_mark: end_mark,
  549. anchor: anchor,
  550. tag: tag,
  551. implicit: implicit,
  552. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  553. }
  554. yaml_parser_set_event_comments(parser, event)
  555. return true
  556. }
  557. if block && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN {
  558. end_mark = token.end_mark
  559. parser.state = yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE
  560. *event = yaml_event_t{
  561. typ: yaml_SEQUENCE_START_EVENT,
  562. start_mark: start_mark,
  563. end_mark: end_mark,
  564. anchor: anchor,
  565. tag: tag,
  566. implicit: implicit,
  567. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  568. }
  569. if parser.stem_comment != nil {
  570. event.head_comment = parser.stem_comment
  571. parser.stem_comment = nil
  572. }
  573. return true
  574. }
  575. if block && token.typ == yaml_BLOCK_MAPPING_START_TOKEN {
  576. end_mark = token.end_mark
  577. parser.state = yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE
  578. *event = yaml_event_t{
  579. typ: yaml_MAPPING_START_EVENT,
  580. start_mark: start_mark,
  581. end_mark: end_mark,
  582. anchor: anchor,
  583. tag: tag,
  584. implicit: implicit,
  585. style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE),
  586. }
  587. if parser.stem_comment != nil {
  588. event.head_comment = parser.stem_comment
  589. parser.stem_comment = nil
  590. }
  591. return true
  592. }
  593. if len(anchor) > 0 || len(tag) > 0 {
  594. parser.state = parser.states[len(parser.states)-1]
  595. parser.states = parser.states[:len(parser.states)-1]
  596. *event = yaml_event_t{
  597. typ: yaml_SCALAR_EVENT,
  598. start_mark: start_mark,
  599. end_mark: end_mark,
  600. anchor: anchor,
  601. tag: tag,
  602. implicit: implicit,
  603. quoted_implicit: false,
  604. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  605. }
  606. return true
  607. }
  608. context := "while parsing a flow node"
  609. if block {
  610. context = "while parsing a block node"
  611. }
  612. yaml_parser_set_parser_error_context(parser, context, start_mark,
  613. "did not find expected node content", token.start_mark)
  614. return false
  615. }
  616. // Parse the productions:
  617. // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
  618. // ******************** *********** * *********
  619. //
  620. func yaml_parser_parse_block_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  621. if first {
  622. token := peek_token(parser)
  623. if token == nil {
  624. return false
  625. }
  626. parser.marks = append(parser.marks, token.start_mark)
  627. skip_token(parser)
  628. }
  629. token := peek_token(parser)
  630. if token == nil {
  631. return false
  632. }
  633. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  634. mark := token.end_mark
  635. prior_head_len := len(parser.head_comment)
  636. skip_token(parser)
  637. yaml_parser_split_stem_comment(parser, prior_head_len)
  638. token = peek_token(parser)
  639. if token == nil {
  640. return false
  641. }
  642. if token.typ != yaml_BLOCK_ENTRY_TOKEN && token.typ != yaml_BLOCK_END_TOKEN {
  643. parser.states = append(parser.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE)
  644. return yaml_parser_parse_node(parser, event, true, false)
  645. } else {
  646. parser.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE
  647. return yaml_parser_process_empty_scalar(parser, event, mark)
  648. }
  649. }
  650. if token.typ == yaml_BLOCK_END_TOKEN {
  651. parser.state = parser.states[len(parser.states)-1]
  652. parser.states = parser.states[:len(parser.states)-1]
  653. parser.marks = parser.marks[:len(parser.marks)-1]
  654. *event = yaml_event_t{
  655. typ: yaml_SEQUENCE_END_EVENT,
  656. start_mark: token.start_mark,
  657. end_mark: token.end_mark,
  658. }
  659. skip_token(parser)
  660. return true
  661. }
  662. context_mark := parser.marks[len(parser.marks)-1]
  663. parser.marks = parser.marks[:len(parser.marks)-1]
  664. return yaml_parser_set_parser_error_context(parser,
  665. "while parsing a block collection", context_mark,
  666. "did not find expected '-' indicator", token.start_mark)
  667. }
  668. // Parse the productions:
  669. // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
  670. // *********** *
  671. func yaml_parser_parse_indentless_sequence_entry(parser *yaml_parser_t, event *yaml_event_t) bool {
  672. token := peek_token(parser)
  673. if token == nil {
  674. return false
  675. }
  676. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  677. mark := token.end_mark
  678. prior_head_len := len(parser.head_comment)
  679. skip_token(parser)
  680. yaml_parser_split_stem_comment(parser, prior_head_len)
  681. token = peek_token(parser)
  682. if token == nil {
  683. return false
  684. }
  685. if token.typ != yaml_BLOCK_ENTRY_TOKEN &&
  686. token.typ != yaml_KEY_TOKEN &&
  687. token.typ != yaml_VALUE_TOKEN &&
  688. token.typ != yaml_BLOCK_END_TOKEN {
  689. parser.states = append(parser.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE)
  690. return yaml_parser_parse_node(parser, event, true, false)
  691. }
  692. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  693. return yaml_parser_process_empty_scalar(parser, event, mark)
  694. }
  695. parser.state = parser.states[len(parser.states)-1]
  696. parser.states = parser.states[:len(parser.states)-1]
  697. *event = yaml_event_t{
  698. typ: yaml_SEQUENCE_END_EVENT,
  699. start_mark: token.start_mark,
  700. end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark?
  701. }
  702. return true
  703. }
  704. // Split stem comment from head comment.
  705. //
  706. // When a sequence or map is found under a sequence entry, the former head comment
  707. // is assigned to the underlying sequence or map as a whole, not the individual
  708. // sequence or map entry as would be expected otherwise. To handle this case the
  709. // previous head comment is moved aside as the stem comment.
  710. func yaml_parser_split_stem_comment(parser *yaml_parser_t, stem_len int) {
  711. if stem_len == 0 {
  712. return
  713. }
  714. token := peek_token(parser)
  715. if token == nil || token.typ != yaml_BLOCK_SEQUENCE_START_TOKEN && token.typ != yaml_BLOCK_MAPPING_START_TOKEN {
  716. return
  717. }
  718. parser.stem_comment = parser.head_comment[:stem_len]
  719. if len(parser.head_comment) == stem_len {
  720. parser.head_comment = nil
  721. } else {
  722. // Copy suffix to prevent very strange bugs if someone ever appends
  723. // further bytes to the prefix in the stem_comment slice above.
  724. parser.head_comment = append([]byte(nil), parser.head_comment[stem_len+1:]...)
  725. }
  726. }
  727. // Parse the productions:
  728. // block_mapping ::= BLOCK-MAPPING_START
  729. // *******************
  730. // ((KEY block_node_or_indentless_sequence?)?
  731. // *** *
  732. // (VALUE block_node_or_indentless_sequence?)?)*
  733. //
  734. // BLOCK-END
  735. // *********
  736. //
  737. func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  738. if first {
  739. token := peek_token(parser)
  740. if token == nil {
  741. return false
  742. }
  743. parser.marks = append(parser.marks, token.start_mark)
  744. skip_token(parser)
  745. }
  746. token := peek_token(parser)
  747. if token == nil {
  748. return false
  749. }
  750. // [Go] A tail comment was left from the prior mapping value processed. Emit an event
  751. // as it needs to be processed with that value and not the following key.
  752. if len(parser.tail_comment) > 0 {
  753. *event = yaml_event_t{
  754. typ: yaml_TAIL_COMMENT_EVENT,
  755. start_mark: token.start_mark,
  756. end_mark: token.end_mark,
  757. foot_comment: parser.tail_comment,
  758. }
  759. parser.tail_comment = nil
  760. return true
  761. }
  762. if token.typ == yaml_KEY_TOKEN {
  763. mark := token.end_mark
  764. skip_token(parser)
  765. token = peek_token(parser)
  766. if token == nil {
  767. return false
  768. }
  769. if token.typ != yaml_KEY_TOKEN &&
  770. token.typ != yaml_VALUE_TOKEN &&
  771. token.typ != yaml_BLOCK_END_TOKEN {
  772. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE)
  773. return yaml_parser_parse_node(parser, event, true, true)
  774. } else {
  775. parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE
  776. return yaml_parser_process_empty_scalar(parser, event, mark)
  777. }
  778. } else if token.typ == yaml_BLOCK_END_TOKEN {
  779. parser.state = parser.states[len(parser.states)-1]
  780. parser.states = parser.states[:len(parser.states)-1]
  781. parser.marks = parser.marks[:len(parser.marks)-1]
  782. *event = yaml_event_t{
  783. typ: yaml_MAPPING_END_EVENT,
  784. start_mark: token.start_mark,
  785. end_mark: token.end_mark,
  786. }
  787. yaml_parser_set_event_comments(parser, event)
  788. skip_token(parser)
  789. return true
  790. }
  791. context_mark := parser.marks[len(parser.marks)-1]
  792. parser.marks = parser.marks[:len(parser.marks)-1]
  793. return yaml_parser_set_parser_error_context(parser,
  794. "while parsing a block mapping", context_mark,
  795. "did not find expected key", token.start_mark)
  796. }
  797. // Parse the productions:
  798. // block_mapping ::= BLOCK-MAPPING_START
  799. //
  800. // ((KEY block_node_or_indentless_sequence?)?
  801. //
  802. // (VALUE block_node_or_indentless_sequence?)?)*
  803. // ***** *
  804. // BLOCK-END
  805. //
  806. //
  807. func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  808. token := peek_token(parser)
  809. if token == nil {
  810. return false
  811. }
  812. if token.typ == yaml_VALUE_TOKEN {
  813. mark := token.end_mark
  814. skip_token(parser)
  815. token = peek_token(parser)
  816. if token == nil {
  817. return false
  818. }
  819. if token.typ != yaml_KEY_TOKEN &&
  820. token.typ != yaml_VALUE_TOKEN &&
  821. token.typ != yaml_BLOCK_END_TOKEN {
  822. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE)
  823. return yaml_parser_parse_node(parser, event, true, true)
  824. }
  825. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  826. return yaml_parser_process_empty_scalar(parser, event, mark)
  827. }
  828. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  829. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  830. }
  831. // Parse the productions:
  832. // flow_sequence ::= FLOW-SEQUENCE-START
  833. // *******************
  834. // (flow_sequence_entry FLOW-ENTRY)*
  835. // * **********
  836. // flow_sequence_entry?
  837. // *
  838. // FLOW-SEQUENCE-END
  839. // *****************
  840. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  841. // *
  842. //
  843. func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  844. if first {
  845. token := peek_token(parser)
  846. if token == nil {
  847. return false
  848. }
  849. parser.marks = append(parser.marks, token.start_mark)
  850. skip_token(parser)
  851. }
  852. token := peek_token(parser)
  853. if token == nil {
  854. return false
  855. }
  856. if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  857. if !first {
  858. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  859. skip_token(parser)
  860. token = peek_token(parser)
  861. if token == nil {
  862. return false
  863. }
  864. } else {
  865. context_mark := parser.marks[len(parser.marks)-1]
  866. parser.marks = parser.marks[:len(parser.marks)-1]
  867. return yaml_parser_set_parser_error_context(parser,
  868. "while parsing a flow sequence", context_mark,
  869. "did not find expected ',' or ']'", token.start_mark)
  870. }
  871. }
  872. if token.typ == yaml_KEY_TOKEN {
  873. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE
  874. *event = yaml_event_t{
  875. typ: yaml_MAPPING_START_EVENT,
  876. start_mark: token.start_mark,
  877. end_mark: token.end_mark,
  878. implicit: true,
  879. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  880. }
  881. skip_token(parser)
  882. return true
  883. } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  884. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE)
  885. return yaml_parser_parse_node(parser, event, false, false)
  886. }
  887. }
  888. parser.state = parser.states[len(parser.states)-1]
  889. parser.states = parser.states[:len(parser.states)-1]
  890. parser.marks = parser.marks[:len(parser.marks)-1]
  891. *event = yaml_event_t{
  892. typ: yaml_SEQUENCE_END_EVENT,
  893. start_mark: token.start_mark,
  894. end_mark: token.end_mark,
  895. }
  896. yaml_parser_set_event_comments(parser, event)
  897. skip_token(parser)
  898. return true
  899. }
  900. //
  901. // Parse the productions:
  902. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  903. // *** *
  904. //
  905. func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool {
  906. token := peek_token(parser)
  907. if token == nil {
  908. return false
  909. }
  910. if token.typ != yaml_VALUE_TOKEN &&
  911. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  912. token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  913. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE)
  914. return yaml_parser_parse_node(parser, event, false, false)
  915. }
  916. mark := token.end_mark
  917. skip_token(parser)
  918. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE
  919. return yaml_parser_process_empty_scalar(parser, event, mark)
  920. }
  921. // Parse the productions:
  922. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  923. // ***** *
  924. //
  925. func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  926. token := peek_token(parser)
  927. if token == nil {
  928. return false
  929. }
  930. if token.typ == yaml_VALUE_TOKEN {
  931. skip_token(parser)
  932. token := peek_token(parser)
  933. if token == nil {
  934. return false
  935. }
  936. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  937. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE)
  938. return yaml_parser_parse_node(parser, event, false, false)
  939. }
  940. }
  941. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE
  942. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  943. }
  944. // Parse the productions:
  945. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  946. // *
  947. //
  948. func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  949. token := peek_token(parser)
  950. if token == nil {
  951. return false
  952. }
  953. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE
  954. *event = yaml_event_t{
  955. typ: yaml_MAPPING_END_EVENT,
  956. start_mark: token.start_mark,
  957. end_mark: token.start_mark, // [Go] Shouldn't this be end_mark?
  958. }
  959. return true
  960. }
  961. // Parse the productions:
  962. // flow_mapping ::= FLOW-MAPPING-START
  963. // ******************
  964. // (flow_mapping_entry FLOW-ENTRY)*
  965. // * **********
  966. // flow_mapping_entry?
  967. // ******************
  968. // FLOW-MAPPING-END
  969. // ****************
  970. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  971. // * *** *
  972. //
  973. func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  974. if first {
  975. token := peek_token(parser)
  976. parser.marks = append(parser.marks, token.start_mark)
  977. skip_token(parser)
  978. }
  979. token := peek_token(parser)
  980. if token == nil {
  981. return false
  982. }
  983. if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  984. if !first {
  985. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  986. skip_token(parser)
  987. token = peek_token(parser)
  988. if token == nil {
  989. return false
  990. }
  991. } else {
  992. context_mark := parser.marks[len(parser.marks)-1]
  993. parser.marks = parser.marks[:len(parser.marks)-1]
  994. return yaml_parser_set_parser_error_context(parser,
  995. "while parsing a flow mapping", context_mark,
  996. "did not find expected ',' or '}'", token.start_mark)
  997. }
  998. }
  999. if token.typ == yaml_KEY_TOKEN {
  1000. skip_token(parser)
  1001. token = peek_token(parser)
  1002. if token == nil {
  1003. return false
  1004. }
  1005. if token.typ != yaml_VALUE_TOKEN &&
  1006. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  1007. token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  1008. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE)
  1009. return yaml_parser_parse_node(parser, event, false, false)
  1010. } else {
  1011. parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE
  1012. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  1013. }
  1014. } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  1015. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE)
  1016. return yaml_parser_parse_node(parser, event, false, false)
  1017. }
  1018. }
  1019. parser.state = parser.states[len(parser.states)-1]
  1020. parser.states = parser.states[:len(parser.states)-1]
  1021. parser.marks = parser.marks[:len(parser.marks)-1]
  1022. *event = yaml_event_t{
  1023. typ: yaml_MAPPING_END_EVENT,
  1024. start_mark: token.start_mark,
  1025. end_mark: token.end_mark,
  1026. }
  1027. yaml_parser_set_event_comments(parser, event)
  1028. skip_token(parser)
  1029. return true
  1030. }
  1031. // Parse the productions:
  1032. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  1033. // * ***** *
  1034. //
  1035. func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool {
  1036. token := peek_token(parser)
  1037. if token == nil {
  1038. return false
  1039. }
  1040. if empty {
  1041. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  1042. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  1043. }
  1044. if token.typ == yaml_VALUE_TOKEN {
  1045. skip_token(parser)
  1046. token = peek_token(parser)
  1047. if token == nil {
  1048. return false
  1049. }
  1050. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  1051. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE)
  1052. return yaml_parser_parse_node(parser, event, false, false)
  1053. }
  1054. }
  1055. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  1056. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  1057. }
  1058. // Generate an empty scalar event.
  1059. func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool {
  1060. *event = yaml_event_t{
  1061. typ: yaml_SCALAR_EVENT,
  1062. start_mark: mark,
  1063. end_mark: mark,
  1064. value: nil, // Empty
  1065. implicit: true,
  1066. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  1067. }
  1068. return true
  1069. }
  1070. var default_tag_directives = []yaml_tag_directive_t{
  1071. {[]byte("!"), []byte("!")},
  1072. {[]byte("!!"), []byte("tag:yaml.org,2002:")},
  1073. }
  1074. // Parse directives.
  1075. func yaml_parser_process_directives(parser *yaml_parser_t,
  1076. version_directive_ref **yaml_version_directive_t,
  1077. tag_directives_ref *[]yaml_tag_directive_t) bool {
  1078. var version_directive *yaml_version_directive_t
  1079. var tag_directives []yaml_tag_directive_t
  1080. token := peek_token(parser)
  1081. if token == nil {
  1082. return false
  1083. }
  1084. for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  1085. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN {
  1086. if version_directive != nil {
  1087. yaml_parser_set_parser_error(parser,
  1088. "found duplicate %YAML directive", token.start_mark)
  1089. return false
  1090. }
  1091. if token.major != 1 || token.minor != 1 {
  1092. yaml_parser_set_parser_error(parser,
  1093. "found incompatible YAML document", token.start_mark)
  1094. return false
  1095. }
  1096. version_directive = &yaml_version_directive_t{
  1097. major: token.major,
  1098. minor: token.minor,
  1099. }
  1100. } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  1101. value := yaml_tag_directive_t{
  1102. handle: token.value,
  1103. prefix: token.prefix,
  1104. }
  1105. if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) {
  1106. return false
  1107. }
  1108. tag_directives = append(tag_directives, value)
  1109. }
  1110. skip_token(parser)
  1111. token = peek_token(parser)
  1112. if token == nil {
  1113. return false
  1114. }
  1115. }
  1116. for i := range default_tag_directives {
  1117. if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) {
  1118. return false
  1119. }
  1120. }
  1121. if version_directive_ref != nil {
  1122. *version_directive_ref = version_directive
  1123. }
  1124. if tag_directives_ref != nil {
  1125. *tag_directives_ref = tag_directives
  1126. }
  1127. return true
  1128. }
  1129. // Append a tag directive to the directives stack.
  1130. func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool {
  1131. for i := range parser.tag_directives {
  1132. if bytes.Equal(value.handle, parser.tag_directives[i].handle) {
  1133. if allow_duplicates {
  1134. return true
  1135. }
  1136. return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark)
  1137. }
  1138. }
  1139. // [Go] I suspect the copy is unnecessary. This was likely done
  1140. // because there was no way to track ownership of the data.
  1141. value_copy := yaml_tag_directive_t{
  1142. handle: make([]byte, len(value.handle)),
  1143. prefix: make([]byte, len(value.prefix)),
  1144. }
  1145. copy(value_copy.handle, value.handle)
  1146. copy(value_copy.prefix, value.prefix)
  1147. parser.tag_directives = append(parser.tag_directives, value_copy)
  1148. return true
  1149. }