forked from go-yaml/yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scannerc.go
2718 lines (2419 loc) · 75.7 KB
/
scannerc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package yaml
import (
"bytes"
"fmt"
)
// Introduction
// ************
//
// The following notes assume that you are familiar with the YAML specification
// (http://yaml.org/spec/1.2/spec.html). We mostly follow it, although in
// some cases we are less restrictive that it requires.
//
// The process of transforming a YAML stream into a sequence of events is
// divided on two steps: Scanning and Parsing.
//
// The Scanner transforms the input stream into a sequence of tokens, while the
// parser transform the sequence of tokens produced by the Scanner into a
// sequence of parsing events.
//
// The Scanner is rather clever and complicated. The Parser, on the contrary,
// is a straightforward implementation of a recursive-descendant parser (or,
// LL(1) parser, as it is usually called).
//
// Actually there are two issues of Scanning that might be called "clever", the
// rest is quite straightforward. The issues are "block collection start" and
// "simple keys". Both issues are explained below in details.
//
// Here the Scanning step is explained and implemented. We start with the list
// of all the tokens produced by the Scanner together with short descriptions.
//
// Now, tokens:
//
// STREAM-START(encoding) # The stream start.
// STREAM-END # The stream end.
// VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
// TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
// DOCUMENT-START # '---'
// DOCUMENT-END # '...'
// BLOCK-SEQUENCE-START # Indentation increase denoting a block
// BLOCK-MAPPING-START # sequence or a block mapping.
// BLOCK-END # Indentation decrease.
// FLOW-SEQUENCE-START # '['
// FLOW-SEQUENCE-END # ']'
// BLOCK-SEQUENCE-START # '{'
// BLOCK-SEQUENCE-END # '}'
// BLOCK-ENTRY # '-'
// FLOW-ENTRY # ','
// KEY # '?' or nothing (simple keys).
// VALUE # ':'
// ALIAS(anchor) # '*anchor'
// ANCHOR(anchor) # '&anchor'
// TAG(handle,suffix) # '!handle!suffix'
// SCALAR(value,style) # A scalar.
//
// The following two tokens are "virtual" tokens denoting the beginning and the
// end of the stream:
//
// STREAM-START(encoding)
// STREAM-END
//
// We pass the information about the input stream encoding with the
// STREAM-START token.
//
// The next two tokens are responsible for tags:
//
// VERSION-DIRECTIVE(major,minor)
// TAG-DIRECTIVE(handle,prefix)
//
// Example:
//
// %YAML 1.1
// %TAG ! !foo
// %TAG !yaml! tag:yaml.org,2002:
// ---
//
// The correspoding sequence of tokens:
//
// STREAM-START(utf-8)
// VERSION-DIRECTIVE(1,1)
// TAG-DIRECTIVE("!","!foo")
// TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
// DOCUMENT-START
// STREAM-END
//
// Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
// line.
//
// The document start and end indicators are represented by:
//
// DOCUMENT-START
// DOCUMENT-END
//
// Note that if a YAML stream contains an implicit document (without '---'
// and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
// produced.
//
// In the following examples, we present whole documents together with the
// produced tokens.
//
// 1. An implicit document:
//
// 'a scalar'
//
// Tokens:
//
// STREAM-START(utf-8)
// SCALAR("a scalar",single-quoted)
// STREAM-END
//
// 2. An explicit document:
//
// ---
// 'a scalar'
// ...
//
// Tokens:
//
// STREAM-START(utf-8)
// DOCUMENT-START
// SCALAR("a scalar",single-quoted)
// DOCUMENT-END
// STREAM-END
//
// 3. Several documents in a stream:
//
// 'a scalar'
// ---
// 'another scalar'
// ---
// 'yet another scalar'
//
// Tokens:
//
// STREAM-START(utf-8)
// SCALAR("a scalar",single-quoted)
// DOCUMENT-START
// SCALAR("another scalar",single-quoted)
// DOCUMENT-START
// SCALAR("yet another scalar",single-quoted)
// STREAM-END
//
// We have already introduced the SCALAR token above. The following tokens are
// used to describe aliases, anchors, tag, and scalars:
//
// ALIAS(anchor)
// ANCHOR(anchor)
// TAG(handle,suffix)
// SCALAR(value,style)
//
// The following series of examples illustrate the usage of these tokens:
//
// 1. A recursive sequence:
//
// &A [ *A ]
//
// Tokens:
//
// STREAM-START(utf-8)
// ANCHOR("A")
// FLOW-SEQUENCE-START
// ALIAS("A")
// FLOW-SEQUENCE-END
// STREAM-END
//
// 2. A tagged scalar:
//
// !!float "3.14" # A good approximation.
//
// Tokens:
//
// STREAM-START(utf-8)
// TAG("!!","float")
// SCALAR("3.14",double-quoted)
// STREAM-END
//
// 3. Various scalar styles:
//
// --- # Implicit empty plain scalars do not produce tokens.
// --- a plain scalar
// --- 'a single-quoted scalar'
// --- "a double-quoted scalar"
// --- |-
// a literal scalar
// --- >-
// a folded
// scalar
//
// Tokens:
//
// STREAM-START(utf-8)
// DOCUMENT-START
// DOCUMENT-START
// SCALAR("a plain scalar",plain)
// DOCUMENT-START
// SCALAR("a single-quoted scalar",single-quoted)
// DOCUMENT-START
// SCALAR("a double-quoted scalar",double-quoted)
// DOCUMENT-START
// SCALAR("a literal scalar",literal)
// DOCUMENT-START
// SCALAR("a folded scalar",folded)
// STREAM-END
//
// Now it's time to review collection-related tokens. We will start with
// flow collections:
//
// FLOW-SEQUENCE-START
// FLOW-SEQUENCE-END
// FLOW-MAPPING-START
// FLOW-MAPPING-END
// FLOW-ENTRY
// KEY
// VALUE
//
// The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
// FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
// correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
// indicators '?' and ':', which are used for denoting mapping keys and values,
// are represented by the KEY and VALUE tokens.
//
// The following examples show flow collections:
//
// 1. A flow sequence:
//
// [item 1, item 2, item 3]
//
// Tokens:
//
// STREAM-START(utf-8)
// FLOW-SEQUENCE-START
// SCALAR("item 1",plain)
// FLOW-ENTRY
// SCALAR("item 2",plain)
// FLOW-ENTRY
// SCALAR("item 3",plain)
// FLOW-SEQUENCE-END
// STREAM-END
//
// 2. A flow mapping:
//
// {
// a simple key: a value, # Note that the KEY token is produced.
// ? a complex key: another value,
// }
//
// Tokens:
//
// STREAM-START(utf-8)
// FLOW-MAPPING-START
// KEY
// SCALAR("a simple key",plain)
// VALUE
// SCALAR("a value",plain)
// FLOW-ENTRY
// KEY
// SCALAR("a complex key",plain)
// VALUE
// SCALAR("another value",plain)
// FLOW-ENTRY
// FLOW-MAPPING-END
// STREAM-END
//
// A simple key is a key which is not denoted by the '?' indicator. Note that
// the Scanner still produce the KEY token whenever it encounters a simple key.
//
// For scanning block collections, the following tokens are used (note that we
// repeat KEY and VALUE here):
//
// BLOCK-SEQUENCE-START
// BLOCK-MAPPING-START
// BLOCK-END
// BLOCK-ENTRY
// KEY
// VALUE
//
// The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
// increase that precedes a block collection (cf. the INDENT token in Python).
// The token BLOCK-END denote indentation decrease that ends a block collection
// (cf. the DEDENT token in Python). However YAML has some syntax pecularities
// that makes detections of these tokens more complex.
//
// The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
// '-', '?', and ':' correspondingly.
//
// The following examples show how the tokens BLOCK-SEQUENCE-START,
// BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
//
// 1. Block sequences:
//
// - item 1
// - item 2
// -
// - item 3.1
// - item 3.2
// -
// key 1: value 1
// key 2: value 2
//
// Tokens:
//
// STREAM-START(utf-8)
// BLOCK-SEQUENCE-START
// BLOCK-ENTRY
// SCALAR("item 1",plain)
// BLOCK-ENTRY
// SCALAR("item 2",plain)
// BLOCK-ENTRY
// BLOCK-SEQUENCE-START
// BLOCK-ENTRY
// SCALAR("item 3.1",plain)
// BLOCK-ENTRY
// SCALAR("item 3.2",plain)
// BLOCK-END
// BLOCK-ENTRY
// BLOCK-MAPPING-START
// KEY
// SCALAR("key 1",plain)
// VALUE
// SCALAR("value 1",plain)
// KEY
// SCALAR("key 2",plain)
// VALUE
// SCALAR("value 2",plain)
// BLOCK-END
// BLOCK-END
// STREAM-END
//
// 2. Block mappings:
//
// a simple key: a value # The KEY token is produced here.
// ? a complex key
// : another value
// a mapping:
// key 1: value 1
// key 2: value 2
// a sequence:
// - item 1
// - item 2
//
// Tokens:
//
// STREAM-START(utf-8)
// BLOCK-MAPPING-START
// KEY
// SCALAR("a simple key",plain)
// VALUE
// SCALAR("a value",plain)
// KEY
// SCALAR("a complex key",plain)
// VALUE
// SCALAR("another value",plain)
// KEY
// SCALAR("a mapping",plain)
// BLOCK-MAPPING-START
// KEY
// SCALAR("key 1",plain)
// VALUE
// SCALAR("value 1",plain)
// KEY
// SCALAR("key 2",plain)
// VALUE
// SCALAR("value 2",plain)
// BLOCK-END
// KEY
// SCALAR("a sequence",plain)
// VALUE
// BLOCK-SEQUENCE-START
// BLOCK-ENTRY
// SCALAR("item 1",plain)
// BLOCK-ENTRY
// SCALAR("item 2",plain)
// BLOCK-END
// BLOCK-END
// STREAM-END
//
// YAML does not always require to start a new block collection from a new
// line. If the current line contains only '-', '?', and ':' indicators, a new
// block collection may start at the current line. The following examples
// illustrate this case:
//
// 1. Collections in a sequence:
//
// - - item 1
// - item 2
// - key 1: value 1
// key 2: value 2
// - ? complex key
// : complex value
//
// Tokens:
//
// STREAM-START(utf-8)
// BLOCK-SEQUENCE-START
// BLOCK-ENTRY
// BLOCK-SEQUENCE-START
// BLOCK-ENTRY
// SCALAR("item 1",plain)
// BLOCK-ENTRY
// SCALAR("item 2",plain)
// BLOCK-END
// BLOCK-ENTRY
// BLOCK-MAPPING-START
// KEY
// SCALAR("key 1",plain)
// VALUE
// SCALAR("value 1",plain)
// KEY
// SCALAR("key 2",plain)
// VALUE
// SCALAR("value 2",plain)
// BLOCK-END
// BLOCK-ENTRY
// BLOCK-MAPPING-START
// KEY
// SCALAR("complex key")
// VALUE
// SCALAR("complex value")
// BLOCK-END
// BLOCK-END
// STREAM-END
//
// 2. Collections in a mapping:
//
// ? a sequence
// : - item 1
// - item 2
// ? a mapping
// : key 1: value 1
// key 2: value 2
//
// Tokens:
//
// STREAM-START(utf-8)
// BLOCK-MAPPING-START
// KEY
// SCALAR("a sequence",plain)
// VALUE
// BLOCK-SEQUENCE-START
// BLOCK-ENTRY
// SCALAR("item 1",plain)
// BLOCK-ENTRY
// SCALAR("item 2",plain)
// BLOCK-END
// KEY
// SCALAR("a mapping",plain)
// VALUE
// BLOCK-MAPPING-START
// KEY
// SCALAR("key 1",plain)
// VALUE
// SCALAR("value 1",plain)
// KEY
// SCALAR("key 2",plain)
// VALUE
// SCALAR("value 2",plain)
// BLOCK-END
// BLOCK-END
// STREAM-END
//
// YAML also permits non-indented sequences if they are included into a block
// mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
//
// key:
// - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
// - item 2
//
// Tokens:
//
// STREAM-START(utf-8)
// BLOCK-MAPPING-START
// KEY
// SCALAR("key",plain)
// VALUE
// BLOCK-ENTRY
// SCALAR("item 1",plain)
// BLOCK-ENTRY
// SCALAR("item 2",plain)
// BLOCK-END
//
// Ensure that the buffer contains the required number of characters.
// Return true on success, false on failure (reader error or memory error).
func cache(parser *yaml_parser_t, length int) bool {
// [Go] This was inlined: !cache(A, B) -> unread < B && !update(A, B)
return parser.unread >= length || yaml_parser_update_buffer(parser, length)
}
// Advance the buffer pointer.
func skip(parser *yaml_parser_t) {
parser.mark.index++
parser.mark.column++
parser.unread--
parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
}
func skip_line(parser *yaml_parser_t) {
if is_crlf(parser.buffer, parser.buffer_pos) {
parser.mark.index += 2
parser.mark.column = 0
parser.mark.line++
parser.unread -= 2
parser.buffer_pos += 2
} else if is_break(parser.buffer, parser.buffer_pos) {
parser.mark.index++
parser.mark.column = 0
parser.mark.line++
parser.unread--
parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
}
}
// Copy a character to a string buffer and advance pointers.
func read(parser *yaml_parser_t, s []byte) []byte {
w := width(parser.buffer[parser.buffer_pos])
if w == 0 {
panic("invalid character sequence")
}
if len(s) == 0 {
s = make([]byte, 0, 32)
}
if w == 1 && len(s)+w <= cap(s) {
s = s[:len(s)+1]
s[len(s)-1] = parser.buffer[parser.buffer_pos]
parser.buffer_pos++
} else {
s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...)
parser.buffer_pos += w
}
parser.mark.index++
parser.mark.column++
parser.unread--
return s
}
// Copy a line break character to a string buffer and advance pointers.
func read_line(parser *yaml_parser_t, s []byte) []byte {
buf := parser.buffer
pos := parser.buffer_pos
switch {
case buf[pos] == '\r' && buf[pos+1] == '\n':
// CR LF . LF
s = append(s, '\n')
parser.buffer_pos += 2
parser.mark.index++
parser.unread--
case buf[pos] == '\r' || buf[pos] == '\n':
// CR|LF . LF
s = append(s, '\n')
parser.buffer_pos += 1
case buf[pos] == '\xC2' && buf[pos+1] == '\x85':
// NEL . LF
s = append(s, '\n')
parser.buffer_pos += 2
case buf[pos] == '\xE2' && buf[pos+1] == '\x80' && (buf[pos+2] == '\xA8' || buf[pos+2] == '\xA9'):
// LS|PS . LS|PS
s = append(s, buf[parser.buffer_pos:pos+3]...)
parser.buffer_pos += 3
default:
return s
}
parser.mark.index++
parser.mark.column = 0
parser.mark.line++
parser.unread--
return s
}
// Get the next token.
func yaml_parser_scan(parser *yaml_parser_t, token *yaml_token_t) bool {
// Erase the token object.
*token = yaml_token_t{} // [Go] Is this necessary?
// No tokens after STREAM-END or error.
if parser.stream_end_produced || parser.error != yaml_NO_ERROR {
return true
}
// Ensure that the tokens queue contains enough tokens.
if !parser.token_available {
if !yaml_parser_fetch_more_tokens(parser) {
return false
}
}
// Fetch the next token from the queue.
*token = parser.tokens[parser.tokens_head]
parser.tokens_head++
parser.tokens_parsed++
parser.token_available = false
if token.typ == yaml_STREAM_END_TOKEN {
parser.stream_end_produced = true
}
return true
}
// Set the scanner error and return false.
func yaml_parser_set_scanner_error(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string) bool {
parser.error = yaml_SCANNER_ERROR
parser.context = context
parser.context_mark = context_mark
parser.problem = problem
parser.problem_mark = parser.mark
return false
}
func yaml_parser_set_scanner_tag_error(parser *yaml_parser_t, directive bool, context_mark yaml_mark_t, problem string) bool {
context := "while parsing a tag"
if directive {
context = "while parsing a %TAG directive"
}
return yaml_parser_set_scanner_error(parser, context, context_mark, problem)
}
func trace(args ...interface{}) func() {
pargs := append([]interface{}{"+++"}, args...)
fmt.Println(pargs...)
pargs = append([]interface{}{"---"}, args...)
return func() { fmt.Println(pargs...) }
}
// Ensure that the tokens queue contains at least one token which can be
// returned to the Parser.
func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
// While we need more tokens to fetch, do it.
for {
// Check if we really need to fetch more tokens.
need_more_tokens := false
if parser.tokens_head == len(parser.tokens) {
// Queue is empty.
need_more_tokens = true
} else {
// Check if any potential simple key may occupy the head position.
for i := len(parser.simple_keys) - 1; i >= 0; i-- {
simple_key := &parser.simple_keys[i]
if simple_key.token_number < parser.tokens_parsed {
break
}
if valid, ok := yaml_simple_key_is_valid(parser, simple_key); !ok {
return false
} else if valid && simple_key.token_number == parser.tokens_parsed {
need_more_tokens = true
break
}
}
}
// We are finished.
if !need_more_tokens {
break
}
// Fetch the next token.
if !yaml_parser_fetch_next_token(parser) {
return false
}
}
parser.token_available = true
return true
}
// The dispatcher for token fetchers.
func yaml_parser_fetch_next_token(parser *yaml_parser_t) bool {
// Ensure that the buffer is initialized.
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
return false
}
// Check if we just started scanning. Fetch STREAM-START then.
if !parser.stream_start_produced {
return yaml_parser_fetch_stream_start(parser)
}
// Eat whitespaces and comments until we reach the next token.
if !yaml_parser_scan_to_next_token(parser) {
return false
}
// Check the indentation level against the current column.
if !yaml_parser_unroll_indent(parser, parser.mark.column) {
return false
}
// Ensure that the buffer contains at least 4 characters. 4 is the length
// of the longest indicators ('--- ' and '... ').
if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
return false
}
// Is it the end of the stream?
if is_z(parser.buffer, parser.buffer_pos) {
return yaml_parser_fetch_stream_end(parser)
}
// Is it a directive?
if parser.mark.column == 0 && parser.buffer[parser.buffer_pos] == '%' {
return yaml_parser_fetch_directive(parser)
}
buf := parser.buffer
pos := parser.buffer_pos
// Is it the document start indicator?
if parser.mark.column == 0 && buf[pos] == '-' && buf[pos+1] == '-' && buf[pos+2] == '-' && is_blankz(buf, pos+3) {
return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_START_TOKEN)
}
// Is it the document end indicator?
if parser.mark.column == 0 && buf[pos] == '.' && buf[pos+1] == '.' && buf[pos+2] == '.' && is_blankz(buf, pos+3) {
return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_END_TOKEN)
}
// Is it the flow sequence start indicator?
if buf[pos] == '[' {
return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_SEQUENCE_START_TOKEN)
}
// Is it the flow mapping start indicator?
if parser.buffer[parser.buffer_pos] == '{' {
return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_MAPPING_START_TOKEN)
}
// Is it the flow sequence end indicator?
if parser.buffer[parser.buffer_pos] == ']' {
return yaml_parser_fetch_flow_collection_end(parser,
yaml_FLOW_SEQUENCE_END_TOKEN)
}
// Is it the flow mapping end indicator?
if parser.buffer[parser.buffer_pos] == '}' {
return yaml_parser_fetch_flow_collection_end(parser,
yaml_FLOW_MAPPING_END_TOKEN)
}
// Is it the flow entry indicator?
if parser.buffer[parser.buffer_pos] == ',' {
return yaml_parser_fetch_flow_entry(parser)
}
// Is it the block entry indicator?
if parser.buffer[parser.buffer_pos] == '-' && is_blankz(parser.buffer, parser.buffer_pos+1) {
return yaml_parser_fetch_block_entry(parser)
}
// Is it the key indicator?
if parser.buffer[parser.buffer_pos] == '?' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
return yaml_parser_fetch_key(parser)
}
// Is it the value indicator?
if parser.buffer[parser.buffer_pos] == ':' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
return yaml_parser_fetch_value(parser)
}
// Is it an alias?
if parser.buffer[parser.buffer_pos] == '*' {
return yaml_parser_fetch_anchor(parser, yaml_ALIAS_TOKEN)
}
// Is it an anchor?
if parser.buffer[parser.buffer_pos] == '&' {
return yaml_parser_fetch_anchor(parser, yaml_ANCHOR_TOKEN)
}
// Is it a tag?
if parser.buffer[parser.buffer_pos] == '!' {
return yaml_parser_fetch_tag(parser)
}
// Is it a literal scalar?
if parser.buffer[parser.buffer_pos] == '|' && parser.flow_level == 0 {
return yaml_parser_fetch_block_scalar(parser, true)
}
// Is it a folded scalar?
if parser.buffer[parser.buffer_pos] == '>' && parser.flow_level == 0 {
return yaml_parser_fetch_block_scalar(parser, false)
}
// Is it a single-quoted scalar?
if parser.buffer[parser.buffer_pos] == '\'' {
return yaml_parser_fetch_flow_scalar(parser, true)
}
// Is it a double-quoted scalar?
if parser.buffer[parser.buffer_pos] == '"' {
return yaml_parser_fetch_flow_scalar(parser, false)
}
// Is it a plain scalar?
//
// A plain scalar may start with any non-blank characters except
//
// '-', '?', ':', ',', '[', ']', '{', '}',
// '#', '&', '*', '!', '|', '>', '\'', '\"',
// '%', '@', '`'.
//
// In the block context (and, for the '-' indicator, in the flow context
// too), it may also start with the characters
//
// '-', '?', ':'
//
// if it is followed by a non-space character.
//
// The last rule is more restrictive than the specification requires.
// [Go] Make this logic more reasonable.
//switch parser.buffer[parser.buffer_pos] {
//case '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`':
//}
if !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '-' ||
parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':' ||
parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '[' ||
parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
parser.buffer[parser.buffer_pos] == '}' || parser.buffer[parser.buffer_pos] == '#' ||
parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '*' ||
parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '|' ||
parser.buffer[parser.buffer_pos] == '>' || parser.buffer[parser.buffer_pos] == '\'' ||
parser.buffer[parser.buffer_pos] == '"' || parser.buffer[parser.buffer_pos] == '%' ||
parser.buffer[parser.buffer_pos] == '@' || parser.buffer[parser.buffer_pos] == '`') ||
(parser.buffer[parser.buffer_pos] == '-' && !is_blank(parser.buffer, parser.buffer_pos+1)) ||
(parser.flow_level == 0 &&
(parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':') &&
!is_blankz(parser.buffer, parser.buffer_pos+1)) {
return yaml_parser_fetch_plain_scalar(parser)
}
// If we don't determine the token type so far, it is an error.
return yaml_parser_set_scanner_error(parser,
"while scanning for the next token", parser.mark,
"found character that cannot start any token")
}
func yaml_simple_key_is_valid(parser *yaml_parser_t, simple_key *yaml_simple_key_t) (valid, ok bool) {
if !simple_key.possible {
return false, true
}
// The 1.2 specification says:
//
// "If the ? indicator is omitted, parsing needs to see past the
// implicit key to recognize it as such. To limit the amount of
// lookahead required, the “:” indicator must appear at most 1024
// Unicode characters beyond the start of the key. In addition, the key
// is restricted to a single line."
//
if simple_key.mark.line < parser.mark.line || simple_key.mark.index+1024 < parser.mark.index {
// Check if the potential simple key to be removed is required.
if simple_key.required {
return false, yaml_parser_set_scanner_error(parser,
"while scanning a simple key", simple_key.mark,
"could not find expected ':'")
}
simple_key.possible = false
return false, true
}
return true, true
}
// Check if a simple key may start at the current position and add it if
// needed.
func yaml_parser_save_simple_key(parser *yaml_parser_t) bool {
// A simple key is required at the current position if the scanner is in
// the block context and the current column coincides with the indentation
// level.
required := parser.flow_level == 0 && parser.indent == parser.mark.column
//
// If the current position may start a simple key, save it.
//
if parser.simple_key_allowed {
simple_key := yaml_simple_key_t{
possible: true,
required: required,
token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
mark: parser.mark,
}
if !yaml_parser_remove_simple_key(parser) {
return false
}
parser.simple_keys[len(parser.simple_keys)-1] = simple_key
}
return true
}
// Remove a potential simple key at the current flow level.
func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
i := len(parser.simple_keys) - 1
if parser.simple_keys[i].possible {
// If the key is required, it is an error.
if parser.simple_keys[i].required {
return yaml_parser_set_scanner_error(parser,
"while scanning a simple key", parser.simple_keys[i].mark,
"could not find expected ':'")
}
}
// Remove the key from the stack.
parser.simple_keys[i].possible = false
return true
}
// max_flow_level limits the flow_level
const max_flow_level = 10000
// Increase the flow level and resize the simple key list if needed.
func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
// Reset the simple key on the next level.
parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{
possible: false,
required: false,
token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
mark: parser.mark,
})
// Increase the flow level.
parser.flow_level++
if parser.flow_level > max_flow_level {
return yaml_parser_set_scanner_error(parser,
"while increasing flow level", parser.simple_keys[len(parser.simple_keys)-1].mark,
fmt.Sprintf("exceeded max depth of %d", max_flow_level))
}
return true
}
// Decrease the flow level.
func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
if parser.flow_level > 0 {
parser.flow_level--
parser.simple_keys = parser.simple_keys[:len(parser.simple_keys)-1]
}
return true
}
// max_indents limits the indents stack size
const max_indents = 10000
// Push the current indentation level to the stack and set the new level
// the current column is greater than the indentation level. In this case,
// append or insert the specified token into the token queue.
func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool {
// In the flow context, do nothing.
if parser.flow_level > 0 {
return true
}
if parser.indent < column {
// Push the current indentation level to the stack and set the new
// indentation level.
parser.indents = append(parser.indents, parser.indent)
parser.indent = column
if len(parser.indents) > max_indents {
return yaml_parser_set_scanner_error(parser,
"while increasing indent level", parser.simple_keys[len(parser.simple_keys)-1].mark,
fmt.Sprintf("exceeded max depth of %d", max_indents))
}
// Create a token and insert it into the queue.
token := yaml_token_t{
typ: typ,
start_mark: mark,
end_mark: mark,
}
if number > -1 {
number -= parser.tokens_parsed
}
yaml_insert_token(parser, number, &token)
}
return true
}
// Pop indentation levels from the indents stack until the current level
// becomes less or equal to the column. For each indentation level, append
// the BLOCK-END token.
func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool {
// In the flow context, do nothing.
if parser.flow_level > 0 {
return true
}
// Loop through the indentation levels in the stack.
for parser.indent > column {
// Create a token and append it to the queue.
token := yaml_token_t{
typ: yaml_BLOCK_END_TOKEN,
start_mark: parser.mark,
end_mark: parser.mark,
}
yaml_insert_token(parser, -1, &token)
// Pop the indentation level.
parser.indent = parser.indents[len(parser.indents)-1]
parser.indents = parser.indents[:len(parser.indents)-1]
}
return true
}