parserc.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. }