aboutsummaryrefslogtreecommitdiff
path: root/src/parcom.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-08 20:55:04 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-08 20:55:04 +1300
commite199329cbb23352fbd18c2eb2586b564ad9b206c (patch)
tree72e98e0339991ae194ad14a18568fde55dde69f3 /src/parcom.cr
parentb61aba7186cb7c616d6c8f9a3b9e6442173cbbf6 (diff)
Add Parser#recover
Diffstat (limited to 'src/parcom.cr')
-rw-r--r--src/parcom.cr10
1 files changed, 4 insertions, 6 deletions
diff --git a/src/parcom.cr b/src/parcom.cr
index 87c73c5..a2b8b92 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -74,6 +74,10 @@ module Parcom
def map(f : V -> U) : Map(T, V, U) forall U
Map.new(self, f)
end
+
+ def recover(default : T)
+ Recover.new(self, default)
+ end
end
class Flunk(T, V) < Parser(T, V)
@@ -209,12 +213,6 @@ module Parcom
def parse(tokens : TokenStream(T)) : Result(T, V?)
r = @p.parse?(tokens)
- if r.nil?
- Result.new(tokens, nil)
- else
- Result.new(r.tokens, r.value || nil)
- end
-
new_tokens = r.nil? ? tokens : r.tokens
new_value = r.nil? ? nil : r.value
Result.new(new_tokens, new_value)