diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-11 00:54:33 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-11 00:54:33 +1300 |
| commit | 6f93f16468d83b84ea386fcf74666a89e80bc704 (patch) | |
| tree | c532afb7b396a789720fd0b797a88e78d987f3a2 /spec | |
| parent | 27fb4fa0babc2c3db41c93865a35430515ff3630 (diff) | |
Implement SepBy
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/parcom_spec.cr | 48 |
1 files changed, 38 insertions, 10 deletions
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 |
