cleanup
This commit is contained in:
parent
9e95592e3c
commit
fcc06319b3
15
boot.go
15
boot.go
@ -1,10 +1,17 @@
|
||||
package treerack
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
)
|
||||
|
||||
func bootSyntax() (*Syntax, error) {
|
||||
f, _ := os.Open("syntax.treerack")
|
||||
defer f.Close()
|
||||
b, err := os.ReadFile("syntax.treerack")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(b)
|
||||
s := &Syntax{}
|
||||
return s, s.ReadSyntax(f)
|
||||
return s, s.ReadSyntax(buf)
|
||||
}
|
||||
|
||||
29
boot_test.go
29
boot_test.go
@ -1,16 +1,13 @@
|
||||
package treerack
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func parseWithSyntax(s *Syntax, f io.ReadSeeker) (Node, error) {
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
return Node{}, err
|
||||
}
|
||||
|
||||
func parseWithSyntax(s *Syntax, f io.Reader) (Node, error) {
|
||||
return s.Parse(f)
|
||||
}
|
||||
|
||||
@ -27,7 +24,7 @@ func syntaxFromTree(n Node) (*Syntax, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func testParseFromTree(t *testing.T, n Node, f io.ReadSeeker) Node {
|
||||
func testParseFromTree(t *testing.T, n Node, f io.Reader) Node {
|
||||
s, err := syntaxFromTree(n)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -45,31 +42,27 @@ func testParseFromTree(t *testing.T, n Node, f io.ReadSeeker) Node {
|
||||
}
|
||||
|
||||
func TestBoot(t *testing.T) {
|
||||
t.Skip()
|
||||
b, err := bootSyntax()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open("syntax.treerack")
|
||||
p, err := os.ReadFile("syntax.treerack")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
n0, err := parseWithSyntax(b, bytes.NewBuffer(p))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
n0, err := parseWithSyntax(b, f)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
n1 := testParseFromTree(t, n0, f)
|
||||
n1 := testParseFromTree(t, n0, bytes.NewBuffer(p))
|
||||
if t.Failed() {
|
||||
return
|
||||
}
|
||||
|
||||
testParseFromTree(t, n1, f)
|
||||
testParseFromTree(t, n1, bytes.NewBuffer(p))
|
||||
}
|
||||
|
||||
@ -30,18 +30,16 @@ type checkOptions struct {
|
||||
// Syntax can be provided via a filename option or an inline string option. Input can be provided via a filename
|
||||
// option, a positional argument filename, an inline string option, or piped from standard input.
|
||||
func check(o checkOptions, stdin io.Reader, args ...string) error {
|
||||
syntax, finalizeSyntax, err := initInput(o.Syntax, o.SyntaxString, nil, nil)
|
||||
syntax, err := initInput(o.Syntax, o.SyntaxString, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer finalizeSyntax()
|
||||
input, finalizeInput, err := initInput(o.Input, o.InputString, stdin, args)
|
||||
input, err := initInput(o.Input, o.InputString, stdin, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer finalizeInput()
|
||||
mtl := defaultMaxTraceLength
|
||||
if o.MaxTraceLength != nil {
|
||||
mtl = *o.MaxTraceLength
|
||||
|
||||
@ -25,12 +25,11 @@ type checkSyntaxOptions struct {
|
||||
// checkSyntax validates a syntax definition. The syntax may be provided via a file path (using an option or a
|
||||
// positional argument), an inline string, or piped from standard input.
|
||||
func checkSyntax(o checkSyntaxOptions, stdin io.Reader, args ...string) error {
|
||||
syntax, finalize, err := initInput(o.Syntax, o.SyntaxString, stdin, args)
|
||||
syntax, err := initInput(o.Syntax, o.SyntaxString, stdin, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer finalize()
|
||||
mtl := defaultMaxTraceLength
|
||||
if o.MaxTraceLength != nil {
|
||||
mtl = *o.MaxTraceLength
|
||||
|
||||
@ -32,12 +32,11 @@ type generateOptions struct {
|
||||
// The syntax may be provided via a file path (using an option or a positional argument), an
|
||||
// inline string, or piped from standard input.
|
||||
func generate(o generateOptions, stdin io.Reader, stdout io.Writer, args ...string) error {
|
||||
syntax, finalizeSyntax, err := initInput(o.Syntax, o.SyntaxString, stdin, args)
|
||||
syntax, err := initInput(o.Syntax, o.SyntaxString, stdin, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer finalizeSyntax()
|
||||
mtl := defaultMaxTraceLength
|
||||
if o.MaxTraceLength != nil {
|
||||
mtl = *o.MaxTraceLength
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
@ -16,13 +15,7 @@ var (
|
||||
|
||||
func noop() {}
|
||||
|
||||
func initInput(
|
||||
filename, stringValue *string, stdin io.Reader, args []string,
|
||||
) (
|
||||
input io.Reader, finalize func(), err error,
|
||||
) {
|
||||
finalize = noop
|
||||
|
||||
func initInput(filename, stringValue *string, stdin io.Reader, args []string) (input io.Reader, err error) {
|
||||
var inputCount int
|
||||
if filename != nil {
|
||||
inputCount++
|
||||
@ -53,19 +46,12 @@ func initInput(
|
||||
return
|
||||
}
|
||||
|
||||
var f io.ReadCloser
|
||||
f, err = os.Open(*filename)
|
||||
if err != nil {
|
||||
var b []byte
|
||||
if b, err = os.ReadFile(*filename); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
finalize = func() {
|
||||
if err := f.Close(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
input = f
|
||||
input = bytes.NewBuffer(b)
|
||||
case stringValue != nil:
|
||||
input = bytes.NewBufferString(*stringValue)
|
||||
default:
|
||||
|
||||
@ -64,18 +64,16 @@ func mapNode(n treerack.Node) node {
|
||||
// provided via a filename option, a positional argument filename, an inline string option, or piped from
|
||||
// standard input.
|
||||
func show(o showOptions, stdin io.Reader, stdout io.Writer, args ...string) error {
|
||||
syntax, finalizeSyntax, err := initInput(o.Syntax, o.SyntaxString, nil, nil)
|
||||
syntax, err := initInput(o.Syntax, o.SyntaxString, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer finalizeSyntax()
|
||||
input, finalizeInput, err := initInput(o.Input, o.InputString, stdin, args)
|
||||
input, err := initInput(o.Input, o.InputString, stdin, args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer finalizeInput()
|
||||
mtl := defaultMaxTraceLength
|
||||
if o.MaxTraceLength != nil {
|
||||
mtl = *o.MaxTraceLength
|
||||
|
||||
10
makefile
10
makefile
@ -130,12 +130,18 @@ cover-cmd: .coverprofile-cmd
|
||||
showcover-cmd: .coverprofile-cmd
|
||||
go tool cover -html .coverprofile-cmd
|
||||
|
||||
cpu.out: $(sources)
|
||||
go test -v -run TestMMLFile -cpuprofile cpu.out
|
||||
cpu.out: $(sources) $(parsers)
|
||||
go test -v -count 1 -run TestMMLFile -cpuprofile cpu.out
|
||||
|
||||
cpu: cpu.out
|
||||
go tool pprof -top cpu.out
|
||||
|
||||
mem.out: $(sources) $(parsers)
|
||||
go test -v -count 1 -run TestMMLFile -memprofile mem.out
|
||||
|
||||
mem: mem.out
|
||||
go tool pprof -top mem.out
|
||||
|
||||
fmt: $(sources) $(parsers) head.gen.go headexported.gen.go internal/self/self.gen.go cmd/treerack/docreflect.gen.go
|
||||
go fmt ./...
|
||||
go run script/format.go $(parsers)
|
||||
|
||||
@ -2,7 +2,6 @@ package treerack
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@ -2995,32 +2994,18 @@ func TestMMLFile(t *testing.T) {
|
||||
|
||||
s.Init()
|
||||
|
||||
f, err := os.Open("doc/example/test.mml")
|
||||
b, err := os.ReadFile("doc/example/test.mml")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
var d time.Duration
|
||||
for i := 0; i < n && !t.Failed(); i++ {
|
||||
func() {
|
||||
if _, err := f.Seek(0, 0); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
b := bytes.NewBuffer(nil)
|
||||
if _, err := io.Copy(b, f); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(b)
|
||||
start := time.Now()
|
||||
_, err = s.Parse(b)
|
||||
_, err = s.Parse(buf)
|
||||
d += time.Now().Sub(start)
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@ -30,13 +30,13 @@ func openSyntaxReader(r io.Reader) (*Syntax, error) {
|
||||
}
|
||||
|
||||
func openSyntaxFile(file string) (*Syntax, error) {
|
||||
f, err := os.Open(file)
|
||||
b, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
return openSyntaxReader(f)
|
||||
r := bytes.NewBuffer(b)
|
||||
return openSyntaxReader(r)
|
||||
}
|
||||
|
||||
func openSyntaxString(syntax string) (*Syntax, error) {
|
||||
|
||||
@ -111,13 +111,12 @@ func main() {
|
||||
|
||||
var files []io.Reader
|
||||
for _, fn := range flag.Args() {
|
||||
f, err := os.Open(fn)
|
||||
b, err := os.ReadFile(fn)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
files = append(files, f)
|
||||
files = append(files, bytes.NewBuffer(b))
|
||||
}
|
||||
|
||||
var headCode bytes.Buffer
|
||||
|
||||
Loading…
Reference in New Issue
Block a user