-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathyml.go
167 lines (145 loc) · 4.31 KB
/
yml.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
package main
import (
"math"
"regexp"
"slices"
"strings"
)
type Yml struct {
Copyright string `yaml:"copyright"`
Name string `yaml:"name"`
Doc string `yaml:"doc"`
EnumPrefix uint16 `yaml:"enum_prefix"`
Constants []Constant `yaml:"constants"`
Typedefs []Typedef `yaml:"typedefs"`
Enums []Enum `yaml:"enums"`
Bitflags []Bitflag `yaml:"bitflags"`
Structs []Struct `yaml:"structs"`
Callbacks []Callback `yaml:"callbacks"`
Functions []Function `yaml:"functions"`
Objects []Object `yaml:"objects"`
}
type Constant struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
Doc string `yaml:"doc"`
}
type Typedef struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Type string `yaml:"type"`
}
type Enum struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Entries []*EnumEntry `yaml:"entries"`
Extended bool `yaml:"extended"`
}
type EnumEntry struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Value string `yaml:"value"`
}
type Bitflag struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Entries []BitflagEntry `yaml:"entries"`
Extended bool `yaml:"extended"`
}
type BitflagEntry struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Value string `yaml:"value"`
ValueCombination []string `yaml:"value_combination"`
}
type PointerType string
const (
PointerTypeMutable PointerType = "mutable"
PointerTypeImmutable PointerType = "immutable"
)
type ParameterType struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Type string `yaml:"type"`
PassedWithOwnership *bool `yaml:"passed_with_ownership"`
Pointer PointerType `yaml:"pointer"`
Optional bool `yaml:"optional"`
Namespace string `yaml:"namespace"`
Default *string `yaml:"default"`
}
type Callback struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Style string `yaml:"style"`
Args []ParameterType `yaml:"args"`
}
type Function struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Returns *ParameterType `yaml:"returns"`
Callback *string `yaml:"callback"`
Args []ParameterType `yaml:"args"`
}
type Struct struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Doc string `yaml:"doc"`
FreeMembers bool `yaml:"free_members"`
Members []ParameterType `yaml:"members"`
}
type Object struct {
Name string `yaml:"name"`
Doc string `yaml:"doc"`
Methods []Function `yaml:"methods"`
Extended bool `yaml:"extended"`
Namespace string `yaml:"namespace"`
IsStruct bool `yaml:"-"`
}
var arrayTypeRegexp = regexp.MustCompile(`array<([a-zA-Z0-9._]+)>`)
func SortStructs(structs []Struct) {
type node struct {
visited bool
depth int
Struct
}
nodeMap := make(map[string]node, len(structs))
for _, s := range structs {
nodeMap[s.Name] = node{Struct: s}
}
slices.SortStableFunc(structs, func(a, b Struct) int {
return strings.Compare(PascalCase(a.Name), PascalCase(b.Name))
})
var computeDepth func(string) int
computeDepth = func(name string) int {
if nodeMap[name].visited {
return nodeMap[name].depth
}
maxDependentDepth := 0
for _, member := range nodeMap[name].Members {
if strings.HasPrefix(member.Type, "struct.") {
dependentDepth := computeDepth(strings.TrimPrefix(member.Type, "struct."))
maxDependentDepth = int(math.Max(float64(maxDependentDepth), float64(dependentDepth+1)))
} else {
matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) == 2 {
typ := matches[1]
if strings.HasPrefix(typ, "struct.") {
dependentDepth := computeDepth(strings.TrimPrefix(typ, "struct."))
maxDependentDepth = int(math.Max(float64(maxDependentDepth), float64(dependentDepth+1)))
}
}
}
}
node := nodeMap[name]
node.depth = maxDependentDepth
node.visited = true
nodeMap[name] = node
return maxDependentDepth
}
for _, s := range structs {
computeDepth(s.Name)
}
slices.SortStableFunc(structs, func(a, b Struct) int {
return nodeMap[a.Name].depth - nodeMap[b.Name].depth
})
}