|
@@ -626,31 +626,18 @@ func trace(args ...interface{}) func() {
|
626
|
626
|
func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
|
627
|
627
|
// While we need more tokens to fetch, do it.
|
628
|
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) {
|
|
629
|
+ if parser.tokens_head != len(parser.tokens) {
|
|
630
|
+ // If queue is non-empty, check if any potential simple key may
|
|
631
|
+ // occupy the head position.
|
|
632
|
+ head_tok_idx, ok := parser.simple_keys_by_tok[parser.tokens_parsed]
|
|
633
|
+ if !ok {
|
|
634
|
+ break
|
|
635
|
+ } else if valid, ok := yaml_simple_key_is_valid(parser, &parser.simple_keys[head_tok_idx]); !ok {
|
638
|
636
|
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
|
|
- }
|
|
637
|
+ } else if !valid {
|
|
638
|
+ break
|
647
|
639
|
}
|
648
|
640
|
}
|
649
|
|
-
|
650
|
|
- // We are finished.
|
651
|
|
- if !need_more_tokens {
|
652
|
|
- break
|
653
|
|
- }
|
654
|
641
|
// Fetch the next token.
|
655
|
642
|
if !yaml_parser_fetch_next_token(parser) {
|
656
|
643
|
return false
|
|
@@ -678,11 +665,6 @@ func yaml_parser_fetch_next_token(parser *yaml_parser_t) bool {
|
678
|
665
|
return false
|
679
|
666
|
}
|
680
|
667
|
|
681
|
|
- // Remove obsolete potential simple keys.
|
682
|
|
- if !yaml_parser_stale_simple_keys(parser) {
|
683
|
|
- return false
|
684
|
|
- }
|
685
|
|
-
|
686
|
668
|
// Check the indentation level against the current column.
|
687
|
669
|
if !yaml_parser_unroll_indent(parser, parser.mark.column) {
|
688
|
670
|
return false
|
|
@@ -837,29 +819,30 @@ func yaml_parser_fetch_next_token(parser *yaml_parser_t) bool {
|
837
|
819
|
"found character that cannot start any token")
|
838
|
820
|
}
|
839
|
821
|
|
840
|
|
-// Check the list of potential simple keys and remove the positions that
|
841
|
|
-// cannot contain simple keys anymore.
|
842
|
|
-func yaml_parser_stale_simple_keys(parser *yaml_parser_t) bool {
|
843
|
|
- // Check for a potential simple key for each flow level.
|
844
|
|
- for i := range parser.simple_keys {
|
845
|
|
- simple_key := &parser.simple_keys[i]
|
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
|
|
822
|
+func yaml_simple_key_is_valid(parser *yaml_parser_t, simple_key *yaml_simple_key_t) (valid, ok bool) {
|
|
823
|
+ if !simple_key.possible {
|
|
824
|
+ return false, true
|
|
825
|
+ }
|
|
826
|
+
|
|
827
|
+ // The 1.2 specification says:
|
|
828
|
+ //
|
|
829
|
+ // "If the ? indicator is omitted, parsing needs to see past the
|
|
830
|
+ // implicit key to recognize it as such. To limit the amount of
|
|
831
|
+ // lookahead required, the “:” indicator must appear at most 1024
|
|
832
|
+ // Unicode characters beyond the start of the key. In addition, the key
|
|
833
|
+ // is restricted to a single line."
|
|
834
|
+ //
|
|
835
|
+ if simple_key.mark.line < parser.mark.line || simple_key.mark.index+1024 < parser.mark.index {
|
|
836
|
+ // Check if the potential simple key to be removed is required.
|
|
837
|
+ if simple_key.required {
|
|
838
|
+ return false, yaml_parser_set_scanner_error(parser,
|
|
839
|
+ "while scanning a simple key", simple_key.mark,
|
|
840
|
+ "could not find expected ':'")
|
860
|
841
|
}
|
|
842
|
+ simple_key.possible = false
|
|
843
|
+ return false, true
|
861
|
844
|
}
|
862
|
|
- return true
|
|
845
|
+ return true, true
|
863
|
846
|
}
|
864
|
847
|
|
865
|
848
|
// Check if a simple key may start at the current position and add it if
|
|
@@ -879,13 +862,14 @@ func yaml_parser_save_simple_key(parser *yaml_parser_t) bool {
|
879
|
862
|
possible: true,
|
880
|
863
|
required: required,
|
881
|
864
|
token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
|
|
865
|
+ mark: parser.mark,
|
882
|
866
|
}
|
883
|
|
- simple_key.mark = parser.mark
|
884
|
867
|
|
885
|
868
|
if !yaml_parser_remove_simple_key(parser) {
|
886
|
869
|
return false
|
887
|
870
|
}
|
888
|
871
|
parser.simple_keys[len(parser.simple_keys)-1] = simple_key
|
|
872
|
+ parser.simple_keys_by_tok[simple_key.token_number] = len(parser.simple_keys) - 1
|
889
|
873
|
}
|
890
|
874
|
return true
|
891
|
875
|
}
|
|
@@ -900,9 +884,10 @@ func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
|
900
|
884
|
"while scanning a simple key", parser.simple_keys[i].mark,
|
901
|
885
|
"could not find expected ':'")
|
902
|
886
|
}
|
|
887
|
+ // Remove the key from the stack.
|
|
888
|
+ parser.simple_keys[i].possible = false
|
|
889
|
+ delete(parser.simple_keys_by_tok, parser.simple_keys[i].token_number)
|
903
|
890
|
}
|
904
|
|
- // Remove the key from the stack.
|
905
|
|
- parser.simple_keys[i].possible = false
|
906
|
891
|
return true
|
907
|
892
|
}
|
908
|
893
|
|
|
@@ -912,7 +897,12 @@ const max_flow_level = 10000
|
912
|
897
|
// Increase the flow level and resize the simple key list if needed.
|
913
|
898
|
func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
|
914
|
899
|
// Reset the simple key on the next level.
|
915
|
|
- parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
|
|
900
|
+ parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{
|
|
901
|
+ possible: false,
|
|
902
|
+ required: false,
|
|
903
|
+ token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
|
|
904
|
+ mark: parser.mark,
|
|
905
|
+ })
|
916
|
906
|
|
917
|
907
|
// Increase the flow level.
|
918
|
908
|
parser.flow_level++
|
|
@@ -928,7 +918,9 @@ func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
|
928
|
918
|
func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
|
929
|
919
|
if parser.flow_level > 0 {
|
930
|
920
|
parser.flow_level--
|
931
|
|
- parser.simple_keys = parser.simple_keys[:len(parser.simple_keys)-1]
|
|
921
|
+ last := len(parser.simple_keys) - 1
|
|
922
|
+ delete(parser.simple_keys_by_tok, parser.simple_keys[last].token_number)
|
|
923
|
+ parser.simple_keys = parser.simple_keys[:last]
|
932
|
924
|
}
|
933
|
925
|
return true
|
934
|
926
|
}
|
|
@@ -1005,6 +997,8 @@ func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
|
1005
|
997
|
// Initialize the simple key stack.
|
1006
|
998
|
parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
|
1007
|
999
|
|
|
1000
|
+ parser.simple_keys_by_tok = make(map[int]int)
|
|
1001
|
+
|
1008
|
1002
|
// A simple key is allowed at the beginning of the stream.
|
1009
|
1003
|
parser.simple_key_allowed = true
|
1010
|
1004
|
|
|
@@ -1286,7 +1280,11 @@ func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
|
1286
|
1280
|
simple_key := &parser.simple_keys[len(parser.simple_keys)-1]
|
1287
|
1281
|
|
1288
|
1282
|
// Have we found a simple key?
|
1289
|
|
- if simple_key.possible {
|
|
1283
|
+ if valid, ok := yaml_simple_key_is_valid(parser, simple_key); !ok {
|
|
1284
|
+ return false
|
|
1285
|
+
|
|
1286
|
+ } else if valid {
|
|
1287
|
+
|
1290
|
1288
|
// Create the KEY token and insert it into the queue.
|
1291
|
1289
|
token := yaml_token_t{
|
1292
|
1290
|
typ: yaml_KEY_TOKEN,
|
|
@@ -1304,6 +1302,7 @@ func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
|
1304
|
1302
|
|
1305
|
1303
|
// Remove the simple key.
|
1306
|
1304
|
simple_key.possible = false
|
|
1305
|
+ delete(parser.simple_keys_by_tok, simple_key.token_number)
|
1307
|
1306
|
|
1308
|
1307
|
// A simple key cannot follow another simple key.
|
1309
|
1308
|
parser.simple_key_allowed = false
|