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
31
32
33
34
35
36
37
38
|
require "./parser.cr"
require "./map.cr"
require "./many.cr"
module Parcom
# `SepBy` is a `Parser` that tries to parse one or more times with one
# parser, alternating with a second parser.
#
# Example:
# ```
# letter_a = Token.new('a')
# letter_b = Token.new('b')
# p = SepBy(Char, Car, Char).new(letter_a, letter_b)
# tokens = Tokens.from_string("ababababa")
#
# result = p.parse(tokens)
# puts result.value # => ['a', 'a', 'a', 'a', 'a']
# ```
class SepBy(T, V, U) < Parser(T, Array(V))
@p : Map(T, {V, Array(V)}, Array(V))
# Accepts the parser that parses the result values, and the
# parser that parses the sepatators.
def initialize(elem : Parser(T, V), sep : Parser(T, U))
@p = (elem + Many(T, U).new(sep >> elem)).map do |tup|
[tup[0]] + tup[1]
end
end
# Tries to parse, alternating the first and second parsers.
def parse(tokens : Tokens(T)) : Result(T, Array(V))
@p.parse(tokens)
rescue ex : ParserFail
raise ParserFail.new("SepBy: #{ex.message}")
end
end
end
|