pkger/parser/decl.go

61 lines
943 B
Go
Raw Permalink Normal View History

2019-10-21 17:27:04 +03:00
package parser
import (
2019-11-05 23:04:38 +03:00
"fmt"
2019-10-21 17:27:04 +03:00
"go/token"
"sort"
)
type Decl interface {
File() (*File, error)
Position() (token.Position, error)
2019-10-21 17:27:04 +03:00
Value() (string, error)
}
type Filer interface {
2019-10-23 20:19:39 +03:00
Files(map[string]string) ([]*File, error)
}
type Virtualer interface {
VirtualPaths() []string
2019-10-21 17:27:04 +03:00
}
type Decls []Decl
func (decls Decls) Files() ([]*File, error) {
m := map[string]*File{}
2019-10-23 20:19:39 +03:00
v := map[string]string{}
2019-10-21 17:27:04 +03:00
for _, d := range decls {
2019-10-23 20:19:39 +03:00
if vt, ok := d.(Virtualer); ok {
for _, s := range vt.VirtualPaths() {
v[s] = s
}
}
2019-10-21 17:27:04 +03:00
fl, ok := d.(Filer)
if !ok {
continue
}
2019-10-23 20:19:39 +03:00
files, err := fl.Files(v)
2019-10-21 17:27:04 +03:00
if err != nil {
2019-12-03 23:29:17 +03:00
return nil, fmt.Errorf("%s: %s", err, d)
2019-10-21 17:27:04 +03:00
}
for _, f := range files {
2019-11-01 22:53:54 +03:00
m[f.Abs] = f
2019-11-05 23:04:38 +03:00
v[f.Abs] = f.Abs
2019-10-21 17:27:04 +03:00
}
}
var files []*File
for _, f := range m {
files = append(files, f)
}
sort.Slice(files, func(i, j int) bool {
return files[i].Path.String() < files[j].Path.String()
})
return files, nil
}