blob: 570b8ecd10fd5b827f79ad1a14b62ddf8a13f821 [file] [log] [blame]
Scott Baker1fe72732019-10-21 10:58:51 -07001package yaml
2
3import (
4 "bytes"
5 "fmt"
6)
7
8// Introduction
9// ************
10//
11// The following notes assume that you are familiar with the YAML specification
12// (http://yaml.org/spec/1.2/spec.html). We mostly follow it, although in
13// some cases we are less restrictive that it requires.
14//
15// The process of transforming a YAML stream into a sequence of events is
16// divided on two steps: Scanning and Parsing.
17//
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.
21//
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).
25//
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.
29//
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.
32//
33// Now, tokens:
34//
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 # '}'
48// BLOCK-ENTRY # '-'
49// FLOW-ENTRY # ','
50// KEY # '?' or nothing (simple keys).
51// VALUE # ':'
52// ALIAS(anchor) # '*anchor'
53// ANCHOR(anchor) # '&anchor'
54// TAG(handle,suffix) # '!handle!suffix'
55// SCALAR(value,style) # A scalar.
56//
57// The following two tokens are "virtual" tokens denoting the beginning and the
58// end of the stream:
59//
60// STREAM-START(encoding)
61// STREAM-END
62//
63// We pass the information about the input stream encoding with the
64// STREAM-START token.
65//
66// The next two tokens are responsible for tags:
67//
68// VERSION-DIRECTIVE(major,minor)
69// TAG-DIRECTIVE(handle,prefix)
70//
71// Example:
72//
73// %YAML 1.1
74// %TAG ! !foo
75// %TAG !yaml! tag:yaml.org,2002:
76// ---
77//
78// The correspoding sequence of tokens:
79//
80// STREAM-START(utf-8)
81// VERSION-DIRECTIVE(1,1)
82// TAG-DIRECTIVE("!","!foo")
83// TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
84// DOCUMENT-START
85// STREAM-END
86//
87// Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
88// line.
89//
90// The document start and end indicators are represented by:
91//
92// DOCUMENT-START
93// DOCUMENT-END
94//
95// Note that if a YAML stream contains an implicit document (without '---'
96// and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
97// produced.
98//
99// In the following examples, we present whole documents together with the
100// produced tokens.
101//
102// 1. An implicit document:
103//
104// 'a scalar'
105//
106// Tokens:
107//
108// STREAM-START(utf-8)
109// SCALAR("a scalar",single-quoted)
110// STREAM-END
111//
112// 2. An explicit document:
113//
114// ---
115// 'a scalar'
116// ...
117//
118// Tokens:
119//
120// STREAM-START(utf-8)
121// DOCUMENT-START
122// SCALAR("a scalar",single-quoted)
123// DOCUMENT-END
124// STREAM-END
125//
126// 3. Several documents in a stream:
127//
128// 'a scalar'
129// ---
130// 'another scalar'
131// ---
132// 'yet another scalar'
133//
134// Tokens:
135//
136// STREAM-START(utf-8)
137// SCALAR("a scalar",single-quoted)
138// DOCUMENT-START
139// SCALAR("another scalar",single-quoted)
140// DOCUMENT-START
141// SCALAR("yet another scalar",single-quoted)
142// STREAM-END
143//
144// We have already introduced the SCALAR token above. The following tokens are
145// used to describe aliases, anchors, tag, and scalars:
146//
147// ALIAS(anchor)
148// ANCHOR(anchor)
149// TAG(handle,suffix)
150// SCALAR(value,style)
151//
152// The following series of examples illustrate the usage of these tokens:
153//
154// 1. A recursive sequence:
155//
156// &A [ *A ]
157//
158// Tokens:
159//
160// STREAM-START(utf-8)
161// ANCHOR("A")
162// FLOW-SEQUENCE-START
163// ALIAS("A")
164// FLOW-SEQUENCE-END
165// STREAM-END
166//
167// 2. A tagged scalar:
168//
169// !!float "3.14" # A good approximation.
170//
171// Tokens:
172//
173// STREAM-START(utf-8)
174// TAG("!!","float")
175// SCALAR("3.14",double-quoted)
176// STREAM-END
177//
178// 3. Various scalar styles:
179//
180// --- # Implicit empty plain scalars do not produce tokens.
181// --- a plain scalar
182// --- 'a single-quoted scalar'
183// --- "a double-quoted scalar"
184// --- |-
185// a literal scalar
186// --- >-
187// a folded
188// scalar
189//
190// Tokens:
191//
192// STREAM-START(utf-8)
193// DOCUMENT-START
194// DOCUMENT-START
195// SCALAR("a plain scalar",plain)
196// DOCUMENT-START
197// SCALAR("a single-quoted scalar",single-quoted)
198// DOCUMENT-START
199// SCALAR("a double-quoted scalar",double-quoted)
200// DOCUMENT-START
201// SCALAR("a literal scalar",literal)
202// DOCUMENT-START
203// SCALAR("a folded scalar",folded)
204// STREAM-END
205//
206// Now it's time to review collection-related tokens. We will start with
207// flow collections:
208//
209// FLOW-SEQUENCE-START
210// FLOW-SEQUENCE-END
211// FLOW-MAPPING-START
212// FLOW-MAPPING-END
213// FLOW-ENTRY
214// KEY
215// VALUE
216//
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.
222//
223// The following examples show flow collections:
224//
225// 1. A flow sequence:
226//
227// [item 1, item 2, item 3]
228//
229// Tokens:
230//
231// STREAM-START(utf-8)
232// FLOW-SEQUENCE-START
233// SCALAR("item 1",plain)
234// FLOW-ENTRY
235// SCALAR("item 2",plain)
236// FLOW-ENTRY
237// SCALAR("item 3",plain)
238// FLOW-SEQUENCE-END
239// STREAM-END
240//
241// 2. A flow mapping:
242//
243// {
244// a simple key: a value, # Note that the KEY token is produced.
245// ? a complex key: another value,
246// }
247//
248// Tokens:
249//
250// STREAM-START(utf-8)
251// FLOW-MAPPING-START
252// KEY
253// SCALAR("a simple key",plain)
254// VALUE
255// SCALAR("a value",plain)
256// FLOW-ENTRY
257// KEY
258// SCALAR("a complex key",plain)
259// VALUE
260// SCALAR("another value",plain)
261// FLOW-ENTRY
262// FLOW-MAPPING-END
263// STREAM-END
264//
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.
267//
268// For scanning block collections, the following tokens are used (note that we
269// repeat KEY and VALUE here):
270//
271// BLOCK-SEQUENCE-START
272// BLOCK-MAPPING-START
273// BLOCK-END
274// BLOCK-ENTRY
275// KEY
276// VALUE
277//
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.
283//
284// The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
285// '-', '?', and ':' correspondingly.
286//
287// The following examples show how the tokens BLOCK-SEQUENCE-START,
288// BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
289//
290// 1. Block sequences:
291//
292// - item 1
293// - item 2
294// -
295// - item 3.1
296// - item 3.2
297// -
298// key 1: value 1
299// key 2: value 2
300//
301// Tokens:
302//
303// STREAM-START(utf-8)
304// BLOCK-SEQUENCE-START
305// BLOCK-ENTRY
306// SCALAR("item 1",plain)
307// BLOCK-ENTRY
308// SCALAR("item 2",plain)
309// BLOCK-ENTRY
310// BLOCK-SEQUENCE-START
311// BLOCK-ENTRY
312// SCALAR("item 3.1",plain)
313// BLOCK-ENTRY
314// SCALAR("item 3.2",plain)
315// BLOCK-END
316// BLOCK-ENTRY
317// BLOCK-MAPPING-START
318// KEY
319// SCALAR("key 1",plain)
320// VALUE
321// SCALAR("value 1",plain)
322// KEY
323// SCALAR("key 2",plain)
324// VALUE
325// SCALAR("value 2",plain)
326// BLOCK-END
327// BLOCK-END
328// STREAM-END
329//
330// 2. Block mappings:
331//
332// a simple key: a value # The KEY token is produced here.
333// ? a complex key
334// : another value
335// a mapping:
336// key 1: value 1
337// key 2: value 2
338// a sequence:
339// - item 1
340// - item 2
341//
342// Tokens:
343//
344// STREAM-START(utf-8)
345// BLOCK-MAPPING-START
346// KEY
347// SCALAR("a simple key",plain)
348// VALUE
349// SCALAR("a value",plain)
350// KEY
351// SCALAR("a complex key",plain)
352// VALUE
353// SCALAR("another value",plain)
354// KEY
355// SCALAR("a mapping",plain)
356// BLOCK-MAPPING-START
357// KEY
358// SCALAR("key 1",plain)
359// VALUE
360// SCALAR("value 1",plain)
361// KEY
362// SCALAR("key 2",plain)
363// VALUE
364// SCALAR("value 2",plain)
365// BLOCK-END
366// KEY
367// SCALAR("a sequence",plain)
368// VALUE
369// BLOCK-SEQUENCE-START
370// BLOCK-ENTRY
371// SCALAR("item 1",plain)
372// BLOCK-ENTRY
373// SCALAR("item 2",plain)
374// BLOCK-END
375// BLOCK-END
376// STREAM-END
377//
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:
382//
383// 1. Collections in a sequence:
384//
385// - - item 1
386// - item 2
387// - key 1: value 1
388// key 2: value 2
389// - ? complex key
390// : complex value
391//
392// Tokens:
393//
394// STREAM-START(utf-8)
395// BLOCK-SEQUENCE-START
396// BLOCK-ENTRY
397// BLOCK-SEQUENCE-START
398// BLOCK-ENTRY
399// SCALAR("item 1",plain)
400// BLOCK-ENTRY
401// SCALAR("item 2",plain)
402// BLOCK-END
403// BLOCK-ENTRY
404// BLOCK-MAPPING-START
405// KEY
406// SCALAR("key 1",plain)
407// VALUE
408// SCALAR("value 1",plain)
409// KEY
410// SCALAR("key 2",plain)
411// VALUE
412// SCALAR("value 2",plain)
413// BLOCK-END
414// BLOCK-ENTRY
415// BLOCK-MAPPING-START
416// KEY
417// SCALAR("complex key")
418// VALUE
419// SCALAR("complex value")
420// BLOCK-END
421// BLOCK-END
422// STREAM-END
423//
424// 2. Collections in a mapping:
425//
426// ? a sequence
427// : - item 1
428// - item 2
429// ? a mapping
430// : key 1: value 1
431// key 2: value 2
432//
433// Tokens:
434//
435// STREAM-START(utf-8)
436// BLOCK-MAPPING-START
437// KEY
438// SCALAR("a sequence",plain)
439// VALUE
440// BLOCK-SEQUENCE-START
441// BLOCK-ENTRY
442// SCALAR("item 1",plain)
443// BLOCK-ENTRY
444// SCALAR("item 2",plain)
445// BLOCK-END
446// KEY
447// SCALAR("a mapping",plain)
448// VALUE
449// BLOCK-MAPPING-START
450// KEY
451// SCALAR("key 1",plain)
452// VALUE
453// SCALAR("value 1",plain)
454// KEY
455// SCALAR("key 2",plain)
456// VALUE
457// SCALAR("value 2",plain)
458// BLOCK-END
459// BLOCK-END
460// STREAM-END
461//
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:
464//
465// key:
466// - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
467// - item 2
468//
469// Tokens:
470//
471// STREAM-START(utf-8)
472// BLOCK-MAPPING-START
473// KEY
474// SCALAR("key",plain)
475// VALUE
476// BLOCK-ENTRY
477// SCALAR("item 1",plain)
478// BLOCK-ENTRY
479// SCALAR("item 2",plain)
480// BLOCK-END
481//
482
483// Ensure that the buffer contains the required number of characters.
484// Return true on success, false on failure (reader error or memory error).
485func 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)
488}
489
490// Advance the buffer pointer.
491func skip(parser *yaml_parser_t) {
492 parser.mark.index++
493 parser.mark.column++
494 parser.unread--
495 parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
496}
497
498func 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
502 parser.mark.line++
503 parser.unread -= 2
504 parser.buffer_pos += 2
505 } else if is_break(parser.buffer, parser.buffer_pos) {
506 parser.mark.index++
507 parser.mark.column = 0
508 parser.mark.line++
509 parser.unread--
510 parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
511 }
512}
513
514// Copy a character to a string buffer and advance pointers.
515func read(parser *yaml_parser_t, s []byte) []byte {
516 w := width(parser.buffer[parser.buffer_pos])
517 if w == 0 {
518 panic("invalid character sequence")
519 }
520 if len(s) == 0 {
521 s = make([]byte, 0, 32)
522 }
523 if w == 1 && len(s)+w <= cap(s) {
524 s = s[:len(s)+1]
525 s[len(s)-1] = parser.buffer[parser.buffer_pos]
526 parser.buffer_pos++
527 } else {
528 s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...)
529 parser.buffer_pos += w
530 }
531 parser.mark.index++
532 parser.mark.column++
533 parser.unread--
534 return s
535}
536
537// Copy a line break character to a string buffer and advance pointers.
538func read_line(parser *yaml_parser_t, s []byte) []byte {
539 buf := parser.buffer
540 pos := parser.buffer_pos
541 switch {
542 case buf[pos] == '\r' && buf[pos+1] == '\n':
543 // CR LF . LF
544 s = append(s, '\n')
545 parser.buffer_pos += 2
546 parser.mark.index++
547 parser.unread--
548 case buf[pos] == '\r' || buf[pos] == '\n':
549 // CR|LF . LF
550 s = append(s, '\n')
551 parser.buffer_pos += 1
552 case buf[pos] == '\xC2' && buf[pos+1] == '\x85':
553 // NEL . LF
554 s = append(s, '\n')
555 parser.buffer_pos += 2
556 case buf[pos] == '\xE2' && buf[pos+1] == '\x80' && (buf[pos+2] == '\xA8' || buf[pos+2] == '\xA9'):
557 // LS|PS . LS|PS
558 s = append(s, buf[parser.buffer_pos:pos+3]...)
559 parser.buffer_pos += 3
560 default:
561 return s
562 }
563 parser.mark.index++
564 parser.mark.column = 0
565 parser.mark.line++
566 parser.unread--
567 return s
568}
569
570// Get the next token.
571func 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?
574
575 // No tokens after STREAM-END or error.
576 if parser.stream_end_produced || parser.error != yaml_NO_ERROR {
577 return true
578 }
579
580 // Ensure that the tokens queue contains enough tokens.
581 if !parser.token_available {
582 if !yaml_parser_fetch_more_tokens(parser) {
583 return false
584 }
585 }
586
587 // Fetch the next token from the queue.
588 *token = parser.tokens[parser.tokens_head]
589 parser.tokens_head++
590 parser.tokens_parsed++
591 parser.token_available = false
592
593 if token.typ == yaml_STREAM_END_TOKEN {
594 parser.stream_end_produced = true
595 }
596 return true
597}
598
599// Set the scanner error and return false.
600func 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
606 return false
607}
608
609func 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"
611 if directive {
612 context = "while parsing a %TAG directive"
613 }
614 return yaml_parser_set_scanner_error(parser, context, context_mark, problem)
615}
616
617func trace(args ...interface{}) func() {
618 pargs := append([]interface{}{"+++"}, args...)
619 fmt.Println(pargs...)
620 pargs = append([]interface{}{"---"}, args...)
621 return func() { fmt.Println(pargs...) }
622}
623
624// Ensure that the tokens queue contains at least one token which can be
625// returned to the Parser.
626func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
627 // While we need more tokens to fetch, do it.
628 for {
629 // Check if we really need to fetch more tokens.
630 need_more_tokens := false
631
632 if parser.tokens_head == len(parser.tokens) {
633 // Queue is empty.
634 need_more_tokens = true
635 } else {
636 // Check if any potential simple key may occupy the head position.
637 if !yaml_parser_stale_simple_keys(parser) {
638 return false
639 }
640
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
645 break
646 }
647 }
648 }
649
650 // We are finished.
651 if !need_more_tokens {
652 break
653 }
654 // Fetch the next token.
655 if !yaml_parser_fetch_next_token(parser) {
656 return false
657 }
658 }
659
660 parser.token_available = true
661 return true
662}
663
664// The dispatcher for token fetchers.
665func 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) {
668 return false
669 }
670
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)
674 }
675
676 // Eat whitespaces and comments until we reach the next token.
677 if !yaml_parser_scan_to_next_token(parser) {
678 return false
679 }
680
681 // Remove obsolete potential simple keys.
682 if !yaml_parser_stale_simple_keys(parser) {
683 return false
684 }
685
686 // Check the indentation level against the current column.
687 if !yaml_parser_unroll_indent(parser, parser.mark.column) {
688 return false
689 }
690
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) {
694 return false
695 }
696
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)
700 }
701
702 // Is it a directive?
703 if parser.mark.column == 0 && parser.buffer[parser.buffer_pos] == '%' {
704 return yaml_parser_fetch_directive(parser)
705 }
706
707 buf := parser.buffer
708 pos := parser.buffer_pos
709
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)
713 }
714
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)
718 }
719
720 // Is it the flow sequence start indicator?
721 if buf[pos] == '[' {
722 return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_SEQUENCE_START_TOKEN)
723 }
724
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)
728 }
729
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)
734 }
735
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)
740 }
741
742 // Is it the flow entry indicator?
743 if parser.buffer[parser.buffer_pos] == ',' {
744 return yaml_parser_fetch_flow_entry(parser)
745 }
746
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)
750 }
751
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)
755 }
756
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)
760 }
761
762 // Is it an alias?
763 if parser.buffer[parser.buffer_pos] == '*' {
764 return yaml_parser_fetch_anchor(parser, yaml_ALIAS_TOKEN)
765 }
766
767 // Is it an anchor?
768 if parser.buffer[parser.buffer_pos] == '&' {
769 return yaml_parser_fetch_anchor(parser, yaml_ANCHOR_TOKEN)
770 }
771
772 // Is it a tag?
773 if parser.buffer[parser.buffer_pos] == '!' {
774 return yaml_parser_fetch_tag(parser)
775 }
776
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)
780 }
781
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)
785 }
786
787 // Is it a single-quoted scalar?
788 if parser.buffer[parser.buffer_pos] == '\'' {
789 return yaml_parser_fetch_flow_scalar(parser, true)
790 }
791
792 // Is it a double-quoted scalar?
793 if parser.buffer[parser.buffer_pos] == '"' {
794 return yaml_parser_fetch_flow_scalar(parser, false)
795 }
796
797 // Is it a plain scalar?
798 //
799 // A plain scalar may start with any non-blank characters except
800 //
801 // '-', '?', ':', ',', '[', ']', '{', '}',
802 // '#', '&', '*', '!', '|', '>', '\'', '\"',
803 // '%', '@', '`'.
804 //
805 // In the block context (and, for the '-' indicator, in the flow context
806 // too), it may also start with the characters
807 //
808 // '-', '?', ':'
809 //
810 // if it is followed by a non-space character.
811 //
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 '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`':
816 //}
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)
832 }
833
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")
838}
839
840// Check the list of potential simple keys and remove the positions that
841// cannot contain simple keys anymore.
842func 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]
846
847 // The specification requires that a simple key
848 //
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) {
852
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 ':'")
858 }
859 simple_key.possible = false
860 }
861 }
862 return true
863}
864
865// Check if a simple key may start at the current position and add it if
866// needed.
867func 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
870 // level.
871
872 required := parser.flow_level == 0 && parser.indent == parser.mark.column
873
874 //
875 // If the current position may start a simple key, save it.
876 //
877 if parser.simple_key_allowed {
878 simple_key := yaml_simple_key_t{
879 possible: true,
880 required: required,
881 token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
882 }
883 simple_key.mark = parser.mark
884
885 if !yaml_parser_remove_simple_key(parser) {
886 return false
887 }
888 parser.simple_keys[len(parser.simple_keys)-1] = simple_key
889 }
890 return true
891}
892
893// Remove a potential simple key at the current flow level.
894func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
895 i := len(parser.simple_keys) - 1
896 if parser.simple_keys[i].possible {
897 // If the key is required, it is an error.
898 if parser.simple_keys[i].required {
899 return yaml_parser_set_scanner_error(parser,
900 "while scanning a simple key", parser.simple_keys[i].mark,
901 "could not find expected ':'")
902 }
903 }
904 // Remove the key from the stack.
905 parser.simple_keys[i].possible = false
906 return true
907}
908
909// max_flow_level limits the flow_level
910const max_flow_level = 10000
911
912// Increase the flow level and resize the simple key list if needed.
913func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
914 // Reset the simple key on the next level.
915 parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
916
917 // Increase the flow level.
918 parser.flow_level++
919 if parser.flow_level > max_flow_level {
920 return yaml_parser_set_scanner_error(parser,
921 "while increasing flow level", parser.simple_keys[len(parser.simple_keys)-1].mark,
922 fmt.Sprintf("exceeded max depth of %d", max_flow_level))
923 }
924 return true
925}
926
927// Decrease the flow level.
928func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
929 if parser.flow_level > 0 {
930 parser.flow_level--
931 parser.simple_keys = parser.simple_keys[:len(parser.simple_keys)-1]
932 }
933 return true
934}
935
936// max_indents limits the indents stack size
937const max_indents = 10000
938
939// Push the current indentation level to the stack and set the new level
940// the current column is greater than the indentation level. In this case,
941// append or insert the specified token into the token queue.
942func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool {
943 // In the flow context, do nothing.
944 if parser.flow_level > 0 {
945 return true
946 }
947
948 if parser.indent < column {
949 // Push the current indentation level to the stack and set the new
950 // indentation level.
951 parser.indents = append(parser.indents, parser.indent)
952 parser.indent = column
953 if len(parser.indents) > max_indents {
954 return yaml_parser_set_scanner_error(parser,
955 "while increasing indent level", parser.simple_keys[len(parser.simple_keys)-1].mark,
956 fmt.Sprintf("exceeded max depth of %d", max_indents))
957 }
958
959 // Create a token and insert it into the queue.
960 token := yaml_token_t{
961 typ: typ,
962 start_mark: mark,
963 end_mark: mark,
964 }
965 if number > -1 {
966 number -= parser.tokens_parsed
967 }
968 yaml_insert_token(parser, number, &token)
969 }
970 return true
971}
972
973// Pop indentation levels from the indents stack until the current level
974// becomes less or equal to the column. For each indentation level, append
975// the BLOCK-END token.
976func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool {
977 // In the flow context, do nothing.
978 if parser.flow_level > 0 {
979 return true
980 }
981
982 // Loop through the indentation levels in the stack.
983 for parser.indent > column {
984 // Create a token and append it to the queue.
985 token := yaml_token_t{
986 typ: yaml_BLOCK_END_TOKEN,
987 start_mark: parser.mark,
988 end_mark: parser.mark,
989 }
990 yaml_insert_token(parser, -1, &token)
991
992 // Pop the indentation level.
993 parser.indent = parser.indents[len(parser.indents)-1]
994 parser.indents = parser.indents[:len(parser.indents)-1]
995 }
996 return true
997}
998
999// Initialize the scanner and produce the STREAM-START token.
1000func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
1001
1002 // Set the initial indentation.
1003 parser.indent = -1
1004
1005 // Initialize the simple key stack.
1006 parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
1007
1008 // A simple key is allowed at the beginning of the stream.
1009 parser.simple_key_allowed = true
1010
1011 // We have started.
1012 parser.stream_start_produced = true
1013
1014 // Create the STREAM-START token and append it to the queue.
1015 token := yaml_token_t{
1016 typ: yaml_STREAM_START_TOKEN,
1017 start_mark: parser.mark,
1018 end_mark: parser.mark,
1019 encoding: parser.encoding,
1020 }
1021 yaml_insert_token(parser, -1, &token)
1022 return true
1023}
1024
1025// Produce the STREAM-END token and shut down the scanner.
1026func yaml_parser_fetch_stream_end(parser *yaml_parser_t) bool {
1027
1028 // Force new line.
1029 if parser.mark.column != 0 {
1030 parser.mark.column = 0
1031 parser.mark.line++
1032 }
1033
1034 // Reset the indentation level.
1035 if !yaml_parser_unroll_indent(parser, -1) {
1036 return false
1037 }
1038
1039 // Reset simple keys.
1040 if !yaml_parser_remove_simple_key(parser) {
1041 return false
1042 }
1043
1044 parser.simple_key_allowed = false
1045
1046 // Create the STREAM-END token and append it to the queue.
1047 token := yaml_token_t{
1048 typ: yaml_STREAM_END_TOKEN,
1049 start_mark: parser.mark,
1050 end_mark: parser.mark,
1051 }
1052 yaml_insert_token(parser, -1, &token)
1053 return true
1054}
1055
1056// Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
1057func yaml_parser_fetch_directive(parser *yaml_parser_t) bool {
1058 // Reset the indentation level.
1059 if !yaml_parser_unroll_indent(parser, -1) {
1060 return false
1061 }
1062
1063 // Reset simple keys.
1064 if !yaml_parser_remove_simple_key(parser) {
1065 return false
1066 }
1067
1068 parser.simple_key_allowed = false
1069
1070 // Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
1071 token := yaml_token_t{}
1072 if !yaml_parser_scan_directive(parser, &token) {
1073 return false
1074 }
1075 // Append the token to the queue.
1076 yaml_insert_token(parser, -1, &token)
1077 return true
1078}
1079
1080// Produce the DOCUMENT-START or DOCUMENT-END token.
1081func yaml_parser_fetch_document_indicator(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1082 // Reset the indentation level.
1083 if !yaml_parser_unroll_indent(parser, -1) {
1084 return false
1085 }
1086
1087 // Reset simple keys.
1088 if !yaml_parser_remove_simple_key(parser) {
1089 return false
1090 }
1091
1092 parser.simple_key_allowed = false
1093
1094 // Consume the token.
1095 start_mark := parser.mark
1096
1097 skip(parser)
1098 skip(parser)
1099 skip(parser)
1100
1101 end_mark := parser.mark
1102
1103 // Create the DOCUMENT-START or DOCUMENT-END token.
1104 token := yaml_token_t{
1105 typ: typ,
1106 start_mark: start_mark,
1107 end_mark: end_mark,
1108 }
1109 // Append the token to the queue.
1110 yaml_insert_token(parser, -1, &token)
1111 return true
1112}
1113
1114// Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
1115func yaml_parser_fetch_flow_collection_start(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1116 // The indicators '[' and '{' may start a simple key.
1117 if !yaml_parser_save_simple_key(parser) {
1118 return false
1119 }
1120
1121 // Increase the flow level.
1122 if !yaml_parser_increase_flow_level(parser) {
1123 return false
1124 }
1125
1126 // A simple key may follow the indicators '[' and '{'.
1127 parser.simple_key_allowed = true
1128
1129 // Consume the token.
1130 start_mark := parser.mark
1131 skip(parser)
1132 end_mark := parser.mark
1133
1134 // Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
1135 token := yaml_token_t{
1136 typ: typ,
1137 start_mark: start_mark,
1138 end_mark: end_mark,
1139 }
1140 // Append the token to the queue.
1141 yaml_insert_token(parser, -1, &token)
1142 return true
1143}
1144
1145// Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
1146func yaml_parser_fetch_flow_collection_end(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1147 // Reset any potential simple key on the current flow level.
1148 if !yaml_parser_remove_simple_key(parser) {
1149 return false
1150 }
1151
1152 // Decrease the flow level.
1153 if !yaml_parser_decrease_flow_level(parser) {
1154 return false
1155 }
1156
1157 // No simple keys after the indicators ']' and '}'.
1158 parser.simple_key_allowed = false
1159
1160 // Consume the token.
1161
1162 start_mark := parser.mark
1163 skip(parser)
1164 end_mark := parser.mark
1165
1166 // Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token.
1167 token := yaml_token_t{
1168 typ: typ,
1169 start_mark: start_mark,
1170 end_mark: end_mark,
1171 }
1172 // Append the token to the queue.
1173 yaml_insert_token(parser, -1, &token)
1174 return true
1175}
1176
1177// Produce the FLOW-ENTRY token.
1178func yaml_parser_fetch_flow_entry(parser *yaml_parser_t) bool {
1179 // Reset any potential simple keys on the current flow level.
1180 if !yaml_parser_remove_simple_key(parser) {
1181 return false
1182 }
1183
1184 // Simple keys are allowed after ','.
1185 parser.simple_key_allowed = true
1186
1187 // Consume the token.
1188 start_mark := parser.mark
1189 skip(parser)
1190 end_mark := parser.mark
1191
1192 // Create the FLOW-ENTRY token and append it to the queue.
1193 token := yaml_token_t{
1194 typ: yaml_FLOW_ENTRY_TOKEN,
1195 start_mark: start_mark,
1196 end_mark: end_mark,
1197 }
1198 yaml_insert_token(parser, -1, &token)
1199 return true
1200}
1201
1202// Produce the BLOCK-ENTRY token.
1203func yaml_parser_fetch_block_entry(parser *yaml_parser_t) bool {
1204 // Check if the scanner is in the block context.
1205 if parser.flow_level == 0 {
1206 // Check if we are allowed to start a new entry.
1207 if !parser.simple_key_allowed {
1208 return yaml_parser_set_scanner_error(parser, "", parser.mark,
1209 "block sequence entries are not allowed in this context")
1210 }
1211 // Add the BLOCK-SEQUENCE-START token if needed.
1212 if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, parser.mark) {
1213 return false
1214 }
1215 } else {
1216 // It is an error for the '-' indicator to occur in the flow context,
1217 // but we let the Parser detect and report about it because the Parser
1218 // is able to point to the context.
1219 }
1220
1221 // Reset any potential simple keys on the current flow level.
1222 if !yaml_parser_remove_simple_key(parser) {
1223 return false
1224 }
1225
1226 // Simple keys are allowed after '-'.
1227 parser.simple_key_allowed = true
1228
1229 // Consume the token.
1230 start_mark := parser.mark
1231 skip(parser)
1232 end_mark := parser.mark
1233
1234 // Create the BLOCK-ENTRY token and append it to the queue.
1235 token := yaml_token_t{
1236 typ: yaml_BLOCK_ENTRY_TOKEN,
1237 start_mark: start_mark,
1238 end_mark: end_mark,
1239 }
1240 yaml_insert_token(parser, -1, &token)
1241 return true
1242}
1243
1244// Produce the KEY token.
1245func yaml_parser_fetch_key(parser *yaml_parser_t) bool {
1246
1247 // In the block context, additional checks are required.
1248 if parser.flow_level == 0 {
1249 // Check if we are allowed to start a new key (not nessesary simple).
1250 if !parser.simple_key_allowed {
1251 return yaml_parser_set_scanner_error(parser, "", parser.mark,
1252 "mapping keys are not allowed in this context")
1253 }
1254 // Add the BLOCK-MAPPING-START token if needed.
1255 if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
1256 return false
1257 }
1258 }
1259
1260 // Reset any potential simple keys on the current flow level.
1261 if !yaml_parser_remove_simple_key(parser) {
1262 return false
1263 }
1264
1265 // Simple keys are allowed after '?' in the block context.
1266 parser.simple_key_allowed = parser.flow_level == 0
1267
1268 // Consume the token.
1269 start_mark := parser.mark
1270 skip(parser)
1271 end_mark := parser.mark
1272
1273 // Create the KEY token and append it to the queue.
1274 token := yaml_token_t{
1275 typ: yaml_KEY_TOKEN,
1276 start_mark: start_mark,
1277 end_mark: end_mark,
1278 }
1279 yaml_insert_token(parser, -1, &token)
1280 return true
1281}
1282
1283// Produce the VALUE token.
1284func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
1285
1286 simple_key := &parser.simple_keys[len(parser.simple_keys)-1]
1287
1288 // Have we found a simple key?
1289 if simple_key.possible {
1290 // Create the KEY token and insert it into the queue.
1291 token := yaml_token_t{
1292 typ: yaml_KEY_TOKEN,
1293 start_mark: simple_key.mark,
1294 end_mark: simple_key.mark,
1295 }
1296 yaml_insert_token(parser, simple_key.token_number-parser.tokens_parsed, &token)
1297
1298 // In the block context, we may need to add the BLOCK-MAPPING-START token.
1299 if !yaml_parser_roll_indent(parser, simple_key.mark.column,
1300 simple_key.token_number,
1301 yaml_BLOCK_MAPPING_START_TOKEN, simple_key.mark) {
1302 return false
1303 }
1304
1305 // Remove the simple key.
1306 simple_key.possible = false
1307
1308 // A simple key cannot follow another simple key.
1309 parser.simple_key_allowed = false
1310
1311 } else {
1312 // The ':' indicator follows a complex key.
1313
1314 // In the block context, extra checks are required.
1315 if parser.flow_level == 0 {
1316
1317 // Check if we are allowed to start a complex value.
1318 if !parser.simple_key_allowed {
1319 return yaml_parser_set_scanner_error(parser, "", parser.mark,
1320 "mapping values are not allowed in this context")
1321 }
1322
1323 // Add the BLOCK-MAPPING-START token if needed.
1324 if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
1325 return false
1326 }
1327 }
1328
1329 // Simple keys after ':' are allowed in the block context.
1330 parser.simple_key_allowed = parser.flow_level == 0
1331 }
1332
1333 // Consume the token.
1334 start_mark := parser.mark
1335 skip(parser)
1336 end_mark := parser.mark
1337
1338 // Create the VALUE token and append it to the queue.
1339 token := yaml_token_t{
1340 typ: yaml_VALUE_TOKEN,
1341 start_mark: start_mark,
1342 end_mark: end_mark,
1343 }
1344 yaml_insert_token(parser, -1, &token)
1345 return true
1346}
1347
1348// Produce the ALIAS or ANCHOR token.
1349func yaml_parser_fetch_anchor(parser *yaml_parser_t, typ yaml_token_type_t) bool {
1350 // An anchor or an alias could be a simple key.
1351 if !yaml_parser_save_simple_key(parser) {
1352 return false
1353 }
1354
1355 // A simple key cannot follow an anchor or an alias.
1356 parser.simple_key_allowed = false
1357
1358 // Create the ALIAS or ANCHOR token and append it to the queue.
1359 var token yaml_token_t
1360 if !yaml_parser_scan_anchor(parser, &token, typ) {
1361 return false
1362 }
1363 yaml_insert_token(parser, -1, &token)
1364 return true
1365}
1366
1367// Produce the TAG token.
1368func yaml_parser_fetch_tag(parser *yaml_parser_t) bool {
1369 // A tag could be a simple key.
1370 if !yaml_parser_save_simple_key(parser) {
1371 return false
1372 }
1373
1374 // A simple key cannot follow a tag.
1375 parser.simple_key_allowed = false
1376
1377 // Create the TAG token and append it to the queue.
1378 var token yaml_token_t
1379 if !yaml_parser_scan_tag(parser, &token) {
1380 return false
1381 }
1382 yaml_insert_token(parser, -1, &token)
1383 return true
1384}
1385
1386// Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
1387func yaml_parser_fetch_block_scalar(parser *yaml_parser_t, literal bool) bool {
1388 // Remove any potential simple keys.
1389 if !yaml_parser_remove_simple_key(parser) {
1390 return false
1391 }
1392
1393 // A simple key may follow a block scalar.
1394 parser.simple_key_allowed = true
1395
1396 // Create the SCALAR token and append it to the queue.
1397 var token yaml_token_t
1398 if !yaml_parser_scan_block_scalar(parser, &token, literal) {
1399 return false
1400 }
1401 yaml_insert_token(parser, -1, &token)
1402 return true
1403}
1404
1405// Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
1406func yaml_parser_fetch_flow_scalar(parser *yaml_parser_t, single bool) bool {
1407 // A plain scalar could be a simple key.
1408 if !yaml_parser_save_simple_key(parser) {
1409 return false
1410 }
1411
1412 // A simple key cannot follow a flow scalar.
1413 parser.simple_key_allowed = false
1414
1415 // Create the SCALAR token and append it to the queue.
1416 var token yaml_token_t
1417 if !yaml_parser_scan_flow_scalar(parser, &token, single) {
1418 return false
1419 }
1420 yaml_insert_token(parser, -1, &token)
1421 return true
1422}
1423
1424// Produce the SCALAR(...,plain) token.
1425func yaml_parser_fetch_plain_scalar(parser *yaml_parser_t) bool {
1426 // A plain scalar could be a simple key.
1427 if !yaml_parser_save_simple_key(parser) {
1428 return false
1429 }
1430
1431 // A simple key cannot follow a flow scalar.
1432 parser.simple_key_allowed = false
1433
1434 // Create the SCALAR token and append it to the queue.
1435 var token yaml_token_t
1436 if !yaml_parser_scan_plain_scalar(parser, &token) {
1437 return false
1438 }
1439 yaml_insert_token(parser, -1, &token)
1440 return true
1441}
1442
1443// Eat whitespaces and comments until the next token is found.
1444func yaml_parser_scan_to_next_token(parser *yaml_parser_t) bool {
1445
1446 // Until the next token is not found.
1447 for {
1448 // Allow the BOM mark to start a line.
1449 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1450 return false
1451 }
1452 if parser.mark.column == 0 && is_bom(parser.buffer, parser.buffer_pos) {
1453 skip(parser)
1454 }
1455
1456 // Eat whitespaces.
1457 // Tabs are allowed:
1458 // - in the flow context
1459 // - in the block context, but not at the beginning of the line or
1460 // after '-', '?', or ':' (complex value).
1461 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1462 return false
1463 }
1464
1465 for parser.buffer[parser.buffer_pos] == ' ' || ((parser.flow_level > 0 || !parser.simple_key_allowed) && parser.buffer[parser.buffer_pos] == '\t') {
1466 skip(parser)
1467 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1468 return false
1469 }
1470 }
1471
1472 // Eat a comment until a line break.
1473 if parser.buffer[parser.buffer_pos] == '#' {
1474 for !is_breakz(parser.buffer, parser.buffer_pos) {
1475 skip(parser)
1476 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1477 return false
1478 }
1479 }
1480 }
1481
1482 // If it is a line break, eat it.
1483 if is_break(parser.buffer, parser.buffer_pos) {
1484 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
1485 return false
1486 }
1487 skip_line(parser)
1488
1489 // In the block context, a new line may start a simple key.
1490 if parser.flow_level == 0 {
1491 parser.simple_key_allowed = true
1492 }
1493 } else {
1494 break // We have found a token.
1495 }
1496 }
1497
1498 return true
1499}
1500
1501// Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
1502//
1503// Scope:
1504// %YAML 1.1 # a comment \n
1505// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1506// %TAG !yaml! tag:yaml.org,2002: \n
1507// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1508//
1509func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool {
1510 // Eat '%'.
1511 start_mark := parser.mark
1512 skip(parser)
1513
1514 // Scan the directive name.
1515 var name []byte
1516 if !yaml_parser_scan_directive_name(parser, start_mark, &name) {
1517 return false
1518 }
1519
1520 // Is it a YAML directive?
1521 if bytes.Equal(name, []byte("YAML")) {
1522 // Scan the VERSION directive value.
1523 var major, minor int8
1524 if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) {
1525 return false
1526 }
1527 end_mark := parser.mark
1528
1529 // Create a VERSION-DIRECTIVE token.
1530 *token = yaml_token_t{
1531 typ: yaml_VERSION_DIRECTIVE_TOKEN,
1532 start_mark: start_mark,
1533 end_mark: end_mark,
1534 major: major,
1535 minor: minor,
1536 }
1537
1538 // Is it a TAG directive?
1539 } else if bytes.Equal(name, []byte("TAG")) {
1540 // Scan the TAG directive value.
1541 var handle, prefix []byte
1542 if !yaml_parser_scan_tag_directive_value(parser, start_mark, &handle, &prefix) {
1543 return false
1544 }
1545 end_mark := parser.mark
1546
1547 // Create a TAG-DIRECTIVE token.
1548 *token = yaml_token_t{
1549 typ: yaml_TAG_DIRECTIVE_TOKEN,
1550 start_mark: start_mark,
1551 end_mark: end_mark,
1552 value: handle,
1553 prefix: prefix,
1554 }
1555
1556 // Unknown directive.
1557 } else {
1558 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1559 start_mark, "found unknown directive name")
1560 return false
1561 }
1562
1563 // Eat the rest of the line including any comments.
1564 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1565 return false
1566 }
1567
1568 for is_blank(parser.buffer, parser.buffer_pos) {
1569 skip(parser)
1570 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1571 return false
1572 }
1573 }
1574
1575 if parser.buffer[parser.buffer_pos] == '#' {
1576 for !is_breakz(parser.buffer, parser.buffer_pos) {
1577 skip(parser)
1578 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1579 return false
1580 }
1581 }
1582 }
1583
1584 // Check if we are at the end of the line.
1585 if !is_breakz(parser.buffer, parser.buffer_pos) {
1586 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1587 start_mark, "did not find expected comment or line break")
1588 return false
1589 }
1590
1591 // Eat a line break.
1592 if is_break(parser.buffer, parser.buffer_pos) {
1593 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
1594 return false
1595 }
1596 skip_line(parser)
1597 }
1598
1599 return true
1600}
1601
1602// Scan the directive name.
1603//
1604// Scope:
1605// %YAML 1.1 # a comment \n
1606// ^^^^
1607// %TAG !yaml! tag:yaml.org,2002: \n
1608// ^^^
1609//
1610func yaml_parser_scan_directive_name(parser *yaml_parser_t, start_mark yaml_mark_t, name *[]byte) bool {
1611 // Consume the directive name.
1612 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1613 return false
1614 }
1615
1616 var s []byte
1617 for is_alpha(parser.buffer, parser.buffer_pos) {
1618 s = read(parser, s)
1619 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1620 return false
1621 }
1622 }
1623
1624 // Check if the name is empty.
1625 if len(s) == 0 {
1626 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1627 start_mark, "could not find expected directive name")
1628 return false
1629 }
1630
1631 // Check for an blank character after the name.
1632 if !is_blankz(parser.buffer, parser.buffer_pos) {
1633 yaml_parser_set_scanner_error(parser, "while scanning a directive",
1634 start_mark, "found unexpected non-alphabetical character")
1635 return false
1636 }
1637 *name = s
1638 return true
1639}
1640
1641// Scan the value of VERSION-DIRECTIVE.
1642//
1643// Scope:
1644// %YAML 1.1 # a comment \n
1645// ^^^^^^
1646func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool {
1647 // Eat whitespaces.
1648 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1649 return false
1650 }
1651 for is_blank(parser.buffer, parser.buffer_pos) {
1652 skip(parser)
1653 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1654 return false
1655 }
1656 }
1657
1658 // Consume the major version number.
1659 if !yaml_parser_scan_version_directive_number(parser, start_mark, major) {
1660 return false
1661 }
1662
1663 // Eat '.'.
1664 if parser.buffer[parser.buffer_pos] != '.' {
1665 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
1666 start_mark, "did not find expected digit or '.' character")
1667 }
1668
1669 skip(parser)
1670
1671 // Consume the minor version number.
1672 if !yaml_parser_scan_version_directive_number(parser, start_mark, minor) {
1673 return false
1674 }
1675 return true
1676}
1677
1678const max_number_length = 2
1679
1680// Scan the version number of VERSION-DIRECTIVE.
1681//
1682// Scope:
1683// %YAML 1.1 # a comment \n
1684// ^
1685// %YAML 1.1 # a comment \n
1686// ^
1687func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool {
1688
1689 // Repeat while the next character is digit.
1690 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1691 return false
1692 }
1693 var value, length int8
1694 for is_digit(parser.buffer, parser.buffer_pos) {
1695 // Check if the number is too long.
1696 length++
1697 if length > max_number_length {
1698 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
1699 start_mark, "found extremely long version number")
1700 }
1701 value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos))
1702 skip(parser)
1703 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1704 return false
1705 }
1706 }
1707
1708 // Check if the number was present.
1709 if length == 0 {
1710 return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
1711 start_mark, "did not find expected version number")
1712 }
1713 *number = value
1714 return true
1715}
1716
1717// Scan the value of a TAG-DIRECTIVE token.
1718//
1719// Scope:
1720// %TAG !yaml! tag:yaml.org,2002: \n
1721// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1722//
1723func yaml_parser_scan_tag_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, handle, prefix *[]byte) bool {
1724 var handle_value, prefix_value []byte
1725
1726 // Eat whitespaces.
1727 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1728 return false
1729 }
1730
1731 for is_blank(parser.buffer, parser.buffer_pos) {
1732 skip(parser)
1733 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1734 return false
1735 }
1736 }
1737
1738 // Scan a handle.
1739 if !yaml_parser_scan_tag_handle(parser, true, start_mark, &handle_value) {
1740 return false
1741 }
1742
1743 // Expect a whitespace.
1744 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1745 return false
1746 }
1747 if !is_blank(parser.buffer, parser.buffer_pos) {
1748 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
1749 start_mark, "did not find expected whitespace")
1750 return false
1751 }
1752
1753 // Eat whitespaces.
1754 for is_blank(parser.buffer, parser.buffer_pos) {
1755 skip(parser)
1756 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1757 return false
1758 }
1759 }
1760
1761 // Scan a prefix.
1762 if !yaml_parser_scan_tag_uri(parser, true, nil, start_mark, &prefix_value) {
1763 return false
1764 }
1765
1766 // Expect a whitespace or line break.
1767 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1768 return false
1769 }
1770 if !is_blankz(parser.buffer, parser.buffer_pos) {
1771 yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
1772 start_mark, "did not find expected whitespace or line break")
1773 return false
1774 }
1775
1776 *handle = handle_value
1777 *prefix = prefix_value
1778 return true
1779}
1780
1781func yaml_parser_scan_anchor(parser *yaml_parser_t, token *yaml_token_t, typ yaml_token_type_t) bool {
1782 var s []byte
1783
1784 // Eat the indicator character.
1785 start_mark := parser.mark
1786 skip(parser)
1787
1788 // Consume the value.
1789 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1790 return false
1791 }
1792
1793 for is_alpha(parser.buffer, parser.buffer_pos) {
1794 s = read(parser, s)
1795 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1796 return false
1797 }
1798 }
1799
1800 end_mark := parser.mark
1801
1802 /*
1803 * Check if length of the anchor is greater than 0 and it is followed by
1804 * a whitespace character or one of the indicators:
1805 *
1806 * '?', ':', ',', ']', '}', '%', '@', '`'.
1807 */
1808
1809 if len(s) == 0 ||
1810 !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '?' ||
1811 parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == ',' ||
1812 parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '}' ||
1813 parser.buffer[parser.buffer_pos] == '%' || parser.buffer[parser.buffer_pos] == '@' ||
1814 parser.buffer[parser.buffer_pos] == '`') {
1815 context := "while scanning an alias"
1816 if typ == yaml_ANCHOR_TOKEN {
1817 context = "while scanning an anchor"
1818 }
1819 yaml_parser_set_scanner_error(parser, context, start_mark,
1820 "did not find expected alphabetic or numeric character")
1821 return false
1822 }
1823
1824 // Create a token.
1825 *token = yaml_token_t{
1826 typ: typ,
1827 start_mark: start_mark,
1828 end_mark: end_mark,
1829 value: s,
1830 }
1831
1832 return true
1833}
1834
1835/*
1836 * Scan a TAG token.
1837 */
1838
1839func yaml_parser_scan_tag(parser *yaml_parser_t, token *yaml_token_t) bool {
1840 var handle, suffix []byte
1841
1842 start_mark := parser.mark
1843
1844 // Check if the tag is in the canonical form.
1845 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
1846 return false
1847 }
1848
1849 if parser.buffer[parser.buffer_pos+1] == '<' {
1850 // Keep the handle as ''
1851
1852 // Eat '!<'
1853 skip(parser)
1854 skip(parser)
1855
1856 // Consume the tag value.
1857 if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
1858 return false
1859 }
1860
1861 // Check for '>' and eat it.
1862 if parser.buffer[parser.buffer_pos] != '>' {
1863 yaml_parser_set_scanner_error(parser, "while scanning a tag",
1864 start_mark, "did not find the expected '>'")
1865 return false
1866 }
1867
1868 skip(parser)
1869 } else {
1870 // The tag has either the '!suffix' or the '!handle!suffix' form.
1871
1872 // First, try to scan a handle.
1873 if !yaml_parser_scan_tag_handle(parser, false, start_mark, &handle) {
1874 return false
1875 }
1876
1877 // Check if it is, indeed, handle.
1878 if handle[0] == '!' && len(handle) > 1 && handle[len(handle)-1] == '!' {
1879 // Scan the suffix now.
1880 if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
1881 return false
1882 }
1883 } else {
1884 // It wasn't a handle after all. Scan the rest of the tag.
1885 if !yaml_parser_scan_tag_uri(parser, false, handle, start_mark, &suffix) {
1886 return false
1887 }
1888
1889 // Set the handle to '!'.
1890 handle = []byte{'!'}
1891
1892 // A special case: the '!' tag. Set the handle to '' and the
1893 // suffix to '!'.
1894 if len(suffix) == 0 {
1895 handle, suffix = suffix, handle
1896 }
1897 }
1898 }
1899
1900 // Check the character which ends the tag.
1901 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1902 return false
1903 }
1904 if !is_blankz(parser.buffer, parser.buffer_pos) {
1905 yaml_parser_set_scanner_error(parser, "while scanning a tag",
1906 start_mark, "did not find expected whitespace or line break")
1907 return false
1908 }
1909
1910 end_mark := parser.mark
1911
1912 // Create a token.
1913 *token = yaml_token_t{
1914 typ: yaml_TAG_TOKEN,
1915 start_mark: start_mark,
1916 end_mark: end_mark,
1917 value: handle,
1918 suffix: suffix,
1919 }
1920 return true
1921}
1922
1923// Scan a tag handle.
1924func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, handle *[]byte) bool {
1925 // Check the initial '!' character.
1926 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1927 return false
1928 }
1929 if parser.buffer[parser.buffer_pos] != '!' {
1930 yaml_parser_set_scanner_tag_error(parser, directive,
1931 start_mark, "did not find expected '!'")
1932 return false
1933 }
1934
1935 var s []byte
1936
1937 // Copy the '!' character.
1938 s = read(parser, s)
1939
1940 // Copy all subsequent alphabetical and numerical characters.
1941 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1942 return false
1943 }
1944 for is_alpha(parser.buffer, parser.buffer_pos) {
1945 s = read(parser, s)
1946 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1947 return false
1948 }
1949 }
1950
1951 // Check if the trailing character is '!' and copy it.
1952 if parser.buffer[parser.buffer_pos] == '!' {
1953 s = read(parser, s)
1954 } else {
1955 // It's either the '!' tag or not really a tag handle. If it's a %TAG
1956 // directive, it's an error. If it's a tag token, it must be a part of URI.
1957 if directive && string(s) != "!" {
1958 yaml_parser_set_scanner_tag_error(parser, directive,
1959 start_mark, "did not find expected '!'")
1960 return false
1961 }
1962 }
1963
1964 *handle = s
1965 return true
1966}
1967
1968// Scan a tag.
1969func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool {
1970 //size_t length = head ? strlen((char *)head) : 0
1971 var s []byte
1972 hasTag := len(head) > 0
1973
1974 // Copy the head if needed.
1975 //
1976 // Note that we don't copy the leading '!' character.
1977 if len(head) > 1 {
1978 s = append(s, head[1:]...)
1979 }
1980
1981 // Scan the tag.
1982 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
1983 return false
1984 }
1985
1986 // The set of characters that may appear in URI is as follows:
1987 //
1988 // '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
1989 // '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
1990 // '%'.
1991 // [Go] Convert this into more reasonable logic.
1992 for is_alpha(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == ';' ||
1993 parser.buffer[parser.buffer_pos] == '/' || parser.buffer[parser.buffer_pos] == '?' ||
1994 parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == '@' ||
1995 parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '=' ||
1996 parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '$' ||
1997 parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '.' ||
1998 parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '~' ||
1999 parser.buffer[parser.buffer_pos] == '*' || parser.buffer[parser.buffer_pos] == '\'' ||
2000 parser.buffer[parser.buffer_pos] == '(' || parser.buffer[parser.buffer_pos] == ')' ||
2001 parser.buffer[parser.buffer_pos] == '[' || parser.buffer[parser.buffer_pos] == ']' ||
2002 parser.buffer[parser.buffer_pos] == '%' {
2003 // Check if it is a URI-escape sequence.
2004 if parser.buffer[parser.buffer_pos] == '%' {
2005 if !yaml_parser_scan_uri_escapes(parser, directive, start_mark, &s) {
2006 return false
2007 }
2008 } else {
2009 s = read(parser, s)
2010 }
2011 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2012 return false
2013 }
2014 hasTag = true
2015 }
2016
2017 if !hasTag {
2018 yaml_parser_set_scanner_tag_error(parser, directive,
2019 start_mark, "did not find expected tag URI")
2020 return false
2021 }
2022 *uri = s
2023 return true
2024}
2025
2026// Decode an URI-escape sequence corresponding to a single UTF-8 character.
2027func yaml_parser_scan_uri_escapes(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, s *[]byte) bool {
2028
2029 // Decode the required number of characters.
2030 w := 1024
2031 for w > 0 {
2032 // Check for a URI-escaped octet.
2033 if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
2034 return false
2035 }
2036
2037 if !(parser.buffer[parser.buffer_pos] == '%' &&
2038 is_hex(parser.buffer, parser.buffer_pos+1) &&
2039 is_hex(parser.buffer, parser.buffer_pos+2)) {
2040 return yaml_parser_set_scanner_tag_error(parser, directive,
2041 start_mark, "did not find URI escaped octet")
2042 }
2043
2044 // Get the octet.
2045 octet := byte((as_hex(parser.buffer, parser.buffer_pos+1) << 4) + as_hex(parser.buffer, parser.buffer_pos+2))
2046
2047 // If it is the leading octet, determine the length of the UTF-8 sequence.
2048 if w == 1024 {
2049 w = width(octet)
2050 if w == 0 {
2051 return yaml_parser_set_scanner_tag_error(parser, directive,
2052 start_mark, "found an incorrect leading UTF-8 octet")
2053 }
2054 } else {
2055 // Check if the trailing octet is correct.
2056 if octet&0xC0 != 0x80 {
2057 return yaml_parser_set_scanner_tag_error(parser, directive,
2058 start_mark, "found an incorrect trailing UTF-8 octet")
2059 }
2060 }
2061
2062 // Copy the octet and move the pointers.
2063 *s = append(*s, octet)
2064 skip(parser)
2065 skip(parser)
2066 skip(parser)
2067 w--
2068 }
2069 return true
2070}
2071
2072// Scan a block scalar.
2073func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, literal bool) bool {
2074 // Eat the indicator '|' or '>'.
2075 start_mark := parser.mark
2076 skip(parser)
2077
2078 // Scan the additional block scalar indicators.
2079 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2080 return false
2081 }
2082
2083 // Check for a chomping indicator.
2084 var chomping, increment int
2085 if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
2086 // Set the chomping method and eat the indicator.
2087 if parser.buffer[parser.buffer_pos] == '+' {
2088 chomping = +1
2089 } else {
2090 chomping = -1
2091 }
2092 skip(parser)
2093
2094 // Check for an indentation indicator.
2095 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2096 return false
2097 }
2098 if is_digit(parser.buffer, parser.buffer_pos) {
2099 // Check that the indentation is greater than 0.
2100 if parser.buffer[parser.buffer_pos] == '0' {
2101 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2102 start_mark, "found an indentation indicator equal to 0")
2103 return false
2104 }
2105
2106 // Get the indentation level and eat the indicator.
2107 increment = as_digit(parser.buffer, parser.buffer_pos)
2108 skip(parser)
2109 }
2110
2111 } else if is_digit(parser.buffer, parser.buffer_pos) {
2112 // Do the same as above, but in the opposite order.
2113
2114 if parser.buffer[parser.buffer_pos] == '0' {
2115 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2116 start_mark, "found an indentation indicator equal to 0")
2117 return false
2118 }
2119 increment = as_digit(parser.buffer, parser.buffer_pos)
2120 skip(parser)
2121
2122 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2123 return false
2124 }
2125 if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
2126 if parser.buffer[parser.buffer_pos] == '+' {
2127 chomping = +1
2128 } else {
2129 chomping = -1
2130 }
2131 skip(parser)
2132 }
2133 }
2134
2135 // Eat whitespaces and comments to the end of the line.
2136 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2137 return false
2138 }
2139 for is_blank(parser.buffer, parser.buffer_pos) {
2140 skip(parser)
2141 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2142 return false
2143 }
2144 }
2145 if parser.buffer[parser.buffer_pos] == '#' {
2146 for !is_breakz(parser.buffer, parser.buffer_pos) {
2147 skip(parser)
2148 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2149 return false
2150 }
2151 }
2152 }
2153
2154 // Check if we are at the end of the line.
2155 if !is_breakz(parser.buffer, parser.buffer_pos) {
2156 yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2157 start_mark, "did not find expected comment or line break")
2158 return false
2159 }
2160
2161 // Eat a line break.
2162 if is_break(parser.buffer, parser.buffer_pos) {
2163 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2164 return false
2165 }
2166 skip_line(parser)
2167 }
2168
2169 end_mark := parser.mark
2170
2171 // Set the indentation level if it was specified.
2172 var indent int
2173 if increment > 0 {
2174 if parser.indent >= 0 {
2175 indent = parser.indent + increment
2176 } else {
2177 indent = increment
2178 }
2179 }
2180
2181 // Scan the leading line breaks and determine the indentation level if needed.
2182 var s, leading_break, trailing_breaks []byte
2183 if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
2184 return false
2185 }
2186
2187 // Scan the block scalar content.
2188 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2189 return false
2190 }
2191 var leading_blank, trailing_blank bool
2192 for parser.mark.column == indent && !is_z(parser.buffer, parser.buffer_pos) {
2193 // We are at the beginning of a non-empty line.
2194
2195 // Is it a trailing whitespace?
2196 trailing_blank = is_blank(parser.buffer, parser.buffer_pos)
2197
2198 // Check if we need to fold the leading line break.
2199 if !literal && !leading_blank && !trailing_blank && len(leading_break) > 0 && leading_break[0] == '\n' {
2200 // Do we need to join the lines by space?
2201 if len(trailing_breaks) == 0 {
2202 s = append(s, ' ')
2203 }
2204 } else {
2205 s = append(s, leading_break...)
2206 }
2207 leading_break = leading_break[:0]
2208
2209 // Append the remaining line breaks.
2210 s = append(s, trailing_breaks...)
2211 trailing_breaks = trailing_breaks[:0]
2212
2213 // Is it a leading whitespace?
2214 leading_blank = is_blank(parser.buffer, parser.buffer_pos)
2215
2216 // Consume the current line.
2217 for !is_breakz(parser.buffer, parser.buffer_pos) {
2218 s = read(parser, s)
2219 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2220 return false
2221 }
2222 }
2223
2224 // Consume the line break.
2225 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2226 return false
2227 }
2228
2229 leading_break = read_line(parser, leading_break)
2230
2231 // Eat the following indentation spaces and line breaks.
2232 if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
2233 return false
2234 }
2235 }
2236
2237 // Chomp the tail.
2238 if chomping != -1 {
2239 s = append(s, leading_break...)
2240 }
2241 if chomping == 1 {
2242 s = append(s, trailing_breaks...)
2243 }
2244
2245 // Create a token.
2246 *token = yaml_token_t{
2247 typ: yaml_SCALAR_TOKEN,
2248 start_mark: start_mark,
2249 end_mark: end_mark,
2250 value: s,
2251 style: yaml_LITERAL_SCALAR_STYLE,
2252 }
2253 if !literal {
2254 token.style = yaml_FOLDED_SCALAR_STYLE
2255 }
2256 return true
2257}
2258
2259// Scan indentation spaces and line breaks for a block scalar. Determine the
2260// indentation level if needed.
2261func 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 {
2262 *end_mark = parser.mark
2263
2264 // Eat the indentation spaces and line breaks.
2265 max_indent := 0
2266 for {
2267 // Eat the indentation spaces.
2268 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2269 return false
2270 }
2271 for (*indent == 0 || parser.mark.column < *indent) && is_space(parser.buffer, parser.buffer_pos) {
2272 skip(parser)
2273 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2274 return false
2275 }
2276 }
2277 if parser.mark.column > max_indent {
2278 max_indent = parser.mark.column
2279 }
2280
2281 // Check for a tab character messing the indentation.
2282 if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) {
2283 return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
2284 start_mark, "found a tab character where an indentation space is expected")
2285 }
2286
2287 // Have we found a non-empty line?
2288 if !is_break(parser.buffer, parser.buffer_pos) {
2289 break
2290 }
2291
2292 // Consume the line break.
2293 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2294 return false
2295 }
2296 // [Go] Should really be returning breaks instead.
2297 *breaks = read_line(parser, *breaks)
2298 *end_mark = parser.mark
2299 }
2300
2301 // Determine the indentation level if needed.
2302 if *indent == 0 {
2303 *indent = max_indent
2304 if *indent < parser.indent+1 {
2305 *indent = parser.indent + 1
2306 }
2307 if *indent < 1 {
2308 *indent = 1
2309 }
2310 }
2311 return true
2312}
2313
2314// Scan a quoted scalar.
2315func yaml_parser_scan_flow_scalar(parser *yaml_parser_t, token *yaml_token_t, single bool) bool {
2316 // Eat the left quote.
2317 start_mark := parser.mark
2318 skip(parser)
2319
2320 // Consume the content of the quoted scalar.
2321 var s, leading_break, trailing_breaks, whitespaces []byte
2322 for {
2323 // Check that there are no document indicators at the beginning of the line.
2324 if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2325 return false
2326 }
2327
2328 if parser.mark.column == 0 &&
2329 ((parser.buffer[parser.buffer_pos+0] == '-' &&
2330 parser.buffer[parser.buffer_pos+1] == '-' &&
2331 parser.buffer[parser.buffer_pos+2] == '-') ||
2332 (parser.buffer[parser.buffer_pos+0] == '.' &&
2333 parser.buffer[parser.buffer_pos+1] == '.' &&
2334 parser.buffer[parser.buffer_pos+2] == '.')) &&
2335 is_blankz(parser.buffer, parser.buffer_pos+3) {
2336 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
2337 start_mark, "found unexpected document indicator")
2338 return false
2339 }
2340
2341 // Check for EOF.
2342 if is_z(parser.buffer, parser.buffer_pos) {
2343 yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
2344 start_mark, "found unexpected end of stream")
2345 return false
2346 }
2347
2348 // Consume non-blank characters.
2349 leading_blanks := false
2350 for !is_blankz(parser.buffer, parser.buffer_pos) {
2351 if single && parser.buffer[parser.buffer_pos] == '\'' && parser.buffer[parser.buffer_pos+1] == '\'' {
2352 // Is is an escaped single quote.
2353 s = append(s, '\'')
2354 skip(parser)
2355 skip(parser)
2356
2357 } else if single && parser.buffer[parser.buffer_pos] == '\'' {
2358 // It is a right single quote.
2359 break
2360 } else if !single && parser.buffer[parser.buffer_pos] == '"' {
2361 // It is a right double quote.
2362 break
2363
2364 } else if !single && parser.buffer[parser.buffer_pos] == '\\' && is_break(parser.buffer, parser.buffer_pos+1) {
2365 // It is an escaped line break.
2366 if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
2367 return false
2368 }
2369 skip(parser)
2370 skip_line(parser)
2371 leading_blanks = true
2372 break
2373
2374 } else if !single && parser.buffer[parser.buffer_pos] == '\\' {
2375 // It is an escape sequence.
2376 code_length := 0
2377
2378 // Check the escape character.
2379 switch parser.buffer[parser.buffer_pos+1] {
2380 case '0':
2381 s = append(s, 0)
2382 case 'a':
2383 s = append(s, '\x07')
2384 case 'b':
2385 s = append(s, '\x08')
2386 case 't', '\t':
2387 s = append(s, '\x09')
2388 case 'n':
2389 s = append(s, '\x0A')
2390 case 'v':
2391 s = append(s, '\x0B')
2392 case 'f':
2393 s = append(s, '\x0C')
2394 case 'r':
2395 s = append(s, '\x0D')
2396 case 'e':
2397 s = append(s, '\x1B')
2398 case ' ':
2399 s = append(s, '\x20')
2400 case '"':
2401 s = append(s, '"')
2402 case '\'':
2403 s = append(s, '\'')
2404 case '\\':
2405 s = append(s, '\\')
2406 case 'N': // NEL (#x85)
2407 s = append(s, '\xC2')
2408 s = append(s, '\x85')
2409 case '_': // #xA0
2410 s = append(s, '\xC2')
2411 s = append(s, '\xA0')
2412 case 'L': // LS (#x2028)
2413 s = append(s, '\xE2')
2414 s = append(s, '\x80')
2415 s = append(s, '\xA8')
2416 case 'P': // PS (#x2029)
2417 s = append(s, '\xE2')
2418 s = append(s, '\x80')
2419 s = append(s, '\xA9')
2420 case 'x':
2421 code_length = 2
2422 case 'u':
2423 code_length = 4
2424 case 'U':
2425 code_length = 8
2426 default:
2427 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2428 start_mark, "found unknown escape character")
2429 return false
2430 }
2431
2432 skip(parser)
2433 skip(parser)
2434
2435 // Consume an arbitrary escape code.
2436 if code_length > 0 {
2437 var value int
2438
2439 // Scan the character value.
2440 if parser.unread < code_length && !yaml_parser_update_buffer(parser, code_length) {
2441 return false
2442 }
2443 for k := 0; k < code_length; k++ {
2444 if !is_hex(parser.buffer, parser.buffer_pos+k) {
2445 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2446 start_mark, "did not find expected hexdecimal number")
2447 return false
2448 }
2449 value = (value << 4) + as_hex(parser.buffer, parser.buffer_pos+k)
2450 }
2451
2452 // Check the value and write the character.
2453 if (value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF {
2454 yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
2455 start_mark, "found invalid Unicode character escape code")
2456 return false
2457 }
2458 if value <= 0x7F {
2459 s = append(s, byte(value))
2460 } else if value <= 0x7FF {
2461 s = append(s, byte(0xC0+(value>>6)))
2462 s = append(s, byte(0x80+(value&0x3F)))
2463 } else if value <= 0xFFFF {
2464 s = append(s, byte(0xE0+(value>>12)))
2465 s = append(s, byte(0x80+((value>>6)&0x3F)))
2466 s = append(s, byte(0x80+(value&0x3F)))
2467 } else {
2468 s = append(s, byte(0xF0+(value>>18)))
2469 s = append(s, byte(0x80+((value>>12)&0x3F)))
2470 s = append(s, byte(0x80+((value>>6)&0x3F)))
2471 s = append(s, byte(0x80+(value&0x3F)))
2472 }
2473
2474 // Advance the pointer.
2475 for k := 0; k < code_length; k++ {
2476 skip(parser)
2477 }
2478 }
2479 } else {
2480 // It is a non-escaped non-blank character.
2481 s = read(parser, s)
2482 }
2483 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2484 return false
2485 }
2486 }
2487
2488 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2489 return false
2490 }
2491
2492 // Check if we are at the end of the scalar.
2493 if single {
2494 if parser.buffer[parser.buffer_pos] == '\'' {
2495 break
2496 }
2497 } else {
2498 if parser.buffer[parser.buffer_pos] == '"' {
2499 break
2500 }
2501 }
2502
2503 // Consume blank characters.
2504 for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
2505 if is_blank(parser.buffer, parser.buffer_pos) {
2506 // Consume a space or a tab character.
2507 if !leading_blanks {
2508 whitespaces = read(parser, whitespaces)
2509 } else {
2510 skip(parser)
2511 }
2512 } else {
2513 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2514 return false
2515 }
2516
2517 // Check if it is a first line break.
2518 if !leading_blanks {
2519 whitespaces = whitespaces[:0]
2520 leading_break = read_line(parser, leading_break)
2521 leading_blanks = true
2522 } else {
2523 trailing_breaks = read_line(parser, trailing_breaks)
2524 }
2525 }
2526 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2527 return false
2528 }
2529 }
2530
2531 // Join the whitespaces or fold line breaks.
2532 if leading_blanks {
2533 // Do we need to fold line breaks?
2534 if len(leading_break) > 0 && leading_break[0] == '\n' {
2535 if len(trailing_breaks) == 0 {
2536 s = append(s, ' ')
2537 } else {
2538 s = append(s, trailing_breaks...)
2539 }
2540 } else {
2541 s = append(s, leading_break...)
2542 s = append(s, trailing_breaks...)
2543 }
2544 trailing_breaks = trailing_breaks[:0]
2545 leading_break = leading_break[:0]
2546 } else {
2547 s = append(s, whitespaces...)
2548 whitespaces = whitespaces[:0]
2549 }
2550 }
2551
2552 // Eat the right quote.
2553 skip(parser)
2554 end_mark := parser.mark
2555
2556 // Create a token.
2557 *token = yaml_token_t{
2558 typ: yaml_SCALAR_TOKEN,
2559 start_mark: start_mark,
2560 end_mark: end_mark,
2561 value: s,
2562 style: yaml_SINGLE_QUOTED_SCALAR_STYLE,
2563 }
2564 if !single {
2565 token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2566 }
2567 return true
2568}
2569
2570// Scan a plain scalar.
2571func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) bool {
2572
2573 var s, leading_break, trailing_breaks, whitespaces []byte
2574 var leading_blanks bool
2575 var indent = parser.indent + 1
2576
2577 start_mark := parser.mark
2578 end_mark := parser.mark
2579
2580 // Consume the content of the plain scalar.
2581 for {
2582 // Check for a document indicator.
2583 if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
2584 return false
2585 }
2586 if parser.mark.column == 0 &&
2587 ((parser.buffer[parser.buffer_pos+0] == '-' &&
2588 parser.buffer[parser.buffer_pos+1] == '-' &&
2589 parser.buffer[parser.buffer_pos+2] == '-') ||
2590 (parser.buffer[parser.buffer_pos+0] == '.' &&
2591 parser.buffer[parser.buffer_pos+1] == '.' &&
2592 parser.buffer[parser.buffer_pos+2] == '.')) &&
2593 is_blankz(parser.buffer, parser.buffer_pos+3) {
2594 break
2595 }
2596
2597 // Check for a comment.
2598 if parser.buffer[parser.buffer_pos] == '#' {
2599 break
2600 }
2601
2602 // Consume non-blank characters.
2603 for !is_blankz(parser.buffer, parser.buffer_pos) {
2604
2605 // Check for indicators that may end a plain scalar.
2606 if (parser.buffer[parser.buffer_pos] == ':' && is_blankz(parser.buffer, parser.buffer_pos+1)) ||
2607 (parser.flow_level > 0 &&
2608 (parser.buffer[parser.buffer_pos] == ',' ||
2609 parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == '[' ||
2610 parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
2611 parser.buffer[parser.buffer_pos] == '}')) {
2612 break
2613 }
2614
2615 // Check if we need to join whitespaces and breaks.
2616 if leading_blanks || len(whitespaces) > 0 {
2617 if leading_blanks {
2618 // Do we need to fold line breaks?
2619 if leading_break[0] == '\n' {
2620 if len(trailing_breaks) == 0 {
2621 s = append(s, ' ')
2622 } else {
2623 s = append(s, trailing_breaks...)
2624 }
2625 } else {
2626 s = append(s, leading_break...)
2627 s = append(s, trailing_breaks...)
2628 }
2629 trailing_breaks = trailing_breaks[:0]
2630 leading_break = leading_break[:0]
2631 leading_blanks = false
2632 } else {
2633 s = append(s, whitespaces...)
2634 whitespaces = whitespaces[:0]
2635 }
2636 }
2637
2638 // Copy the character.
2639 s = read(parser, s)
2640
2641 end_mark = parser.mark
2642 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2643 return false
2644 }
2645 }
2646
2647 // Is it the end?
2648 if !(is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos)) {
2649 break
2650 }
2651
2652 // Consume blank characters.
2653 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2654 return false
2655 }
2656
2657 for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
2658 if is_blank(parser.buffer, parser.buffer_pos) {
2659
2660 // Check for tab characters that abuse indentation.
2661 if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) {
2662 yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
2663 start_mark, "found a tab character that violates indentation")
2664 return false
2665 }
2666
2667 // Consume a space or a tab character.
2668 if !leading_blanks {
2669 whitespaces = read(parser, whitespaces)
2670 } else {
2671 skip(parser)
2672 }
2673 } else {
2674 if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
2675 return false
2676 }
2677
2678 // Check if it is a first line break.
2679 if !leading_blanks {
2680 whitespaces = whitespaces[:0]
2681 leading_break = read_line(parser, leading_break)
2682 leading_blanks = true
2683 } else {
2684 trailing_breaks = read_line(parser, trailing_breaks)
2685 }
2686 }
2687 if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
2688 return false
2689 }
2690 }
2691
2692 // Check indentation level.
2693 if parser.flow_level == 0 && parser.mark.column < indent {
2694 break
2695 }
2696 }
2697
2698 // Create a token.
2699 *token = yaml_token_t{
2700 typ: yaml_SCALAR_TOKEN,
2701 start_mark: start_mark,
2702 end_mark: end_mark,
2703 value: s,
2704 style: yaml_PLAIN_SCALAR_STYLE,
2705 }
2706
2707 // Note that we change the 'simple_key_allowed' flag.
2708 if leading_blanks {
2709 parser.simple_key_allowed = true
2710 }
2711 return true
2712}