-
Notifications
You must be signed in to change notification settings - Fork 4
/
transforms.go
55 lines (50 loc) · 1.2 KB
/
transforms.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
package denada
import "fmt"
var importGrammar = `
import(file="$string", recursive?="$bool") "import*";
`
func ImportTransform(root ElementList) (ret ElementList, err error) {
g, err := ParseString(importGrammar)
if err != nil {
err = fmt.Errorf("Error parsing import statement grammar: %v", err)
return
}
ret = ElementList{}
for _, e := range root {
match := matchElement(e, g[0], g[0].Contents, false, "", "", NullContext())
if match == nil {
file := e.Modifications["file"].MustString()
insert, err := ParseFile(file)
if err != nil {
return nil, fmt.Errorf("Error parsing import contents from %s: %v", file, err)
}
rval, present := e.Modifications["recursive"]
recursive := false
if present {
recursive = rval.MustBool()
}
if recursive {
insert, err = ImportTransform(insert)
if err != nil {
return nil, err
}
}
for _, i := range insert {
ret = append(ret, i)
}
} else {
if e.IsDefinition() {
newchildren, err := ImportTransform(e.Contents)
if err != nil {
return nil, err
}
newe := e.Clone()
newe.Contents = newchildren
ret = append(ret, newe)
} else {
ret = append(ret, e)
}
}
}
return ret, nil
}