forked from bufbuild/protocompile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extended_syntax.go
46 lines (42 loc) · 1.06 KB
/
extended_syntax.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
package ast
import (
"fmt"
"sort"
)
var ExtendedSyntaxEnabled = true
func createCommaSeparatedNodes[T Node](
leadingNodes []Node,
nodes []T,
commas []*RuneNode,
trailingNodes []Node,
) []Node {
for i, node := range leadingNodes {
if node == nil {
panic(fmt.Sprintf("leadingNodes[%d] is nil", i))
}
}
for i, node := range trailingNodes {
if node == nil {
panic(fmt.Sprintf("trailingNodes[%d] is nil", i))
}
}
if !ExtendedSyntaxEnabled {
if len(commas) != len(nodes)-1 {
panic(fmt.Sprintf("%d nodes requires %d commas, not %d", len(nodes), len(nodes)-1, len(commas)))
}
}
children := make([]Node, 0, len(leadingNodes)+len(nodes)+len(commas)+len(trailingNodes))
children = append(children, leadingNodes...)
for _, node := range nodes {
children = append(children, node)
}
for _, comma := range commas {
children = append(children, comma)
}
off := len(leadingNodes)
sort.Slice(children[off:], func(i, j int) bool {
return children[off+i].Start() < children[off+j].Start()
})
children = append(children, trailingNodes...)
return children
}