From 61a58de90c2781562aacc8006616e97a3ff12005 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 8 Mar 2023 20:22:33 +1300 Subject: Implement recover --- src/parcom.cr | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/parcom.cr b/src/parcom.cr index 4d49a97..f102728 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -192,7 +192,13 @@ module Parcom end end - class Recover + class Recover(T, V) < Parser(T, V) + def initialize(@p : Parser(T, V), @default : V) + end + + def parse(tokens : TokenStream(T)) : Result(T, V) + @p.parse?(tokens) || Result.new(tokens, @default) + end end class Optional -- cgit v1.2.1