-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.go
73 lines (66 loc) · 1.61 KB
/
parse.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
package thriftlint
import (
"io"
"os"
"path/filepath"
"github.com/alecthomas/go-thrift/parser"
)
// Parse a set of .thrift source files into their corresponding ASTs.
func Parse(includeDirs []string, sources []string) (map[string]*parser.Thrift, error) {
p := parser.New()
p.Filesystem = &includeFilesystem{IncludeDirs: includeDirs}
var files map[string]*parser.Thrift
for _, path := range sources {
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
files, _, err = p.ParseFile(path)
if err != nil {
return nil, err
}
}
for _, file := range files {
file.Imports = map[string]*parser.Thrift{}
for symbol, path := range file.Includes {
file.Imports[symbol] = files[path]
}
}
return files, nil
}
// A go-thrift/parser.Filesystem implementation that searches include dirs when attempting to open
// sources.
type includeFilesystem struct {
IncludeDirs []string
}
func (i *includeFilesystem) Open(filename string) (io.ReadCloser, error) {
if filepath.IsAbs(filename) {
return os.Open(filename)
}
for _, d := range i.IncludeDirs {
path := filepath.Join(d, filename)
r, err := os.Open(path)
if os.IsNotExist(err) {
continue
} else if err != nil {
return nil, err
}
return r, nil
}
return nil, os.ErrNotExist
}
func (i *includeFilesystem) Abs(dir, path string) (string, error) {
if filepath.IsAbs(path) {
return path, nil
}
for _, d := range i.IncludeDirs {
p, err := filepath.Abs(filepath.Join(d, path))
if err != nil {
continue
}
if _, err := os.Stat(p); err == nil {
return filepath.Abs(p)
}
}
return filepath.Abs(filepath.Join(dir, path))
}