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