mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Generate .wuffs summary API code
This commit is contained in:
1 parent
7fea629f10
commit
b5d4d104e8
3 files changed
+104
-4
No files matched your search
@@ -1,4 +1,5 @@
|
||||
*.exe
|
||||
*~
|
||||
/gen/lib
|
||||
/gen/wuffs
|
||||
a.out
|
||||
+100
-1
@@ -25,7 +25,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/google/wuffs/lang/base38"
|
||||
"github.com/google/wuffs/lang/generate"
|
||||
"github.com/google/wuffs/lang/parse"
|
||||
|
||||
cf "github.com/google/wuffs/cmd/commonflags"
|
||||
|
||||
@@ -172,13 +174,18 @@ func (h *genHelper) genDir(dirname string, filenames []string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(h.langs) > 0 {
|
||||
if err := h.genWuffs(dirname, qualifiedFilenames); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var cHeaderEndsHere = []byte("\n// C HEADER ENDS HERE.\n\n")
|
||||
|
||||
func (h *genHelper) genDirDependencies(qualifiedFilenames []string) error {
|
||||
files, err := generate.ParseFiles(&h.tm, qualifiedFilenames)
|
||||
files, err := generate.ParseFiles(&h.tm, qualifiedFilenames, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -213,6 +220,98 @@ func (h *genHelper) genFile(dirname string, lang string, out []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error {
|
||||
files, err := generate.ParseFiles(&h.tm, qualifiedFilenames, &parse.Options{
|
||||
AllowDoubleUnderscoreNames: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pkgIDNode := (*a.PackageID)(nil)
|
||||
for _, f := range files {
|
||||
for _, n := range f.TopLevelDecls() {
|
||||
if n.Kind() == a.KPackageID {
|
||||
pkgIDNode = n.PackageID()
|
||||
}
|
||||
}
|
||||
}
|
||||
if pkgIDNode == nil {
|
||||
return fmt.Errorf("missing packageid declaration")
|
||||
}
|
||||
pkgIDStr, ok := t.Unescape(pkgIDNode.ID().Str(&h.tm))
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid packageid declaration")
|
||||
}
|
||||
pkgIDNum, ok := base38.Encode(pkgIDStr)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid packageid declaration")
|
||||
}
|
||||
prefix := fmt.Sprintf("__pkg%08X", pkgIDNum)
|
||||
|
||||
out := &bytes.Buffer{}
|
||||
fmt.Fprintf(out, "// Code generated by running \"wuffs gen\". DO NOT EDIT.\n\n")
|
||||
fmt.Fprintf(out, "packageid %q // %s\n\n", pkgIDStr, prefix)
|
||||
|
||||
for _, f := range files {
|
||||
for _, n := range f.TopLevelDecls() {
|
||||
switch n.Kind() {
|
||||
case a.KFunc:
|
||||
n := n.Func()
|
||||
if !n.Public() {
|
||||
continue
|
||||
}
|
||||
effect := ""
|
||||
if n.Suspendible() {
|
||||
effect = "?"
|
||||
} else if n.Impure() {
|
||||
effect = "!"
|
||||
}
|
||||
if n.Receiver() == 0 {
|
||||
return fmt.Errorf("TODO: genWuffs for a free-standing function")
|
||||
}
|
||||
// TODO: look at n.Asserts().
|
||||
fmt.Fprintf(out, "pub func %s_%s.%s%s(",
|
||||
prefix, n.Receiver().Str(&h.tm), n.Name().Str(&h.tm), effect)
|
||||
for i, param := range [2]*a.Struct{n.In(), n.Out()} {
|
||||
if i > 0 {
|
||||
fmt.Fprintf(out, ")(")
|
||||
}
|
||||
for j, field := range param.Fields() {
|
||||
field := field.Field()
|
||||
if j > 0 {
|
||||
fmt.Fprintf(out, ", ")
|
||||
}
|
||||
// TODO: what happens if the XType is from another
|
||||
// package?
|
||||
fmt.Fprintf(out, "%s %s", field.Name().Str(&h.tm), field.XType().Str(&h.tm))
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(out, ") { }\n")
|
||||
|
||||
case a.KStatus:
|
||||
n := n.Status()
|
||||
if !n.Public() {
|
||||
continue
|
||||
}
|
||||
// TODO: Qualify status names with the prefix somehow??
|
||||
fmt.Fprintf(out, "pub %s %s\n", n.Keyword().Str(&h.tm), n.Message().Str(&h.tm))
|
||||
|
||||
case a.KStruct:
|
||||
n := n.Struct()
|
||||
if !n.Public() {
|
||||
continue
|
||||
}
|
||||
effect := ""
|
||||
if n.Suspendible() {
|
||||
effect = "?"
|
||||
}
|
||||
fmt.Fprintf(out, "pub struct %s_%s%s()\n", prefix, n.Name().Str(&h.tm), effect)
|
||||
}
|
||||
}
|
||||
}
|
||||
return h.genFile(dirname, "wuffs", out.Bytes())
|
||||
}
|
||||
|
||||
func (h *genHelper) genlibAffected() error {
|
||||
for _, lang := range h.langs {
|
||||
command := "wuffs-" + lang
|
||||
|
||||
@@ -99,10 +99,10 @@ func parseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error
|
||||
}
|
||||
return []*ast.File{f}, nil
|
||||
}
|
||||
return ParseFiles(tm, filenames)
|
||||
return ParseFiles(tm, filenames, nil)
|
||||
}
|
||||
|
||||
func ParseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error) {
|
||||
func ParseFiles(tm *token.Map, filenames []string, opts *parse.Options) (files []*ast.File, err error) {
|
||||
for _, filename := range filenames {
|
||||
src, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
@@ -112,7 +112,7 @@ func ParseFiles(tm *token.Map, filenames []string) (files []*ast.File, err error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := parse.Parse(tm, filename, tokens, nil)
|
||||
f, err := parse.Parse(tm, filename, tokens, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user