parserc.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. package yaml
  2. import (
  3. "bytes"
  4. )
  5. // The parser implements the following grammar:
  6. //
  7. // stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
  8. // implicit_document ::= block_node DOCUMENT-END*
  9. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  10. // block_node_or_indentless_sequence ::=
  11. // ALIAS
  12. // | properties (block_content | indentless_block_sequence)?
  13. // | block_content
  14. // | indentless_block_sequence
  15. // block_node ::= ALIAS
  16. // | properties block_content?
  17. // | block_content
  18. // flow_node ::= ALIAS
  19. // | properties flow_content?
  20. // | flow_content
  21. // properties ::= TAG ANCHOR? | ANCHOR TAG?
  22. // block_content ::= block_collection | flow_collection | SCALAR
  23. // flow_content ::= flow_collection | SCALAR
  24. // block_collection ::= block_sequence | block_mapping
  25. // flow_collection ::= flow_sequence | flow_mapping
  26. // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
  27. // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
  28. // block_mapping ::= BLOCK-MAPPING_START
  29. // ((KEY block_node_or_indentless_sequence?)?
  30. // (VALUE block_node_or_indentless_sequence?)?)*
  31. // BLOCK-END
  32. // flow_sequence ::= FLOW-SEQUENCE-START
  33. // (flow_sequence_entry FLOW-ENTRY)*
  34. // flow_sequence_entry?
  35. // FLOW-SEQUENCE-END
  36. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  37. // flow_mapping ::= FLOW-MAPPING-START
  38. // (flow_mapping_entry FLOW-ENTRY)*
  39. // flow_mapping_entry?
  40. // FLOW-MAPPING-END
  41. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  42. // Peek the next token in the token queue.
  43. func peek_token(parser *yaml_parser_t) *yaml_token_t {
  44. if parser.token_available || yaml_parser_fetch_more_tokens(parser) {
  45. return &parser.tokens[parser.tokens_head]
  46. }
  47. return nil
  48. }
  49. // Remove the next token from the queue (must be called after peek_token).
  50. func skip_token(parser *yaml_parser_t) {
  51. parser.token_available = false
  52. parser.tokens_parsed++
  53. parser.stream_end_produced = parser.tokens[parser.tokens_head].typ == yaml_STREAM_END_TOKEN
  54. parser.tokens_head++
  55. }
  56. // Get the next event.
  57. func yaml_parser_parse(parser *yaml_parser_t, event *yaml_event_t) bool {
  58. // Erase the event object.
  59. *event = yaml_event_t{}
  60. // No events after the end of the stream or error.
  61. if parser.stream_end_produced || parser.error != yaml_NO_ERROR || parser.state == yaml_PARSE_END_STATE {
  62. return true
  63. }
  64. // Generate the next event.
  65. return yaml_parser_state_machine(parser, event)
  66. }
  67. // Set parser error.
  68. func yaml_parser_set_parser_error(parser *yaml_parser_t, problem string, problem_mark yaml_mark_t) bool {
  69. parser.error = yaml_PARSER_ERROR
  70. parser.problem = problem
  71. parser.problem_mark = problem_mark
  72. return false
  73. }
  74. 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 {
  75. parser.error = yaml_PARSER_ERROR
  76. parser.context = context
  77. parser.context_mark = context_mark
  78. parser.problem = problem
  79. parser.problem_mark = problem_mark
  80. return false
  81. }
  82. // State dispatcher.
  83. func yaml_parser_state_machine(parser *yaml_parser_t, event *yaml_event_t) bool {
  84. //trace("yaml_parser_state_machine", "state:", parser.state.String())
  85. switch parser.state {
  86. case yaml_PARSE_STREAM_START_STATE:
  87. return yaml_parser_parse_stream_start(parser, event)
  88. case yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE:
  89. return yaml_parser_parse_document_start(parser, event, true)
  90. case yaml_PARSE_DOCUMENT_START_STATE:
  91. return yaml_parser_parse_document_start(parser, event, false)
  92. case yaml_PARSE_DOCUMENT_CONTENT_STATE:
  93. return yaml_parser_parse_document_content(parser, event)
  94. case yaml_PARSE_DOCUMENT_END_STATE:
  95. return yaml_parser_parse_document_end(parser, event)
  96. case yaml_PARSE_BLOCK_NODE_STATE:
  97. return yaml_parser_parse_node(parser, event, true, false)
  98. case yaml_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE:
  99. return yaml_parser_parse_node(parser, event, true, true)
  100. case yaml_PARSE_FLOW_NODE_STATE:
  101. return yaml_parser_parse_node(parser, event, false, false)
  102. case yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE:
  103. return yaml_parser_parse_block_sequence_entry(parser, event, true)
  104. case yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE:
  105. return yaml_parser_parse_block_sequence_entry(parser, event, false)
  106. case yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE:
  107. return yaml_parser_parse_indentless_sequence_entry(parser, event)
  108. case yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE:
  109. return yaml_parser_parse_block_mapping_key(parser, event, true)
  110. case yaml_PARSE_BLOCK_MAPPING_KEY_STATE:
  111. return yaml_parser_parse_block_mapping_key(parser, event, false)
  112. case yaml_PARSE_BLOCK_MAPPING_VALUE_STATE:
  113. return yaml_parser_parse_block_mapping_value(parser, event)
  114. case yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE:
  115. return yaml_parser_parse_flow_sequence_entry(parser, event, true)
  116. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE:
  117. return yaml_parser_parse_flow_sequence_entry(parser, event, false)
  118. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE:
  119. return yaml_parser_parse_flow_sequence_entry_mapping_key(parser, event)
  120. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE:
  121. return yaml_parser_parse_flow_sequence_entry_mapping_value(parser, event)
  122. case yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE:
  123. return yaml_parser_parse_flow_sequence_entry_mapping_end(parser, event)
  124. case yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE:
  125. return yaml_parser_parse_flow_mapping_key(parser, event, true)
  126. case yaml_PARSE_FLOW_MAPPING_KEY_STATE:
  127. return yaml_parser_parse_flow_mapping_key(parser, event, false)
  128. case yaml_PARSE_FLOW_MAPPING_VALUE_STATE:
  129. return yaml_parser_parse_flow_mapping_value(parser, event, false)
  130. case yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE:
  131. return yaml_parser_parse_flow_mapping_value(parser, event, true)
  132. default:
  133. panic("invalid parser state")
  134. }
  135. return false
  136. }
  137. // Parse the production:
  138. // stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
  139. // ************
  140. func yaml_parser_parse_stream_start(parser *yaml_parser_t, event *yaml_event_t) bool {
  141. token := peek_token(parser)
  142. if token == nil {
  143. return false
  144. }
  145. if token.typ != yaml_STREAM_START_TOKEN {
  146. return yaml_parser_set_parser_error(parser, "did not find expected <stream-start>", token.start_mark)
  147. }
  148. parser.state = yaml_PARSE_IMPLICIT_DOCUMENT_START_STATE
  149. *event = yaml_event_t{
  150. typ: yaml_STREAM_START_EVENT,
  151. start_mark: token.start_mark,
  152. end_mark: token.end_mark,
  153. encoding: token.encoding,
  154. }
  155. skip_token(parser)
  156. return true
  157. }
  158. // Parse the productions:
  159. // implicit_document ::= block_node DOCUMENT-END*
  160. // *
  161. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  162. // *************************
  163. func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t, implicit bool) bool {
  164. token := peek_token(parser)
  165. if token == nil {
  166. return false
  167. }
  168. // Parse extra document end indicators.
  169. if !implicit {
  170. for token.typ == yaml_DOCUMENT_END_TOKEN {
  171. skip_token(parser)
  172. token = peek_token(parser)
  173. if token == nil {
  174. return false
  175. }
  176. }
  177. }
  178. if implicit && token.typ != yaml_VERSION_DIRECTIVE_TOKEN &&
  179. token.typ != yaml_TAG_DIRECTIVE_TOKEN &&
  180. token.typ != yaml_DOCUMENT_START_TOKEN &&
  181. token.typ != yaml_STREAM_END_TOKEN {
  182. // Parse an implicit document.
  183. if !yaml_parser_process_directives(parser, nil, nil) {
  184. return false
  185. }
  186. parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE)
  187. parser.state = yaml_PARSE_BLOCK_NODE_STATE
  188. *event = yaml_event_t{
  189. typ: yaml_DOCUMENT_START_EVENT,
  190. start_mark: token.start_mark,
  191. end_mark: token.end_mark,
  192. }
  193. } else if token.typ != yaml_STREAM_END_TOKEN {
  194. // Parse an explicit document.
  195. var version_directive *yaml_version_directive_t
  196. var tag_directives []yaml_tag_directive_t
  197. start_mark := token.start_mark
  198. if !yaml_parser_process_directives(parser, &version_directive, &tag_directives) {
  199. return false
  200. }
  201. token = peek_token(parser)
  202. if token == nil {
  203. return false
  204. }
  205. if token.typ != yaml_DOCUMENT_START_TOKEN {
  206. yaml_parser_set_parser_error(parser,
  207. "did not find expected <document start>", token.start_mark)
  208. return false
  209. }
  210. parser.states = append(parser.states, yaml_PARSE_DOCUMENT_END_STATE)
  211. parser.state = yaml_PARSE_DOCUMENT_CONTENT_STATE
  212. end_mark := token.end_mark
  213. *event = yaml_event_t{
  214. typ: yaml_DOCUMENT_START_EVENT,
  215. start_mark: start_mark,
  216. end_mark: end_mark,
  217. version_directive: version_directive,
  218. tag_directives: tag_directives,
  219. implicit: false,
  220. }
  221. skip_token(parser)
  222. } else {
  223. // Parse the stream end.
  224. parser.state = yaml_PARSE_END_STATE
  225. *event = yaml_event_t{
  226. typ: yaml_STREAM_END_EVENT,
  227. start_mark: token.start_mark,
  228. end_mark: token.end_mark,
  229. }
  230. skip_token(parser)
  231. }
  232. return true
  233. }
  234. // Parse the productions:
  235. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  236. // ***********
  237. //
  238. func yaml_parser_parse_document_content(parser *yaml_parser_t, event *yaml_event_t) bool {
  239. token := peek_token(parser)
  240. if token == nil {
  241. return false
  242. }
  243. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN ||
  244. token.typ == yaml_TAG_DIRECTIVE_TOKEN ||
  245. token.typ == yaml_DOCUMENT_START_TOKEN ||
  246. token.typ == yaml_DOCUMENT_END_TOKEN ||
  247. token.typ == yaml_STREAM_END_TOKEN {
  248. parser.state = parser.states[len(parser.states)-1]
  249. parser.states = parser.states[:len(parser.states)-1]
  250. return yaml_parser_process_empty_scalar(parser, event,
  251. token.start_mark)
  252. }
  253. return yaml_parser_parse_node(parser, event, true, false)
  254. }
  255. // Parse the productions:
  256. // implicit_document ::= block_node DOCUMENT-END*
  257. // *************
  258. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  259. //
  260. func yaml_parser_parse_document_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  261. token := peek_token(parser)
  262. if token == nil {
  263. return false
  264. }
  265. start_mark := token.start_mark
  266. end_mark := token.start_mark
  267. implicit := true
  268. if token.typ == yaml_DOCUMENT_END_TOKEN {
  269. end_mark = token.end_mark
  270. skip_token(parser)
  271. implicit = false
  272. }
  273. parser.tag_directives = parser.tag_directives[:0]
  274. parser.state = yaml_PARSE_DOCUMENT_START_STATE
  275. *event = yaml_event_t{
  276. typ: yaml_DOCUMENT_END_EVENT,
  277. start_mark: start_mark,
  278. end_mark: end_mark,
  279. implicit: implicit,
  280. }
  281. return true
  282. }
  283. // Parse the productions:
  284. // block_node_or_indentless_sequence ::=
  285. // ALIAS
  286. // *****
  287. // | properties (block_content | indentless_block_sequence)?
  288. // ********** *
  289. // | block_content | indentless_block_sequence
  290. // *
  291. // block_node ::= ALIAS
  292. // *****
  293. // | properties block_content?
  294. // ********** *
  295. // | block_content
  296. // *
  297. // flow_node ::= ALIAS
  298. // *****
  299. // | properties flow_content?
  300. // ********** *
  301. // | flow_content
  302. // *
  303. // properties ::= TAG ANCHOR? | ANCHOR TAG?
  304. // *************************
  305. // block_content ::= block_collection | flow_collection | SCALAR
  306. // ******
  307. // flow_content ::= flow_collection | SCALAR
  308. // ******
  309. func yaml_parser_parse_node(parser *yaml_parser_t, event *yaml_event_t, block, indentless_sequence bool) bool {
  310. //defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)()
  311. token := peek_token(parser)
  312. if token == nil {
  313. return false
  314. }
  315. if token.typ == yaml_ALIAS_TOKEN {
  316. parser.state = parser.states[len(parser.states)-1]
  317. parser.states = parser.states[:len(parser.states)-1]
  318. *event = yaml_event_t{
  319. typ: yaml_ALIAS_EVENT,
  320. start_mark: token.start_mark,
  321. end_mark: token.end_mark,
  322. anchor: token.value,
  323. }
  324. skip_token(parser)
  325. return true
  326. }
  327. start_mark := token.start_mark
  328. end_mark := token.start_mark
  329. var tag_token bool
  330. var tag_handle, tag_suffix, anchor []byte
  331. var tag_mark yaml_mark_t
  332. if token.typ == yaml_ANCHOR_TOKEN {
  333. anchor = token.value
  334. start_mark = token.start_mark
  335. end_mark = token.end_mark
  336. skip_token(parser)
  337. token = peek_token(parser)
  338. if token == nil {
  339. return false
  340. }
  341. if token.typ == yaml_TAG_TOKEN {
  342. tag_token = true
  343. tag_handle = token.value
  344. tag_suffix = token.suffix
  345. tag_mark = token.start_mark
  346. end_mark = token.end_mark
  347. skip_token(parser)
  348. token = peek_token(parser)
  349. if token == nil {
  350. return false
  351. }
  352. }
  353. } else if token.typ == yaml_TAG_TOKEN {
  354. tag_token = true
  355. tag_handle = token.value
  356. tag_suffix = token.suffix
  357. start_mark = token.start_mark
  358. tag_mark = token.start_mark
  359. end_mark = token.end_mark
  360. skip_token(parser)
  361. token = peek_token(parser)
  362. if token == nil {
  363. return false
  364. }
  365. if token.typ == yaml_ANCHOR_TOKEN {
  366. anchor = token.value
  367. end_mark = token.end_mark
  368. skip_token(parser)
  369. token = peek_token(parser)
  370. if token == nil {
  371. return false
  372. }
  373. }
  374. }
  375. var tag []byte
  376. if tag_token {
  377. if len(tag_handle) == 0 {
  378. tag = tag_suffix
  379. tag_suffix = nil
  380. } else {
  381. for i := range parser.tag_directives {
  382. if bytes.Equal(parser.tag_directives[i].handle, tag_handle) {
  383. tag = append([]byte(nil), parser.tag_directives[i].prefix...)
  384. tag = append(tag, tag_suffix...)
  385. break
  386. }
  387. }
  388. if len(tag) == 0 {
  389. yaml_parser_set_parser_error_context(parser,
  390. "while parsing a node", start_mark,
  391. "found undefined tag handle", tag_mark)
  392. return false
  393. }
  394. }
  395. }
  396. implicit := len(tag) == 0
  397. if indentless_sequence && token.typ == yaml_BLOCK_ENTRY_TOKEN {
  398. end_mark = token.end_mark
  399. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  400. *event = yaml_event_t{
  401. typ: yaml_SEQUENCE_START_EVENT,
  402. start_mark: start_mark,
  403. end_mark: end_mark,
  404. anchor: anchor,
  405. tag: tag,
  406. implicit: implicit,
  407. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  408. }
  409. return true
  410. }
  411. if token.typ == yaml_SCALAR_TOKEN {
  412. var plain_implicit, quoted_implicit bool
  413. end_mark = token.end_mark
  414. if (len(tag) == 0 && token.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') {
  415. plain_implicit = true
  416. } else if len(tag) == 0 {
  417. quoted_implicit = true
  418. }
  419. parser.state = parser.states[len(parser.states)-1]
  420. parser.states = parser.states[:len(parser.states)-1]
  421. *event = yaml_event_t{
  422. typ: yaml_SCALAR_EVENT,
  423. start_mark: start_mark,
  424. end_mark: end_mark,
  425. anchor: anchor,
  426. tag: tag,
  427. value: token.value,
  428. implicit: plain_implicit,
  429. quoted_implicit: quoted_implicit,
  430. style: yaml_style_t(token.style),
  431. }
  432. skip_token(parser)
  433. return true
  434. }
  435. if token.typ == yaml_FLOW_SEQUENCE_START_TOKEN {
  436. // [Go] Some of the events below can be merged as they differ only on style.
  437. end_mark = token.end_mark
  438. parser.state = yaml_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE
  439. *event = yaml_event_t{
  440. typ: yaml_SEQUENCE_START_EVENT,
  441. start_mark: start_mark,
  442. end_mark: end_mark,
  443. anchor: anchor,
  444. tag: tag,
  445. implicit: implicit,
  446. style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE),
  447. }
  448. return true
  449. }
  450. if token.typ == yaml_FLOW_MAPPING_START_TOKEN {
  451. end_mark = token.end_mark
  452. parser.state = yaml_PARSE_FLOW_MAPPING_FIRST_KEY_STATE
  453. *event = yaml_event_t{
  454. typ: yaml_MAPPING_START_EVENT,
  455. start_mark: start_mark,
  456. end_mark: end_mark,
  457. anchor: anchor,
  458. tag: tag,
  459. implicit: implicit,
  460. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  461. }
  462. return true
  463. }
  464. if block && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN {
  465. end_mark = token.end_mark
  466. parser.state = yaml_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE
  467. *event = yaml_event_t{
  468. typ: yaml_SEQUENCE_START_EVENT,
  469. start_mark: start_mark,
  470. end_mark: end_mark,
  471. anchor: anchor,
  472. tag: tag,
  473. implicit: implicit,
  474. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  475. }
  476. return true
  477. }
  478. if block && token.typ == yaml_BLOCK_MAPPING_START_TOKEN {
  479. end_mark = token.end_mark
  480. parser.state = yaml_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE
  481. *event = yaml_event_t{
  482. typ: yaml_MAPPING_START_EVENT,
  483. start_mark: start_mark,
  484. end_mark: end_mark,
  485. anchor: anchor,
  486. tag: tag,
  487. implicit: implicit,
  488. style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE),
  489. }
  490. return true
  491. }
  492. if len(anchor) > 0 || len(tag) > 0 {
  493. parser.state = parser.states[len(parser.states)-1]
  494. parser.states = parser.states[:len(parser.states)-1]
  495. *event = yaml_event_t{
  496. typ: yaml_SCALAR_EVENT,
  497. start_mark: start_mark,
  498. end_mark: end_mark,
  499. anchor: anchor,
  500. tag: tag,
  501. implicit: implicit,
  502. quoted_implicit: false,
  503. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  504. }
  505. return true
  506. }
  507. context := "while parsing a flow node"
  508. if block {
  509. context = "while parsing a block node"
  510. }
  511. yaml_parser_set_parser_error_context(parser, context, start_mark,
  512. "did not find expected node content", token.start_mark)
  513. return false
  514. }
  515. // Parse the productions:
  516. // block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
  517. // ******************** *********** * *********
  518. //
  519. func yaml_parser_parse_block_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  520. if first {
  521. token := peek_token(parser)
  522. parser.marks = append(parser.marks, token.start_mark)
  523. skip_token(parser)
  524. }
  525. token := peek_token(parser)
  526. if token == nil {
  527. return false
  528. }
  529. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  530. mark := token.end_mark
  531. skip_token(parser)
  532. token = peek_token(parser)
  533. if token == nil {
  534. return false
  535. }
  536. if token.typ != yaml_BLOCK_ENTRY_TOKEN && token.typ != yaml_BLOCK_END_TOKEN {
  537. parser.states = append(parser.states, yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE)
  538. return yaml_parser_parse_node(parser, event, true, false)
  539. } else {
  540. parser.state = yaml_PARSE_BLOCK_SEQUENCE_ENTRY_STATE
  541. return yaml_parser_process_empty_scalar(parser, event, mark)
  542. }
  543. }
  544. if token.typ == yaml_BLOCK_END_TOKEN {
  545. parser.state = parser.states[len(parser.states)-1]
  546. parser.states = parser.states[:len(parser.states)-1]
  547. parser.marks = parser.marks[:len(parser.marks)-1]
  548. *event = yaml_event_t{
  549. typ: yaml_SEQUENCE_END_EVENT,
  550. start_mark: token.start_mark,
  551. end_mark: token.end_mark,
  552. }
  553. skip_token(parser)
  554. return true
  555. }
  556. context_mark := parser.marks[len(parser.marks)-1]
  557. parser.marks = parser.marks[:len(parser.marks)-1]
  558. return yaml_parser_set_parser_error_context(parser,
  559. "while parsing a block collection", context_mark,
  560. "did not find expected '-' indicator", token.start_mark)
  561. }
  562. // Parse the productions:
  563. // indentless_sequence ::= (BLOCK-ENTRY block_node?)+
  564. // *********** *
  565. func yaml_parser_parse_indentless_sequence_entry(parser *yaml_parser_t, event *yaml_event_t) bool {
  566. token := peek_token(parser)
  567. if token == nil {
  568. return false
  569. }
  570. if token.typ == yaml_BLOCK_ENTRY_TOKEN {
  571. mark := token.end_mark
  572. skip_token(parser)
  573. token = peek_token(parser)
  574. if token == nil {
  575. return false
  576. }
  577. if token.typ != yaml_BLOCK_ENTRY_TOKEN &&
  578. token.typ != yaml_KEY_TOKEN &&
  579. token.typ != yaml_VALUE_TOKEN &&
  580. token.typ != yaml_BLOCK_END_TOKEN {
  581. parser.states = append(parser.states, yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE)
  582. return yaml_parser_parse_node(parser, event, true, false)
  583. }
  584. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  585. return yaml_parser_process_empty_scalar(parser, event, mark)
  586. }
  587. parser.state = parser.states[len(parser.states)-1]
  588. parser.states = parser.states[:len(parser.states)-1]
  589. *event = yaml_event_t{
  590. typ: yaml_SEQUENCE_END_EVENT,
  591. start_mark: token.start_mark,
  592. end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark?
  593. }
  594. return true
  595. }
  596. // Parse the productions:
  597. // block_mapping ::= BLOCK-MAPPING_START
  598. // *******************
  599. // ((KEY block_node_or_indentless_sequence?)?
  600. // *** *
  601. // (VALUE block_node_or_indentless_sequence?)?)*
  602. //
  603. // BLOCK-END
  604. // *********
  605. //
  606. func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  607. if first {
  608. token := peek_token(parser)
  609. parser.marks = append(parser.marks, token.start_mark)
  610. skip_token(parser)
  611. }
  612. token := peek_token(parser)
  613. if token == nil {
  614. return false
  615. }
  616. if token.typ == yaml_KEY_TOKEN {
  617. mark := token.end_mark
  618. skip_token(parser)
  619. token = peek_token(parser)
  620. if token == nil {
  621. return false
  622. }
  623. if token.typ != yaml_KEY_TOKEN &&
  624. token.typ != yaml_VALUE_TOKEN &&
  625. token.typ != yaml_BLOCK_END_TOKEN {
  626. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE)
  627. return yaml_parser_parse_node(parser, event, true, true)
  628. } else {
  629. parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE
  630. return yaml_parser_process_empty_scalar(parser, event, mark)
  631. }
  632. } else if token.typ == yaml_BLOCK_END_TOKEN {
  633. parser.state = parser.states[len(parser.states)-1]
  634. parser.states = parser.states[:len(parser.states)-1]
  635. parser.marks = parser.marks[:len(parser.marks)-1]
  636. *event = yaml_event_t{
  637. typ: yaml_MAPPING_END_EVENT,
  638. start_mark: token.start_mark,
  639. end_mark: token.end_mark,
  640. }
  641. skip_token(parser)
  642. return true
  643. }
  644. context_mark := parser.marks[len(parser.marks)-1]
  645. parser.marks = parser.marks[:len(parser.marks)-1]
  646. return yaml_parser_set_parser_error_context(parser,
  647. "while parsing a block mapping", context_mark,
  648. "did not find expected key", token.start_mark)
  649. }
  650. // Parse the productions:
  651. // block_mapping ::= BLOCK-MAPPING_START
  652. //
  653. // ((KEY block_node_or_indentless_sequence?)?
  654. //
  655. // (VALUE block_node_or_indentless_sequence?)?)*
  656. // ***** *
  657. // BLOCK-END
  658. //
  659. //
  660. func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  661. token := peek_token(parser)
  662. if token == nil {
  663. return false
  664. }
  665. if token.typ == yaml_VALUE_TOKEN {
  666. mark := token.end_mark
  667. skip_token(parser)
  668. token = peek_token(parser)
  669. if token == nil {
  670. return false
  671. }
  672. if token.typ != yaml_KEY_TOKEN &&
  673. token.typ != yaml_VALUE_TOKEN &&
  674. token.typ != yaml_BLOCK_END_TOKEN {
  675. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE)
  676. return yaml_parser_parse_node(parser, event, true, true)
  677. }
  678. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  679. return yaml_parser_process_empty_scalar(parser, event, mark)
  680. }
  681. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  682. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  683. }
  684. // Parse the productions:
  685. // flow_sequence ::= FLOW-SEQUENCE-START
  686. // *******************
  687. // (flow_sequence_entry FLOW-ENTRY)*
  688. // * **********
  689. // flow_sequence_entry?
  690. // *
  691. // FLOW-SEQUENCE-END
  692. // *****************
  693. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  694. // *
  695. //
  696. func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  697. if first {
  698. token := peek_token(parser)
  699. parser.marks = append(parser.marks, token.start_mark)
  700. skip_token(parser)
  701. }
  702. token := peek_token(parser)
  703. if token == nil {
  704. return false
  705. }
  706. if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  707. if !first {
  708. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  709. skip_token(parser)
  710. token = peek_token(parser)
  711. if token == nil {
  712. return false
  713. }
  714. } else {
  715. context_mark := parser.marks[len(parser.marks)-1]
  716. parser.marks = parser.marks[:len(parser.marks)-1]
  717. return yaml_parser_set_parser_error_context(parser,
  718. "while parsing a flow sequence", context_mark,
  719. "did not find expected ',' or ']'", token.start_mark)
  720. }
  721. }
  722. if token.typ == yaml_KEY_TOKEN {
  723. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE
  724. *event = yaml_event_t{
  725. typ: yaml_MAPPING_START_EVENT,
  726. start_mark: token.start_mark,
  727. end_mark: token.end_mark,
  728. implicit: true,
  729. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  730. }
  731. skip_token(parser)
  732. return true
  733. } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  734. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE)
  735. return yaml_parser_parse_node(parser, event, false, false)
  736. }
  737. }
  738. parser.state = parser.states[len(parser.states)-1]
  739. parser.states = parser.states[:len(parser.states)-1]
  740. parser.marks = parser.marks[:len(parser.marks)-1]
  741. *event = yaml_event_t{
  742. typ: yaml_SEQUENCE_END_EVENT,
  743. start_mark: token.start_mark,
  744. end_mark: token.end_mark,
  745. }
  746. skip_token(parser)
  747. return true
  748. }
  749. //
  750. // Parse the productions:
  751. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  752. // *** *
  753. //
  754. func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool {
  755. token := peek_token(parser)
  756. if token == nil {
  757. return false
  758. }
  759. if token.typ != yaml_VALUE_TOKEN &&
  760. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  761. token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  762. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE)
  763. return yaml_parser_parse_node(parser, event, false, false)
  764. }
  765. mark := token.end_mark
  766. skip_token(parser)
  767. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE
  768. return yaml_parser_process_empty_scalar(parser, event, mark)
  769. }
  770. // Parse the productions:
  771. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  772. // ***** *
  773. //
  774. func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  775. token := peek_token(parser)
  776. if token == nil {
  777. return false
  778. }
  779. if token.typ == yaml_VALUE_TOKEN {
  780. skip_token(parser)
  781. token := peek_token(parser)
  782. if token == nil {
  783. return false
  784. }
  785. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  786. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE)
  787. return yaml_parser_parse_node(parser, event, false, false)
  788. }
  789. }
  790. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE
  791. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  792. }
  793. // Parse the productions:
  794. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  795. // *
  796. //
  797. func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  798. token := peek_token(parser)
  799. if token == nil {
  800. return false
  801. }
  802. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE
  803. *event = yaml_event_t{
  804. typ: yaml_MAPPING_END_EVENT,
  805. start_mark: token.start_mark,
  806. end_mark: token.start_mark, // [Go] Shouldn't this be end_mark?
  807. }
  808. return true
  809. }
  810. // Parse the productions:
  811. // flow_mapping ::= FLOW-MAPPING-START
  812. // ******************
  813. // (flow_mapping_entry FLOW-ENTRY)*
  814. // * **********
  815. // flow_mapping_entry?
  816. // ******************
  817. // FLOW-MAPPING-END
  818. // ****************
  819. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  820. // * *** *
  821. //
  822. func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  823. if first {
  824. token := peek_token(parser)
  825. parser.marks = append(parser.marks, token.start_mark)
  826. skip_token(parser)
  827. }
  828. token := peek_token(parser)
  829. if token == nil {
  830. return false
  831. }
  832. if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  833. if !first {
  834. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  835. skip_token(parser)
  836. token = peek_token(parser)
  837. if token == nil {
  838. return false
  839. }
  840. } else {
  841. context_mark := parser.marks[len(parser.marks)-1]
  842. parser.marks = parser.marks[:len(parser.marks)-1]
  843. return yaml_parser_set_parser_error_context(parser,
  844. "while parsing a flow mapping", context_mark,
  845. "did not find expected ',' or '}'", token.start_mark)
  846. }
  847. }
  848. if token.typ == yaml_KEY_TOKEN {
  849. skip_token(parser)
  850. token = peek_token(parser)
  851. if token == nil {
  852. return false
  853. }
  854. if token.typ != yaml_VALUE_TOKEN &&
  855. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  856. token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  857. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE)
  858. return yaml_parser_parse_node(parser, event, false, false)
  859. } else {
  860. parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE
  861. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  862. }
  863. } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  864. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE)
  865. return yaml_parser_parse_node(parser, event, false, false)
  866. }
  867. }
  868. parser.state = parser.states[len(parser.states)-1]
  869. parser.states = parser.states[:len(parser.states)-1]
  870. parser.marks = parser.marks[:len(parser.marks)-1]
  871. *event = yaml_event_t{
  872. typ: yaml_MAPPING_END_EVENT,
  873. start_mark: token.start_mark,
  874. end_mark: token.end_mark,
  875. }
  876. skip_token(parser)
  877. return true
  878. }
  879. // Parse the productions:
  880. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  881. // * ***** *
  882. //
  883. func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool {
  884. token := peek_token(parser)
  885. if token == nil {
  886. return false
  887. }
  888. if empty {
  889. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  890. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  891. }
  892. if token.typ == yaml_VALUE_TOKEN {
  893. skip_token(parser)
  894. token = peek_token(parser)
  895. if token == nil {
  896. return false
  897. }
  898. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  899. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE)
  900. return yaml_parser_parse_node(parser, event, false, false)
  901. }
  902. }
  903. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  904. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  905. }
  906. // Generate an empty scalar event.
  907. func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool {
  908. *event = yaml_event_t{
  909. typ: yaml_SCALAR_EVENT,
  910. start_mark: mark,
  911. end_mark: mark,
  912. value: nil, // Empty
  913. implicit: true,
  914. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  915. }
  916. return true
  917. }
  918. var default_tag_directives = []yaml_tag_directive_t{
  919. {[]byte("!"), []byte("!")},
  920. {[]byte("!!"), []byte("tag:yaml.org,2002:")},
  921. }
  922. // Parse directives.
  923. func yaml_parser_process_directives(parser *yaml_parser_t,
  924. version_directive_ref **yaml_version_directive_t,
  925. tag_directives_ref *[]yaml_tag_directive_t) bool {
  926. var version_directive *yaml_version_directive_t
  927. var tag_directives []yaml_tag_directive_t
  928. token := peek_token(parser)
  929. if token == nil {
  930. return false
  931. }
  932. for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  933. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN {
  934. if version_directive != nil {
  935. yaml_parser_set_parser_error(parser,
  936. "found duplicate %YAML directive", token.start_mark)
  937. return false
  938. }
  939. if token.major != 1 || token.minor != 1 {
  940. yaml_parser_set_parser_error(parser,
  941. "found incompatible YAML document", token.start_mark)
  942. return false
  943. }
  944. version_directive = &yaml_version_directive_t{
  945. major: token.major,
  946. minor: token.minor,
  947. }
  948. } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  949. value := yaml_tag_directive_t{
  950. handle: token.value,
  951. prefix: token.prefix,
  952. }
  953. if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) {
  954. return false
  955. }
  956. tag_directives = append(tag_directives, value)
  957. }
  958. skip_token(parser)
  959. token = peek_token(parser)
  960. if token == nil {
  961. return false
  962. }
  963. }
  964. for i := range default_tag_directives {
  965. if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) {
  966. return false
  967. }
  968. }
  969. if version_directive_ref != nil {
  970. *version_directive_ref = version_directive
  971. }
  972. if tag_directives_ref != nil {
  973. *tag_directives_ref = tag_directives
  974. }
  975. return true
  976. }
  977. // Append a tag directive to the directives stack.
  978. func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool {
  979. for i := range parser.tag_directives {
  980. if bytes.Equal(value.handle, parser.tag_directives[i].handle) {
  981. if allow_duplicates {
  982. return true
  983. }
  984. return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark)
  985. }
  986. }
  987. // [Go] I suspect the copy is unnecessary. This was likely done
  988. // because there was no way to track ownership of the data.
  989. value_copy := yaml_tag_directive_t{
  990. handle: make([]byte, len(value.handle)),
  991. prefix: make([]byte, len(value.prefix)),
  992. }
  993. copy(value_copy.handle, value.handle)
  994. copy(value_copy.prefix, value.prefix)
  995. parser.tag_directives = append(parser.tag_directives, value_copy)
  996. return true
  997. }