diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-08 20:22:33 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-08 20:22:33 +1300 |
| commit | 61a58de90c2781562aacc8006616e97a3ff12005 (patch) | |
| tree | d5764dd7c8aef213e4999cfeb5bcc798a4bb0c3b | |
| parent | f7b089d954cb028ee3c46ad45f0f81ae2e5386cf (diff) | |
Implement recover
| -rw-r--r-- | spec/parcom_spec.cr | 21 | ||||
| -rw-r--r-- | src/parcom.cr | 8 |
2 files changed, 27 insertions, 2 deletions
diff --git a/spec/parcom_spec.cr b/spec/parcom_spec.cr index c395636..d9af3b9 100644 --- a/spec/parcom_spec.cr +++ b/spec/parcom_spec.cr @@ -336,7 +336,26 @@ describe Phrase do end end -pending Recover do +describe Recover do + p = Recover.new(Token.new('t'), '@') + + describe "#parse" do + it "succeeds and returns the wrapped parser's value if it succeeds" do + tokens = TokenStream.from_string("testing") + result = p.parse(tokens) + + result.tokens.should eq(tokens[1..]) + result.value.should eq('t') + end + + it "succeeds and returns the default value without modifying the input of the wrapped parser fails" do + tokens = TokenStream.from_string("_____") + result = p.parse(tokens) + + result.tokens.should eq(tokens) + result.value.should eq('@') + end + end end pending Optional do diff --git a/src/parcom.cr b/src/parcom.cr index 4d49a97..f102728 100644 --- a/src/parcom.cr +++ b/src/parcom.cr @@ -192,7 +192,13 @@ module Parcom end end - class Recover + class Recover(T, V) < Parser(T, V) + def initialize(@p : Parser(T, V), @default : V) + end + + def parse(tokens : TokenStream(T)) : Result(T, V) + @p.parse?(tokens) || Result.new(tokens, @default) + end end class Optional |
