aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--spec/parcom_spec.cr7
-rw-r--r--src/parcom.cr17
2 files changed, 11 insertions, 13 deletions
diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr
index 346e61b..bdf8b5b 100644
--- a/spec/parcom_spec.cr
+++ b/spec/parcom_spec.cr
@@ -163,7 +163,6 @@ end
describe Assert do
test_f = ->(x : Char) { x == 't' }
- #p_proc = Assert.new(AnyToken(Char).new, test_f)
p = AnyToken(Char).new.assert { |x| x == 't' }
describe "#parse" do
@@ -245,15 +244,13 @@ end
describe Map do
describe "#parse" do
it "fails if the wrapped parser fails" do
- id = ->(x : Char) { x }
- p = Map.new(AnyToken(Char).new, id)
+ p = AnyToken(Char).new.map { |x| x }
expect_raises(ParserException) { p.parse(TokenStream.new([] of Char)) }
end
it "changes the result value via the provided proc" do
- is_letter = ->(x : Char) { x.letter? }
- p = Map.new(AnyToken(Char).new, is_letter)
+ p = AnyToken(Char).new.map { |x| x.letter? }
result = p.parse(TokenStream.from_string("testing"))
result.value.should be_true
diff --git a/src/parcom.cr b/src/parcom.cr
index 3aed672..de88a31 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -75,9 +75,12 @@ module Parcom
Assert.new(self, &block)
end
- # TODO: allow/change to support block invocation
+ def map(&block : V -> U) : Map(T, V, U) forall U
+ Map.new(self, &block)
+ end
+
def map(f : V -> U) : Map(T, V, U) forall U
- Map.new(self, f)
+ Map.new(self, &f)
end
def recover(default : V) : Recover(T, V)
@@ -167,9 +170,9 @@ module Parcom
end
end
- # TODO: allow/change to support block invocation
class Map(T, V, U) < Parser(T, U)
- def initialize(@p : Parser(T, V), @f : V -> U)
+ def initialize(@p : Parser(T, V), &block : V -> U)
+ @f = block
end
def parse(tokens : TokenStream(T)) : Result(T, U)
@@ -182,8 +185,7 @@ module Parcom
@p : Map(T, {V, Nil}, V)
def initialize(p : Parser(T, V))
- f = ->(tup : {V, Nil}) { tup[0] }
- @p = (p + Eof(T).new).map(f)
+ @p = (p + Eof(T).new).map &.first
end
def parse(tokens : TokenStream(T)) : Result(T, V)
@@ -206,8 +208,7 @@ module Parcom
@p : Map(T, V?, V)
def initialize(p : Parser(T, V), default : V)
- f = ->(x : V?) { x.nil? ? default : x }
- @p = Optional.new(p).map(f)
+ @p = Optional.new(p).map { |x| x.nil? ? default : x }
end
def parse(tokens : TokenStream(T)) : Result(T, V)