-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
199 lines (147 loc) · 3.05 KB
/
parser.go
File metadata and controls
199 lines (147 loc) · 3.05 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// nolint: govet, golint
package parser
import (
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
const (
suffix = ".mint"
)
type Document struct {
Pos lexer.Position
Entries []*Entry `( @@ ";"* )*`
}
type Entry struct {
Pos lexer.Position
Type *Type ` @@`
Enum *Enum `| @@`
}
type Enum struct {
Pos lexer.Position
Name string `"enum" @Ident`
Values []*EnumEntry `"{" ( @@ ( ";" )* )* "}"`
}
func (e Enum) name() string {
return e.Name
}
func (e Enum) pos() lexer.Position {
return e.Pos
}
type EnumEntry struct {
Pos lexer.Position
Value *EnumValue ` @@`
}
type EnumValue struct {
Pos lexer.Position
Key string `@Ident`
}
type Type struct {
Pos lexer.Position
Name string `"type" @Ident`
Entries []*MessageEntry `"{" @@* "}"`
}
type MessageEntry struct {
Pos lexer.Position
Annotation *Annotation `@@`
Field *Field `| @@ ";"*`
}
type Annotation struct {
Pos lexer.Position
Provider string `"+" @Ident`
Type string `":" @Ident ":"`
Func string `( @Ident`
Value string `| @String )`
}
type Field struct {
Pos lexer.Position
DataType *DataType `@@`
Name string `@Ident`
Tag int `"=" @Int`
}
type DataType struct {
Pos lexer.Position
FixedSizeSlice *FixedSizedSliceType `@@`
Slice *SliceType `| @@`
Map *MapType `| @@`
Scalar *Scalar `| @@`
}
type MapType struct {
Pos lexer.Position
Key string `"map" "<" @Ident`
Value string `"," @Ident ">"`
}
type SliceType struct {
Pos lexer.Position
Type string `"[" "]" @Ident`
}
type FixedSizedSliceType struct {
Pos lexer.Position
Size int `"[" @Int "]"`
Type string `@Ident`
}
type Scalar struct {
Pos lexer.Position
Type string `@Ident`
}
var p = participle.MustBuild[Document](participle.UseLookahead(2), participle.Elide("Comment"), participle.Unquote())
func parse(fn string, in io.Reader) (*AST, error) {
d, err := p.Parse(fn, in)
if err != nil {
return nil, err
}
return toAST(*d)
}
func Parse(fn string, in io.Reader) (*AST, error) {
a, err := parse(fn, in)
if err != nil {
return nil, err
}
// Because the solver does some magic around uniqueness
// we need to call it here for a len 1 slice
return merge([]*AST{a})
}
func ParseFile(fn string) (*AST, error) {
// #nosec: G304
f, err := os.Open(fn)
if err != nil {
return nil, err
}
// #nosec: G307
defer f.Close()
return Parse(fn, f)
}
func ParseDir(dir string) (*AST, error) {
asts := make([]*AST, 0)
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if strings.HasSuffix(path, suffix) {
// #nosec: G304
f, err := os.Open(path)
if err != nil {
return err
}
// #nosec: G307
defer f.Close()
a, err := parse(path, f)
if err != nil {
return err
}
asts = append(asts, a)
}
return nil
})
if err != nil {
return nil, err
}
return merge(asts)
}