gigaparsec/internal/bindgen/bind.go.tmpl

39 lines
1.2 KiB
Cheetah
Raw Permalink Normal View History

{{/* SPDX-License-Identifier: Unlicense */ -}}
2024-09-14 00:28:38 +00:00
{{define "fparams" -}}
{{with $max := .}}{{range .Count}} f{{.}} func(T{{.}}) Parser[In, {{if eq . $max}}Out{{else}}T{{.Next}}{{end}}],
{{end}}{{end}}{{end -}}
{{define "func" -}}
2024-09-14 01:24:23 +00:00
{{if eq . 1}}// Bind combines p with a parser created by f that depends on p's result value.
{{- else}}// Bind{{.}} is equivalent to {{.}} nested calls to Bind.{{end}}
2024-09-14 00:28:38 +00:00
func Bind{{.}}[In, Out{{range .Count}}, T{{.}}{{end}} any](
p Parser[In, T],
{{template "fparams" .}}) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
var anyConsumed bool
var next = s
{{range .Next.Count}}
r{{.}}, err := {{if eq . 1}}p(next){{else}}f{{.Prev}}(val{{.Prev}})(next){{end}}
if err != nil {
return Result[In, Out]{}, err
}
anyConsumed = anyConsumed || r{{.}}.Consumed()
success, val{{.}}, next := r{{.}}.Status()
if !success {
2024-09-24 18:56:10 +00:00
return Fail[In, Out](anyConsumed, r{{.}}.Message()), nil
2024-09-14 00:28:38 +00:00
}
{{end}}
return Succeed(anyConsumed, val{{.Next}}, next, MessageOK(s.Pos())), nil
}
}{{end -}}
// GENERATED FILE. DO NOT EDIT.
// SPDX-License-Identifier: Unlicense
2024-09-14 00:28:38 +00:00
package {{.Package}}
{{range .Count}}
{{template "func" .}}
{{end}}