Remove cgen templates

They didn't turn out to be as useful as first thought.
This commit is contained in:
Nigel Tao committed 2018-08-04 09:48:39 +10:00
1 parent f2ee78aa2a
commit 913adc8543
3 files changed
-188

No files matched your search

-149
View File
@@ -21,14 +21,11 @@ package main
// Invoke it via "go generate".
import (
"bufio"
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"sort"
"strings"
)
const columns = 1024
@@ -64,9 +61,6 @@ func main1() error {
if err := genBase(out); err != nil {
return err
}
if err := genTemplates(out); err != nil {
return err
}
formatted, err := format.Source(out.Bytes())
if err != nil {
@@ -129,147 +123,4 @@ func writeStringConst(out *bytes.Buffer, s []byte) {
out.WriteString("\"\" +\n")
}
}
}
type template struct {
name string
args [][2]string
format string
formatArgs []string
}
func genTemplates(out *bytes.Buffer) error {
templates, err := readTemplates()
if err != nil {
return err
}
sort.Slice(templates, func(i, j int) bool {
return templates[i].name < templates[j].name
})
for _, t := range templates {
out.WriteString("\n")
fmt.Fprintf(out, "type template_args_%s struct{\n", t.name)
for _, a := range t.args {
key, val := stripq(a[0]), a[1]
switch val {
case "%s":
fmt.Fprintf(out, "%s string\n", key)
}
}
fmt.Fprintf(out, "}\n\n")
fmt.Fprintf(out, "func template_%s(b *buffer, args template_args_%s) error {\n", t.name, t.name)
fmt.Fprintf(out, "b.printf(%q,\n", t.format)
for _, a := range t.formatArgs {
fmt.Fprintf(out, "args.%s,\n", stripq(a))
}
fmt.Fprintf(out, ")\n")
fmt.Fprintf(out, "return nil\n")
fmt.Fprintf(out, "}\n")
}
return nil
}
func readTemplates() ([]template, error) {
f, err := os.Open("templates.h")
if err != nil {
return nil, err
}
defer f.Close()
templates := []template(nil)
name := ""
args := [][2]string(nil)
argsMap := map[string]string{}
format := []byte(nil)
formatArgs := []string(nil)
r := bufio.NewScanner(f)
for r.Scan() {
s := r.Text()
if len(s) == 0 || strings.HasPrefix(s, "//") {
continue
}
if name == "" {
line := s
const prefix = "template "
if !strings.HasPrefix(s, prefix) {
return nil, fmt.Errorf("bad template line %q", line)
}
s = s[len(prefix):]
if i := strings.IndexByte(s, '('); i < 0 {
return nil, fmt.Errorf("bad template line %q", line)
} else {
name = s[:i]
s = s[i+1:]
}
if i := strings.IndexByte(s, ')'); i < 0 {
return nil, fmt.Errorf("bad template line %q", line)
} else {
s = s[:i]
}
for _, x := range strings.Split(s, ",") {
x = strings.TrimSpace(x)
key, val := "", ""
switch {
case strings.HasPrefix(x, "string "):
key = x[len("string "):]
val = "%s"
}
if key == "" || argsMap[key] != "" {
return nil, fmt.Errorf("bad template line %q", line)
}
args = append(args, [2]string{key, val})
argsMap[key] = val
}
continue
}
if s == "}" {
templates = append(templates, template{
name: name,
args: args,
format: string(format),
formatArgs: formatArgs,
})
name = ""
args = nil
argsMap = map[string]string{}
format = nil
formatArgs = nil
continue
}
s = strings.TrimSpace(s)
if len(s) == 0 || strings.HasPrefix(s, "//") {
continue
}
s = strings.Replace(s, "%", "%%", -1)
for _, a := range args {
key, val := a[0], a[1]
for {
i := strings.Index(s, key)
if i < 0 {
break
}
s = s[:i] + val + s[i+len(key):]
formatArgs = append(formatArgs, key)
}
}
format = append(format, s...)
format = append(format, '\n')
}
if err := r.Err(); err != nil {
return nil, err
}
return templates, nil
}
func stripq(s string) string {
if len(s) >= 2 && s[0] == 'q' && s[len(s)-1] == 'q' {
return s[1 : len(s)-1]
}
return s
}