You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parserc.go 40 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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. return true
  588. }
  589. if len(anchor) > 0 || len(tag) > 0 {
  590. parser.state = parser.states[len(parser.states)-1]
  591. parser.states = parser.states[:len(parser.states)-1]
  592. *event = yaml_event_t{
  593. typ: yaml_SCALAR_EVENT,
  594. start_mark: start_mark,
  595. end_mark: end_mark,
  596. anchor: anchor,
  597. tag: tag,
  598. implicit: implicit,
  599. quoted_implicit: false,
  600. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  601. }
  602. return true
  603. }
  604. context := "while parsing a flow node"
  605. if block {
  606. context = "while parsing a block node"
  607. }
  608. yaml_parser_set_parser_error_context(parser, context, start_mark,
  609. "did not find expected node content", token.start_mark)
  610. return false
  611. }
  612. // Parse the productions:
  613. // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
  614. // ******************** *********** * *********
  615. //
  616. func yaml_parser_parse_block_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  617. if first {
  618. token := peek_token(parser)
  619. parser.marks = append(parser.marks, token.start_mark)
  620. skip_token(parser)
  621. }
  622. token := peek_token(parser)
  623. if token == nil {
  624. return false
  625. }
  626. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  627. mark := token.end_mark
  628. prior_head := len(parser.head_comment)
  629. skip_token(parser)
  630. token = peek_token(parser)
  631. if token == nil {
  632. return false
  633. }
  634. if prior_head > 0 && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN {
  635. // [Go] It's a sequence under a sequence entry, so the former head comment
  636. // is for the list itself, not the first list item under it.
  637. parser.stem_comment = parser.head_comment[:prior_head]
  638. if len(parser.head_comment) == prior_head {
  639. parser.head_comment = nil
  640. } else {
  641. // Copy suffix to prevent very strange bugs if someone ever appends
  642. // further bytes to the prefix in the stem_comment slice above.
  643. parser.head_comment = append([]byte(nil), parser.head_comment[prior_head+1:]...)
  644. }
  645. }
  646. if token.typ != yaml_BLOCK_ENTRY_TOKEN && token.typ != yaml_BLOCK_END_TOKEN {
  647. parser.states = append(parser.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE)
  648. return yaml_parser_parse_node(parser, event, true, false)
  649. } else {
  650. parser.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE
  651. return yaml_parser_process_empty_scalar(parser, event, mark)
  652. }
  653. }
  654. if token.typ == yaml_BLOCK_END_TOKEN {
  655. parser.state = parser.states[len(parser.states)-1]
  656. parser.states = parser.states[:len(parser.states)-1]
  657. parser.marks = parser.marks[:len(parser.marks)-1]
  658. *event = yaml_event_t{
  659. typ: yaml_SEQUENCE_END_EVENT,
  660. start_mark: token.start_mark,
  661. end_mark: token.end_mark,
  662. }
  663. skip_token(parser)
  664. return true
  665. }
  666. context_mark := parser.marks[len(parser.marks)-1]
  667. parser.marks = parser.marks[:len(parser.marks)-1]
  668. return yaml_parser_set_parser_error_context(parser,
  669. "while parsing a block collection", context_mark,
  670. "did not find expected '-' indicator", token.start_mark)
  671. }
  672. // Parse the productions:
  673. // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
  674. // *********** *
  675. func yaml_parser_parse_indentless_sequence_entry(parser *yaml_parser_t, event *yaml_event_t) bool {
  676. token := peek_token(parser)
  677. if token == nil {
  678. return false
  679. }
  680. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  681. mark := token.end_mark
  682. skip_token(parser)
  683. token = peek_token(parser)
  684. if token == nil {
  685. return false
  686. }
  687. if token.typ != yaml_BLOCK_ENTRY_TOKEN &&
  688. token.typ != yaml_KEY_TOKEN &&
  689. token.typ != yaml_VALUE_TOKEN &&
  690. token.typ != yaml_BLOCK_END_TOKEN {
  691. parser.states = append(parser.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE)
  692. return yaml_parser_parse_node(parser, event, true, false)
  693. }
  694. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  695. return yaml_parser_process_empty_scalar(parser, event, mark)
  696. }
  697. parser.state = parser.states[len(parser.states)-1]
  698. parser.states = parser.states[:len(parser.states)-1]
  699. *event = yaml_event_t{
  700. typ: yaml_SEQUENCE_END_EVENT,
  701. start_mark: token.start_mark,
  702. end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark?
  703. }
  704. return true
  705. }
  706. // Parse the productions:
  707. // block_mapping ::= BLOCK-MAPPING_START
  708. // *******************
  709. // ((KEY block_node_or_indentless_sequence?)?
  710. // *** *
  711. // (VALUE block_node_or_indentless_sequence?)?)*
  712. //
  713. // BLOCK-END
  714. // *********
  715. //
  716. func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  717. if first {
  718. token := peek_token(parser)
  719. parser.marks = append(parser.marks, token.start_mark)
  720. skip_token(parser)
  721. }
  722. token := peek_token(parser)
  723. if token == nil {
  724. return false
  725. }
  726. // [Go] A tail comment was left from the prior mapping value processed. Emit an event
  727. // as it needs to be processed with that value and not the following key.
  728. if len(parser.tail_comment) > 0 {
  729. *event = yaml_event_t{
  730. typ: yaml_TAIL_COMMENT_EVENT,
  731. start_mark: token.start_mark,
  732. end_mark: token.end_mark,
  733. foot_comment: parser.tail_comment,
  734. }
  735. parser.tail_comment = nil
  736. return true
  737. }
  738. if token.typ == yaml_KEY_TOKEN {
  739. mark := token.end_mark
  740. skip_token(parser)
  741. token = peek_token(parser)
  742. if token == nil {
  743. return false
  744. }
  745. if token.typ != yaml_KEY_TOKEN &&
  746. token.typ != yaml_VALUE_TOKEN &&
  747. token.typ != yaml_BLOCK_END_TOKEN {
  748. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE)
  749. return yaml_parser_parse_node(parser, event, true, true)
  750. } else {
  751. parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE
  752. return yaml_parser_process_empty_scalar(parser, event, mark)
  753. }
  754. } else if token.typ == yaml_BLOCK_END_TOKEN {
  755. parser.state = parser.states[len(parser.states)-1]
  756. parser.states = parser.states[:len(parser.states)-1]
  757. parser.marks = parser.marks[:len(parser.marks)-1]
  758. *event = yaml_event_t{
  759. typ: yaml_MAPPING_END_EVENT,
  760. start_mark: token.start_mark,
  761. end_mark: token.end_mark,
  762. }
  763. yaml_parser_set_event_comments(parser, event)
  764. skip_token(parser)
  765. return true
  766. }
  767. context_mark := parser.marks[len(parser.marks)-1]
  768. parser.marks = parser.marks[:len(parser.marks)-1]
  769. return yaml_parser_set_parser_error_context(parser,
  770. "while parsing a block mapping", context_mark,
  771. "did not find expected key", token.start_mark)
  772. }
  773. // Parse the productions:
  774. // block_mapping ::= BLOCK-MAPPING_START
  775. //
  776. // ((KEY block_node_or_indentless_sequence?)?
  777. //
  778. // (VALUE block_node_or_indentless_sequence?)?)*
  779. // ***** *
  780. // BLOCK-END
  781. //
  782. //
  783. func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  784. token := peek_token(parser)
  785. if token == nil {
  786. return false
  787. }
  788. if token.typ == yaml_VALUE_TOKEN {
  789. mark := token.end_mark
  790. skip_token(parser)
  791. token = peek_token(parser)
  792. if token == nil {
  793. return false
  794. }
  795. if token.typ != yaml_KEY_TOKEN &&
  796. token.typ != yaml_VALUE_TOKEN &&
  797. token.typ != yaml_BLOCK_END_TOKEN {
  798. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE)
  799. return yaml_parser_parse_node(parser, event, true, true)
  800. }
  801. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  802. return yaml_parser_process_empty_scalar(parser, event, mark)
  803. }
  804. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  805. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  806. }
  807. // Parse the productions:
  808. // flow_sequence ::= FLOW-SEQUENCE-START
  809. // *******************
  810. // (flow_sequence_entry FLOW-ENTRY)*
  811. // * **********
  812. // flow_sequence_entry?
  813. // *
  814. // FLOW-SEQUENCE-END
  815. // *****************
  816. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  817. // *
  818. //
  819. func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  820. if first {
  821. token := peek_token(parser)
  822. parser.marks = append(parser.marks, token.start_mark)
  823. skip_token(parser)
  824. }
  825. token := peek_token(parser)
  826. if token == nil {
  827. return false
  828. }
  829. if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  830. if !first {
  831. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  832. skip_token(parser)
  833. token = peek_token(parser)
  834. if token == nil {
  835. return false
  836. }
  837. } else {
  838. context_mark := parser.marks[len(parser.marks)-1]
  839. parser.marks = parser.marks[:len(parser.marks)-1]
  840. return yaml_parser_set_parser_error_context(parser,
  841. "while parsing a flow sequence", context_mark,
  842. "did not find expected ',' or ']'", token.start_mark)
  843. }
  844. }
  845. if token.typ == yaml_KEY_TOKEN {
  846. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE
  847. *event = yaml_event_t{
  848. typ: yaml_MAPPING_START_EVENT,
  849. start_mark: token.start_mark,
  850. end_mark: token.end_mark,
  851. implicit: true,
  852. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  853. }
  854. skip_token(parser)
  855. return true
  856. } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  857. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE)
  858. return yaml_parser_parse_node(parser, event, false, false)
  859. }
  860. }
  861. parser.state = parser.states[len(parser.states)-1]
  862. parser.states = parser.states[:len(parser.states)-1]
  863. parser.marks = parser.marks[:len(parser.marks)-1]
  864. *event = yaml_event_t{
  865. typ: yaml_SEQUENCE_END_EVENT,
  866. start_mark: token.start_mark,
  867. end_mark: token.end_mark,
  868. }
  869. yaml_parser_set_event_comments(parser, event)
  870. skip_token(parser)
  871. return true
  872. }
  873. //
  874. // Parse the productions:
  875. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  876. // *** *
  877. //
  878. func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool {
  879. token := peek_token(parser)
  880. if token == nil {
  881. return false
  882. }
  883. if token.typ != yaml_VALUE_TOKEN &&
  884. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  885. token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  886. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE)
  887. return yaml_parser_parse_node(parser, event, false, false)
  888. }
  889. mark := token.end_mark
  890. skip_token(parser)
  891. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE
  892. return yaml_parser_process_empty_scalar(parser, event, mark)
  893. }
  894. // Parse the productions:
  895. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  896. // ***** *
  897. //
  898. func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  899. token := peek_token(parser)
  900. if token == nil {
  901. return false
  902. }
  903. if token.typ == yaml_VALUE_TOKEN {
  904. skip_token(parser)
  905. token := peek_token(parser)
  906. if token == nil {
  907. return false
  908. }
  909. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  910. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE)
  911. return yaml_parser_parse_node(parser, event, false, false)
  912. }
  913. }
  914. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE
  915. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  916. }
  917. // Parse the productions:
  918. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  919. // *
  920. //
  921. func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  922. token := peek_token(parser)
  923. if token == nil {
  924. return false
  925. }
  926. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE
  927. *event = yaml_event_t{
  928. typ: yaml_MAPPING_END_EVENT,
  929. start_mark: token.start_mark,
  930. end_mark: token.start_mark, // [Go] Shouldn't this be end_mark?
  931. }
  932. return true
  933. }
  934. // Parse the productions:
  935. // flow_mapping ::= FLOW-MAPPING-START
  936. // ******************
  937. // (flow_mapping_entry FLOW-ENTRY)*
  938. // * **********
  939. // flow_mapping_entry?
  940. // ******************
  941. // FLOW-MAPPING-END
  942. // ****************
  943. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  944. // * *** *
  945. //
  946. func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  947. if first {
  948. token := peek_token(parser)
  949. parser.marks = append(parser.marks, token.start_mark)
  950. skip_token(parser)
  951. }
  952. token := peek_token(parser)
  953. if token == nil {
  954. return false
  955. }
  956. if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  957. if !first {
  958. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  959. skip_token(parser)
  960. token = peek_token(parser)
  961. if token == nil {
  962. return false
  963. }
  964. } else {
  965. context_mark := parser.marks[len(parser.marks)-1]
  966. parser.marks = parser.marks[:len(parser.marks)-1]
  967. return yaml_parser_set_parser_error_context(parser,
  968. "while parsing a flow mapping", context_mark,
  969. "did not find expected ',' or '}'", token.start_mark)
  970. }
  971. }
  972. if token.typ == yaml_KEY_TOKEN {
  973. skip_token(parser)
  974. token = peek_token(parser)
  975. if token == nil {
  976. return false
  977. }
  978. if token.typ != yaml_VALUE_TOKEN &&
  979. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  980. token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  981. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE)
  982. return yaml_parser_parse_node(parser, event, false, false)
  983. } else {
  984. parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE
  985. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  986. }
  987. } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  988. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE)
  989. return yaml_parser_parse_node(parser, event, false, false)
  990. }
  991. }
  992. parser.state = parser.states[len(parser.states)-1]
  993. parser.states = parser.states[:len(parser.states)-1]
  994. parser.marks = parser.marks[:len(parser.marks)-1]
  995. *event = yaml_event_t{
  996. typ: yaml_MAPPING_END_EVENT,
  997. start_mark: token.start_mark,
  998. end_mark: token.end_mark,
  999. }
  1000. yaml_parser_set_event_comments(parser, event)
  1001. skip_token(parser)
  1002. return true
  1003. }
  1004. // Parse the productions:
  1005. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  1006. // * ***** *
  1007. //
  1008. func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool {
  1009. token := peek_token(parser)
  1010. if token == nil {
  1011. return false
  1012. }
  1013. if empty {
  1014. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  1015. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  1016. }
  1017. if token.typ == yaml_VALUE_TOKEN {
  1018. skip_token(parser)
  1019. token = peek_token(parser)
  1020. if token == nil {
  1021. return false
  1022. }
  1023. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  1024. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE)
  1025. return yaml_parser_parse_node(parser, event, false, false)
  1026. }
  1027. }
  1028. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  1029. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  1030. }
  1031. // Generate an empty scalar event.
  1032. func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool {
  1033. *event = yaml_event_t{
  1034. typ: yaml_SCALAR_EVENT,
  1035. start_mark: mark,
  1036. end_mark: mark,
  1037. value: nil, // Empty
  1038. implicit: true,
  1039. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  1040. }
  1041. return true
  1042. }
  1043. var default_tag_directives = []yaml_tag_directive_t{
  1044. {[]byte("!"), []byte("!")},
  1045. {[]byte("!!"), []byte("tag:yaml.org,2002:")},
  1046. }
  1047. // Parse directives.
  1048. func yaml_parser_process_directives(parser *yaml_parser_t,
  1049. version_directive_ref **yaml_version_directive_t,
  1050. tag_directives_ref *[]yaml_tag_directive_t) bool {
  1051. var version_directive *yaml_version_directive_t
  1052. var tag_directives []yaml_tag_directive_t
  1053. token := peek_token(parser)
  1054. if token == nil {
  1055. return false
  1056. }
  1057. for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  1058. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN {
  1059. if version_directive != nil {
  1060. yaml_parser_set_parser_error(parser,
  1061. "found duplicate %YAML directive", token.start_mark)
  1062. return false
  1063. }
  1064. if token.major != 1 || token.minor != 1 {
  1065. yaml_parser_set_parser_error(parser,
  1066. "found incompatible YAML document", token.start_mark)
  1067. return false
  1068. }
  1069. version_directive = &yaml_version_directive_t{
  1070. major: token.major,
  1071. minor: token.minor,
  1072. }
  1073. } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  1074. value := yaml_tag_directive_t{
  1075. handle: token.value,
  1076. prefix: token.prefix,
  1077. }
  1078. if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) {
  1079. return false
  1080. }
  1081. tag_directives = append(tag_directives, value)
  1082. }
  1083. skip_token(parser)
  1084. token = peek_token(parser)
  1085. if token == nil {
  1086. return false
  1087. }
  1088. }
  1089. for i := range default_tag_directives {
  1090. if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) {
  1091. return false
  1092. }
  1093. }
  1094. if version_directive_ref != nil {
  1095. *version_directive_ref = version_directive
  1096. }
  1097. if tag_directives_ref != nil {
  1098. *tag_directives_ref = tag_directives
  1099. }
  1100. return true
  1101. }
  1102. // Append a tag directive to the directives stack.
  1103. func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool {
  1104. for i := range parser.tag_directives {
  1105. if bytes.Equal(value.handle, parser.tag_directives[i].handle) {
  1106. if allow_duplicates {
  1107. return true
  1108. }
  1109. return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark)
  1110. }
  1111. }
  1112. // [Go] I suspect the copy is unnecessary. This was likely done
  1113. // because there was no way to track ownership of the data.
  1114. value_copy := yaml_tag_directive_t{
  1115. handle: make([]byte, len(value.handle)),
  1116. prefix: make([]byte, len(value.prefix)),
  1117. }
  1118. copy(value_copy.handle, value.handle)
  1119. copy(value_copy.prefix, value.prefix)
  1120. parser.tag_directives = append(parser.tag_directives, value_copy)
  1121. return true
  1122. }