diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-30 22:10:35 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-30 22:10:35 +1300 |
| commit | 2e9ae8cc26dd8c004edb9231fa219059e037eebf (patch) | |
| tree | fc795f0cfa00cc78769c9a2a77a2d5924e8b3a51 | |
| parent | e4df40c822467d6e394b7522b7c4dae79c712692 (diff) | |
Re-implement assert in terms of and_then
| -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 |
