From 2e9ae8cc26dd8c004edb9231fa219059e037eebf Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 30 Mar 2023 22:10:35 +1300 Subject: Re-implement assert in terms of and_then --- src/parcom/parser.cr | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr index 2b058d4..d8f44c8 100644 --- a/src/parcom/parser.cr +++ b/src/parcom/parser.cr @@ -178,12 +178,12 @@ module Parcom # Creates a new parser from `self` that behaves the same, but fails # if the result does not satisfy the given predicate. def assert(f : U -> Bool) : Parser(T, U) - Parser(T, U).new("#{@name} (assertion)") do |tokens| - result = self.parse(tokens) - unless f.call(result.value) - raise ParserFail.new("Assertion failed for value #{result.value}") + self.and_then do |x| + if f.call(x) + Parser(T, U).pure(x) + else + Parser(T, U).flunk("Value <#{x}> failed assertion") end - result end end -- cgit v1.2.1