Now I understand how Try works

This commit is contained in:
Brandon Dyck 2024-09-02 12:48:48 -06:00
parent ffe949b97b
commit 0a1fe0821a

View File

@ -85,6 +85,11 @@ func Choose[In, Out any](p, q Parser[In, Out]) Parser[In, Out] {
}
}
// Try behaves identically to p, except that if p returns an error,
// Try will pretend that no input was consumed. This allows infinite
// lookahead: Since Choose only calls another parser when the previous
// parser consumed nothing, Try will allow backing out of a complex
// parser that did partially succeeded.
func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
return func(input State[In]) (bool, Result[In, Out], error) {
consumed, reply, err := p(input)