aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-09 00:49:16 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-09 00:49:16 +1300
commitce254c83be3cf613a8f655f55f2207073404148f (patch)
tree7dc6b40ed46b57ba2d1805cb346f366642e2237a
parentca4ef7c14c8398b24db54c58bb4062607eb44067 (diff)
Rewrite Assert to use blocks
-rw-r--r--spec/parcom_spec.cr7
-rw-r--r--src/parcom.cr12
2 files changed, 12 insertions, 7 deletions
diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr
index 00c59a0..346e61b 100644
--- a/spec/parcom_spec.cr
+++ b/spec/parcom_spec.cr
@@ -163,11 +163,14 @@ end
describe Assert do
test_f = ->(x : Char) { x == 't' }
- p = Assert.new(AnyToken(Char).new, test_f)
+ #p_proc = Assert.new(AnyToken(Char).new, test_f)
+ p = AnyToken(Char).new.assert { |x| x == 't' }
describe "#parse" do
it "fails if the wrapped parser fails" do
- expect_raises(ParserException) { p.parse(TokenStream.new([] of Char)) }
+ expect_raises(ParserException) do
+ p.parse(TokenStream.new([] of Char))
+ end
end
it "fails if the result value fails the test" do
diff --git a/src/parcom.cr b/src/parcom.cr
index dbdd44d..3aed672 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -67,9 +67,12 @@ module Parcom
Plus.new(self, other)
end
- # TODO: allow/change to support block invocation
def assert(f : V -> Bool)
- Assert.new(self, f)
+ Assert.new(self, &f)
+ end
+
+ def assert(&block : V -> Bool)
+ Assert.new(self, &block)
end
# TODO: allow/change to support block invocation
@@ -118,9 +121,9 @@ module Parcom
end
end
- # TODO: allow/change to support block invocation
class Assert(T, V) < Parser(T, V)
- def initialize(@p : Parser(T, V), @f : V -> Bool)
+ def initialize(@p : Parser(T, V), &block : V -> Bool)
+ @f = block
end
def parse(tokens : TokenStream(T)) : Result(T, V)
@@ -212,7 +215,6 @@ module Parcom
end
end
- # TODO: find a way to make this use Recover on the back end
class Optional(T, V) < Parser(T, V?)
def initialize(@p : Parser(T, V))
end