/
pants.go
119 lines (105 loc) · 2.55 KB
/
pants.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
package pyast
import (
"fmt"
log "github.com/sirupsen/logrus"
file "github.com/nicois/file"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Pants struct {
Directives []*Directive `@@*`
}
func (p *Pants) Overrides() []string {
result := []string{}
if p.Directives == nil {
return result
}
for _, d := range p.Directives {
if d.Name == "python_sources" {
for _, a := range d.Args {
if a.Name == "overrides" {
for _, o := range a.Value.Dict.DictElements {
result = append(result, o.Name)
}
}
}
}
}
return result
}
var graphQLLexer = lexer.MustSimple([]lexer.SimpleRule{
{"Comment", `(?:#)[^\n]*\n?`},
{"Ident", `[a-zA-Z\-_0-9]+`},
{"Punctuation", `[()=,\[\]:{}]`},
{"Text", `"[^"]*"`},
{"Whitespace", `[ \t\n\r]+`},
})
var parser = participle.MustBuild[Pants](
participle.Lexer(graphQLLexer),
participle.Elide("Comment", "Whitespace"),
participle.Unquote("Text"),
participle.UseLookahead(2),
)
func ParsePants(path string) (*Pants, error) {
b, err := file.ReadBytes(path)
if err != nil {
return nil, err
}
p, err := parser.ParseString(path, string(b))
if err != nil {
l, lerr :=graphQLLexer.LexString(path, string(b))
if lerr != nil {
return nil, lerr
}
for {
next, nerr := l.Next()
if nerr != nil {
return nil, nerr
}
if next.EOF() {
break
}
log.Info(next.GoString())
}
return nil, err
}
return p, nil
}
type Directive struct {
Name string `@Ident`
Args []*Arg `( "(" ( @@ ","? )+ ")" | "(" ")" )`
}
type Arg struct {
Name string `@Ident "="`
Value *Value `@@`
}
type Dict struct {
DictElements []*DictElement `( "{" ( @@ ","? )+ "}" | "{}" )`
}
type DictElement struct {
Name string `@Text ":"`
Value *Value `@@ ","?`
}
type List struct {
ListElement []*ListElement `( "[" ( @@ ","? )+ "]" | "[" "]" )`
}
type ListElement struct {
Value *Value `@@`
}
type Value struct {
String *string `@Text`
Dict *Dict `| @@`
List *List `| @@`
}
func (v Value) Repr() string {
if v.String != nil {
return fmt.Sprintf("%v [string]", *(v.String))
}
if v.Dict != nil {
return fmt.Sprintf("%v [dict]", *(v.Dict))
}
if v.List != nil {
return fmt.Sprintf("%v [list]", *(v.List))
}
return "[no value]"
}