mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 16:32:11 +01:00
Generalize the "!! INSERT" mechanism
This commit is contained in:
1 parent
634789722d
commit
9c5d068f22
4 files changed
+52
-27
No files matched your search
@@ -14,9 +14,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// !! base-public.h goes here.
|
||||
// !! INSERT base-public.h.
|
||||
|
||||
// !! wuffs_base__status__string data goes here.
|
||||
// !! INSERT wuffs_base__status__string data.
|
||||
|
||||
const char* wuffs_base__status__string(wuffs_base__status s) {
|
||||
return "unknown status";
|
||||
|
||||
@@ -58,6 +58,8 @@ typedef struct {
|
||||
// pause in processing) or negative (a non-recoverable error).
|
||||
typedef int32_t wuffs_base__status;
|
||||
|
||||
// !! INSERT wuffs_base__status names.
|
||||
|
||||
static inline bool wuffs_base__status__is_error(wuffs_base__status s) {
|
||||
return s < 0;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package cgen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -26,6 +27,7 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/wuffs/lang/base38"
|
||||
@@ -77,11 +79,17 @@ func Do(args []string) error {
|
||||
if len(files) != 0 {
|
||||
return nil, fmt.Errorf("base package shouldn't have any .wuffs files")
|
||||
}
|
||||
var err error
|
||||
unformatted, err = genBase()
|
||||
if err != nil {
|
||||
buf := make(buffer, 0, 128*1024)
|
||||
if err := expandBangBangInsert(&buf, baseImplC, map[string]func(*buffer) error{
|
||||
"// !! INSERT base-public.h.\n": insertBasePublicH,
|
||||
"// !! INSERT wuffs_base__status__string data.\n": func(b *buffer) error {
|
||||
// TODO.
|
||||
return nil
|
||||
},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
unformatted = []byte(buf)
|
||||
|
||||
} else {
|
||||
g := &gen{
|
||||
@@ -173,29 +181,30 @@ func (b *buffer) writeb(x byte) { *b = append(*b, x)
|
||||
func (b *buffer) writes(s string) { *b = append(*b, s...) }
|
||||
func (b *buffer) writex(s []byte) { *b = append(*b, s...) }
|
||||
|
||||
func genBase() ([]byte, error) {
|
||||
b := make(buffer, 0, 128*1024)
|
||||
for s := baseImplC; ; {
|
||||
func expandBangBangInsert(b *buffer, s string, m map[string]func(*buffer) error) error {
|
||||
for {
|
||||
remaining := ""
|
||||
if i := strings.IndexByte(s, '\n'); i >= 0 {
|
||||
s, remaining = s[:i+1], s[i+1:]
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(s, "// !! ") {
|
||||
const prefix = "// !! INSERT "
|
||||
if !strings.HasPrefix(s, prefix) {
|
||||
b.writes(s)
|
||||
} else {
|
||||
switch s {
|
||||
default:
|
||||
return nil, fmt.Errorf("unrecognized base-impl.c line %q", s)
|
||||
|
||||
case "// !! base-public.h goes here.\n":
|
||||
b.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
|
||||
b.writes(basePublicH)
|
||||
b.writeb('\n')
|
||||
|
||||
case "// !! wuffs_base__status__string data goes here.\n":
|
||||
// TODO.
|
||||
} else if f := m[s]; f == nil {
|
||||
msg := []byte(fmt.Sprintf("unrecognized line %q, want one of:\n", s))
|
||||
keys := []string(nil)
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
msg = append(msg, '\t')
|
||||
msg = append(msg, k...)
|
||||
}
|
||||
return errors.New(string(msg))
|
||||
} else if err := f(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if remaining == "" {
|
||||
@@ -203,7 +212,21 @@ func genBase() ([]byte, error) {
|
||||
}
|
||||
s = remaining
|
||||
}
|
||||
return b, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertBasePublicH(buf *buffer) error {
|
||||
buf.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
|
||||
if err := expandBangBangInsert(buf, basePublicH, map[string]func(*buffer) error{
|
||||
"// !! INSERT wuffs_base__status names.\n": func(b *buffer) error {
|
||||
// TODO.
|
||||
return nil
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
buf.writeb('\n')
|
||||
return nil
|
||||
}
|
||||
|
||||
type gen struct {
|
||||
@@ -273,9 +296,9 @@ func (g *gen) genHeader(b *buffer) error {
|
||||
includeGuard := "WUFFS_" + strings.ToUpper(g.pkgName) + "_H"
|
||||
b.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
|
||||
|
||||
b.writes("// Code generated by wuffs-c. DO NOT EDIT.\n\n")
|
||||
b.writes(basePublicH)
|
||||
b.writeb('\n')
|
||||
if err := insertBasePublicH(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.writes("// ---------------- Use Declarations\n\n")
|
||||
if err := g.forEachUse(b, (*gen).writeUse); err != nil {
|
||||
|
||||
@@ -21,7 +21,7 @@ const basePublicH = "" +
|
||||
"as per https://semver.org/,\n// as a uint64_t. The major number is the high 32 bits. The minor number is the\n// middle 16 bits. The patch number is the low 16 bits. The version extension\n// (such as \"\", \"beta\" or \"rc.1\") is part of the string representation (such as\n// \"1.2.3-beta\") but not the uint64_t representation.\n//\n// All three of major, minor and patch being zero means that this is a\n// work-in-progress version, not a release version, and has no backwards or\n// forwards compatibility guarantees.\n//\n// !! Some code generation programs can override WUFFS_VERSION.\n#define WUFFS_VERSION ((uint64_t)0)\n#define WUFFS_VERSION_MAJOR ((uint64_t)0)\n#define WUFFS_VERSION_MINOR ((uint64_t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_EXTENSION \"\"\n#define WUFFS_VERSION_STRING \"0.0.0\"\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g(" +
|
||||
")\".\ntypedef struct {\n} wuffs_base__empty_struct;\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// A status code is either zero (OK), positive (a recoverable suspension or\n// pause in processing) or negative (a non-recoverable error).\ntypedef int32_t wuffs_base__status;\n\nstatic inline bool wuffs_base__status__is_error(wuffs_base__status s) {\n return s < 0;\n}\n\nstatic inline bool wuffs_base__status__is_ok(wuffs_base__status s) {\n return s == 0;\n}\n\nstatic inline bool wuffs_base__status__is_suspension(wuffs_base__status s) {\n return s > 0;\n}\n\nconst char* wuffs_base__status__string(wuffs_base__status s);\n\n" +
|
||||
"// --------\n\n// A status code is either zero (OK), positive (a recoverable suspension or\n// pause in processing) or negative (a non-recoverable error).\ntypedef int32_t wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool wuffs_base__status__is_error(wuffs_base__status s) {\n return s < 0;\n}\n\nstatic inline bool wuffs_base__status__is_ok(wuffs_base__status s) {\n return s == 0;\n}\n\nstatic inline bool wuffs_base__status__is_suspension(wuffs_base__status s) {\n return s > 0;\n}\n\nconst char* wuffs_base__status__string(wuffs_base__status s);\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a\n// second. See https://github.com/OculusVR/Flicks\ntypedef int64_t wuffs_base__flicks;\n\n#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)\n#define WUFFS_BASE__FLICKS_PER_MILLISECOND ((uint64_t)705600)\n\n" +
|
||||
"" +
|
||||
@@ -123,7 +123,7 @@ const basePrivateH = "" +
|
||||
""
|
||||
|
||||
const baseImplC = "" +
|
||||
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! base-public.h goes here.\n\n// !! wuffs_base__status__string data goes here.\n\nconst char* wuffs_base__status__string(wuffs_base__status s) {\n return \"unknown status\";\n}\n" +
|
||||
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! INSERT base-public.h.\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* wuffs_base__status__string(wuffs_base__status s) {\n return \"unknown status\";\n}\n" +
|
||||
""
|
||||
|
||||
type template_args_short_read struct {
|
||||
|
||||
Reference in new issue
Block a user