parser.go 35 KB

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