From 6f93f16468d83b84ea386fcf74666a89e80bc704 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Sat, 11 Mar 2023 00:54:33 +1300 Subject: Implement SepBy --- spec/parcom_spec.cr | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'spec/parcom_spec.cr') diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index d67f19a..9b70c2e 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -637,15 +637,6 @@ describe Between do end end -pending StopAt do -end - -pending StopAfter do -end - -pending StopIf do -end - describe FirstOf do tokens = TokenStream.from_string("abcd") letter_a = Token.new('a') @@ -680,6 +671,43 @@ describe FirstOf do end end -pending SepBy do +describe SepBy do + describe "#parse" do + letter_a = Token.new('a') + comma = Token.new(',') + tokens = TokenStream.from_string("a,a,a,a") + + it "fails if no elements can be parsed" do + p = SepBy(Char, Char, Char).new(comma, comma) + expect_raises(ParserException) { p.parse(tokens) } + end + + it "succeeds if only one element can be parsed" do + t1 = TokenStream.from_string("a") + t2 = TokenStream.from_string("a,") + p = SepBy(Char, Char, Char).new(letter_a, comma) + + result = p.parse(t1) + result.value.should eq(['a']) + result.tokens.should eq(t1[1..]) + + result = p.parse(t2) + result.value.should eq(['a']) + result.tokens.should eq(t2[1..]) + end + + it "parses 1 element, then 0 or more (sep >> element)" do + p = SepBy(Char, Char, Char).new(letter_a, comma) + + result = p.parse(tokens) + result.value.should eq("aaaa".chars) + result.tokens.empty?.should be_true + + # drop last char in tokens, should parse three elements + result = p.parse(tokens[..5]) + result.value.should eq("aaa".chars) + result.tokens.should eq(TokenStream.from_string(",")) + end + end end -- cgit v1.2.1