Added Lazy combinator

This commit is contained in:
Brandon Dyck 2025-04-02 15:50:12 -06:00
parent 82ed6b5546
commit 035fa7da14

View File

@ -403,3 +403,12 @@ func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
}
}
}
// Lazy delays creating a parser from p until the parser is called.
// This is useful for preventing recursive function calls in the
// definition of a recursive parser.
func Lazy[In, Out any](p func() Parser[In, Out]) Parser[In, Out] {
return func(s State[In]) (Result[In, Out], error) {
return p()(s)
}
}