From 035fa7da1412b0b801405b1be9ab50a2438a33e4 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 2 Apr 2025 15:50:12 -0600 Subject: [PATCH] Added Lazy combinator --- gigaparsec.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gigaparsec.go b/gigaparsec.go index 7471db6..0a2da77 100644 --- a/gigaparsec.go +++ b/gigaparsec.go @@ -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) + } +}