From 27a2d4281c5b270d6f3bba06ff1fd82ebfb94df6 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 9 Mar 2023 01:13:08 +1300 Subject: Rewrite Satisfy to use blocks --- src/parcom.cr | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/parcom.cr b/src/parcom.cr index de88a31..14d2e44 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -140,22 +140,19 @@ module Parcom end end - # TODO: allow/change to support block invocation - class Satisfy(T) < Parser(T, T) - def initialize(@f : T -> Bool) - end - - def parse(tokens : TokenStream(T)) : Result(T, T) - AnyToken(T).new.assert(@f).parse(tokens) + class Satisfy(T) < Assert(T, T) + def initialize(&block : T -> Bool) + super(AnyToken(T).new, &block) end end class Token(T) < Parser(T, T) def initialize(@expected : T) + @p = Satisfy(T).new { |x| x == @expected } end def parse(tokens : TokenStream(T)) : Result(T, T) - Satisfy(T).new(->(x : T) { x == @expected }).parse(tokens) + @p.parse(tokens) end end -- cgit v1.2.1