-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
65 lines (53 loc) · 1.3 KB
/
doc.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
package ergotool
import (
"bufio"
"github.com/labstack/gommon/log"
"github.com/mlilley/go-sexpr"
)
type Doc struct {
root *sexpr.Sexpr
}
func NewDoc(r *bufio.Reader) (*Doc, error) {
s, err := sexpr.Parse(r)
if err != nil {
return nil, err
}
return &Doc{root: s}, nil
}
func (d *Doc) Root() *sexpr.Sexpr {
return d.root
}
func (d *Doc) EnumerateFootprints() (map[string]*Footprint, error) {
var err error
footprints := map[string]*Footprint{}
err = d.enumerateFootprints("footprint", &footprints)
if err != nil {
return nil, err
}
err = d.enumerateFootprints("module", &footprints)
if err != nil {
return nil, err
}
return footprints, nil
}
func (d *Doc) enumerateFootprints(name string, footprints *map[string]*Footprint) error {
sexprs := d.root.FindDirectChildrenByName(name)
for _, s := range sexprs {
fp, err := NewFootprintFromSexpr(s)
if err != nil {
l, c := s.Location()
log.Warnf("ignoring invalid footprint at Line %d, Col %d: %s", l, c, err.Error())
continue
}
if _, has := (*footprints)[fp.Ref().Value()]; has {
l, c := s.Location()
log.Warnf("ignoring footprint with duplicate ref '%s' at Line %d, Col %d", fp.Ref().Value(), l, c)
continue
}
(*footprints)[fp.Ref().Value()] = fp
}
return nil
}
func (d *Doc) String() string {
return d.root.String()
}