This repository was archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.go
134 lines (113 loc) · 2.75 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2015 The PL0 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
import (
"bytes"
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
)
func yy() (nm string, err error) {
y, err := os.Create("parser.y")
if err != nil {
return "", err
}
nm = y.Name()
cmd := exec.Command(
"yy",
"-astImport", "\"go/token\"\n\n\"github.com/cznic/xc\"",
"-kind", "Case",
"-o", nm,
"-prettyString", "PrettyString",
"parser.yy",
)
if out, err := cmd.CombinedOutput(); err != nil {
os.Remove(nm)
log.Printf("%s", out)
log.Printf("(To install yy: $ go get github.com/cznic/yy)")
return "", err
}
return nm, nil
}
func goyacc(y string) (err error) {
t, err := ioutil.TempFile("", "go-generate-xegen-")
if err != nil {
log.Fatal(err)
}
defer func() {
if e := os.Remove(t.Name()); e != nil && err == nil {
err = e
}
}()
cmd := exec.Command("goyacc", "-o", os.DevNull, "-xegen", t.Name(), y)
if out, err := cmd.CombinedOutput(); err != nil {
log.Printf("%s\n", out)
log.Printf("(To install goyacc: $ go get github.com/cznic/yy)")
return err
}
xerrors, err := ioutil.ReadFile("xerrors")
if err != nil {
return err
}
if _, err := t.Seek(0, 2); err != nil {
return err
}
if _, err := t.Write(xerrors); err != nil {
return err
}
cmd = exec.Command("goyacc", "-cr", "-xe", t.Name(), "-o", "parser.go", "-dlvalf", "%v", "-dlval", "PrettyString(lval.Token)", y)
if out, err := cmd.CombinedOutput(); err != nil {
log.Printf("%s", out)
return err
} else {
log.Printf("%s", out)
}
return nil
}
func main() {
if err := main0(); err != nil {
log.Fatal(err)
}
}
func main0() (err error) {
log.SetFlags(log.Lshortfile)
p2 := flag.Bool("2", false, "")
flag.Parse()
if *p2 {
return main2()
}
os.Remove("ast.go")
os.Remove("ast_test.go")
y, err := yy()
if err != nil {
return err
}
return goyacc(y)
}
func main2() (err error) {
goCmd := exec.Command("go", "test", "-run", "^Example[^_]")
out, err := goCmd.CombinedOutput() // Errors are expected and wanted here.
feCmd := exec.Command("fe")
feCmd.Stdin = bytes.NewBuffer(out)
if out, err = feCmd.CombinedOutput(); err != nil {
log.Printf("%s", out)
log.Printf("(To install fe: $ go get github.com/cznic/fe)")
return err
}
matches, err := filepath.Glob("*_test.go")
if err != nil {
return err
}
//TODO remove dependency on pcregrep.
cmd := exec.Command("pcregrep", append([]string{"-nM", `\/\/ <nil>|// false|// -1|Output:\n}`}, matches...)...)
if out, _ = cmd.CombinedOutput(); len(out) != 0 { // Error != nil when no matches
log.Printf("%s", out)
log.Printf("(To install pcregrep: use your distro package manager.)")
}
return nil
}