Generate Seq
This commit is contained in:
@ -13,7 +13,10 @@ import (
|
||||
//go:embed bind.go.tmpl
|
||||
var bind string
|
||||
|
||||
var tmpl *template.Template
|
||||
//go:embed seq.go.tmpl
|
||||
var seq string
|
||||
|
||||
var bindTmpl, seqTmpl *template.Template
|
||||
|
||||
func main() {
|
||||
err := run()
|
||||
@ -24,13 +27,17 @@ func main() {
|
||||
}
|
||||
|
||||
func run() error {
|
||||
outputPath := flag.String("output", "", "output file path")
|
||||
bindPath := flag.String("bindpath", "", "bind file path")
|
||||
seqPath := flag.String("seqpath", "", "seq file path")
|
||||
maxBindLen := flag.Int("max", 0, "max bind length")
|
||||
pkg := flag.String("pkg", "", "output package")
|
||||
flag.Parse()
|
||||
|
||||
if *outputPath == "" {
|
||||
return errors.New("output required")
|
||||
if *bindPath == "" {
|
||||
return errors.New("bindpath required")
|
||||
}
|
||||
if *seqPath == "" {
|
||||
return errors.New("seqpath required")
|
||||
}
|
||||
if *maxBindLen == 0 {
|
||||
return errors.New("maxbind required")
|
||||
@ -39,23 +46,31 @@ func run() error {
|
||||
return errors.New("pkg required")
|
||||
}
|
||||
|
||||
tmpl = template.New("")
|
||||
_, err := tmpl.New("bind").Parse(bind)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse bind: %w", err)
|
||||
}
|
||||
bindTmpl = template.Must(template.New("bind").Parse(bind))
|
||||
seqTmpl = template.Must(template.New("seq").Parse(seq))
|
||||
|
||||
f, err := os.OpenFile(*outputPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
fbind, err := os.OpenFile(*bindPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
defer fbind.Close()
|
||||
|
||||
data := File{
|
||||
Package: *pkg,
|
||||
Counter: Counter(*maxBindLen),
|
||||
}
|
||||
return tmpl.ExecuteTemplate(f, "bind", data)
|
||||
err = bindTmpl.Execute(fbind, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fseq, err := os.OpenFile(*seqPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fseq.Close()
|
||||
|
||||
return seqTmpl.Execute(fseq, data)
|
||||
}
|
||||
|
||||
type File struct {
|
||||
|
34
internal/bindgen/seq.go.tmpl
Normal file
34
internal/bindgen/seq.go.tmpl
Normal file
@ -0,0 +1,34 @@
|
||||
{{define "func"}}{{if gt . 1 -}}
|
||||
func Seq{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
|
||||
{{- range .Count}}
|
||||
p{{.}} Parser[In, T{{.}}],{{end}}
|
||||
f func({{range .Count}}{{if ne . 1}}, {{end}}T{{.}}{{end}}) Out,
|
||||
) Parser[In, Out] {
|
||||
return func(s State[In]) (Result[In, Out], error) {
|
||||
var anyConsumed bool
|
||||
var failed bool
|
||||
var msg Message
|
||||
var next = s
|
||||
{{range .Count}}
|
||||
r{{.}}, err := p{{.}}(next)
|
||||
if err != nil {
|
||||
return Result[In, Out]{}, err
|
||||
}
|
||||
anyConsumed = anyConsumed || r{{.}}.Consumed()
|
||||
failed, _, msg = r{{.}}.Failed()
|
||||
if failed {
|
||||
return Fail[In, Out](anyConsumed, msg), nil
|
||||
}
|
||||
_, _, val{{.}}, next, _ := r{{.}}.Succeeded()
|
||||
{{end}}
|
||||
final := f({{range .Count}}{{if ne . 1}}, {{end}}val{{.}}{{end}})
|
||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||
}
|
||||
}{{end}}{{end -}}
|
||||
|
||||
// GENERATED FILE. DO NOT EDIT.
|
||||
|
||||
package {{.Package}}
|
||||
{{range .Count}}{{template "func" .}}
|
||||
|
||||
{{end}}
|
Reference in New Issue
Block a user