-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
126 lines (106 loc) · 2.9 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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
// XXX fix extensions to handle more than one
type Template struct {
Name string
Extensions string
Structures map[string]string
Types map[string]string
Names map[string]string
Comments string
Keywords map[string]string
Expressions map[string]string
Libraries map[string]string
}
func templateParse(templatePath string, flagBools []bool) map[string]*Template {
count := 0
files := getFiles(templatePath)
r, _ := regexp.Compile("^templates/\\w+\\.template$")
skip, _ := regexp.Compile("^templates/example\\.template$")
for _, file := range files {
// XXX
if r.MatchString(file) && !skip.MatchString(file) {
count++
if !flagBools[0] && flagBools[2] {
fmt.Println("found a specific template:", file)
}
}
}
// XXX fix handling count
templates := map[string]*Template{}
if !flagBools[0] && flagBools[2] {
fmt.Println("found a few templates:", count)
}
for _, file := range files {
// XXX
if r.MatchString(file) && !skip.MatchString(file) {
t := templateLoad(file, flagBools)
templates[t.Name] = t
}
}
return templates
}
func templateLoad(templateFile string, flagBools []bool) *Template {
t := new(Template)
nameparts := regexp.MustCompile("[/.]").Split(templateFile, -1)
t.Name = nameparts[1]
//fmt.Println("setting name to", nameparts[1], "from ", templateFile)
section := "unknown"
sectionr, _ := regexp.Compile("^[\\w\\s]+\\:$")
cleanr, _ := regexp.Compile("^\\s*")
//paramr, _ := regexp.Compile("^\\s+(\\w+)\\: (.*)$")
file, err := os.Open(templateFile)
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if sectionr.MatchString(scanner.Text()) {
section = scanner.Text()[:len(scanner.Text())-1]
if !flagBools[0] && flagBools[2] {
fmt.Println("found section", section, "from file", templateFile)
}
} else {
parts := regexp.MustCompile(": ").Split(scanner.Text(), 2)
param := cleanr.ReplaceAllString(parts[0], "")
if len(parts) > 1 {
if !flagBools[0] && flagBools[2] {
fmt.Println("adding content match", parts[1], "to param", param)
}
// XXX literal issue
if section == "names" {
if !flagBools[0] && flagBools[2] {
fmt.Println(colorize("found names section"))
}
if param == "comments" {
if !flagBools[0] && flagBools[2] {
fmt.Println("found comments section(s)", parts[1])
}
t.Comments = parts[1]
}
}
} else {
if len(param) > 0 {
if !flagBools[0] && flagBools[2] {
fmt.Println("adding single param", param, "to section", section)
}
// XXX literal issue here, ugly
if section == "extensions" {
t.Extensions = param
}
} else {
//fmt.Println("skipping blank line", param)
}
}
}
}
if err := scanner.Err(); err != nil {
check(err)
}
//fmt.Printf("template has this struct: %v\n", t)
return t
}