aboutsummaryrefslogtreecommitdiff
path: root/src/__OLD_parcom/map.cr
blob: 34961b574a60a39071cd8c4ca26c5d0e2ce555eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require "./parser.cr"

module Parcom
  # `Map` is a `Parser` that tries to parse using another parser,
  # and then changing the result of that parser to a different value.
  #
  # Example:
  # ```
  # # Where `Digits` is some parser that returns an array of digit characters
  # parse_i32 = Digits.new.map { |x| x.join.to_i32 }
  # result = parse_i32.parse(Tokens.from_string("1234"))
  # result.value # => 1234 (Int32)
  # ```
  class Map(T, V, U) < Parser(T, U)
    # Accepts the parser to use and the function to apply to the result.
    def initialize(@p : Parser(T, V), &block : V -> U)
      @f = block
    end

    # Tries to parse with the given parser and applies the
    # function to the result if successful.
    def parse(tokens : Tokens(T)) : Result(T, U)
      result = @p.parse(tokens)
      Result.new(result.tokens, @f.call(result.value))
    rescue ex : ParserFail
      raise ParserFail.new("Map: #{ex.message}")
    end
  end
end