diff --git a/bind.go b/bind.go index b4ce18c..aea40c6 100644 --- a/bind.go +++ b/bind.go @@ -306,4 +306,3 @@ func Bind5[In, Out, T, T2, T3, T4, T5 any]( return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil } } - diff --git a/internal/bindgen/bindgen.go b/internal/bindgen/bindgen.go index 1d5c6fd..e422c73 100644 --- a/internal/bindgen/bindgen.go +++ b/internal/bindgen/bindgen.go @@ -1,10 +1,12 @@ package main import ( + "bytes" _ "embed" "errors" "flag" "fmt" + "go/format" "os" "strconv" "text/template" @@ -49,28 +51,28 @@ func run() error { bindTmpl = template.Must(template.New("bind").Parse(bind)) seqTmpl = template.Must(template.New("seq").Parse(seq)) - fbind, err := os.OpenFile(*bindPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) - if err != nil { - return err - } - defer fbind.Close() - data := File{ Package: *pkg, Counter: Counter(*maxBindLen), } - err = bindTmpl.Execute(fbind, data) + err := genCodeFile(*bindPath, bindTmpl, data) if err != nil { return err } + return genCodeFile(*seqPath, seqTmpl, data) +} - fseq, err := os.OpenFile(*seqPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) +func genCodeFile(path string, tmpl *template.Template, data any) error { + var buf bytes.Buffer + err := tmpl.Execute(&buf, data) if err != nil { return err } - defer fseq.Close() - - return seqTmpl.Execute(fseq, data) + src, err := format.Source(buf.Bytes()) + if err != nil { + return err + } + return os.WriteFile(path, src, 0777) } type File struct { diff --git a/seq.go b/seq.go index ee5da2b..6296d36 100644 --- a/seq.go +++ b/seq.go @@ -39,6 +39,7 @@ func Seq2[In, Out, T, T2 any]( return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil } } + func Seq3[In, Out, T, T2, T3 any]( p Parser[In, T], p2 Parser[In, T2], @@ -88,6 +89,7 @@ func Seq3[In, Out, T, T2, T3 any]( return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil } } + func Seq4[In, Out, T, T2, T3, T4 any]( p Parser[In, T], p2 Parser[In, T2], @@ -149,6 +151,7 @@ func Seq4[In, Out, T, T2, T3, T4 any]( return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil } } + func Seq5[In, Out, T, T2, T3, T4, T5 any]( p Parser[In, T], p2 Parser[In, T2], @@ -222,4 +225,3 @@ func Seq5[In, Out, T, T2, T3, T4, T5 any]( return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil } } -