parserc.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. }
  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. encoding: token.encoding,
  153. }
  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. token.typ != yaml_TAG_DIRECTIVE_TOKEN &&
  179. token.typ != yaml_DOCUMENT_START_TOKEN &&
  180. token.typ != yaml_STREAM_END_TOKEN {
  181. // Parse an implicit document.
  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. version_directive: version_directive,
  217. tag_directives: tag_directives,
  218. implicit: false,
  219. }
  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. }
  252. return yaml_parser_parse_node(parser, event, true, false)
  253. }
  254. // Parse the productions:
  255. // implicit_document ::= block_node DOCUMENT-END*
  256. // *************
  257. // explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
  258. //
  259. func yaml_parser_parse_document_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  260. token := peek_token(parser)
  261. if token == nil {
  262. return false
  263. }
  264. start_mark := token.start_mark
  265. end_mark := token.start_mark
  266. implicit := true
  267. if token.typ == yaml_DOCUMENT_END_TOKEN {
  268. end_mark = token.end_mark
  269. skip_token(parser)
  270. implicit = false
  271. }
  272. parser.tag_directives = parser.tag_directives[:0]
  273. parser.state = yaml_PARSE_DOCUMENT_START_STATE
  274. *event = yaml_event_t{
  275. typ: yaml_DOCUMENT_END_EVENT,
  276. start_mark: start_mark,
  277. end_mark: end_mark,
  278. implicit: implicit,
  279. }
  280. return true
  281. }
  282. // Parse the productions:
  283. // block_node_or_indentless_sequence ::=
  284. // ALIAS
  285. // *****
  286. // | properties (block_content | indentless_block_sequence)?
  287. // ********** *
  288. // | block_content | indentless_block_sequence
  289. // *
  290. // block_node ::= ALIAS
  291. // *****
  292. // | properties block_content?
  293. // ********** *
  294. // | block_content
  295. // *
  296. // flow_node ::= ALIAS
  297. // *****
  298. // | properties flow_content?
  299. // ********** *
  300. // | flow_content
  301. // *
  302. // properties ::= TAG ANCHOR? | ANCHOR TAG?
  303. // *************************
  304. // block_content ::= block_collection | flow_collection | SCALAR
  305. // ******
  306. // flow_content ::= flow_collection | SCALAR
  307. // ******
  308. func yaml_parser_parse_node(parser *yaml_parser_t, event *yaml_event_t, block, indentless_sequence bool) bool {
  309. //defer trace("yaml_parser_parse_node", "block:", block, "indentless_sequence:", indentless_sequence)()
  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. anchor: token.value,
  322. }
  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.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.value
  343. tag_suffix = token.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.value
  355. tag_suffix = token.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.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. anchor: anchor,
  404. tag: tag,
  405. implicit: implicit,
  406. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  407. }
  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.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. anchor: anchor,
  425. tag: tag,
  426. value: token.value,
  427. implicit: plain_implicit,
  428. quoted_implicit: quoted_implicit,
  429. style: yaml_style_t(token.style),
  430. }
  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. anchor: anchor,
  443. tag: tag,
  444. implicit: implicit,
  445. style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE),
  446. }
  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. anchor: anchor,
  457. tag: tag,
  458. implicit: implicit,
  459. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  460. }
  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. anchor: anchor,
  471. tag: tag,
  472. implicit: implicit,
  473. style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE),
  474. }
  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. anchor: anchor,
  485. tag: tag,
  486. implicit: implicit,
  487. style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE),
  488. }
  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. anchor: anchor,
  499. tag: tag,
  500. implicit: implicit,
  501. quoted_implicit: false,
  502. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  503. }
  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. }
  583. parser.state = yaml_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE
  584. return yaml_parser_process_empty_scalar(parser, event, mark)
  585. }
  586. parser.state = parser.states[len(parser.states)-1]
  587. parser.states = parser.states[:len(parser.states)-1]
  588. *event = yaml_event_t{
  589. typ: yaml_SEQUENCE_END_EVENT,
  590. start_mark: token.start_mark,
  591. end_mark: token.start_mark, // [Go] Shouldn't this be token.end_mark?
  592. }
  593. return true
  594. }
  595. // Parse the productions:
  596. // block_mapping ::= BLOCK-MAPPING_START
  597. // *******************
  598. // ((KEY block_node_or_indentless_sequence?)?
  599. // *** *
  600. // (VALUE block_node_or_indentless_sequence?)?)*
  601. //
  602. // BLOCK-END
  603. // *********
  604. //
  605. func yaml_parser_parse_block_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  606. if first {
  607. token := peek_token(parser)
  608. parser.marks = append(parser.marks, token.start_mark)
  609. skip_token(parser)
  610. }
  611. token := peek_token(parser)
  612. if token == nil {
  613. return false
  614. }
  615. if token.typ == yaml_KEY_TOKEN {
  616. mark := token.end_mark
  617. skip_token(parser)
  618. token = peek_token(parser)
  619. if token == nil {
  620. return false
  621. }
  622. if token.typ != yaml_KEY_TOKEN &&
  623. token.typ != yaml_VALUE_TOKEN &&
  624. token.typ != yaml_BLOCK_END_TOKEN {
  625. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_VALUE_STATE)
  626. return yaml_parser_parse_node(parser, event, true, true)
  627. } else {
  628. parser.state = yaml_PARSE_BLOCK_MAPPING_VALUE_STATE
  629. return yaml_parser_process_empty_scalar(parser, event, mark)
  630. }
  631. } else if token.typ == yaml_BLOCK_END_TOKEN {
  632. parser.state = parser.states[len(parser.states)-1]
  633. parser.states = parser.states[:len(parser.states)-1]
  634. parser.marks = parser.marks[:len(parser.marks)-1]
  635. *event = yaml_event_t{
  636. typ: yaml_MAPPING_END_EVENT,
  637. start_mark: token.start_mark,
  638. end_mark: token.end_mark,
  639. }
  640. skip_token(parser)
  641. return true
  642. }
  643. context_mark := parser.marks[len(parser.marks)-1]
  644. parser.marks = parser.marks[:len(parser.marks)-1]
  645. return yaml_parser_set_parser_error_context(parser,
  646. "while parsing a block mapping", context_mark,
  647. "did not find expected key", token.start_mark)
  648. }
  649. // Parse the productions:
  650. // block_mapping ::= BLOCK-MAPPING_START
  651. //
  652. // ((KEY block_node_or_indentless_sequence?)?
  653. //
  654. // (VALUE block_node_or_indentless_sequence?)?)*
  655. // ***** *
  656. // BLOCK-END
  657. //
  658. //
  659. func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  660. token := peek_token(parser)
  661. if token == nil {
  662. return false
  663. }
  664. if token.typ == yaml_VALUE_TOKEN {
  665. mark := token.end_mark
  666. skip_token(parser)
  667. token = peek_token(parser)
  668. if token == nil {
  669. return false
  670. }
  671. if token.typ != yaml_KEY_TOKEN &&
  672. token.typ != yaml_VALUE_TOKEN &&
  673. token.typ != yaml_BLOCK_END_TOKEN {
  674. parser.states = append(parser.states, yaml_PARSE_BLOCK_MAPPING_KEY_STATE)
  675. return yaml_parser_parse_node(parser, event, true, true)
  676. }
  677. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  678. return yaml_parser_process_empty_scalar(parser, event, mark)
  679. }
  680. parser.state = yaml_PARSE_BLOCK_MAPPING_KEY_STATE
  681. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  682. }
  683. // Parse the productions:
  684. // flow_sequence ::= FLOW-SEQUENCE-START
  685. // *******************
  686. // (flow_sequence_entry FLOW-ENTRY)*
  687. // * **********
  688. // flow_sequence_entry?
  689. // *
  690. // FLOW-SEQUENCE-END
  691. // *****************
  692. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  693. // *
  694. //
  695. func yaml_parser_parse_flow_sequence_entry(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  696. if first {
  697. token := peek_token(parser)
  698. parser.marks = append(parser.marks, token.start_mark)
  699. skip_token(parser)
  700. }
  701. token := peek_token(parser)
  702. if token == nil {
  703. return false
  704. }
  705. if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  706. if !first {
  707. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  708. skip_token(parser)
  709. token = peek_token(parser)
  710. if token == nil {
  711. return false
  712. }
  713. } else {
  714. context_mark := parser.marks[len(parser.marks)-1]
  715. parser.marks = parser.marks[:len(parser.marks)-1]
  716. return yaml_parser_set_parser_error_context(parser,
  717. "while parsing a flow sequence", context_mark,
  718. "did not find expected ',' or ']'", token.start_mark)
  719. }
  720. }
  721. if token.typ == yaml_KEY_TOKEN {
  722. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE
  723. *event = yaml_event_t{
  724. typ: yaml_MAPPING_START_EVENT,
  725. start_mark: token.start_mark,
  726. end_mark: token.end_mark,
  727. implicit: true,
  728. style: yaml_style_t(yaml_FLOW_MAPPING_STYLE),
  729. }
  730. skip_token(parser)
  731. return true
  732. } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  733. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE)
  734. return yaml_parser_parse_node(parser, event, false, false)
  735. }
  736. }
  737. parser.state = parser.states[len(parser.states)-1]
  738. parser.states = parser.states[:len(parser.states)-1]
  739. parser.marks = parser.marks[:len(parser.marks)-1]
  740. *event = yaml_event_t{
  741. typ: yaml_SEQUENCE_END_EVENT,
  742. start_mark: token.start_mark,
  743. end_mark: token.end_mark,
  744. }
  745. skip_token(parser)
  746. return true
  747. }
  748. //
  749. // Parse the productions:
  750. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  751. // *** *
  752. //
  753. func yaml_parser_parse_flow_sequence_entry_mapping_key(parser *yaml_parser_t, event *yaml_event_t) bool {
  754. token := peek_token(parser)
  755. if token == nil {
  756. return false
  757. }
  758. if token.typ != yaml_VALUE_TOKEN &&
  759. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  760. token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  761. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE)
  762. return yaml_parser_parse_node(parser, event, false, false)
  763. }
  764. mark := token.end_mark
  765. skip_token(parser)
  766. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE
  767. return yaml_parser_process_empty_scalar(parser, event, mark)
  768. }
  769. // Parse the productions:
  770. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  771. // ***** *
  772. //
  773. func yaml_parser_parse_flow_sequence_entry_mapping_value(parser *yaml_parser_t, event *yaml_event_t) bool {
  774. token := peek_token(parser)
  775. if token == nil {
  776. return false
  777. }
  778. if token.typ == yaml_VALUE_TOKEN {
  779. skip_token(parser)
  780. token := peek_token(parser)
  781. if token == nil {
  782. return false
  783. }
  784. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_SEQUENCE_END_TOKEN {
  785. parser.states = append(parser.states, yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE)
  786. return yaml_parser_parse_node(parser, event, false, false)
  787. }
  788. }
  789. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE
  790. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  791. }
  792. // Parse the productions:
  793. // flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  794. // *
  795. //
  796. func yaml_parser_parse_flow_sequence_entry_mapping_end(parser *yaml_parser_t, event *yaml_event_t) bool {
  797. token := peek_token(parser)
  798. if token == nil {
  799. return false
  800. }
  801. parser.state = yaml_PARSE_FLOW_SEQUENCE_ENTRY_STATE
  802. *event = yaml_event_t{
  803. typ: yaml_MAPPING_END_EVENT,
  804. start_mark: token.start_mark,
  805. end_mark: token.start_mark, // [Go] Shouldn't this be end_mark?
  806. }
  807. return true
  808. }
  809. // Parse the productions:
  810. // flow_mapping ::= FLOW-MAPPING-START
  811. // ******************
  812. // (flow_mapping_entry FLOW-ENTRY)*
  813. // * **********
  814. // flow_mapping_entry?
  815. // ******************
  816. // FLOW-MAPPING-END
  817. // ****************
  818. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  819. // * *** *
  820. //
  821. func yaml_parser_parse_flow_mapping_key(parser *yaml_parser_t, event *yaml_event_t, first bool) bool {
  822. if first {
  823. token := peek_token(parser)
  824. parser.marks = append(parser.marks, token.start_mark)
  825. skip_token(parser)
  826. }
  827. token := peek_token(parser)
  828. if token == nil {
  829. return false
  830. }
  831. if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  832. if !first {
  833. if token.typ == yaml_FLOW_ENTRY_TOKEN {
  834. skip_token(parser)
  835. token = peek_token(parser)
  836. if token == nil {
  837. return false
  838. }
  839. } else {
  840. context_mark := parser.marks[len(parser.marks)-1]
  841. parser.marks = parser.marks[:len(parser.marks)-1]
  842. return yaml_parser_set_parser_error_context(parser,
  843. "while parsing a flow mapping", context_mark,
  844. "did not find expected ',' or '}'", token.start_mark)
  845. }
  846. }
  847. if token.typ == yaml_KEY_TOKEN {
  848. skip_token(parser)
  849. token = peek_token(parser)
  850. if token == nil {
  851. return false
  852. }
  853. if token.typ != yaml_VALUE_TOKEN &&
  854. token.typ != yaml_FLOW_ENTRY_TOKEN &&
  855. token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  856. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_VALUE_STATE)
  857. return yaml_parser_parse_node(parser, event, false, false)
  858. } else {
  859. parser.state = yaml_PARSE_FLOW_MAPPING_VALUE_STATE
  860. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  861. }
  862. } else if token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  863. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE)
  864. return yaml_parser_parse_node(parser, event, false, false)
  865. }
  866. }
  867. parser.state = parser.states[len(parser.states)-1]
  868. parser.states = parser.states[:len(parser.states)-1]
  869. parser.marks = parser.marks[:len(parser.marks)-1]
  870. *event = yaml_event_t{
  871. typ: yaml_MAPPING_END_EVENT,
  872. start_mark: token.start_mark,
  873. end_mark: token.end_mark,
  874. }
  875. skip_token(parser)
  876. return true
  877. }
  878. // Parse the productions:
  879. // flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
  880. // * ***** *
  881. //
  882. func yaml_parser_parse_flow_mapping_value(parser *yaml_parser_t, event *yaml_event_t, empty bool) bool {
  883. token := peek_token(parser)
  884. if token == nil {
  885. return false
  886. }
  887. if empty {
  888. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  889. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  890. }
  891. if token.typ == yaml_VALUE_TOKEN {
  892. skip_token(parser)
  893. token = peek_token(parser)
  894. if token == nil {
  895. return false
  896. }
  897. if token.typ != yaml_FLOW_ENTRY_TOKEN && token.typ != yaml_FLOW_MAPPING_END_TOKEN {
  898. parser.states = append(parser.states, yaml_PARSE_FLOW_MAPPING_KEY_STATE)
  899. return yaml_parser_parse_node(parser, event, false, false)
  900. }
  901. }
  902. parser.state = yaml_PARSE_FLOW_MAPPING_KEY_STATE
  903. return yaml_parser_process_empty_scalar(parser, event, token.start_mark)
  904. }
  905. // Generate an empty scalar event.
  906. func yaml_parser_process_empty_scalar(parser *yaml_parser_t, event *yaml_event_t, mark yaml_mark_t) bool {
  907. *event = yaml_event_t{
  908. typ: yaml_SCALAR_EVENT,
  909. start_mark: mark,
  910. end_mark: mark,
  911. value: nil, // Empty
  912. implicit: true,
  913. style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE),
  914. }
  915. return true
  916. }
  917. var default_tag_directives = []yaml_tag_directive_t{
  918. {[]byte("!"), []byte("!")},
  919. {[]byte("!!"), []byte("tag:yaml.org,2002:")},
  920. }
  921. // Parse directives.
  922. func yaml_parser_process_directives(parser *yaml_parser_t,
  923. version_directive_ref **yaml_version_directive_t,
  924. tag_directives_ref *[]yaml_tag_directive_t) bool {
  925. var version_directive *yaml_version_directive_t
  926. var tag_directives []yaml_tag_directive_t
  927. token := peek_token(parser)
  928. if token == nil {
  929. return false
  930. }
  931. for token.typ == yaml_VERSION_DIRECTIVE_TOKEN || token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  932. if token.typ == yaml_VERSION_DIRECTIVE_TOKEN {
  933. if version_directive != nil {
  934. yaml_parser_set_parser_error(parser,
  935. "found duplicate %YAML directive", token.start_mark)
  936. return false
  937. }
  938. if token.major != 1 || token.minor != 1 {
  939. yaml_parser_set_parser_error(parser,
  940. "found incompatible YAML document", token.start_mark)
  941. return false
  942. }
  943. version_directive = &yaml_version_directive_t{
  944. major: token.major,
  945. minor: token.minor,
  946. }
  947. } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN {
  948. value := yaml_tag_directive_t{
  949. handle: token.value,
  950. prefix: token.prefix,
  951. }
  952. if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) {
  953. return false
  954. }
  955. tag_directives = append(tag_directives, value)
  956. }
  957. skip_token(parser)
  958. token = peek_token(parser)
  959. if token == nil {
  960. return false
  961. }
  962. }
  963. for i := range default_tag_directives {
  964. if !yaml_parser_append_tag_directive(parser, default_tag_directives[i], true, token.start_mark) {
  965. return false
  966. }
  967. }
  968. if version_directive_ref != nil {
  969. *version_directive_ref = version_directive
  970. }
  971. if tag_directives_ref != nil {
  972. *tag_directives_ref = tag_directives
  973. }
  974. return true
  975. }
  976. // Append a tag directive to the directives stack.
  977. func yaml_parser_append_tag_directive(parser *yaml_parser_t, value yaml_tag_directive_t, allow_duplicates bool, mark yaml_mark_t) bool {
  978. for i := range parser.tag_directives {
  979. if bytes.Equal(value.handle, parser.tag_directives[i].handle) {
  980. if allow_duplicates {
  981. return true
  982. }
  983. return yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark)
  984. }
  985. }
  986. // [Go] I suspect the copy is unnecessary. This was likely done
  987. // because there was no way to track ownership of the data.
  988. value_copy := yaml_tag_directive_t{
  989. handle: make([]byte, len(value.handle)),
  990. prefix: make([]byte, len(value.prefix)),
  991. }
  992. copy(value_copy.handle, value.handle)
  993. copy(value_copy.prefix, value.prefix)
  994. parser.tag_directives = append(parser.tag_directives, value_copy)
  995. return true
  996. }