aboutsummaryrefslogtreecommitdiff
path: root/src/__OLD_parcom/at_least.cr
blob: 2fb8dcf87586feeaf1faed92a55448bb8488cd47 (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
require "./parser.cr"
require "./map.cr"
require "./exactly.cr"

module Parcom
  # `AtLeast` is a `Parser` that tries to parser with another parser
  # a specific number of times. The results of each successful parse
  # are returned in an array. If the number of successes is less than
  # the specified number, the parser fails.
  class AtLeast(T, V) < Parser(T, Array(V))
    @p : Map(T, {Array(V), Array(V)}, Array(V))

    # Accepts the number of parsing attempts, and the parser to use.
    # If a negative int is given, it is treated as if it were 0.
    def initialize(i : Int, p : Parser(T, V))
      @p = (Exactly.new(i, p) + Many.new(p)).map do |tup|
        tup[0] + tup[1]
      end
    end

    # Tries to parse the given number of times, or more.
    def parse(tokens : Tokens(T)) : Result(T, Array(V))
      @p.parse(tokens)
    rescue ex : ParserFail
      raise ParserFail.new("AtLeast: #{ex.message}")
    end
  end
end