parser_c.go 34 KB

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