First cut of the puffs tool: "puffs gen".

This commit is contained in:
Nigel Tao committed 2017-05-25 15:33:58 +10:00
1 parent 090c5f057c
commit 3663708405
3 files changed
+254

No files matched your search

+139
View File
@@ -0,0 +1,139 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"sort"
"strings"
"github.com/google/puffs/lang/ast"
"github.com/google/puffs/lang/check"
"github.com/google/puffs/lang/parse"
"github.com/google/puffs/lang/render"
"github.com/google/puffs/lang/token"
)
func doGen(puffsRoot string, args []string) error {
flags := flag.NewFlagSet("gen", flag.ExitOnError)
langsStr := flags.String("langs", "c", langsUsage)
if err := flags.Parse(args); err != nil {
return err
}
langs, err := parseLangs(*langsStr)
if err != nil {
return err
}
args = flags.Args()
if len(args) == 0 {
args = []string{"std/..."}
}
for _, arg := range args {
recursive := strings.HasSuffix(arg, "/...")
if recursive {
arg = arg[:len(arg)-4]
}
arg = filepath.FromSlash(arg)
if err := gen(puffsRoot, filepath.Join(puffsRoot, arg), langs, recursive); err != nil {
return err
}
}
return nil
}
func gen(puffsRoot, dirname string, langs []string, recursive bool) error {
filenames, subDirs := []string(nil), []string(nil)
infos, err := readdir(dirname)
if err != nil {
return err
}
for _, o := range infos {
name := o.Name()
if o.IsDir() {
if recursive {
subDirs = append(subDirs, name)
}
} else if strings.HasSuffix(name, ".puffs") {
filenames = append(filenames, name)
}
}
if len(filenames) > 0 {
sort.Strings(filenames)
if err := genDir(puffsRoot, dirname, filenames, langs); err != nil {
return err
}
}
if len(subDirs) > 0 {
sort.Strings(subDirs)
for _, d := range subDirs {
if err := gen(puffsRoot, filepath.Join(dirname, d), langs, recursive); err != nil {
return err
}
}
}
return nil
}
func genDir(puffsRoot string, dirname string, filenames []string, langs []string) error {
idMap := &token.IDMap{}
files := []*ast.File(nil)
buf := &bytes.Buffer{}
for _, filename := range filenames {
filename = filepath.Join(dirname, filename)
src, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
tokens, _, err := token.Tokenize(idMap, filename, src)
if err != nil {
return err
}
f, err := parse.Parse(idMap, filename, tokens)
if err != nil {
return err
}
files = append(files, f)
if err := render.Render(buf, idMap, tokens, nil); err != nil {
return err
}
}
if _, err := check.Check(idMap, files...); err != nil {
return err
}
combinedSrc := buf.Bytes()
for _, lang := range langs {
command := "puffs-gen-" + lang
cmd := exec.Command(command, "-assume_already_checked")
cmd.Stdin = bytes.NewReader(combinedSrc)
out, err := cmd.CombinedOutput()
if _, ok := err.(*exec.ExitError); ok && len(out) > 0 {
// Go error messages usually don't end in "\n". The caller, who sees
// the error we return here, usually adds the "\n" themselves.
if out[len(out)-1] == '\n' {
out = out[:len(out)-1]
}
// The exec.ExitError's Error message doesn't include stderr, so we
// return that explicitly.
return fmt.Errorf("%s: %s", command, out)
}
if err != nil {
return fmt.Errorf("%s: %v", command, err)
}
outFilename := filepath.Join(puffsRoot, "gen", lang, filepath.Base(dirname)+"."+lang)
if err := ioutil.WriteFile(outFilename, out, 0644); err != nil {
return err
}
fmt.Println(outFilename)
}
return nil
}
+101
View File
@@ -0,0 +1,101 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
// puffs is a tool for managing Puffs source code.
package main
import (
"errors"
"flag"
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
)
var commands = []struct {
name string
do func(puffsRoot string, args []string) error
}{
{"gen", doGen},
}
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
flag.Usage = usage
flag.Parse()
puffsRoot, err := findPuffsRoot()
if err != nil {
return err
}
if args := flag.Args(); len(args) > 0 {
for _, c := range commands {
if args[0] == c.name {
return c.do(puffsRoot, args[1:])
}
}
}
usage()
os.Exit(1)
return nil
}
func findPuffsRoot() (string, error) {
// TODO: look for a PUFFSROOT environment variable?
for _, p := range filepath.SplitList(build.Default.GOPATH) {
p = filepath.Join(p, "src", "github.com", "google", "puffs")
if o, err := os.Stat(p); err == nil && o.IsDir() {
return p, nil
}
}
return "", errors.New("could not find Puffs root directory")
}
func readdir(dirname string) ([]os.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdir(-1)
}
func usage() {
fmt.Fprintf(os.Stderr, `Puffs is a tool for managing Puffs source code.
Usage:
puffs command [arguments]
The commands are:
gen generate code for packages and dependencies
`)
}
const langsUsage = `comma-separated list of target languages, e.g. "c,go,rs"`
func parseLangs(commaSeparated string) ([]string, error) {
ret := []string(nil)
for _, s := range strings.Split(commaSeparated, ",") {
if len(s) == 0 {
return nil, fmt.Errorf(`invalid empty lang ""`)
}
for _, c := range s {
if ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') {
continue
}
return nil, fmt.Errorf(`invalid lang %q, not in [a-z0-9]+`, s)
}
ret = append(ret, s)
}
return ret, nil
}
+14
View File
@@ -0,0 +1,14 @@
// Code generated by puffs-gen-c. DO NOT EDIT.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
} // extern "C"
#endif