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

-11
View File
@@ -159,14 +159,3 @@ const baseImagePublicH = "" +
"buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.planes[p]\n : ((wuffs_base__table_u8){});\n}\n\n// wuffs_base__pixel_buffer__palette returns the palette that the pixel data\n// can index. The backing array is inside b and has length 1024.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* b) {\n return b ? ((wuffs_base__slice_u8){.ptr = b->private_impl.palette,\n .len = 1024})\n : ((wuffs_base__slice_u8){});\n}\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg, pixbuf_memory);\n}\n\ninline void //\nwuffs_base__pixel_buffer::set_palette(wuffs_base__slice_u8 palette) {\n wuffs_base__pixel_buffer__set_pa" +
"lette(this, palette);\n}\n\ninline wuffs_base__pixel_config* //\nwuffs_base__pixel_buffer::pixel_config() {\n return wuffs_base__pixel_buffer__pixel_config(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_buffer::bounds() {\n return wuffs_base__pixel_buffer__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_buffer::width() {\n return wuffs_base__pixel_buffer__width(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_buffer::height() {\n return wuffs_base__pixel_buffer__height(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer::plane(uint32_t p) {\n return wuffs_base__pixel_buffer__plane(this, p);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer::palette() {\n return wuffs_base__pixel_buffer__palette(this);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" +
""
type template_args_short_read struct {
name string
}
func template_short_read(b *buffer, args template_args_short_read) error {
b.printf("short_read_%s:\nstatus = WUFFS_BASE__SUSPENSION_SHORT_READ;\ngoto suspend;\n",
args.name,
)
return nil
}
-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
}
-28
View File
@@ -1,28 +0,0 @@
// After editing this file, run "go generate" in this directory.
// Copyright 2017 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file contains C-like code. It is close enough to C so that clang-format
// can format it well. But it is not C code per se. It contains templates that
// the code generator expands via a custom mechanism.
//
// This file has a .h extension, not a .c extension, so that Go tools such as
// "go generate" don't try to build it as part of the Go package, via cgo.
template short_read(string qnameq) {
short_read_qnameq:
status = WUFFS_BASE__SUSPENSION_SHORT_READ;
goto suspend;
}