11 type SingleRule struct {
16 type MultiRule struct {
22 // JoinRule takes a syntax rule (which can be multiple regular expressions)
23 // and joins it into one regular expression by ORing everything together
24 func JoinRule(rule string) string {
25 split := strings.Split(rule, `" "`)
26 joined := strings.Join(split, "|")
30 func parseFile(text, filename string) (filetype, syntax, header string, rules []interface{}) {
31 lines := strings.Split(text, "\n")
33 // Regex for parsing syntax statements
34 syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
35 // Regex for parsing header statements
36 headerParser := regexp.MustCompile(`header "(.*)"`)
38 // Regex for parsing standard syntax rules
39 ruleParser := regexp.MustCompile(`color (.*?)\s+(?:\((.+?)?\)\s+)?"(.*)"`)
40 // Regex for parsing syntax rules with start="..." end="..."
41 ruleStartEndParser := regexp.MustCompile(`color (.*?)\s+(?:\((.+?)?\)\s+)?start="(.*)"\s+end="(.*)"`)
43 for lineNum, line := range lines {
44 line = strings.TrimSpace(line)
48 if strings.HasPrefix(line, "#") {
51 if strings.HasPrefix(line, "syntax") {
52 syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
53 if len(syntaxMatches) == 3 {
54 filetype = string(syntaxMatches[1])
55 syntax = JoinRule(string(syntaxMatches[2]))
57 fmt.Println(filename, lineNum, "Syntax statement is not valid: "+line)
61 if strings.HasPrefix(line, "header") {
63 headerMatches := headerParser.FindSubmatch([]byte(line))
64 if len(headerMatches) == 2 {
65 header = JoinRule(string(headerMatches[1]))
67 fmt.Println(filename, lineNum, "Header statement is not valid: "+line)
72 // Syntax rule, but it could be standard or start-end
73 if ruleParser.MatchString(line) {
74 // Standard syntax rule
76 submatch := ruleParser.FindSubmatch([]byte(line))
80 if len(submatch) == 4 {
81 // If len is 4 then the user specified some additional flags to use
82 color = string(submatch[1])
83 flags = string(submatch[2])
85 regexStr = "(?" + flags + ")" + JoinRule(string(submatch[3]))
87 regexStr = JoinRule(string(submatch[3]))
89 } else if len(submatch) == 3 {
90 // If len is 3, no additional flags were given
91 color = string(submatch[1])
92 regexStr = JoinRule(string(submatch[2]))
94 // If len is not 3 or 4 there is a problem
95 fmt.Println(filename, lineNum, "Invalid statement: "+line)
99 rules = append(rules, SingleRule{color, regexStr})
100 } else if ruleStartEndParser.MatchString(line) {
101 // Start-end syntax rule
102 submatch := ruleStartEndParser.FindSubmatch([]byte(line))
106 // Use m and s flags by default
107 if len(submatch) == 5 {
108 // If len is 5 the user provided some additional flags
109 color = string(submatch[1])
110 start = string(submatch[3])
111 end = string(submatch[4])
112 } else if len(submatch) == 4 {
113 // If len is 4 the user did not provide additional flags
114 color = string(submatch[1])
115 start = string(submatch[2])
116 end = string(submatch[3])
118 // If len is not 4 or 5 there is a problem
119 fmt.Println(filename, lineNum, "Invalid statement: "+line)
123 // rules[color] = "(?" + flags + ")" + "(" + start + ").*?(" + end + ")"
124 rules = append(rules, MultiRule{color, start, end})
131 func generateFile(filetype, syntax, header string, rules []interface{}) string {
134 output += fmt.Sprintf("filetype: %s\n\n", filetype)
135 output += fmt.Sprintf("detect: \n filename: \"%s\"\n", strings.Replace(strings.Replace(syntax, "\\", "\\\\", -1), "\"", "\\\"", -1))
138 output += fmt.Sprintf(" header: \"%s\"\n", strings.Replace(strings.Replace(header, "\\", "\\\\", -1), "\"", "\\\"", -1))
141 output += "\nrules:\n"
143 for _, r := range rules {
144 if rule, ok := r.(SingleRule); ok {
145 output += fmt.Sprintf(" - %s: \"%s\"\n", rule.color, strings.Replace(strings.Replace(rule.regex, "\\", "\\\\", -1), "\"", "\\\"", -1))
146 } else if rule, ok := r.(MultiRule); ok {
147 output += fmt.Sprintf(" - %s:\n", rule.color)
148 output += fmt.Sprintf(" start: \"%s\"\n", strings.Replace(strings.Replace(rule.start, "\\", "\\\\", -1), "\"", "\\\"", -1))
149 output += fmt.Sprintf(" end: \"%s\"\n", strings.Replace(strings.Replace(rule.end, "\\", "\\\\", -1), "\"", "\\\"", -1))
150 output += fmt.Sprintf(" rules: []\n\n")
158 if len(os.Args) < 2 {
159 fmt.Println("no args")
163 data, _ := ioutil.ReadFile(os.Args[1])
164 fmt.Print(generateFile(parseFile(string(data), os.Args[1])))