Format generated code

This commit is contained in:
Brandon Dyck 2024-09-13 21:03:10 -06:00
parent 1e34c14bb0
commit 4dd6885ff6
3 changed files with 16 additions and 13 deletions

View File

@ -306,4 +306,3 @@ func Bind5[In, Out, T, T2, T3, T4, T5 any](
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
}
}

View File

@ -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 {

4
seq.go
View File

@ -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
}
}