Repository navigation
Expand file tree
/
Copy pathnesting.go
More file actions
165 lines (138 loc) · 4.36 KB
/
Copy pathnesting.go
File metadata and controls
165 lines (138 loc) · 4.36 KB
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
package publiccode
import (
"github.com/goccy/go-yaml/token"
)
// maxNestingDepth is how many collections a document may nest. The YAML
// parser uses memory quadratic in the depth, so a few hundred kilobytes
// of brackets take gigabytes before any check on the AST can run.
// publiccode.yml keys nest a handful of levels.
const maxNestingDepth = 100
// findNestingOverflow returns the token that opens a collection past
// maxNestingDepth levels, or nil. It works on the tokens, before the
// parser builds the AST.
//
// Block collections track their column and kind: an indentless sequence
// can share its column with its parent mapping. Flow collections count
// their brackets and implicit mappings inside sequences.
func findNestingOverflow(tokens token.Tokens) *token.Token {
var block []blockCollection
flow := 0
var implicit []bool
starts := implicitMappingStarts(tokens)
for i, tk := range tokens {
opens := false
if starts[i] {
implicit[len(implicit)-1] = true
flow++
if len(block)+flow > maxNestingDepth {
return tk
}
}
switch tk.Type { //nolint:exhaustive // other tokens don't nest.
case token.DocumentHeaderType:
block = block[:0]
flow = 0
implicit = implicit[:0]
case token.SequenceStartType, token.MappingStartType:
flow++
implicit = append(implicit, false)
opens = true
case token.SequenceEndType, token.MappingEndType:
if len(implicit) > 0 {
if implicit[len(implicit)-1] {
flow--
}
implicit = implicit[:len(implicit)-1]
}
flow = max(flow-1, 0)
case token.CollectEntryType:
if len(implicit) > 0 && implicit[len(implicit)-1] {
flow--
implicit[len(implicit)-1] = false
}
case token.SequenceEntryType, token.MappingKeyType:
if flow == 0 {
block = enterBlock(block, tk.Position.Column, tk.Type == token.SequenceEntryType)
opens = true
}
case token.MappingValueType:
if flow == 0 {
block = enterBlock(block, keyColumn(tokens, i), false)
opens = true
}
}
if opens && len(block)+flow > maxNestingDepth {
return tk
}
}
return nil
}
// keyColumn returns the column of the key before the mapping value
// indicator at tokens[i], including alias indicators, anchors and tags.
// It returns the indicator's own column when the key isn't on its line,
// as after an explicit "? key".
func keyColumn(tokens token.Tokens, i int) int {
value := tokens[i]
if i > 0 && tokens[i-1].Position.Line == value.Position.Line {
key := i - 1
for key > 0 && tokens[key-1].Position.Line == value.Position.Line {
switch {
case tokens[key-1].Type == token.TagType || tokens[key-1].Type == token.AliasType:
key--
case key > 1 && tokens[key-2].Type == token.AnchorType &&
tokens[key-2].Position.Line == value.Position.Line:
key -= 2
default:
return tokens[key].Position.Column
}
}
return tokens[key].Position.Column
}
return value.Position.Column
}
type blockCollection struct {
column int
sequence bool
}
func enterBlock(block []blockCollection, column int, sequence bool) []blockCollection {
for len(block) > 0 {
parent := block[len(block)-1]
if parent.column < column || (parent.column == column && !parent.sequence && sequence) {
break
}
block = block[:len(block)-1]
}
return append(block, blockCollection{column: column, sequence: sequence})
}
// implicitMappingStarts marks the first token of each flow sequence item
// containing a mapping. Looking ahead also counts the mapping around a
// collection used as its key, before traversing that key.
func implicitMappingStarts(tokens token.Tokens) map[int]bool {
type collection struct {
sequence bool
item int
}
var stack []collection
starts := make(map[int]bool)
for i, tk := range tokens {
switch tk.Type { //nolint:exhaustive // only flow delimiters and mapping indicators matter.
case token.DocumentHeaderType:
stack = stack[:0]
case token.SequenceStartType, token.MappingStartType:
stack = append(stack, collection{sequence: tk.Type == token.SequenceStartType, item: i + 1})
case token.SequenceEndType, token.MappingEndType:
if len(stack) > 0 {
stack = stack[:len(stack)-1]
}
case token.CollectEntryType:
if len(stack) > 0 {
stack[len(stack)-1].item = i + 1
}
case token.MappingKeyType, token.MappingValueType:
if len(stack) > 0 && stack[len(stack)-1].sequence {
starts[stack[len(stack)-1].item] = true
}
}
}
return starts
}