forked from project-chip/alchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
145 lines (134 loc) · 3.85 KB
/
global.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
package spec
import (
"iter"
"log/slog"
"github.com/project-chip/alchemy/asciidoc"
"github.com/project-chip/alchemy/internal/parse"
"github.com/project-chip/alchemy/matter"
"github.com/project-chip/alchemy/matter/types"
)
func addGlobalEntities(spec *Specification, doc *Doc) error {
globalEntities, err := doc.GlobalObjects()
if err != nil {
return err
}
for _, m := range globalEntities {
spec.DocRefs[m] = doc
switch m := m.(type) {
case *matter.Bitmap:
slog.Debug("Found global bitmap", "name", m.Name, "path", doc.Path)
_, ok := spec.bitmapIndex[m.Name]
if ok {
slog.Error("multiple bitmaps with same name", "name", m.Name)
} else {
spec.bitmapIndex[m.Name] = m
}
spec.addEntityByName(m.Name, m, nil)
case *matter.Enum:
slog.Debug("Found global enum", "name", m.Name, "path", doc.Path)
_, ok := spec.enumIndex[m.Name]
if ok {
slog.Error("multiple enums with same name", "name", m.Name)
} else {
spec.enumIndex[m.Name] = m
}
spec.addEntityByName(m.Name, m, nil)
case *matter.Struct:
slog.Debug("Found global struct", "name", m.Name, "path", doc.Path)
_, ok := spec.structIndex[m.Name]
if ok {
slog.Error("multiple structs with same name", "name", m.Name)
} else {
spec.structIndex[m.Name] = m
}
spec.addEntityByName(m.Name, m, nil)
case *matter.TypeDef:
slog.Debug("Found global typedef", "name", m.Name, "path", doc.Path)
_, ok := spec.typeDefIndex[m.Name]
if ok {
slog.Warn("multiple global typedefs with same name", "name", m.Name)
} else {
spec.typeDefIndex[m.Name] = m
}
spec.addEntityByName(m.Name, m, nil)
case *matter.Command:
_, ok := spec.commandIndex[m.Name]
if ok {
slog.Error("multiple commands with same name", "name", m.Name)
} else {
spec.commandIndex[m.Name] = m
}
spec.addEntityByName(m.Name, m, nil)
case *matter.Event:
_, ok := spec.eventIndex[m.Name]
if ok {
slog.Error("multiple events with same name", "name", m.Name)
} else {
spec.eventIndex[m.Name] = m
}
spec.addEntityByName(m.Name, m, nil)
}
spec.GlobalObjects[m] = struct{}{}
}
return nil
}
type globalCommandFactory struct {
commandFactory
}
func (cf *globalCommandFactory) Children(d *Doc, s *Section) iter.Seq[*Section] {
return func(yield func(*Section) bool) {
parse.Traverse(d, d.Elements(), func(sec *Section, parent parse.HasElements, index int) parse.SearchShould {
if s.SecType != matter.SectionCommand {
return parse.SearchShouldContinue
}
if !yield(s) {
return parse.SearchShouldStop
}
return parse.SearchShouldContinue
})
}
}
func (s *Section) toGlobalElements(d *Doc, entityMap map[asciidoc.Attributable][]types.Entity) (entities []types.Entity, err error) {
var commandsTable *asciidoc.Table
parse.SkimFunc(s.Elements(), func(t *asciidoc.Table) bool {
for _, a := range t.AttributeList.Attributes() {
switch a := a.(type) {
case *asciidoc.TitleAttribute:
if a.AsciiDocString() == "Global Commands" {
commandsTable = t
return true
}
}
}
return false
})
if commandsTable == nil {
return
}
var cf globalCommandFactory
var commands matter.CommandSet
commands, err = buildList(d, s, commandsTable, entityMap, commands, &cf)
for _, c := range commands {
entities = append(entities, c)
}
/*var commandMap map[string]*matter.Command
commands, _, err = s.buildCommands(d, commandsTable)
parse.Traverse(d, d.Elements(), func(sec *Section, parent parse.HasElements, index int) parse.SearchShould {
switch s.SecType {
case matter.SectionCommand:
var c *matter.Command
c, err := s.toCommand(d, commandMap, entityMap)
if err != nil {
return parse.SearchShouldContinue
}
if c != nil {
entityMap[s.Base] = append(entityMap[s.Base], c)
}
}
return parse.SearchShouldContinue
})
for _, c := range commands {
entities = append(entities, c)
}*/
return
}