parserc.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. }
  371. // Parse the productions:
  372. // block_node_or_indentless_sequence ::=
  373. // ALIAS
  374. // *****
  375. // | properties (block_content | indentless_block_sequence)?
  376. // ********** *
  377. // | block_content | indentless_block_sequence
  378. // *
  379. // block_node ::= ALIAS
  380. // *****
  381. // | properties block_content?
  382. // ********** *
  383. // | block_content
  384. // *
  385. // flow_node ::= ALIAS
  386. // *****
  387. // | properties flow_content?
  388. // ********** *
  389. // | flow_content
  390. // *
  391. // properties ::= TAG ANCHOR? | ANCHOR TAG?
  392. // *************************
  393. // block_content ::= block_collection | flow_collection | SCALAR
  394. // ******
  395. // flow_content ::= flow_collection | SCALAR
  396. // ******
  397. func yaml_parser_parse_node(parser *yaml_parser_t, event *yaml_event_t, block, indentless_sequence bool) bool {
  398. //defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)()
  399. token := peek_token(parser)
  400. if token == nil {
  401. return false
  402. }
  403. if token.typ == yaml_ALIAS_TOKEN {
  404. parser.state = parser.states[len(parser.states)-1]
  405. parser.states = parser.states[:len(parser.states)-1]
  406. *event = yaml_event_t{
  407. typ: yaml_ALIAS_EVENT,
  408. start_mark: token.start_mark,
  409. end_mark: token.end_mark,
  410. anchor: token.value,
  411. }
  412. yaml_parser_set_event_comments(parser, event)
  413. skip_token(parser)
  414. return true
  415. }
  416. start_mark := token.start_mark
  417. end_mark := token.start_mark
  418. var tag_token bool
  419. var tag_handle, tag_suffix, anchor []byte
  420. var tag_mark yaml_mark_t
  421. if token.typ == yaml_ANCHOR_TOKEN {
  422. anchor = token.value
  423. start_mark = token.start_mark
  424. end_mark = token.end_mark
  425. skip_token(parser)
  426. token = peek_token(parser)
  427. if token == nil {
  428. return false
  429. }
  430. if token.typ == yaml_TAG_TOKEN {
  431. tag_token = true
  432. tag_handle = token.value
  433. tag_suffix = token.suffix
  434. tag_mark = token.start_mark
  435. end_mark = token.end_mark
  436. skip_token(parser)
  437. token = peek_token(parser)
  438. if token == nil {
  439. return false
  440. }
  441. }
  442. } else if token.typ == yaml_TAG_TOKEN {
  443. tag_token = true
  444. tag_handle = token.value
  445. tag_suffix = token.suffix
  446. start_mark = token.start_mark
  447. tag_mark = token.start_mark
  448. end_mark = token.end_mark
  449. skip_token(parser)
  450. token = peek_token(parser)
  451. if token == nil {
  452. return false
  453. }
  454. if token.typ == yaml_ANCHOR_TOKEN {
  455. anchor = token.value
  456. end_mark = token.end_mark
  457. skip_token(parser)
  458. token = peek_token(parser)
  459. if token == nil {
  460. return false
  461. }
  462. }
  463. }
  464. var tag []byte
  465. if tag_token {
  466. if len(tag_handle) == 0 {
  467. tag = tag_suffix
  468. tag_suffix = nil
  469. } else {
  470. for i := range parser.tag_directives {
  471. if bytes.Equal(parser.tag_directives[i].handle, tag_handle) {
  472. tag = append([]byte(nil), parser.tag_directives[i].prefix...)
  473. tag = append(tag, tag_suffix...)
  474. break
  475. }
  476. }
  477. if len(tag) == 0 {
  478. yaml_parser_set_parser_error_context(parser,
  479. "while parsing a node", start_mark,
  480. "found undefined tag handle", tag_mark)
  481. return false
  482. }
  483. }
  484. }
  485. implicit := len(tag) == 0
  486. if indentless_sequence && token.typ == yaml_BLOCK_ENTRY_TOKEN {
  487. end_mark = token.end_mark
  488. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  489. *event = yaml_event_t{
  490. typ: yaml_SEQUENCE_START_EVENT,
  491. start_mark: start_mark,
  492. end_mark: end_mark,
  493. anchor: anchor,
  494. tag: tag,
  495. implicit: implicit,
  496. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  497. }
  498. return true
  499. }
  500. if token.typ == yaml_SCALAR_TOKEN {
  501. var plain_implicit, quoted_implicit bool
  502. end_mark = token.end_mark
  503. if (len(tag) == 0 && token.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') {
  504. plain_implicit = true
  505. } else if len(tag) == 0 {
  506. quoted_implicit = true
  507. }
  508. parser.state = parser.states[len(parser.states)-1]
  509. parser.states = parser.states[:len(parser.states)-1]
  510. *event = yaml_event_t{
  511. typ: yaml_SCALAR_EVENT,
  512. start_mark: start_mark,
  513. end_mark: end_mark,
  514. anchor: anchor,
  515. tag: tag,
  516. value: token.value,
  517. implicit: plain_implicit,
  518. quoted_implicit: quoted_implicit,
  519. style: yaml_style_t(token.style),
  520. }
  521. yaml_parser_set_event_comments(parser, event)
  522. skip_token(parser)
  523. return true
  524. }
  525. if token.typ == yaml_FLOW_SEQUENCE_START_TOKEN {
  526. // [Go] Some of the events below can be merged as they differ only on style.
  527. end_mark = token.end_mark
  528. parser.state = yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE
  529. *event = yaml_event_t{
  530. typ: yaml_SEQUENCE_START_EVENT,
  531. start_mark: start_mark,
  532. end_mark: end_mark,
  533. anchor: anchor,
  534. tag: tag,
  535. implicit: implicit,
  536. style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE),
  537. }
  538. yaml_parser_set_event_comments(parser, event)
  539. return true
  540. }
  541. if token.typ == yaml_FLOW_MAPPING_START_TOKEN {
  542. end_mark = token.end_mark
  543. parser.state = yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE
  544. *event = yaml_event_t{
  545. typ: yaml_MAPPING_START_EVENT,
  546. start_mark: start_mark,
  547. end_mark: end_mark,
  548. anchor: anchor,
  549. tag: tag,
  550. implicit: implicit,
  551. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  552. }
  553. yaml_parser_set_event_comments(parser, event)
  554. return true
  555. }
  556. if block && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN {
  557. end_mark = token.end_mark
  558. parser.state = yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE
  559. *event = yaml_event_t{
  560. typ: yaml_SEQUENCE_START_EVENT,
  561. start_mark: start_mark,
  562. end_mark: end_mark,
  563. anchor: anchor,
  564. tag: tag,
  565. implicit: implicit,
  566. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  567. }
  568. return true
  569. }
  570. if block && token.typ == yaml_BLOCK_MAPPING_START_TOKEN {
  571. end_mark = token.end_mark
  572. parser.state = yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE
  573. *event = yaml_event_t{
  574. typ: yaml_MAPPING_START_EVENT,
  575. start_mark: start_mark,
  576. end_mark: end_mark,
  577. anchor: anchor,
  578. tag: tag,
  579. implicit: implicit,
  580. style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE),
  581. }
  582. return true
  583. }
  584. if len(anchor) > 0 || len(tag) > 0 {
  585. parser.state = parser.states[len(parser.states)-1]
  586. parser.states = parser.states[:len(parser.states)-1]
  587. *event = yaml_event_t{
  588. typ: yaml_SCALAR_EVENT,
  589. start_mark: start_mark,
  590. end_mark: end_mark,
  591. anchor: anchor,
  592. tag: tag,
  593. implicit: implicit,
  594. quoted_implicit: false,
  595. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  596. }
  597. return true
  598. }
  599. context := "while parsing a flow node"
  600. if block {
  601. context = "while parsing a block node"
  602. }
  603. yaml_parser_set_parser_error_context(parser, context, start_mark,
  604. "did not find expected node content", token.start_mark)
  605. return false
  606. }
  607. // Parse the productions:
  608. // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
  609. // ******************** *********** * *********
  610. //
  611. func yaml_parser_parse_block_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  612. if first {
  613. token := peek_token(parser)
  614. parser.marks = append(parser.marks, token.start_mark)
  615. skip_token(parser)
  616. }
  617. token := peek_token(parser)
  618. if token == nil {
  619. return false
  620. }
  621. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  622. mark := token.end_mark
  623. skip_token(parser)
  624. token = peek_token(parser)
  625. if token == nil {
  626. return false
  627. }
  628. if token.typ != yaml_BLOCK_ENTRY_TOKEN && token.typ != yaml_BLOCK_END_TOKEN {
  629. parser.states = append(parser.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE)
  630. return yaml_parser_parse_node(parser, event, true, false)
  631. } else {
  632. parser.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE
  633. return yaml_parser_process_empty_scalar(parser, event, mark)
  634. }
  635. }
  636. if token.typ == yaml_BLOCK_END_TOKEN {
  637. parser.state = parser.states[len(parser.states)-1]
  638. parser.states = parser.states[:len(parser.states)-1]
  639. parser.marks = parser.marks[:len(parser.marks)-1]
  640. *event = yaml_event_t{
  641. typ: yaml_SEQUENCE_END_EVENT,
  642. start_mark: token.start_mark,
  643. end_mark: token.end_mark,
  644. }
  645. skip_token(parser)
  646. return true
  647. }
  648. context_mark := parser.marks[len(parser.marks)-1]
  649. parser.marks = parser.marks[:len(parser.marks)-1]
  650. return yaml_parser_set_parser_error_context(parser,
  651. "while parsing a block collection", context_mark,
  652. "did not find expected '-' indicator", token.start_mark)
  653. }
  654. // Parse the productions:
  655. // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
  656. // *********** *
  657. func yaml_parser_parse_indentless_sequence_entry(parser *yaml_parser_t, event *yaml_event_t) bool {
  658. token := peek_token(parser)
  659. if token == nil {
  660. return false
  661. }
  662. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  663. mark := token.end_mark
  664. skip_token(parser)
  665. token = peek_token(parser)
  666. if token == nil {
  667. return false
  668. }
  669. if token.typ != yaml_BLOCK_ENTRY_TOKEN &&
  670. token.typ != yaml_KEY_TOKEN &&
  671. token.typ != yaml_VALUE_TOKEN &&
  672. token.typ != yaml_BLOCK_END_TOKEN {
  673. parser.states = append(parser.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE)
  674. return yaml_parser_parse_node(parser, event, true, false)
  675. }
  676. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  677. return yaml_parser_process_empty_scalar(parser, event, mark)
  678. }
  679. parser.state = parser.states[len(parser.states)-1]
  680. parser.states = parser.states[:len(parser.states)-1]
  681. *event = yaml_event_t{
  682. typ: yaml_SEQUENCE_END_EVENT,
  683. start_mark: token.start_mark,
  684. end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark?
  685. }
  686. return true
  687. }
  688. // Parse the productions:
  689. // block_mapping ::= BLOCK-MAPPING_START
  690. // *******************
  691. // ((KEY block_node_or_indentless_sequence?)?
  692. // *** *
  693. // (VALUE block_node_or_indentless_sequence?)?)*
  694. //
  695. // BLOCK-END
  696. // *********
  697. //
  698. func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  699. if first {
  700. token := peek_token(parser)
  701. parser.marks = append(parser.marks, token.start_mark)
  702. skip_token(parser)
  703. }
  704. token := peek_token(parser)
  705. if token == nil {
  706. return false
  707. }
  708. // [Go] A tail comment was left from the prior mapping value processed. Emit an event
  709. // as it needs to be processed with that value and not the following key.
  710. if len(parser.tail_comment) > 0 {
  711. *event = yaml_event_t{
  712. typ: yaml_TAIL_COMMENT_EVENT,
  713. start_mark: token.start_mark,
  714. end_mark: token.end_mark,
  715. foot_comment: parser.tail_comment,
  716. }
  717. parser.tail_comment = nil
  718. return true
  719. }
  720. if token.typ == yaml_KEY_TOKEN {
  721. mark := token.end_mark
  722. skip_token(parser)
  723. token = peek_token(parser)
  724. if token == nil {
  725. return false
  726. }
  727. if token.typ != yaml_KEY_TOKEN &&
  728. token.typ != yaml_VALUE_TOKEN &&
  729. token.typ != yaml_BLOCK_END_TOKEN {
  730. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE)
  731. return yaml_parser_parse_node(parser, event, true, true)
  732. } else {
  733. parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE
  734. return yaml_parser_process_empty_scalar(parser, event, mark)
  735. }
  736. } else if token.typ == yaml_BLOCK_END_TOKEN {
  737. parser.state = parser.states[len(parser.states)-1]
  738. parser.states = parser.states[:len(parser.states)-1]
  739. parser.marks = parser.marks[:len(parser.marks)-1]
  740. *event = yaml_event_t{
  741. typ: yaml_MAPPING_END_EVENT,
  742. start_mark: token.start_mark,
  743. end_mark: token.end_mark,
  744. }
  745. yaml_parser_set_event_comments(parser, event)
  746. skip_token(parser)
  747. return true
  748. }
  749. context_mark := parser.marks[len(parser.marks)-1]
  750. parser.marks = parser.marks[:len(parser.marks)-1]
  751. return yaml_parser_set_parser_error_context(parser,
  752. "while parsing a block mapping", context_mark,
  753. "did not find expected key", token.start_mark)
  754. }
  755. // Parse the productions:
  756. // block_mapping ::= BLOCK-MAPPING_START
  757. //
  758. // ((KEY block_node_or_indentless_sequence?)?
  759. //
  760. // (VALUE block_node_or_indentless_sequence?)?)*
  761. // ***** *
  762. // BLOCK-END
  763. //
  764. //
  765. func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  766. token := peek_token(parser)
  767. if token == nil {
  768. return false
  769. }
  770. if token.typ == yaml_VALUE_TOKEN {
  771. mark := token.end_mark
  772. skip_token(parser)
  773. token = peek_token(parser)
  774. if token == nil {
  775. return false
  776. }
  777. if token.typ != yaml_KEY_TOKEN &&
  778. token.typ != yaml_VALUE_TOKEN &&
  779. token.typ != yaml_BLOCK_END_TOKEN {
  780. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE)
  781. return yaml_parser_parse_node(parser, event, true, true)
  782. }
  783. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  784. return yaml_parser_process_empty_scalar(parser, event, mark)
  785. }
  786. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  787. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  788. }
  789. // Parse the productions:
  790. // flow_sequence ::= FLOW-SEQUENCE-START
  791. // *******************
  792. // (flow_sequence_entry FLOW-ENTRY)*
  793. // * **********
  794. // flow_sequence_entry?
  795. // *
  796. // FLOW-SEQUENCE-END
  797. // *****************
  798. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  799. // *
  800. //
  801. func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  802. if first {
  803. token := peek_token(parser)
  804. parser.marks = append(parser.marks, token.start_mark)
  805. skip_token(parser)
  806. }
  807. token := peek_token(parser)
  808. if token == nil {
  809. return false
  810. }
  811. if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  812. if !first {
  813. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  814. skip_token(parser)
  815. token = peek_token(parser)
  816. if token == nil {
  817. return false
  818. }
  819. } else {
  820. context_mark := parser.marks[len(parser.marks)-1]
  821. parser.marks = parser.marks[:len(parser.marks)-1]
  822. return yaml_parser_set_parser_error_context(parser,
  823. "while parsing a flow sequence", context_mark,
  824. "did not find expected ',' or ']'", token.start_mark)
  825. }
  826. }
  827. if token.typ == yaml_KEY_TOKEN {
  828. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE
  829. *event = yaml_event_t{
  830. typ: yaml_MAPPING_START_EVENT,
  831. start_mark: token.start_mark,
  832. end_mark: token.end_mark,
  833. implicit: true,
  834. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  835. }
  836. skip_token(parser)
  837. return true
  838. } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  839. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE)
  840. return yaml_parser_parse_node(parser, event, false, false)
  841. }
  842. }
  843. parser.state = parser.states[len(parser.states)-1]
  844. parser.states = parser.states[:len(parser.states)-1]
  845. parser.marks = parser.marks[:len(parser.marks)-1]
  846. *event = yaml_event_t{
  847. typ: yaml_SEQUENCE_END_EVENT,
  848. start_mark: token.start_mark,
  849. end_mark: token.end_mark,
  850. }
  851. yaml_parser_set_event_comments(parser, event)
  852. skip_token(parser)
  853. return true
  854. }
  855. //
  856. // Parse the productions:
  857. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  858. // *** *
  859. //
  860. func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool {
  861. token := peek_token(parser)
  862. if token == nil {
  863. return false
  864. }
  865. if token.typ != yaml_VALUE_TOKEN &&
  866. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  867. token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  868. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE)
  869. return yaml_parser_parse_node(parser, event, false, false)
  870. }
  871. mark := token.end_mark
  872. skip_token(parser)
  873. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE
  874. return yaml_parser_process_empty_scalar(parser, event, mark)
  875. }
  876. // Parse the productions:
  877. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  878. // ***** *
  879. //
  880. func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  881. token := peek_token(parser)
  882. if token == nil {
  883. return false
  884. }
  885. if token.typ == yaml_VALUE_TOKEN {
  886. skip_token(parser)
  887. token := peek_token(parser)
  888. if token == nil {
  889. return false
  890. }
  891. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  892. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE)
  893. return yaml_parser_parse_node(parser, event, false, false)
  894. }
  895. }
  896. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE
  897. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  898. }
  899. // Parse the productions:
  900. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  901. // *
  902. //
  903. func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  904. token := peek_token(parser)
  905. if token == nil {
  906. return false
  907. }
  908. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE
  909. *event = yaml_event_t{
  910. typ: yaml_MAPPING_END_EVENT,
  911. start_mark: token.start_mark,
  912. end_mark: token.start_mark, // [Go] Shouldn't this be end_mark?
  913. }
  914. return true
  915. }
  916. // Parse the productions:
  917. // flow_mapping ::= FLOW-MAPPING-START
  918. // ******************
  919. // (flow_mapping_entry FLOW-ENTRY)*
  920. // * **********
  921. // flow_mapping_entry?
  922. // ******************
  923. // FLOW-MAPPING-END
  924. // ****************
  925. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  926. // * *** *
  927. //
  928. func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  929. if first {
  930. token := peek_token(parser)
  931. parser.marks = append(parser.marks, token.start_mark)
  932. skip_token(parser)
  933. }
  934. token := peek_token(parser)
  935. if token == nil {
  936. return false
  937. }
  938. if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  939. if !first {
  940. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  941. skip_token(parser)
  942. token = peek_token(parser)
  943. if token == nil {
  944. return false
  945. }
  946. } else {
  947. context_mark := parser.marks[len(parser.marks)-1]
  948. parser.marks = parser.marks[:len(parser.marks)-1]
  949. return yaml_parser_set_parser_error_context(parser,
  950. "while parsing a flow mapping", context_mark,
  951. "did not find expected ',' or '}'", token.start_mark)
  952. }
  953. }
  954. if token.typ == yaml_KEY_TOKEN {
  955. skip_token(parser)
  956. token = peek_token(parser)
  957. if token == nil {
  958. return false
  959. }
  960. if token.typ != yaml_VALUE_TOKEN &&
  961. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  962. token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  963. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE)
  964. return yaml_parser_parse_node(parser, event, false, false)
  965. } else {
  966. parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE
  967. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  968. }
  969. } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  970. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE)
  971. return yaml_parser_parse_node(parser, event, false, false)
  972. }
  973. }
  974. parser.state = parser.states[len(parser.states)-1]
  975. parser.states = parser.states[:len(parser.states)-1]
  976. parser.marks = parser.marks[:len(parser.marks)-1]
  977. *event = yaml_event_t{
  978. typ: yaml_MAPPING_END_EVENT,
  979. start_mark: token.start_mark,
  980. end_mark: token.end_mark,
  981. }
  982. yaml_parser_set_event_comments(parser, event)
  983. skip_token(parser)
  984. return true
  985. }
  986. // Parse the productions:
  987. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  988. // * ***** *
  989. //
  990. func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool {
  991. token := peek_token(parser)
  992. if token == nil {
  993. return false
  994. }
  995. if empty {
  996. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  997. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  998. }
  999. if token.typ == yaml_VALUE_TOKEN {
  1000. skip_token(parser)
  1001. token = peek_token(parser)
  1002. if token == nil {
  1003. return false
  1004. }
  1005. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  1006. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE)
  1007. return yaml_parser_parse_node(parser, event, false, false)
  1008. }
  1009. }
  1010. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  1011. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  1012. }
  1013. // Generate an empty scalar event.
  1014. func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool {
  1015. *event = yaml_event_t{
  1016. typ: yaml_SCALAR_EVENT,
  1017. start_mark: mark,
  1018. end_mark: mark,
  1019. value: nil, // Empty
  1020. implicit: true,
  1021. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  1022. }
  1023. return true
  1024. }
  1025. var default_tag_directives = []yaml_tag_directive_t{
  1026. {[]byte("!"), []byte("!")},
  1027. {[]byte("!!"), []byte("tag:yaml.org,2002:")},
  1028. }
  1029. // Parse directives.
  1030. func yaml_parser_process_directives(parser *yaml_parser_t,
  1031. version_directive_ref **yaml_version_directive_t,
  1032. tag_directives_ref *[]yaml_tag_directive_t) bool {
  1033. var version_directive *yaml_version_directive_t
  1034. var tag_directives []yaml_tag_directive_t
  1035. token := peek_token(parser)
  1036. if token == nil {
  1037. return false
  1038. }
  1039. for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  1040. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN {
  1041. if version_directive != nil {
  1042. yaml_parser_set_parser_error(parser,
  1043. "found duplicate %YAML directive", token.start_mark)
  1044. return false
  1045. }
  1046. if token.major != 1 || token.minor != 1 {
  1047. yaml_parser_set_parser_error(parser,
  1048. "found incompatible YAML document", token.start_mark)
  1049. return false
  1050. }
  1051. version_directive = &yaml_version_directive_t{
  1052. major: token.major,
  1053. minor: token.minor,
  1054. }
  1055. } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  1056. value := yaml_tag_directive_t{
  1057. handle: token.value,
  1058. prefix: token.prefix,
  1059. }
  1060. if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) {
  1061. return false
  1062. }
  1063. tag_directives = append(tag_directives, value)
  1064. }
  1065. skip_token(parser)
  1066. token = peek_token(parser)
  1067. if token == nil {
  1068. return false
  1069. }
  1070. }
  1071. for i := range default_tag_directives {
  1072. if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) {
  1073. return false
  1074. }
  1075. }
  1076. if version_directive_ref != nil {
  1077. *version_directive_ref = version_directive
  1078. }
  1079. if tag_directives_ref != nil {
  1080. *tag_directives_ref = tag_directives
  1081. }
  1082. return true
  1083. }
  1084. // Append a tag directive to the directives stack.
  1085. func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool {
  1086. for i := range parser.tag_directives {
  1087. if bytes.Equal(value.handle, parser.tag_directives[i].handle) {
  1088. if allow_duplicates {
  1089. return true
  1090. }
  1091. return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark)
  1092. }
  1093. }
  1094. // [Go] I suspect the copy is unnecessary. This was likely done
  1095. // because there was no way to track ownership of the data.
  1096. value_copy := yaml_tag_directive_t{
  1097. handle: make([]byte, len(value.handle)),
  1098. prefix: make([]byte, len(value.prefix)),
  1099. }
  1100. copy(value_copy.handle, value.handle)
  1101. copy(value_copy.prefix, value.prefix)
  1102. parser.tag_directives = append(parser.tag_directives, value_copy)
  1103. return true
  1104. }