diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-21 21:13:20 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-21 21:13:20 +1300 |
| commit | 6a27c77a03b64a838ba004ab87780113b5640cc9 (patch) | |
| tree | b5f144a2bedf185a8348290c847e61c71519cc8f /src | |
| parent | 082d54e05a41cde31e2d8378503c4880eceb5c17 (diff) | |
Initial implementation for and_then, I'm not convinced it fully works
Diffstat (limited to 'src')
| -rw-r--r-- | src/parcom/parser.cr | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr index d91ac84..fd161f9 100644 --- a/src/parcom/parser.cr +++ b/src/parcom/parser.cr @@ -266,6 +266,32 @@ module Parcom (p1 + p2).map(&.last).named("#{p1.name} >> #{p2.name}") end + # Creates a new parser from `self` and a function based on + # the result of `self`. `#and_then` is the monadic combinator + # and works like `and_then` in Rust, or like `>>=` in Haskell. + # Example: + # ``` + # p_string = ->(s : String) do + # Parser.token_sequence(s.chars).map(&.join) + # end + # p_any_word = Parser(Char, Array(Char)).satisfy(&.letter?).some.map(&.join) + # space = Parser(Char, Array(Char).satisfy(&.whitespace?).some + # # Parses two of the same word, with whitespace between: + # two_words = (p_any_word << space).and_then(p_string) + # ``` + def and_then(f : U -> Parser(T, V)) : Parser(T, V) forall V + p = self + Parser(T, V).new("#{p.name} and_then..") do |tokens| + r = p.parse(tokens) + f.call(r.value).parse(r.tokens) + end + end + + # :ditto: + def and_then(&block : U -> Parser(T, V)) : Parser(T, V) forall V + and_then(block) + end + # Creates a new parser from `self` that parses with `self` as many times # as possible. Returns an empty list if it never succeeds. def many : Parser(T, Array(U)) |
