13 type SingleRule struct {
18 type MultiRule struct {
24 // JoinRule takes a syntax rule (which can be multiple regular expressions)
25 // and joins it into one regular expression by ORing everything together
26 func JoinRule(rule string) string {
27 split := strings.Split(rule, `" "`)
28 joined := strings.Join(split, "|")
32 func parseFile(text, filename string) (filetype, syntax, header string, rules []interface{}) {
33 lines := strings.Split(text, "\n")
35 // Regex for parsing syntax statements
36 syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
37 // Regex for parsing header statements
38 headerParser := regexp.MustCompile(`header "(.*)"`)
40 // Regex for parsing standard syntax rules
41 ruleParser := regexp.MustCompile(`color (.*?)\s+(?:\((.+?)?\)\s+)?"(.*)"`)
42 // Regex for parsing syntax rules with start="..." end="..."
43 ruleStartEndParser := regexp.MustCompile(`color (.*?)\s+(?:\((.+?)?\)\s+)?start="(.*)"\s+end="(.*)"`)
45 for lineNum, line := range lines {
46 line = strings.TrimSpace(line)
50 if strings.HasPrefix(line, "#") {
53 if strings.HasPrefix(line, "syntax") {
54 syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
55 if len(syntaxMatches) == 3 {
56 filetype = string(syntaxMatches[1])
57 syntax = JoinRule(string(syntaxMatches[2]))
59 fmt.Println(filename, lineNum, "Syntax statement is not valid: "+line)
63 if strings.HasPrefix(line, "header") {
65 headerMatches := headerParser.FindSubmatch([]byte(line))
66 if len(headerMatches) == 2 {
67 header = JoinRule(string(headerMatches[1]))
69 fmt.Println(filename, lineNum, "Header statement is not valid: "+line)
74 // Syntax rule, but it could be standard or start-end
75 if ruleParser.MatchString(line) {
76 // Standard syntax rule
78 submatch := ruleParser.FindSubmatch([]byte(line))
82 if len(submatch) == 4 {
83 // If len is 4 then the user specified some additional flags to use
84 color = string(submatch[1])
85 flags = string(submatch[2])
87 regexStr = "(?" + flags + ")" + JoinRule(string(submatch[3]))
89 regexStr = JoinRule(string(submatch[3]))
91 } else if len(submatch) == 3 {
92 // If len is 3, no additional flags were given
93 color = string(submatch[1])
94 regexStr = JoinRule(string(submatch[2]))
96 // If len is not 3 or 4 there is a problem
97 fmt.Println(filename, lineNum, "Invalid statement: "+line)
101 rules = append(rules, SingleRule{color, regexStr})
102 } else if ruleStartEndParser.MatchString(line) {
103 // Start-end syntax rule
104 submatch := ruleStartEndParser.FindSubmatch([]byte(line))
108 // Use m and s flags by default
109 if len(submatch) == 5 {
110 // If len is 5 the user provided some additional flags
111 color = string(submatch[1])
112 start = string(submatch[3])
113 end = string(submatch[4])
114 } else if len(submatch) == 4 {
115 // If len is 4 the user did not provide additional flags
116 color = string(submatch[1])
117 start = string(submatch[2])
118 end = string(submatch[3])
120 // If len is not 4 or 5 there is a problem
121 fmt.Println(filename, lineNum, "Invalid statement: "+line)
125 // rules[color] = "(?" + flags + ")" + "(" + start + ").*?(" + end + ")"
126 rules = append(rules, MultiRule{color, start, end})
133 func generateFile(filetype, syntax, header string, rules []interface{}) string {
136 output += fmt.Sprintf("filetype: %s\n\n", filetype)
137 output += fmt.Sprintf("detect: \n filename: \"%s\"\n", strings.Replace(strings.Replace(syntax, "\\", "\\\\", -1), "\"", "\\\"", -1))
140 output += fmt.Sprintf(" header: \"%s\"\n", strings.Replace(strings.Replace(header, "\\", "\\\\", -1), "\"", "\\\"", -1))
143 output += "\nrules:\n"
145 for _, r := range rules {
146 if rule, ok := r.(SingleRule); ok {
147 output += fmt.Sprintf(" - %s: \"%s\"\n", rule.color, strings.Replace(strings.Replace(rule.regex, "\\", "\\\\", -1), "\"", "\\\"", -1))
148 } else if rule, ok := r.(MultiRule); ok {
149 output += fmt.Sprintf(" - %s:\n", rule.color)
150 output += fmt.Sprintf(" start: \"%s\"\n", strings.Replace(strings.Replace(rule.start, "\\", "\\\\", -1), "\"", "\\\"", -1))
151 output += fmt.Sprintf(" end: \"%s\"\n", strings.Replace(strings.Replace(rule.end, "\\", "\\\\", -1), "\"", "\\\"", -1))
152 output += fmt.Sprintf(" rules: []\n\n")
160 if len(os.Args) < 2 {
161 fmt.Println("no args")
165 data, _ := ioutil.ReadFile(os.Args[1])
166 fmt.Print(generateFile(parseFile(string(data), os.Args[1])))