From 8835f1de0dbc6a059a42fad78d4acbb0984b94e2 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 8 Mar 2023 20:03:26 +1300 Subject: Implement Plus --- spec/parcom_spec.cr | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'spec/parcom_spec.cr') diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index b2a3a80..f35465c 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -261,10 +261,56 @@ describe Map do end end -pending Phrase do +describe Plus do + describe "#parse" do + tokens = TokenStream.from_string("testing") + p_t = Token(Char).new('t') + p_e = Token(Char).new('e') + p_at = Token(Char).new('@') + + it "fails if the first parser fails" do + p = p_at + p_e + expect_raises(ParserException) { p.parse(tokens) } + end + + it "fails if the second parser fails" do + p = p_t + p_at + expect_raises(ParserException) { p.parse(tokens) } + end + + it "fails if both parsers fail" do + p = p_at + p_at + expect_raises(ParserException) { p.parse(tokens) } + end + + it "succeeds if both parsers succeed" do + p = p_t + p_e + result = p.parse(tokens) + + result.tokens.should eq(tokens[2..]) + result.value[0].should eq('t') + result.value[1].should eq('e') + end + + it "evaluates parsers from left to right (left associative)" do + p_succeeds = p_t + p_e + p_fails = p_e + p_t + + p_succeeds.parse(tokens) # should not raise an exception + expect_raises(ParserException) { p_fails.parse(tokens) } + + p_s = Token(Char).new('s') + + r = (p_t + p_e + p_s).parse(tokens) # should not raise an exception + r.value.should be_a({ {Char, Char}, Char}) + + r = (p_t + (p_e + p_s)).parse(tokens) # should not raise an exception + r.value.should be_a({Char, {Char, Char} }) + end + end end -pending Plus do +pending Phrase do end pending Recover do -- cgit v1.2.1