-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
480 lines (440 loc) · 11.3 KB
/
parser.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package mmk
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer/stateful"
)
var Lex = stateful.Must(stateful.Rules{
"String": {
{"String", `"(\\"|[^"])*"`, nil},
},
"Any": {
{"Any", `[^'\s\\]+`, nil},
{"continue", `\\.*\n\s*`, nil},
},
"Root": {
{"comment", `#.*`, nil},
{"Colon", `:`, nil},
{"Var", "var", stateful.Push("Var")},
{"Include", `^<.*\n`, nil},
{"Ruletype", "ruletype", nil},
stateful.Include("String"),
{"Regex", `'(\\'|[^'])*'`, nil},
stateful.Include("Any"),
{"CmdLine", `^\t.*`, nil},
{"Newline", `\n`, nil},
{"whitespace", `[\t\f\r ]+`, nil}, // Rules starting with a lower-case letter are elided automatically.
},
"Var": {
{"Vname", "[a-zA-Z][a-zA-Z0-9_-]*", nil},
{"Newline", `\n`, stateful.Pop()},
{"Equal", "=", stateful.Push("Val")},
{"whitespace", `[\t\f\r ]+`, nil},
},
"Val": {
stateful.Include("String"),
stateful.Include("Any"),
{"whitespace", `[\t\f\r ]+`, nil},
{"Newline", `\n`, stateful.Push("Root")},
},
})
var Parser = participle.MustBuild(&File{}, participle.Lexer(Lex))
type File struct {
Source string
Vars []*Var
RuleTypes map[string]*RuleType
Directives []*Directive `(Newline* @@*)*`
}
type Directive struct {
Include string `@Include`
Rule *Rule `| @@`
Var *Var `| Var @@`
RuleType *RuleType `| Ruletype @@`
}
type RuleType struct {
RuleType *Elem `@@`
RuleSections []*RuleSection `(Newline? @@)*`
}
type Var struct {
Name string `@Vname`
Value []string `Equal @(Any | String | Vname)*`
}
type Rule struct {
Target []*Elem `@@*`
RuleSections []*RuleSection `@@*`
}
type RuleSection struct {
SecondPart []Elem `Colon @@*`
Colon string `@Colon?`
ThirdPart []Elem `@@* Newline`
Lines []string `(@CmdLine Newline?)*`
}
type Elems struct {
Elems []Elem `@@`
}
type Elem struct {
Any string `@(Any | String)`
//String string `| @String`
Regex string `| @Regex`
}
func (e *Elem) Raw() string {
if e.Any != "" {
return e.Any
} else {
return e.Regex
}
}
func (e *Elem) Combine(e2 *Elem) *Elem {
var (
isRegex bool
s1, s2 string
)
if e.Any == "" {
s1 = strings.Trim(e.Regex, `'`)
isRegex = true
} else {
s1 = strings.Trim(e.Any, `"`)
}
if e2.Any == "" {
s2 = strings.Trim(e2.Regex, `'`)
if !isRegex {
s1 = regexp.QuoteMeta(s1)
}
isRegex = true
} else {
s2 = strings.Trim(e2.Any, `"`)
}
fmt.Printf(" COMBINING [%s] with [%s]\n", s1, s2)
if isRegex {
return &Elem{Regex: s1 + s2}
} else {
return &Elem{Any: s1 + s2}
}
}
func (e *Elem) Expand(m map[string]string) {
if e.Any != "" {
e.Any = os.Expand(e.Any, func(s string) string {
return m[s]
})
}
}
func (e *Elem) Value() *Matcher {
if e.Any != "" {
return &Matcher{Str: strings.Trim(e.Any, `"`)}
} else {
return &Matcher{Regex: regexp.MustCompile("^" + strings.Trim(e.Regex, `'`) + "$")}
}
}
func combineExpandElems(es []*Elem, m map[string]string) *Elem {
var ret *Elem
for i, e := range es {
e.Expand(m)
if i == 0 {
ret = e
} else {
ret = ret.Combine(e)
}
}
return ret
}
type Matcher struct {
Str string
Regex *regexp.Regexp
}
func (m *Matcher) Captures(s string) []string {
if m.Str != "" {
return nil
}
return m.Regex.FindStringSubmatch(s)
}
func (m *Matcher) Matches(s string) bool {
//log.Printf("Checking (%s)%#v matches %s", m, m, s)
//defer func() { log.Printf("Returning %t", ret) }()
if m.Str != "" {
return m.Str == s
}
return m.Regex.MatchString(s)
}
func (m *Matcher) String() string {
if m.Str != "" {
return m.Str
}
return m.Regex.String()
}
type RuleSets struct {
Vars []*Var
RuleSets []*RuleSet
}
type RuleSet struct {
Target *Matcher
Bodies []*RuleBody
}
type RuleBody struct {
RuleType string
FailOK bool
Dependencies []string
Lines []string
}
func (r *RuleSet) SelectBody(ruleType string) *RuleBody {
if ruleType == "" {
if len(r.Bodies) > 0 {
return r.Bodies[0]
}
return nil
}
for _, body := range r.Bodies {
// TODO: Allow other ruleType selection methods
if body.RuleType == ruleType {
return body
}
}
return nil
}
func (r *RuleSets) Print() {
fmt.Printf("[Vars: \n")
for _, v := range r.Vars {
fmt.Printf("\t%s=%#v\n", v.Name, v.Value)
}
fmt.Printf("]\n")
for _, rs := range r.RuleSets {
fmt.Printf("[Target: %s]\n", rs.Target)
for _, body := range rs.Bodies {
fmt.Printf("\t [Type: %s] -> [Deps: %s]:\n", body.RuleType, strings.Join(body.Dependencies, ", "))
for _, line := range body.Lines {
fmt.Printf("\t\t%s\n", line)
}
}
}
}
func (r *RuleSets) RuleFor(target, ruleType string) *RuleSet {
for _, s := range r.RuleSets {
//log.Printf("Finding %s:%s Checking for target %s", target, ruleType, s.Target.String())
//log.Printf("TARGET MATCHES: %t, BODY(%s): %p", s.Target.Matches(target), ruleType, s.SelectBody(ruleType))
if s.Target.Matches(target) && s.SelectBody(ruleType) != nil {
//log.Printf("RETURNING %p", s)
return s
} else {
//log.Printf("RETURNING NIL!")
}
}
return nil
}
func parseFile(file string) (*File, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
ret := &File{}
err = Parser.Parse(file, bufio.NewReader(f), ret)
if err != nil {
return nil, err
}
ret.Source = file
sanitize(ret)
return ret, nil
}
func expand(f *File) error {
var newDirectives []*Directive
ruleTypes := make(map[string]*RuleType)
for _, directive := range f.Directives {
if directive.Include != "" {
incf, err := parseFile(directive.Include)
if err != nil {
return fmt.Errorf("In File %s: <%s: %s", f.Source, directive.Include, err)
}
err = expand(incf)
if err != nil {
return err
}
newDirectives = append(newDirectives, incf.Directives...)
for k, rt := range incf.RuleTypes {
ruleTypes[k] = rt
}
f.Vars = append(f.Vars, incf.Vars...)
} else if directive.Var != nil {
joined := strings.Join(directive.Var.Value, " ")
//joined := directive.Var.Value
if strings.HasPrefix(joined, "$(") && strings.HasSuffix(joined, ")") {
cmdBody := joined[2:]
cmdBody = cmdBody[:len(cmdBody)-1]
//log.Printf("CMD: %s\n", cmdBody)
cmd := exec.Command("bash", "-c", cmdBody)
//cmd.Stdin = strings.NewReader(addHeader(execBody, n.Target, n.Vars))
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
log.Printf("%s Error: %s", cmdBody, err)
directive.Var.Value = []string{""}
//directive.Var.Value = ""
} else {
directive.Var.Value = []string{strings.TrimSpace(string(output))}
//directive.Var.Value = strings.TrimSpace(string(output))
}
}
//log.Printf("Have Variable: %#v\n", directive.Var)
f.Vars = append(f.Vars, directive.Var)
} else if directive.RuleType != nil {
ruleTypes[directive.RuleType.RuleType.Value().String()] = directive.RuleType
//log.Printf("Have RuleType:")
//spew.Dump(directive.RuleType)
} else if directive.Rule != nil {
newDirectives = append(newDirectives, directive)
}
}
f.RuleTypes = ruleTypes
f.Directives = newDirectives
return nil
}
func sanitize(f *File) {
for _, directive := range f.Directives {
if directive.Include != "" {
directive.Include = strings.TrimSpace(directive.Include[1:])
}
if directive.Rule != nil {
for _, section := range directive.Rule.RuleSections {
for i := range section.Lines {
section.Lines[i] = strings.TrimSpace(section.Lines[i])
}
}
}
if directive.RuleType != nil {
for _, section := range directive.RuleType.RuleSections {
for i := range section.Lines {
section.Lines[i] = strings.TrimSpace(section.Lines[i])
}
}
}
}
}
func convert(f *File) (*RuleSets, error) {
err := expand(f)
if err != nil {
return nil, err
}
var sets []*RuleSet
for _, d := range f.Directives {
defaults := make(map[string]*RuleSet)
for ruleType, rt := range f.RuleTypes {
rs := &RuleSet{Bodies: make([]*RuleBody, 0)}
for _, s := range rt.RuleSections {
var rb RuleBody
var ruleTypes []string
// for ruletype definitions, second part is
// *always* the type, third part is optional dependencies.
for i := 0; i < len(s.SecondPart); i++ {
ruleTypes = append(ruleTypes, s.SecondPart[i].Value().String())
}
if s.Colon != "" {
// We are using a non-nil Dependencies to differentiate between unspecified and
// specified but empty.
rb.Dependencies = make([]string, 0)
}
for i := 0; i < len(s.ThirdPart); i++ {
rb.Dependencies = append(rb.Dependencies, s.ThirdPart[i].Raw())
}
for i, t := range ruleTypes {
if i == 0 {
rb.RuleType = t
}
if t == "failok" {
rb.FailOK = true
}
}
rb.Lines = s.Lines
rs.Bodies = append(rs.Bodies, &rb)
}
defaults[ruleType] = rs
}
vars := make(map[string]string)
for _, v := range f.Vars {
vars[v.Name] = strings.Join(v.Value, " ")
}
types := make(map[string]struct{})
rs := &RuleSet{Target: combineExpandElems(d.Rule.Target, vars).Value(), Bodies: make([]*RuleBody, 0)}
for i, s := range d.Rule.RuleSections {
var rb RuleBody
var ruleTypes []string
if i == 0 && s.Colon == "" {
// First rule section, if only second section is present, it is dependencies.
for i := 0; i < len(s.SecondPart); i++ {
rb.Dependencies = append(rb.Dependencies, s.SecondPart[i].Raw())
}
} else {
// Otherwise and in the remaining rule sections, second part is
// *always* the type, third part is optional dependencies.
for i := 0; i < len(s.SecondPart); i++ {
ruleTypes = append(ruleTypes, s.SecondPart[i].Value().String())
}
if s.Colon == "" {
// for subsequent rules, if no dep list is specified, inherit from the first rule.
rb.Dependencies = rs.Bodies[0].Dependencies
} else {
for i := 0; i < len(s.ThirdPart); i++ {
rb.Dependencies = append(rb.Dependencies, s.ThirdPart[i].Raw())
}
}
}
for i, t := range ruleTypes {
if i == 0 {
rb.RuleType = t
}
if t == "failok" {
rb.FailOK = true
}
}
//ruleTypes[0] //strings.Join(ruleTypes, " ")
if _, ok := types[rb.RuleType]; ok {
return nil, fmt.Errorf("Duplicate definition for target %s", combineExpandElems(d.Rule.Target, vars).Value())
}
types[rb.RuleType] = struct{}{}
rb.Lines = s.Lines
rs.Bodies = append(rs.Bodies, &rb)
}
// apply any defaults
var additional []*RuleBody
for i, body := range rs.Bodies {
if defaults, ok := defaults[body.RuleType]; ok {
for _, b := range defaults.Bodies {
if rs.Bodies[0].Dependencies != nil {
b.Dependencies = rs.Bodies[0].Dependencies
}
// if b.Dependencies == nil {
// b.Dependencies = rs.Bodies[0].Dependencies
// }
if len(body.Lines) == 0 && b.RuleType == body.RuleType {
rs.Bodies[i] = b
} else {
additional = append(additional, b)
}
}
}
}
for _, body := range additional {
if _, ok := types[body.RuleType]; ok {
continue
}
types[body.RuleType] = struct{}{}
rs.Bodies = append(rs.Bodies, body)
}
sets = append(sets, rs)
}
// sets are searched in reverse read order.
for i, j := 0, len(sets)-1; i < j; i, j = i+1, j-1 {
sets[i], sets[j] = sets[j], sets[i]
}
return &RuleSets{f.Vars, sets}, nil
}
func Parse(file string) (*RuleSets, error) {
f, err := parseFile(file)
if err != nil {
return nil, err
}
return convert(f)
}