diff options
| -rw-r--r-- | src/parcom/parser.cr | 10 |
1 files changed, 5 insertions, 5 deletions
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 |
