-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_grammar.go
37 lines (34 loc) · 1021 Bytes
/
template_grammar.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
package main
import (
"io/ioutil"
"os"
"regexp"
"text/template"
)
func must(err error) {
if err != nil {
panic(err)
}
}
func main() {
b, err := ioutil.ReadAll(os.Stdin)
must(err)
tmpl, err := template.New("language.html").Parse(string(b))
must(err)
b, err = ioutil.ReadFile("docs/grammar.txt")
must(err)
s := regexp.MustCompile(`("[^ ]+")`).ReplaceAllStringFunc(string(b), func(s string) string {
return `<span class="grammar-string">` + s + `</span>`
})
s = regexp.MustCompile(`(?: [=()|{}\[\]]|;)`).ReplaceAllStringFunc(s, func(s string) string {
return `<span class="grammar-syntax">` + s + `</span>`
})
s = regexp.MustCompile(`(#.*)(?m:$)`).ReplaceAllStringFunc(s, func(s string) string {
return `<span class="grammar-comment">` + s + `</span>`
})
s = regexp.MustCompile(`(?:String|Ident|Int|EOL)`).ReplaceAllStringFunc(s, func(s string) string {
return `<span class="grammar-token">` + s + `</span>`
})
g := struct{ Grammar string }{Grammar: s}
must(tmpl.Execute(os.Stdout, &g))
}