11 // The following notes assume that you are familiar with the YAML specification
12 // (http://yaml.org/spec/cvs/current.html). We mostly follow it, although in
13 // some cases we are less restrictive that it requires.
15 // The process of transforming a YAML stream into a sequence of events is
16 // divided on two steps: Scanning and Parsing.
18 // The Scanner transforms the input stream into a sequence of tokens, while the
19 // parser transform the sequence of tokens produced by the Scanner into a
20 // sequence of parsing events.
22 // The Scanner is rather clever and complicated. The Parser, on the contrary,
23 // is a straightforward implementation of a recursive-descendant parser (or,
24 // LL(1) parser, as it is usually called).
26 // Actually there are two issues of Scanning that might be called "clever", the
27 // rest is quite straightforward. The issues are "block collection start" and
28 // "simple keys". Both issues are explained below in details.
30 // Here the Scanning step is explained and implemented. We start with the list
31 // of all the tokens produced by the Scanner together with short descriptions.
35 // STREAM-START(encoding) # The stream start.
36 // STREAM-END # The stream end.
37 // VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
38 // TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
39 // DOCUMENT-START # '---'
40 // DOCUMENT-END # '...'
41 // BLOCK-SEQUENCE-START # Indentation increase denoting a block
42 // BLOCK-MAPPING-START # sequence or a block mapping.
43 // BLOCK-END # Indentation decrease.
44 // FLOW-SEQUENCE-START # '['
45 // FLOW-SEQUENCE-END # ']'
46 // BLOCK-SEQUENCE-START # '{'
47 // BLOCK-SEQUENCE-END # '}'
50 // KEY # '?' or nothing (simple keys).
52 // ALIAS(anchor) # '*anchor'
53 // ANCHOR(anchor) # '&anchor'
54 // TAG(handle,suffix) # '!handle!suffix'
55 // SCALAR(value,style) # A scalar.
57 // The following two tokens are "virtual" tokens denoting the beginning and the
60 // STREAM-START(encoding)
63 // We pass the information about the input stream encoding with the
64 // STREAM-START token.
66 // The next two tokens are responsible for tags:
68 // VERSION-DIRECTIVE(major,minor)
69 // TAG-DIRECTIVE(handle,prefix)
75 // %TAG !yaml! tag:yaml.org,2002:
78 // The correspoding sequence of tokens:
80 // STREAM-START(utf-8)
81 // VERSION-DIRECTIVE(1,1)
82 // TAG-DIRECTIVE("!","!foo")
83 // TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
87 // Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
90 // The document start and end indicators are represented by:
95 // Note that if a YAML stream contains an implicit document (without '---'
96 // and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
99 // In the following examples, we present whole documents together with the
102 // 1. An implicit document:
108 // STREAM-START(utf-8)
109 // SCALAR("a scalar",single-quoted)
112 // 2. An explicit document:
120 // STREAM-START(utf-8)
122 // SCALAR("a scalar",single-quoted)
126 // 3. Several documents in a stream:
132 // 'yet another scalar'
136 // STREAM-START(utf-8)
137 // SCALAR("a scalar",single-quoted)
139 // SCALAR("another scalar",single-quoted)
141 // SCALAR("yet another scalar",single-quoted)
144 // We have already introduced the SCALAR token above. The following tokens are
145 // used to describe aliases, anchors, tag, and scalars:
149 // TAG(handle,suffix)
150 // SCALAR(value,style)
152 // The following series of examples illustrate the usage of these tokens:
154 // 1. A recursive sequence:
160 // STREAM-START(utf-8)
162 // FLOW-SEQUENCE-START
167 // 2. A tagged scalar:
169 // !!float "3.14" # A good approximation.
173 // STREAM-START(utf-8)
175 // SCALAR("3.14",double-quoted)
178 // 3. Various scalar styles:
180 // --- # Implicit empty plain scalars do not produce tokens.
181 // --- a plain scalar
182 // --- 'a single-quoted scalar'
183 // --- "a double-quoted scalar"
192 // STREAM-START(utf-8)
195 // SCALAR("a plain scalar",plain)
197 // SCALAR("a single-quoted scalar",single-quoted)
199 // SCALAR("a double-quoted scalar",double-quoted)
201 // SCALAR("a literal scalar",literal)
203 // SCALAR("a folded scalar",folded)
206 // Now it's time to review collection-related tokens. We will start with
209 // FLOW-SEQUENCE-START
211 // FLOW-MAPPING-START
217 // The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
218 // FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
219 // correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
220 // indicators '?' and ':', which are used for denoting mapping keys and values,
221 // are represented by the KEY and VALUE tokens.
223 // The following examples show flow collections:
225 // 1. A flow sequence:
227 // [item 1, item 2, item 3]
231 // STREAM-START(utf-8)
232 // FLOW-SEQUENCE-START
233 // SCALAR("item 1",plain)
235 // SCALAR("item 2",plain)
237 // SCALAR("item 3",plain)
241 // 2. A flow mapping:
244 // a simple key: a value, # Note that the KEY token is produced.
245 // ? a complex key: another value,
250 // STREAM-START(utf-8)
251 // FLOW-MAPPING-START
253 // SCALAR("a simple key",plain)
255 // SCALAR("a value",plain)
258 // SCALAR("a complex key",plain)
260 // SCALAR("another value",plain)
265 // A simple key is a key which is not denoted by the '?' indicator. Note that
266 // the Scanner still produce the KEY token whenever it encounters a simple key.
268 // For scanning block collections, the following tokens are used (note that we
269 // repeat KEY and VALUE here):
271 // BLOCK-SEQUENCE-START
272 // BLOCK-MAPPING-START
278 // The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
279 // increase that precedes a block collection (cf. the INDENT token in Python).
280 // The token BLOCK-END denote indentation decrease that ends a block collection
281 // (cf. the DEDENT token in Python). However YAML has some syntax pecularities
282 // that makes detections of these tokens more complex.
284 // The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
285 // '-', '?', and ':' correspondingly.
287 // The following examples show how the tokens BLOCK-SEQUENCE-START,
288 // BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
290 // 1. Block sequences:
303 // STREAM-START(utf-8)
304 // BLOCK-SEQUENCE-START
306 // SCALAR("item 1",plain)
308 // SCALAR("item 2",plain)
310 // BLOCK-SEQUENCE-START
312 // SCALAR("item 3.1",plain)
314 // SCALAR("item 3.2",plain)
317 // BLOCK-MAPPING-START
319 // SCALAR("key 1",plain)
321 // SCALAR("value 1",plain)
323 // SCALAR("key 2",plain)
325 // SCALAR("value 2",plain)
330 // 2. Block mappings:
332 // a simple key: a value # The KEY token is produced here.
344 // STREAM-START(utf-8)
345 // BLOCK-MAPPING-START
347 // SCALAR("a simple key",plain)
349 // SCALAR("a value",plain)
351 // SCALAR("a complex key",plain)
353 // SCALAR("another value",plain)
355 // SCALAR("a mapping",plain)
356 // BLOCK-MAPPING-START
358 // SCALAR("key 1",plain)
360 // SCALAR("value 1",plain)
362 // SCALAR("key 2",plain)
364 // SCALAR("value 2",plain)
367 // SCALAR("a sequence",plain)
369 // BLOCK-SEQUENCE-START
371 // SCALAR("item 1",plain)
373 // SCALAR("item 2",plain)
378 // YAML does not always require to start a new block collection from a new
379 // line. If the current line contains only '-', '?', and ':' indicators, a new
380 // block collection may start at the current line. The following examples
381 // illustrate this case:
383 // 1. Collections in a sequence:
394 // STREAM-START(utf-8)
395 // BLOCK-SEQUENCE-START
397 // BLOCK-SEQUENCE-START
399 // SCALAR("item 1",plain)
401 // SCALAR("item 2",plain)
404 // BLOCK-MAPPING-START
406 // SCALAR("key 1",plain)
408 // SCALAR("value 1",plain)
410 // SCALAR("key 2",plain)
412 // SCALAR("value 2",plain)
415 // BLOCK-MAPPING-START
417 // SCALAR("complex key")
419 // SCALAR("complex value")
424 // 2. Collections in a mapping:
435 // STREAM-START(utf-8)
436 // BLOCK-MAPPING-START
438 // SCALAR("a sequence",plain)
440 // BLOCK-SEQUENCE-START
442 // SCALAR("item 1",plain)
444 // SCALAR("item 2",plain)
447 // SCALAR("a mapping",plain)
449 // BLOCK-MAPPING-START
451 // SCALAR("key 1",plain)
453 // SCALAR("value 1",plain)
455 // SCALAR("key 2",plain)
457 // SCALAR("value 2",plain)
462 // YAML also permits non-indented sequences if they are included into a block
463 // mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
466 // - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
471 // STREAM-START(utf-8)
472 // BLOCK-MAPPING-START
474 // SCALAR("key",plain)
477 // SCALAR("item 1",plain)
479 // SCALAR("item 2",plain)
483 // Ensure that the buffer contains the required number of characters.
484 // Return true on success, false on failure (reader error or memory error).
485 func cache(parser *yaml_parser_t, length int) bool {
486 // [Go] This was inlined: !cache(A, B) -> unread < B && !update(A, B)
487 return parser.unread >= length || yaml_parser_update_buffer(parser, length)
490 // Advance the buffer pointer.
491 func skip(parser *yaml_parser_t) {
495 parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
498 func skip_line(parser *yaml_parser_t) {
499 if is_crlf(parser.buffer, parser.buffer_pos) {
500 parser.mark.index += 2
501 parser.mark.column = 0
504 parser.buffer_pos += 2
505 } else if is_break(parser.buffer, parser.buffer_pos) {
507 parser.mark.column = 0
510 parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
514 // Copy a character to a string buffer and advance pointers.
515 func read(parser *yaml_parser_t, s []byte) []byte {
516 w := width(parser.buffer[parser.buffer_pos])
518 panic("invalid character sequence")
521 s = make([]byte, 0, 32)
523 if w == 1 && len(s)+w <= cap(s) {
525 s[len(s)-1] = parser.buffer[parser.buffer_pos]
528 s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...)
529 parser.buffer_pos += w
537 // Copy a line break character to a string buffer and advance pointers.
538 func read_line(parser *yaml_parser_t, s []byte) []byte {
540 pos := parser.buffer_pos
542 case buf[pos] == '\r' && buf[pos+1] == '\n':
545 parser.buffer_pos += 2
548 case buf[pos] == '\r' || buf[pos] == '\n':
551 parser.buffer_pos += 1
552 case buf[pos] == '\xC2' && buf[pos+1] == '\x85':
555 parser.buffer_pos += 2
556 case buf[pos] == '\xE2' && buf[pos+1] == '\x80' && (buf[pos+2] == '\xA8' || buf[pos+2] == '\xA9'):
558 s = append(s, buf[parser.buffer_pos:pos+3]...)
559 parser.buffer_pos += 3
564 parser.mark.column = 0
570 // Get the next token.
571 func yaml_parser_scan(parser *yaml_parser_t, token *yaml_token_t) bool {
572 // Erase the token object.
573 *token = yaml_token_t{} // [Go] Is this necessary?
575 // No tokens after STREAM-END or error.
576 if parser.stream_end_produced || parser.error != yaml_NO_ERROR {
580 // Ensure that the tokens queue contains enough tokens.
581 if !parser.token_available {
582 if !yaml_parser_fetch_more_tokens(parser) {
587 // Fetch the next token from the queue.
588 *token = parser.tokens[parser.tokens_head]
590 parser.tokens_parsed++
591 parser.token_available = false
593 if token.typ == yaml_STREAM_END_TOKEN {
594 parser.stream_end_produced = true
599 // Set the scanner error and return false.
600 func yaml_parser_set_scanner_error(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string) bool {
601 parser.error = yaml_SCANNER_ERROR
602 parser.context = context
603 parser.context_mark = context_mark
604 parser.problem = problem
605 parser.problem_mark = parser.mark
609 func yaml_parser_set_scanner_tag_error(parser *yaml_parser_t, directive bool, context_mark yaml_mark_t, problem string) bool {
610 context := "while parsing a tag"
612 context = "while parsing a %TAG directive"
614 return yaml_parser_set_scanner_error(parser, context, context_mark, "did not find URI escaped octet")
617 func trace(args ...interface{}) func() {
618 pargs := append([]interface{}{"+++"}, args...)
619 fmt.Println(pargs...)
620 pargs = append([]interface{}{"---"}, args...)
621 return func() { fmt.Println(pargs...) }
624 // Ensure that the tokens queue contains at least one token which can be
625 // returned to the Parser.
626 func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
627 // While we need more tokens to fetch, do it.
629 // Check if we really need to fetch more tokens.
630 need_more_tokens := false
632 if parser.tokens_head == len(parser.tokens) {
634 need_more_tokens = true
636 // Check if any potential simple key may occupy the head position.
637 if !yaml_parser_stale_simple_keys(parser) {
641 for i := range parser.simple_keys {
642 simple_key := &parser.simple_keys[i]
643 if simple_key.possible && simple_key.token_number == parser.tokens_parsed {
644 need_more_tokens = true
651 if !need_more_tokens {
654 // Fetch the next token.
655 if !yaml_parser_fetch_next_token(parser) {
660 parser.token_available = true
664 // The dispatcher for token fetchers.
665 func yaml_parser_fetch_next_token(parser *yaml_parser_t) bool {
666 // Ensure that the buffer is initialized.
667 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
671 // Check if we just started scanning. Fetch STREAM-START then.
672 if !parser.stream_start_produced {
673 return yaml_parser_fetch_stream_start(parser)
676 // Eat whitespaces and comments until we reach the next token.
677 if !yaml_parser_scan_to_next_token(parser) {
681 // Remove obsolete potential simple keys.
682 if !yaml_parser_stale_simple_keys(parser) {
686 // Check the indentation level against the current column.
687 if !yaml_parser_unroll_indent(parser, parser.mark.column) {
691 // Ensure that the buffer contains at least 4 characters. 4 is the length
692 // of the longest indicators ('--- ' and '... ').
693 if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
697 // Is it the end of the stream?
698 if is_z(parser.buffer, parser.buffer_pos) {
699 return yaml_parser_fetch_stream_end(parser)
702 // Is it a directive?
703 if parser.mark.column == 0 && parser.buffer[parser.buffer_pos] == '%' {
704 return yaml_parser_fetch_directive(parser)
708 pos := parser.buffer_pos
710 // Is it the document start indicator?
711 if parser.mark.column == 0 && buf[pos] == '-' && buf[pos+1] == '-' && buf[pos+2] == '-' && is_blankz(buf, pos+3) {
712 return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_START_TOKEN)
715 // Is it the document end indicator?
716 if parser.mark.column == 0 && buf[pos] == '.' && buf[pos+1] == '.' && buf[pos+2] == '.' && is_blankz(buf, pos+3) {
717 return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_END_TOKEN)
720 // Is it the flow sequence start indicator?
722 return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_SEQUENCE_START_TOKEN)
725 // Is it the flow mapping start indicator?
726 if parser.buffer[parser.buffer_pos] == '{' {
727 return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_MAPPING_START_TOKEN)
730 // Is it the flow sequence end indicator?
731 if parser.buffer[parser.buffer_pos] == ']' {
732 return yaml_parser_fetch_flow_collection_end(parser,
733 yaml_FLOW_SEQUENCE_END_TOKEN)
736 // Is it the flow mapping end indicator?
737 if parser.buffer[parser.buffer_pos] == '}' {
738 return yaml_parser_fetch_flow_collection_end(parser,
739 yaml_FLOW_MAPPING_END_TOKEN)
742 // Is it the flow entry indicator?
743 if parser.buffer[parser.buffer_pos] == ',' {
744 return yaml_parser_fetch_flow_entry(parser)
747 // Is it the block entry indicator?
748 if parser.buffer[parser.buffer_pos] == '-' && is_blankz(parser.buffer, parser.buffer_pos+1) {
749 return yaml_parser_fetch_block_entry(parser)
752 // Is it the key indicator?
753 if parser.buffer[parser.buffer_pos] == '?' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
754 return yaml_parser_fetch_key(parser)
757 // Is it the value indicator?
758 if parser.buffer[parser.buffer_pos] == ':' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
759 return yaml_parser_fetch_value(parser)
763 if parser.buffer[parser.buffer_pos] == '*' {
764 return yaml_parser_fetch_anchor(parser, yaml_ALIAS_TOKEN)
768 if parser.buffer[parser.buffer_pos] == '&' {
769 return yaml_parser_fetch_anchor(parser, yaml_ANCHOR_TOKEN)
773 if parser.buffer[parser.buffer_pos] == '!' {
774 return yaml_parser_fetch_tag(parser)
777 // Is it a literal scalar?
778 if parser.buffer[parser.buffer_pos] == '|' && parser.flow_level == 0 {
779 return yaml_parser_fetch_block_scalar(parser, true)
782 // Is it a folded scalar?
783 if parser.buffer[parser.buffer_pos] == '>' && parser.flow_level == 0 {
784 return yaml_parser_fetch_block_scalar(parser, false)
787 // Is it a single-quoted scalar?
788 if parser.buffer[parser.buffer_pos] == '\'' {
789 return yaml_parser_fetch_flow_scalar(parser, true)
792 // Is it a double-quoted scalar?
793 if parser.buffer[parser.buffer_pos] == '"' {
794 return yaml_parser_fetch_flow_scalar(parser, false)
797 // Is it a plain scalar?
799 // A plain scalar may start with any non-blank characters except
801 // '-', '?', ':', ',', '[', ']', '{', '}',
802 // '#', '&', '*', '!', '|', '>', '\'', '\"',
805 // In the block context (and, for the '-' indicator, in the flow context
806 // too), it may also start with the characters
810 // if it is followed by a non-space character.
812 // The last rule is more restrictive than the specification requires.
813 // [Go] Make this logic more reasonable.
814 //switch parser.buffer[parser.buffer_pos] {
815 //case '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`':
817 if !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '-' ||
818 parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':' ||
819 parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '[' ||
820 parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
821 parser.buffer[parser.buffer_pos] == '}' || parser.buffer[parser.buffer_pos] == '#' ||
822 parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '*' ||
823 parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '|' ||
824 parser.buffer[parser.buffer_pos] == '>' || parser.buffer[parser.buffer_pos] == '\'' ||
825 parser.buffer[parser.buffer_pos] == '"' || parser.buffer[parser.buffer_pos] == '%' ||
826 parser.buffer[parser.buffer_pos] == '@' || parser.buffer[parser.buffer_pos] == '`') ||
827 (parser.buffer[parser.buffer_pos] == '-' && !is_blank(parser.buffer, parser.buffer_pos+1)) ||
828 (parser.flow_level == 0 &&
829 (parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':') &&
830 !is_blankz(parser.buffer, parser.buffer_pos+1)) {
831 return yaml_parser_fetch_plain_scalar(parser)
834 // If we don't determine the token type so far, it is an error.
835 return yaml_parser_set_scanner_error(parser,
836 "while scanning for the next token", parser.mark,
837 "found character that cannot start any token")
840 // Check the list of potential simple keys and remove the positions that
841 // cannot contain simple keys anymore.
842 func yaml_parser_stale_simple_keys(parser *yaml_parser_t) bool {
843 // Check for a potential simple key for each flow level.
844 for i := range parser.simple_keys {
845 simple_key := &parser.simple_keys[i]
847 // The specification requires that a simple key
849 // - is limited to a single line,
850 // - is shorter than 1024 characters.
851 if simple_key.possible && (simple_key.mark.line < parser.mark.line || simple_key.mark.index+1024 < parser.mark.index) {
853 // Check if the potential simple key to be removed is required.
854 if simple_key.required {
855 return yaml_parser_set_scanner_error(parser,
856 "while scanning a simple key", simple_key.mark,
857 "could not find expected ':'")
859 simple_key.possible = false
865 // Check if a simple key may start at the current position and add it if
867 func yaml_parser_save_simple_key(parser *yaml_parser_t) bool {
868 // A simple key is required at the current position if the scanner is in
869 // the block context and the current column coincides with the indentation
872 required := parser.flow_level == 0 && parser.indent == parser.mark.column
874 // A simple key is required only when it is the first token in the current
875 // line. Therefore it is always allowed. But we add a check anyway.
876 if required && !parser.simple_key_allowed {
877 panic("should not happen")
881 // If the current position may start a simple key, save it.
883 if parser.simple_key_allowed {
884 simple_key := yaml_simple_key_t{
887 token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
889 simple_key.mark = parser.mark
891 if !yaml_parser_remove_simple_key(parser) {
894 parser.simple_keys[len(parser.simple_keys)-1] = simple_key
899 // Remove a potential simple key at the current flow level.
900 func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
901 i := len(parser.simple_keys) - 1
902 if parser.simple_keys[i].possible {
903 // If the key is required, it is an error.
904 if parser.simple_keys[i].required {
905 return yaml_parser_set_scanner_error(parser,
906 "while scanning a simple key", parser.simple_keys[i].mark,
907 "could not find expected ':'")
910 // Remove the key from the stack.
911 parser.simple_keys[i].possible = false
915 // Increase the flow level and resize the simple key list if needed.
916 func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
917 // Reset the simple key on the next level.
918 parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
920 // Increase the flow level.
925 // Decrease the flow level.
926 func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
927 if parser.flow_level > 0 {
929 parser.simple_keys = parser.simple_keys[:len(parser.simple_keys)-1]
934 // Push the current indentation level to the stack and set the new level
935 // the current column is greater than the indentation level. In this case,
936 // append or insert the specified token into the token queue.
937 func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool {
938 // In the flow context, do nothing.
939 if parser.flow_level > 0 {
943 if parser.indent < column {
944 // Push the current indentation level to the stack and set the new
945 // indentation level.
946 parser.indents = append(parser.indents, parser.indent)
947 parser.indent = column
949 // Create a token and insert it into the queue.
950 token := yaml_token_t{
956 number -= parser.tokens_parsed
958 yaml_insert_token(parser, number, &token)
963 // Pop indentation levels from the indents stack until the current level
964 // becomes less or equal to the column. For each indentation level, append
965 // the BLOCK-END token.
966 func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool {
967 // In the flow context, do nothing.
968 if parser.flow_level > 0 {
972 // Loop through the indentation levels in the stack.
973 for parser.indent > column {
974 // Create a token and append it to the queue.
975 token := yaml_token_t{
976 typ: yaml_BLOCK_END_TOKEN,
977 start_mark: parser.mark,
978 end_mark: parser.mark,
980 yaml_insert_token(parser, -1, &token)
982 // Pop the indentation level.
983 parser.indent = parser.indents[len(parser.indents)-1]
984 parser.indents = parser.indents[:len(parser.indents)-1]
989 // Initialize the scanner and produce the STREAM-START token.
990 func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
992 // Set the initial indentation.
995 // Initialize the simple key stack.
996 parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
998 // A simple key is allowed at the beginning of the stream.
999 parser.simple_key_allowed = true
1002 parser.stream_start_produced = true
1004 // Create the STREAM-START token and append it to the queue.
1005 token := yaml_token_t{
1006 typ: yaml_STREAM_START_TOKEN,
1007 start_mark: parser.mark,
1008 end_mark: parser.mark,
1009 encoding: parser.encoding,
1011 yaml_insert_token(parser, -1, &token)
1015 // Produce the STREAM-END token and shut down the scanner.
1016 func yaml_parser_fetch_stream_end(parser *yaml_parser_t) bool {
1019 if parser.mark.column != 0 {
1020 parser.mark.column = 0
1024 // Reset the indentation level.
1025 if !yaml_parser_unroll_indent(parser, -1) {
1029 // Reset simple keys.
1030 if !yaml_parser_remove_simple_key(parser) {
1034 parser.simple_key_allowed = false
1036 // Create the STREAM-END token and append it to the queue.
1037 token := yaml_token_t{
1038 typ: yaml_STREAM_END_TOKEN,
1039 start_mark: parser.mark,
1040 end_mark: parser.mark,
1042 yaml_insert_token(parser, -1, &token)
1046 // Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1047 func yaml_parser_fetch_directive(parser *yaml_parser_t) bool {
1048 // Reset the indentation level.
1049 if !yaml_parser_unroll_indent(parser, -1) {
1053 // Reset simple keys.
1054 if !yaml_parser_remove_simple_key(parser) {
1058 parser.simple_key_allowed = false
1060 // Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
1061 token := yaml_token_t{}
1062 if !yaml_parser_scan_directive(parser, &token) {
1065 // Append the token to the queue.
1066 yaml_insert_token(parser, -1, &token)
1070 // Produce the DOCUMENT-START or DOCUMENT-END token.
1071 func yaml_parser_fetch_document_indicator(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1072 // Reset the indentation level.
1073 if !yaml_parser_unroll_indent(parser, -1) {
1077 // Reset simple keys.
1078 if !yaml_parser_remove_simple_key(parser) {
1082 parser.simple_key_allowed = false
1084 // Consume the token.
1085 start_mark := parser.mark
1091 end_mark := parser.mark
1093 // Create the DOCUMENT-START or DOCUMENT-END token.
1094 token := yaml_token_t{
1096 start_mark: start_mark,
1099 // Append the token to the queue.
1100 yaml_insert_token(parser, -1, &token)
1104 // Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1105 func yaml_parser_fetch_flow_collection_start(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1106 // The indicators '[' and '{' may start a simple key.
1107 if !yaml_parser_save_simple_key(parser) {
1111 // Increase the flow level.
1112 if !yaml_parser_increase_flow_level(parser) {
1116 // A simple key may follow the indicators '[' and '{'.
1117 parser.simple_key_allowed = true
1119 // Consume the token.
1120 start_mark := parser.mark
1122 end_mark := parser.mark
1124 // Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
1125 token := yaml_token_t{
1127 start_mark: start_mark,
1130 // Append the token to the queue.
1131 yaml_insert_token(parser, -1, &token)
1135 // Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1136 func yaml_parser_fetch_flow_collection_end(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1137 // Reset any potential simple key on the current flow level.
1138 if !yaml_parser_remove_simple_key(parser) {
1142 // Decrease the flow level.
1143 if !yaml_parser_decrease_flow_level(parser) {
1147 // No simple keys after the indicators ']' and '}'.
1148 parser.simple_key_allowed = false
1150 // Consume the token.
1152 start_mark := parser.mark
1154 end_mark := parser.mark
1156 // Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token.
1157 token := yaml_token_t{
1159 start_mark: start_mark,
1162 // Append the token to the queue.
1163 yaml_insert_token(parser, -1, &token)
1167 // Produce the FLOW-ENTRY token.
1168 func yaml_parser_fetch_flow_entry(parser *yaml_parser_t) bool {
1169 // Reset any potential simple keys on the current flow level.
1170 if !yaml_parser_remove_simple_key(parser) {
1174 // Simple keys are allowed after ','.
1175 parser.simple_key_allowed = true
1177 // Consume the token.
1178 start_mark := parser.mark
1180 end_mark := parser.mark
1182 // Create the FLOW-ENTRY token and append it to the queue.
1183 token := yaml_token_t{
1184 typ: yaml_FLOW_ENTRY_TOKEN,
1185 start_mark: start_mark,
1188 yaml_insert_token(parser, -1, &token)
1192 // Produce the BLOCK-ENTRY token.
1193 func yaml_parser_fetch_block_entry(parser *yaml_parser_t) bool {
1194 // Check if the scanner is in the block context.
1195 if parser.flow_level == 0 {
1196 // Check if we are allowed to start a new entry.
1197 if !parser.simple_key_allowed {
1198 return yaml_parser_set_scanner_error(parser, "", parser.mark,
1199 "block sequence entries are not allowed in this context")
1201 // Add the BLOCK-SEQUENCE-START token if needed.
1202 if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, parser.mark) {
1206 // It is an error for the '-' indicator to occur in the flow context,
1207 // but we let the Parser detect and report about it because the Parser
1208 // is able to point to the context.
1211 // Reset any potential simple keys on the current flow level.
1212 if !yaml_parser_remove_simple_key(parser) {
1216 // Simple keys are allowed after '-'.
1217 parser.simple_key_allowed = true
1219 // Consume the token.
1220 start_mark := parser.mark
1222 end_mark := parser.mark
1224 // Create the BLOCK-ENTRY token and append it to the queue.
1225 token := yaml_token_t{
1226 typ: yaml_BLOCK_ENTRY_TOKEN,
1227 start_mark: start_mark,
1230 yaml_insert_token(parser, -1, &token)
1234 // Produce the KEY token.
1235 func yaml_parser_fetch_key(parser *yaml_parser_t) bool {
1237 // In the block context, additional checks are required.
1238 if parser.flow_level == 0 {
1239 // Check if we are allowed to start a new key (not nessesary simple).
1240 if !parser.simple_key_allowed {
1241 return yaml_parser_set_scanner_error(parser, "", parser.mark,
1242 "mapping keys are not allowed in this context")
1244 // Add the BLOCK-MAPPING-START token if needed.
1245 if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
1250 // Reset any potential simple keys on the current flow level.
1251 if !yaml_parser_remove_simple_key(parser) {
1255 // Simple keys are allowed after '?' in the block context.
1256 parser.simple_key_allowed = parser.flow_level == 0
1258 // Consume the token.
1259 start_mark := parser.mark
1261 end_mark := parser.mark
1263 // Create the KEY token and append it to the queue.
1264 token := yaml_token_t{
1265 typ: yaml_KEY_TOKEN,
1266 start_mark: start_mark,
1269 yaml_insert_token(parser, -1, &token)
1273 // Produce the VALUE token.
1274 func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
1276 simple_key := &parser.simple_keys[len(parser.simple_keys)-1]
1278 // Have we found a simple key?
1279 if simple_key.possible {
1280 // Create the KEY token and insert it into the queue.
1281 token := yaml_token_t{
1282 typ: yaml_KEY_TOKEN,
1283 start_mark: simple_key.mark,
1284 end_mark: simple_key.mark,
1286 yaml_insert_token(parser, simple_key.token_number-parser.tokens_parsed, &token)
1288 // In the block context, we may need to add the BLOCK-MAPPING-START token.
1289 if !yaml_parser_roll_indent(parser, simple_key.mark.column,
1290 simple_key.token_number,
1291 yaml_BLOCK_MAPPING_START_TOKEN, simple_key.mark) {
1295 // Remove the simple key.
1296 simple_key.possible = false
1298 // A simple key cannot follow another simple key.
1299 parser.simple_key_allowed = false
1302 // The ':' indicator follows a complex key.
1304 // In the block context, extra checks are required.
1305 if parser.flow_level == 0 {
1307 // Check if we are allowed to start a complex value.
1308 if !parser.simple_key_allowed {
1309 return yaml_parser_set_scanner_error(parser, "", parser.mark,
1310 "mapping values are not allowed in this context")
1313 // Add the BLOCK-MAPPING-START token if needed.
1314 if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
1319 // Simple keys after ':' are allowed in the block context.
1320 parser.simple_key_allowed = parser.flow_level == 0
1323 // Consume the token.
1324 start_mark := parser.mark
1326 end_mark := parser.mark
1328 // Create the VALUE token and append it to the queue.
1329 token := yaml_token_t{
1330 typ: yaml_VALUE_TOKEN,
1331 start_mark: start_mark,
1334 yaml_insert_token(parser, -1, &token)
1338 // Produce the ALIAS or ANCHOR token.
1339 func yaml_parser_fetch_anchor(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1340 // An anchor or an alias could be a simple key.
1341 if !yaml_parser_save_simple_key(parser) {
1345 // A simple key cannot follow an anchor or an alias.
1346 parser.simple_key_allowed = false
1348 // Create the ALIAS or ANCHOR token and append it to the queue.
1349 var token yaml_token_t
1350 if !yaml_parser_scan_anchor(parser, &token, typ) {
1353 yaml_insert_token(parser, -1, &token)
1357 // Produce the TAG token.
1358 func yaml_parser_fetch_tag(parser *yaml_parser_t) bool {
1359 // A tag could be a simple key.
1360 if !yaml_parser_save_simple_key(parser) {
1364 // A simple key cannot follow a tag.
1365 parser.simple_key_allowed = false
1367 // Create the TAG token and append it to the queue.
1368 var token yaml_token_t
1369 if !yaml_parser_scan_tag(parser, &token) {
1372 yaml_insert_token(parser, -1, &token)
1376 // Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1377 func yaml_parser_fetch_block_scalar(parser *yaml_parser_t, literal bool) bool {
1378 // Remove any potential simple keys.
1379 if !yaml_parser_remove_simple_key(parser) {
1383 // A simple key may follow a block scalar.
1384 parser.simple_key_allowed = true
1386 // Create the SCALAR token and append it to the queue.
1387 var token yaml_token_t
1388 if !yaml_parser_scan_block_scalar(parser, &token, literal) {
1391 yaml_insert_token(parser, -1, &token)
1395 // Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1396 func yaml_parser_fetch_flow_scalar(parser *yaml_parser_t, single bool) bool {
1397 // A plain scalar could be a simple key.
1398 if !yaml_parser_save_simple_key(parser) {
1402 // A simple key cannot follow a flow scalar.
1403 parser.simple_key_allowed = false
1405 // Create the SCALAR token and append it to the queue.
1406 var token yaml_token_t
1407 if !yaml_parser_scan_flow_scalar(parser, &token, single) {
1410 yaml_insert_token(parser, -1, &token)
1414 // Produce the SCALAR(...,plain) token.
1415 func yaml_parser_fetch_plain_scalar(parser *yaml_parser_t) bool {
1416 // A plain scalar could be a simple key.
1417 if !yaml_parser_save_simple_key(parser) {
1421 // A simple key cannot follow a flow scalar.
1422 parser.simple_key_allowed = false
1424 // Create the SCALAR token and append it to the queue.
1425 var token yaml_token_t
1426 if !yaml_parser_scan_plain_scalar(parser, &token) {
1429 yaml_insert_token(parser, -1, &token)
1433 // Eat whitespaces and comments until the next token is found.
1434 func yaml_parser_scan_to_next_token(parser *yaml_parser_t) bool {
1436 // Until the next token is not found.
1438 // Allow the BOM mark to start a line.
1439 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1442 if parser.mark.column == 0 && is_bom(parser.buffer, parser.buffer_pos) {
1447 // Tabs are allowed:
1448 // - in the flow context
1449 // - in the block context, but not at the beginning of the line or
1450 // after '-', '?', or ':' (complex value).
1451 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1455 for parser.buffer[parser.buffer_pos] == ' ' || ((parser.flow_level > 0 || !parser.simple_key_allowed) && parser.buffer[parser.buffer_pos] == '\t') {
1457 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1462 // Eat a comment until a line break.
1463 if parser.buffer[parser.buffer_pos] == '#' {
1464 for !is_breakz(parser.buffer, parser.buffer_pos) {
1466 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1472 // If it is a line break, eat it.
1473 if is_break(parser.buffer, parser.buffer_pos) {
1474 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
1479 // In the block context, a new line may start a simple key.
1480 if parser.flow_level == 0 {
1481 parser.simple_key_allowed = true
1484 break // We have found a token.
1491 // Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1494 // %YAML 1.1 # a comment \n
1495 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1496 // %TAG !yaml! tag:yaml.org,2002: \n
1497 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1499 func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool {
1501 start_mark := parser.mark
1504 // Scan the directive name.
1506 if !yaml_parser_scan_directive_name(parser, start_mark, &name) {
1510 // Is it a YAML directive?
1511 if bytes.Equal(name, []byte("YAML")) {
1512 // Scan the VERSION directive value.
1513 var major, minor int8
1514 if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) {
1517 end_mark := parser.mark
1519 // Create a VERSION-DIRECTIVE token.
1520 *token = yaml_token_t{
1521 typ: yaml_VERSION_DIRECTIVE_TOKEN,
1522 start_mark: start_mark,
1528 // Is it a TAG directive?
1529 } else if bytes.Equal(name, []byte("TAG")) {
1530 // Scan the TAG directive value.
1531 var handle, prefix []byte
1532 if !yaml_parser_scan_tag_directive_value(parser, start_mark, &handle, &prefix) {
1535 end_mark := parser.mark
1537 // Create a TAG-DIRECTIVE token.
1538 *token = yaml_token_t{
1539 typ: yaml_TAG_DIRECTIVE_TOKEN,
1540 start_mark: start_mark,
1546 // Unknown directive.
1548 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1549 start_mark, "found unknown directive name")
1553 // Eat the rest of the line including any comments.
1554 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1558 for is_blank(parser.buffer, parser.buffer_pos) {
1560 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1565 if parser.buffer[parser.buffer_pos] == '#' {
1566 for !is_breakz(parser.buffer, parser.buffer_pos) {
1568 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1574 // Check if we are at the end of the line.
1575 if !is_breakz(parser.buffer, parser.buffer_pos) {
1576 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1577 start_mark, "did not find expected comment or line break")
1581 // Eat a line break.
1582 if is_break(parser.buffer, parser.buffer_pos) {
1583 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
1592 // Scan the directive name.
1595 // %YAML 1.1 # a comment \n
1597 // %TAG !yaml! tag:yaml.org,2002: \n
1600 func yaml_parser_scan_directive_name(parser *yaml_parser_t, start_mark yaml_mark_t, name *[]byte) bool {
1601 // Consume the directive name.
1602 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1607 for is_alpha(parser.buffer, parser.buffer_pos) {
1609 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1614 // Check if the name is empty.
1616 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1617 start_mark, "could not find expected directive name")
1621 // Check for an blank character after the name.
1622 if !is_blankz(parser.buffer, parser.buffer_pos) {
1623 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1624 start_mark, "found unexpected non-alphabetical character")
1631 // Scan the value of VERSION-DIRECTIVE.
1634 // %YAML 1.1 # a comment \n
1636 func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool {
1638 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1641 for is_blank(parser.buffer, parser.buffer_pos) {
1643 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1648 // Consume the major version number.
1649 if !yaml_parser_scan_version_directive_number(parser, start_mark, major) {
1654 if parser.buffer[parser.buffer_pos] != '.' {
1655 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
1656 start_mark, "did not find expected digit or '.' character")
1661 // Consume the minor version number.
1662 if !yaml_parser_scan_version_directive_number(parser, start_mark, minor) {
1668 const max_number_length = 2
1670 // Scan the version number of VERSION-DIRECTIVE.
1673 // %YAML 1.1 # a comment \n
1675 // %YAML 1.1 # a comment \n
1677 func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool {
1679 // Repeat while the next character is digit.
1680 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1683 var value, length int8
1684 for is_digit(parser.buffer, parser.buffer_pos) {
1685 // Check if the number is too long.
1687 if length > max_number_length {
1688 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
1689 start_mark, "found extremely long version number")
1691 value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos))
1693 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1698 // Check if the number was present.
1700 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
1701 start_mark, "did not find expected version number")
1707 // Scan the value of a TAG-DIRECTIVE token.
1710 // %TAG !yaml! tag:yaml.org,2002: \n
1711 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1713 func yaml_parser_scan_tag_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, handle, prefix *[]byte) bool {
1714 var handle_value, prefix_value []byte
1717 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1721 for is_blank(parser.buffer, parser.buffer_pos) {
1723 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1729 if !yaml_parser_scan_tag_handle(parser, true, start_mark, &handle_value) {
1733 // Expect a whitespace.
1734 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1737 if !is_blank(parser.buffer, parser.buffer_pos) {
1738 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
1739 start_mark, "did not find expected whitespace")
1744 for is_blank(parser.buffer, parser.buffer_pos) {
1746 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1752 if !yaml_parser_scan_tag_uri(parser, true, nil, start_mark, &prefix_value) {
1756 // Expect a whitespace or line break.
1757 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1760 if !is_blankz(parser.buffer, parser.buffer_pos) {
1761 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
1762 start_mark, "did not find expected whitespace or line break")
1766 *handle = handle_value
1767 *prefix = prefix_value
1771 func yaml_parser_scan_anchor(parser *yaml_parser_t, token *yaml_token_t, typ yaml_token_type_t) bool {
1774 // Eat the indicator character.
1775 start_mark := parser.mark
1778 // Consume the value.
1779 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1783 for is_alpha(parser.buffer, parser.buffer_pos) {
1785 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1790 end_mark := parser.mark
1793 * Check if length of the anchor is greater than 0 and it is followed by
1794 * a whitespace character or one of the indicators:
1796 * '?', ':', ',', ']', '}', '%', '@', '`'.
1800 !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '?' ||
1801 parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == ',' ||
1802 parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '}' ||
1803 parser.buffer[parser.buffer_pos] == '%' || parser.buffer[parser.buffer_pos] == '@' ||
1804 parser.buffer[parser.buffer_pos] == '`') {
1805 context := "while scanning an alias"
1806 if typ == yaml_ANCHOR_TOKEN {
1807 context = "while scanning an anchor"
1809 yaml_parser_set_scanner_error(parser, context, start_mark,
1810 "did not find expected alphabetic or numeric character")
1815 *token = yaml_token_t{
1817 start_mark: start_mark,
1829 func yaml_parser_scan_tag(parser *yaml_parser_t, token *yaml_token_t) bool {
1830 var handle, suffix []byte
1832 start_mark := parser.mark
1834 // Check if the tag is in the canonical form.
1835 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
1839 if parser.buffer[parser.buffer_pos+1] == '<' {
1840 // Keep the handle as ''
1846 // Consume the tag value.
1847 if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
1851 // Check for '>' and eat it.
1852 if parser.buffer[parser.buffer_pos] != '>' {
1853 yaml_parser_set_scanner_error(parser, "while scanning a tag",
1854 start_mark, "did not find the expected '>'")
1860 // The tag has either the '!suffix' or the '!handle!suffix' form.
1862 // First, try to scan a handle.
1863 if !yaml_parser_scan_tag_handle(parser, false, start_mark, &handle) {
1867 // Check if it is, indeed, handle.
1868 if handle[0] == '!' && len(handle) > 1 && handle[len(handle)-1] == '!' {
1869 // Scan the suffix now.
1870 if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
1874 // It wasn't a handle after all. Scan the rest of the tag.
1875 if !yaml_parser_scan_tag_uri(parser, false, handle, start_mark, &suffix) {
1879 // Set the handle to '!'.
1880 handle = []byte{'!'}
1882 // A special case: the '!' tag. Set the handle to '' and the
1884 if len(suffix) == 0 {
1885 handle, suffix = suffix, handle
1890 // Check the character which ends the tag.
1891 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1894 if !is_blankz(parser.buffer, parser.buffer_pos) {
1895 yaml_parser_set_scanner_error(parser, "while scanning a tag",
1896 start_mark, "did not find expected whitespace or line break")
1900 end_mark := parser.mark
1903 *token = yaml_token_t{
1904 typ: yaml_TAG_TOKEN,
1905 start_mark: start_mark,
1913 // Scan a tag handle.
1914 func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, handle *[]byte) bool {
1915 // Check the initial '!' character.
1916 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1919 if parser.buffer[parser.buffer_pos] != '!' {
1920 yaml_parser_set_scanner_tag_error(parser, directive,
1921 start_mark, "did not find expected '!'")
1927 // Copy the '!' character.
1930 // Copy all subsequent alphabetical and numerical characters.
1931 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1934 for is_alpha(parser.buffer, parser.buffer_pos) {
1936 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1941 // Check if the trailing character is '!' and copy it.
1942 if parser.buffer[parser.buffer_pos] == '!' {
1945 // It's either the '!' tag or not really a tag handle. If it's a %TAG
1946 // directive, it's an error. If it's a tag token, it must be a part of URI.
1947 if directive && !(s[0] == '!' && s[1] == 0) {
1948 yaml_parser_set_scanner_tag_error(parser, directive,
1949 start_mark, "did not find expected '!'")
1959 func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool {
1960 //size_t length = head ? strlen((char *)head) : 0
1963 // Copy the head if needed.
1965 // Note that we don't copy the leading '!' character.
1967 s = append(s, head[1:]...)
1971 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1975 // The set of characters that may appear in URI is as follows:
1977 // '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
1978 // '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
1980 // [Go] Convert this into more reasonable logic.
1981 for is_alpha(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == ';' ||
1982 parser.buffer[parser.buffer_pos] == '/' || parser.buffer[parser.buffer_pos] == '?' ||
1983 parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == '@' ||
1984 parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '=' ||
1985 parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '$' ||
1986 parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '.' ||
1987 parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '~' ||
1988 parser.buffer[parser.buffer_pos] == '*' || parser.buffer[parser.buffer_pos] == '\'' ||
1989 parser.buffer[parser.buffer_pos] == '(' || parser.buffer[parser.buffer_pos] == ')' ||
1990 parser.buffer[parser.buffer_pos] == '[' || parser.buffer[parser.buffer_pos] == ']' ||
1991 parser.buffer[parser.buffer_pos] == '%' {
1992 // Check if it is a URI-escape sequence.
1993 if parser.buffer[parser.buffer_pos] == '%' {
1994 if !yaml_parser_scan_uri_escapes(parser, directive, start_mark, &s) {
2000 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2005 // Check if the tag is non-empty.
2007 yaml_parser_set_scanner_tag_error(parser, directive,
2008 start_mark, "did not find expected tag URI")
2015 // Decode an URI-escape sequence corresponding to a single UTF-8 character.
2016 func yaml_parser_scan_uri_escapes(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, s *[]byte) bool {
2018 // Decode the required number of characters.
2021 // Check for a URI-escaped octet.
2022 if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
2026 if !(parser.buffer[parser.buffer_pos] == '%' &&
2027 is_hex(parser.buffer, parser.buffer_pos+1) &&
2028 is_hex(parser.buffer, parser.buffer_pos+2)) {
2029 return yaml_parser_set_scanner_tag_error(parser, directive,
2030 start_mark, "did not find URI escaped octet")
2034 octet := byte((as_hex(parser.buffer, parser.buffer_pos+1) << 4) + as_hex(parser.buffer, parser.buffer_pos+2))
2036 // If it is the leading octet, determine the length of the UTF-8 sequence.
2040 return yaml_parser_set_scanner_tag_error(parser, directive,
2041 start_mark, "found an incorrect leading UTF-8 octet")
2044 // Check if the trailing octet is correct.
2045 if octet&0xC0 != 0x80 {
2046 return yaml_parser_set_scanner_tag_error(parser, directive,
2047 start_mark, "found an incorrect trailing UTF-8 octet")
2051 // Copy the octet and move the pointers.
2052 *s = append(*s, octet)
2061 // Scan a block scalar.
2062 func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, literal bool) bool {
2063 // Eat the indicator '|' or '>'.
2064 start_mark := parser.mark
2067 // Scan the additional block scalar indicators.
2068 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2072 // Check for a chomping indicator.
2073 var chomping, increment int
2074 if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
2075 // Set the chomping method and eat the indicator.
2076 if parser.buffer[parser.buffer_pos] == '+' {
2083 // Check for an indentation indicator.
2084 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2087 if is_digit(parser.buffer, parser.buffer_pos) {
2088 // Check that the indentation is greater than 0.
2089 if parser.buffer[parser.buffer_pos] == '0' {
2090 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2091 start_mark, "found an indentation indicator equal to 0")
2095 // Get the indentation level and eat the indicator.
2096 increment = as_digit(parser.buffer, parser.buffer_pos)
2100 } else if is_digit(parser.buffer, parser.buffer_pos) {
2101 // Do the same as above, but in the opposite order.
2103 if parser.buffer[parser.buffer_pos] == '0' {
2104 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2105 start_mark, "found an indentation indicator equal to 0")
2108 increment = as_digit(parser.buffer, parser.buffer_pos)
2111 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2114 if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
2115 if parser.buffer[parser.buffer_pos] == '+' {
2124 // Eat whitespaces and comments to the end of the line.
2125 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2128 for is_blank(parser.buffer, parser.buffer_pos) {
2130 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2134 if parser.buffer[parser.buffer_pos] == '#' {
2135 for !is_breakz(parser.buffer, parser.buffer_pos) {
2137 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2143 // Check if we are at the end of the line.
2144 if !is_breakz(parser.buffer, parser.buffer_pos) {
2145 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2146 start_mark, "did not find expected comment or line break")
2150 // Eat a line break.
2151 if is_break(parser.buffer, parser.buffer_pos) {
2152 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2158 end_mark := parser.mark
2160 // Set the indentation level if it was specified.
2163 if parser.indent >= 0 {
2164 indent = parser.indent + increment
2170 // Scan the leading line breaks and determine the indentation level if needed.
2171 var s, leading_break, trailing_breaks []byte
2172 if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
2176 // Scan the block scalar content.
2177 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2180 var leading_blank, trailing_blank bool
2181 for parser.mark.column == indent && !is_z(parser.buffer, parser.buffer_pos) {
2182 // We are at the beginning of a non-empty line.
2184 // Is it a trailing whitespace?
2185 trailing_blank = is_blank(parser.buffer, parser.buffer_pos)
2187 // Check if we need to fold the leading line break.
2188 if !literal && !leading_blank && !trailing_blank && len(leading_break) > 0 && leading_break[0] == '\n' {
2189 // Do we need to join the lines by space?
2190 if len(trailing_breaks) == 0 {
2194 s = append(s, leading_break...)
2196 leading_break = leading_break[:0]
2198 // Append the remaining line breaks.
2199 s = append(s, trailing_breaks...)
2200 trailing_breaks = trailing_breaks[:0]
2202 // Is it a leading whitespace?
2203 leading_blank = is_blank(parser.buffer, parser.buffer_pos)
2205 // Consume the current line.
2206 for !is_breakz(parser.buffer, parser.buffer_pos) {
2208 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2213 // Consume the line break.
2214 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2218 leading_break = read_line(parser, leading_break)
2220 // Eat the following indentation spaces and line breaks.
2221 if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
2228 s = append(s, leading_break...)
2231 s = append(s, trailing_breaks...)
2235 *token = yaml_token_t{
2236 typ: yaml_SCALAR_TOKEN,
2237 start_mark: start_mark,
2240 style: yaml_LITERAL_SCALAR_STYLE,
2243 token.style = yaml_FOLDED_SCALAR_STYLE
2248 // Scan indentation spaces and line breaks for a block scalar. Determine the
2249 // indentation level if needed.
2250 func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, breaks *[]byte, start_mark yaml_mark_t, end_mark *yaml_mark_t) bool {
2251 *end_mark = parser.mark
2253 // Eat the indentation spaces and line breaks.
2256 // Eat the indentation spaces.
2257 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2260 for (*indent == 0 || parser.mark.column < *indent) && is_space(parser.buffer, parser.buffer_pos) {
2262 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2266 if parser.mark.column > max_indent {
2267 max_indent = parser.mark.column
2270 // Check for a tab character messing the indentation.
2271 if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) {
2272 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2273 start_mark, "found a tab character where an indentation space is expected")
2276 // Have we found a non-empty line?
2277 if !is_break(parser.buffer, parser.buffer_pos) {
2281 // Consume the line break.
2282 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2285 // [Go] Should really be returning breaks instead.
2286 *breaks = read_line(parser, *breaks)
2287 *end_mark = parser.mark
2290 // Determine the indentation level if needed.
2292 *indent = max_indent
2293 if *indent < parser.indent+1 {
2294 *indent = parser.indent + 1
2303 // Scan a quoted scalar.
2304 func yaml_parser_scan_flow_scalar(parser *yaml_parser_t, token *yaml_token_t, single bool) bool {
2305 // Eat the left quote.
2306 start_mark := parser.mark
2309 // Consume the content of the quoted scalar.
2310 var s, leading_break, trailing_breaks, whitespaces []byte
2312 // Check that there are no document indicators at the beginning of the line.
2313 if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2317 if parser.mark.column == 0 &&
2318 ((parser.buffer[parser.buffer_pos+0] == '-' &&
2319 parser.buffer[parser.buffer_pos+1] == '-' &&
2320 parser.buffer[parser.buffer_pos+2] == '-') ||
2321 (parser.buffer[parser.buffer_pos+0] == '.' &&
2322 parser.buffer[parser.buffer_pos+1] == '.' &&
2323 parser.buffer[parser.buffer_pos+2] == '.')) &&
2324 is_blankz(parser.buffer, parser.buffer_pos+3) {
2325 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
2326 start_mark, "found unexpected document indicator")
2331 if is_z(parser.buffer, parser.buffer_pos) {
2332 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
2333 start_mark, "found unexpected end of stream")
2337 // Consume non-blank characters.
2338 leading_blanks := false
2339 for !is_blankz(parser.buffer, parser.buffer_pos) {
2340 if single && parser.buffer[parser.buffer_pos] == '\'' && parser.buffer[parser.buffer_pos+1] == '\'' {
2341 // Is is an escaped single quote.
2346 } else if single && parser.buffer[parser.buffer_pos] == '\'' {
2347 // It is a right single quote.
2349 } else if !single && parser.buffer[parser.buffer_pos] == '"' {
2350 // It is a right double quote.
2353 } else if !single && parser.buffer[parser.buffer_pos] == '\\' && is_break(parser.buffer, parser.buffer_pos+1) {
2354 // It is an escaped line break.
2355 if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
2360 leading_blanks = true
2363 } else if !single && parser.buffer[parser.buffer_pos] == '\\' {
2364 // It is an escape sequence.
2367 // Check the escape character.
2368 switch parser.buffer[parser.buffer_pos+1] {
2372 s = append(s, '\x07')
2374 s = append(s, '\x08')
2376 s = append(s, '\x09')
2378 s = append(s, '\x0A')
2380 s = append(s, '\x0B')
2382 s = append(s, '\x0C')
2384 s = append(s, '\x0D')
2386 s = append(s, '\x1B')
2388 s = append(s, '\x20')
2395 case 'N': // NEL (#x85)
2396 s = append(s, '\xC2')
2397 s = append(s, '\x85')
2399 s = append(s, '\xC2')
2400 s = append(s, '\xA0')
2401 case 'L': // LS (#x2028)
2402 s = append(s, '\xE2')
2403 s = append(s, '\x80')
2404 s = append(s, '\xA8')
2405 case 'P': // PS (#x2029)
2406 s = append(s, '\xE2')
2407 s = append(s, '\x80')
2408 s = append(s, '\xA9')
2416 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2417 start_mark, "found unknown escape character")
2424 // Consume an arbitrary escape code.
2425 if code_length > 0 {
2428 // Scan the character value.
2429 if parser.unread < code_length && !yaml_parser_update_buffer(parser, code_length) {
2432 for k := 0; k < code_length; k++ {
2433 if !is_hex(parser.buffer, parser.buffer_pos+k) {
2434 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2435 start_mark, "did not find expected hexdecimal number")
2438 value = (value << 4) + as_hex(parser.buffer, parser.buffer_pos+k)
2441 // Check the value and write the character.
2442 if (value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF {
2443 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2444 start_mark, "found invalid Unicode character escape code")
2448 s = append(s, byte(value))
2449 } else if value <= 0x7FF {
2450 s = append(s, byte(0xC0+(value>>6)))
2451 s = append(s, byte(0x80+(value&0x3F)))
2452 } else if value <= 0xFFFF {
2453 s = append(s, byte(0xE0+(value>>12)))
2454 s = append(s, byte(0x80+((value>>6)&0x3F)))
2455 s = append(s, byte(0x80+(value&0x3F)))
2457 s = append(s, byte(0xF0+(value>>18)))
2458 s = append(s, byte(0x80+((value>>12)&0x3F)))
2459 s = append(s, byte(0x80+((value>>6)&0x3F)))
2460 s = append(s, byte(0x80+(value&0x3F)))
2463 // Advance the pointer.
2464 for k := 0; k < code_length; k++ {
2469 // It is a non-escaped non-blank character.
2472 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2477 // Check if we are at the end of the scalar.
2479 if parser.buffer[parser.buffer_pos] == '\'' {
2483 if parser.buffer[parser.buffer_pos] == '"' {
2488 // Consume blank characters.
2489 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2493 for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
2494 if is_blank(parser.buffer, parser.buffer_pos) {
2495 // Consume a space or a tab character.
2496 if !leading_blanks {
2497 whitespaces = read(parser, whitespaces)
2502 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2506 // Check if it is a first line break.
2507 if !leading_blanks {
2508 whitespaces = whitespaces[:0]
2509 leading_break = read_line(parser, leading_break)
2510 leading_blanks = true
2512 trailing_breaks = read_line(parser, trailing_breaks)
2515 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2520 // Join the whitespaces or fold line breaks.
2522 // Do we need to fold line breaks?
2523 if len(leading_break) > 0 && leading_break[0] == '\n' {
2524 if len(trailing_breaks) == 0 {
2527 s = append(s, trailing_breaks...)
2530 s = append(s, leading_break...)
2531 s = append(s, trailing_breaks...)
2533 trailing_breaks = trailing_breaks[:0]
2534 leading_break = leading_break[:0]
2536 s = append(s, whitespaces...)
2537 whitespaces = whitespaces[:0]
2541 // Eat the right quote.
2543 end_mark := parser.mark
2546 *token = yaml_token_t{
2547 typ: yaml_SCALAR_TOKEN,
2548 start_mark: start_mark,
2551 style: yaml_SINGLE_QUOTED_SCALAR_STYLE,
2554 token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2559 // Scan a plain scalar.
2560 func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) bool {
2562 var s, leading_break, trailing_breaks, whitespaces []byte
2563 var leading_blanks bool
2564 var indent = parser.indent + 1
2566 start_mark := parser.mark
2567 end_mark := parser.mark
2569 // Consume the content of the plain scalar.
2571 // Check for a document indicator.
2572 if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2575 if parser.mark.column == 0 &&
2576 ((parser.buffer[parser.buffer_pos+0] == '-' &&
2577 parser.buffer[parser.buffer_pos+1] == '-' &&
2578 parser.buffer[parser.buffer_pos+2] == '-') ||
2579 (parser.buffer[parser.buffer_pos+0] == '.' &&
2580 parser.buffer[parser.buffer_pos+1] == '.' &&
2581 parser.buffer[parser.buffer_pos+2] == '.')) &&
2582 is_blankz(parser.buffer, parser.buffer_pos+3) {
2586 // Check for a comment.
2587 if parser.buffer[parser.buffer_pos] == '#' {
2591 // Consume non-blank characters.
2592 for !is_blankz(parser.buffer, parser.buffer_pos) {
2594 // Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13".
2595 if parser.flow_level > 0 &&
2596 parser.buffer[parser.buffer_pos] == ':' &&
2597 !is_blankz(parser.buffer, parser.buffer_pos+1) {
2598 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
2599 start_mark, "found unexpected ':'")
2603 // Check for indicators that may end a plain scalar.
2604 if (parser.buffer[parser.buffer_pos] == ':' && is_blankz(parser.buffer, parser.buffer_pos+1)) ||
2605 (parser.flow_level > 0 &&
2606 (parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == ':' ||
2607 parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == '[' ||
2608 parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
2609 parser.buffer[parser.buffer_pos] == '}')) {
2613 // Check if we need to join whitespaces and breaks.
2614 if leading_blanks || len(whitespaces) > 0 {
2616 // Do we need to fold line breaks?
2617 if leading_break[0] == '\n' {
2618 if len(trailing_breaks) == 0 {
2621 s = append(s, trailing_breaks...)
2624 s = append(s, leading_break...)
2625 s = append(s, trailing_breaks...)
2627 trailing_breaks = trailing_breaks[:0]
2628 leading_break = leading_break[:0]
2629 leading_blanks = false
2631 s = append(s, whitespaces...)
2632 whitespaces = whitespaces[:0]
2636 // Copy the character.
2639 end_mark = parser.mark
2640 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2646 if !(is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos)) {
2650 // Consume blank characters.
2651 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2655 for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
2656 if is_blank(parser.buffer, parser.buffer_pos) {
2658 // Check for tab character that abuse indentation.
2659 if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) {
2660 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
2661 start_mark, "found a tab character that violate indentation")
2665 // Consume a space or a tab character.
2666 if !leading_blanks {
2667 whitespaces = read(parser, whitespaces)
2672 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2676 // Check if it is a first line break.
2677 if !leading_blanks {
2678 whitespaces = whitespaces[:0]
2679 leading_break = read_line(parser, leading_break)
2680 leading_blanks = true
2682 trailing_breaks = read_line(parser, trailing_breaks)
2685 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2690 // Check indentation level.
2691 if parser.flow_level == 0 && parser.mark.column < indent {
2697 *token = yaml_token_t{
2698 typ: yaml_SCALAR_TOKEN,
2699 start_mark: start_mark,
2702 style: yaml_PLAIN_SCALAR_STYLE,
2705 // Note that we change the 'simple_key_allowed' flag.
2707 parser.simple_key_allowed = true