-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathstructural_tree_api.go
More file actions
304 lines (239 loc) · 7.33 KB
/
Copy pathstructural_tree_api.go
File metadata and controls
304 lines (239 loc) · 7.33 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package spec
import (
"errors"
"fmt"
"strings"
)
// specTree is an opaque handle for a parsed spec structure.
type specTree struct {
root *block
}
// sectionHandle refers to one section in a [specTree].
type sectionHandle struct {
block *block
tree *specTree
}
// mutateTree parses the spec, applies mutate, and validates the resulting tree
// before replacing [structuralSpec.rawLines]. Errors leave the spec unchanged.
func (s *structuralSpec) mutateTree(mutate func(*specTree) error) error {
root, err := parseTree(s.rawLines)
if err != nil {
return fmt.Errorf("parsing spec tree:\n%w", err)
}
tree := &specTree{root: root}
if err := mutate(tree); err != nil {
return err
}
lines := serializeTree(root)
if _, err := parseTree(lines); err != nil {
return fmt.Errorf("validating mutated spec tree:\n%w", err)
}
s.rawLines = lines
return nil
}
// inspectTree parses the spec and passes its structure to inspect without
// modifying [structuralSpec.rawLines].
func (s *structuralSpec) inspectTree(inspect func(*specTree) error) error {
root, err := parseTree(s.rawLines)
if err != nil {
return fmt.Errorf("parsing spec tree:\n%w", err)
}
return inspect(&specTree{root: root})
}
// Section returns the first section with name and pkg, or nil if it is absent.
func (t *specTree) Section(name, pkg string) *sectionHandle {
for _, section := range t.Sections(name, pkg) {
return section
}
return nil
}
// HasSection reports whether a section with name is present for any package.
func (t *specTree) HasSection(name string) bool {
found := false
walkBlocks(t.root, func(blk *block) bool {
if blk.Kind == sectionBlock && blk.Name == name {
found = true
}
return !found
})
return found
}
// Sections returns all matching sections in document order.
func (t *specTree) Sections(name, pkg string) []*sectionHandle {
var matches []*sectionHandle
walkBlocks(t.root, func(blk *block) bool {
if blk.Kind == sectionBlock && blk.Name == name && blk.Package == pkg {
matches = append(matches, §ionHandle{block: blk, tree: t})
}
return true
})
return matches
}
// SectionsByPackage returns every section associated with pkg in document order.
func (t *specTree) SectionsByPackage(pkg string) []*sectionHandle {
var matches []*sectionHandle
walkBlocks(t.root, func(blk *block) bool {
if blk.Kind == sectionBlock && blk.Package == pkg {
matches = append(matches, §ionHandle{block: blk, tree: t})
}
return true
})
return matches
}
// RemoveSections removes sections as one transaction.
func (t *specTree) RemoveSections(handles []*sectionHandle) error {
sections := make(map[*block]bool, len(handles))
for _, handle := range handles {
if handle == nil || handle.tree != t || handle.block == nil {
return errors.New("section handle does not belong to this spec tree")
}
if handle.block.Name == "" && handle.block.Package == "" {
return errors.New("cannot remove the global/preamble section")
}
sections[handle.block] = true
}
if err := validateSectionRemoval(t.root, sections); err != nil {
return err
}
removeSections(t.root, sections)
return nil
}
// Name returns the section keyword. The preamble has an empty name.
func (h *sectionHandle) Name() string {
return h.block.Name
}
// Package returns the section package qualifier.
func (h *sectionHandle) Package() string {
return h.block.Package
}
// AppendLines appends lines to the section's content.
func (h *sectionHandle) AppendLines(lines []string) {
if len(lines) == 0 {
return
}
h.block.Children = append(h.block.Children, &block{Kind: textBlock, Lines: lines})
}
// PrependLines inserts lines immediately after the section header.
func (h *sectionHandle) PrependLines(lines []string) {
if len(lines) == 0 {
return
}
child := &block{Kind: textBlock, Lines: lines}
h.block.Children = append([]*block{child}, h.block.Children...)
}
func walkBlocks(blk *block, visit func(*block) bool) {
if !visit(blk) {
return
}
for _, child := range blk.Children {
walkBlocks(child, visit)
}
if blk.Kind == conditionalBlock {
for _, child := range blk.Else {
walkBlocks(child, visit)
}
}
}
func removeSections(blk *block, removeSet map[*block]bool) {
blk.Children = removeSectionBlocks(blk.Children, removeSet)
if blk.Kind == conditionalBlock {
blk.Else = removeSectionBlocks(blk.Else, removeSet)
}
for _, child := range blk.Children {
removeSections(child, removeSet)
}
if blk.Kind == conditionalBlock {
for _, child := range blk.Else {
removeSections(child, removeSet)
}
}
}
func removeSectionBlocks(blocks []*block, removeSet map[*block]bool) []*block {
result := make([]*block, 0, len(blocks))
for _, blk := range blocks {
if !removeSet[blk] {
result = append(result, blk)
}
}
return result
}
func validateSectionRemoval(root *block, removeSet map[*block]bool) error {
return validateRemovalChildren(root.Children, removeSet, nil)
}
func validateRemovalChildren(children []*block, removeSet map[*block]bool, preceding *block) error {
for index, child := range children {
if child.Kind == sectionBlock {
preceding = child
continue
}
if child.Kind != conditionalBlock {
continue
}
if conditionalHasTextOrMacroContent(child) && containsSectionBlocks(child) {
if preceding != nil && removeSet[preceding] {
return fmt.Errorf("conditional block at %#q contains content belonging to the preceding section:\n%w",
child.Header, ErrConditionalSpansSections)
}
}
if wouldEmptySectionWrapper(child, removeSet) && index+1 < len(children) {
next := children[index+1]
if next.Kind == conditionalBlock && !containsSectionBlocks(next) && conditionalHasTextOrMacroContent(next) {
return fmt.Errorf("content in conditional block at %#q would be orphaned after removing the preceding section:\n%w",
next.Header, ErrConditionalSpansSections)
}
}
if err := validateRemovalChildren(child.Children, removeSet, preceding); err != nil {
return err
}
if err := validateRemovalChildren(child.Else, removeSet, preceding); err != nil {
return err
}
}
return nil
}
func conditionalHasTextOrMacroContent(conditional *block) bool {
return hasTextOrMacroContent(conditional.Children) || hasTextOrMacroContent(conditional.Else)
}
func hasTextOrMacroContent(blocks []*block) bool {
for _, blk := range blocks {
if blk.Kind == macroDefBlock {
return true
}
if blk.Kind == textBlock {
for _, line := range blk.Lines {
trimmed := strings.TrimSpace(line)
if trimmed != "" && !strings.HasPrefix(trimmed, "#") {
return true
}
}
}
if blk.Kind == conditionalBlock &&
(hasTextOrMacroContent(blk.Children) || hasTextOrMacroContent(blk.Else)) {
return true
}
}
return false
}
func wouldEmptySectionWrapper(conditional *block, removeSet map[*block]bool) bool {
if !containsSectionBlocks(conditional) {
return false
}
return !hasRemainingSection(conditional.Children, removeSet) &&
!hasRemainingSection(conditional.Else, removeSet)
}
func hasRemainingSection(blocks []*block, removeSet map[*block]bool) bool {
for _, blk := range blocks {
if blk.Kind == sectionBlock && !removeSet[blk] {
return true
}
if hasRemainingSection(blk.Children, removeSet) {
return true
}
if blk.Kind == conditionalBlock && hasRemainingSection(blk.Else, removeSet) {
return true
}
}
return false
}