aboutsummaryrefslogtreecommitdiff
path: root/src/parcom.cr
blob: d7e66828140bb4c1864997aa255bec6058debd3b (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
module Parcom
  VERSION = "0.1.0"

  class ParserException < Exception
  end

  struct TokenStream(T)
    getter tokens

    def self.from_string(s : String) : TokenStream(Char)
      TokenStream.new(s.chars)
    end

    def initialize(ts : Iterable(T))
      if ts.responds_to?(:to_a)
        @tokens = ts.to_a
      else
        @tokens = [] of T
        ts.each { |t| @tokens << t }
      end
    end

    def [](index : Int) : T
      @tokens[index]
    end

    def [](start : Int, count : Int) : TokenStream(T)
      TokenStream.new(@tokens[start, count])
    end

    def [](range : Range) : TokenStream(T)
      TokenStream.new(@tokens[range])
    end

    def []?(*args)
      self.[](*args)
    rescue IndexError
      nil
    end

    def empty? : Bool
      @tokens.empty?
    end
  end

  struct Result(T, V)
    getter tokens, value

    def initialize(@tokens : TokenStream(T), @value : V)
    end
  end

  abstract class Parser(T, V)
    abstract def parse(tokens : TokenStream(T)) : Result(T, V)

    def parse?(tokens : TokenStream(T)) : Result(T, V)?
      self.parse(tokens)
    rescue
      return nil
    end

    def |(other : Parser(T, V)) : Alt(T, V)
      Alt.new(self, other)
    end

    def +(other : Parser(T, U)) : Plus(T, V, U) forall U
      Plus.new(self, other)
    end

    def assert(f : V -> Bool)
      Assert.new(self, &f)
    end

    def assert(&block : V -> Bool)
      Assert.new(self, &block)
    end

    def map(&block : V -> U) : Map(T, V, U) forall U
      Map.new(self, &block)
    end

    def map(f : V -> U) : Map(T, V, U) forall U
      Map.new(self, &f)
    end

    def recover(default : V) : Recover(T, V)
      Recover.new(self, default)
    end
  end

  class Flunk(T, V) < Parser(T, V)
    def parse(tokens) : Result(T, V)
      raise ParserException.new("Flunk: parsing failed")
    end
  end

  class AnyToken(T) < Parser(T, T)
    def parse(tokens : TokenStream(T)) : Result(T, T)
      if tokens.empty?
        raise ParserException.new("AnyToken: input was empty")
      else
        Result.new(tokens[1..], tokens[0])
      end
    end
  end

  class Eof(T) < Parser(T, Nil)
    def parse(tokens : TokenStream(T)) : Result(T, Nil)
      if tokens.empty?
        Result.new(tokens, nil)
      else
        raise ParserException.new("Eof: input was not empty")
      end
    end
  end

  class Peek(T, V) < Parser(T, V)
    def initialize(@p : Parser(T, V))
    end

    def parse(tokens : TokenStream(T)) : Result(T, V)
      result = @p.parse(tokens)
      Result.new(tokens, result.value)
    end
  end

  class Assert(T, V) < Parser(T, V)
    def initialize(@p : Parser(T, V), &block : V -> Bool)
      @f = block
    end

    def parse(tokens : TokenStream(T)) : Result(T, V)
      result = @p.parse(tokens)

      unless @f.call(result.value)
        raise ParserException.new("Assert: predicate failed for <#{result.value}>")
      end

      result
    end
  end

  class Satisfy(T) < Assert(T, T)
    def initialize(&block : T -> Bool)
      super(AnyToken(T).new, &block)
    end
  end

  class Token(T) < Parser(T, T)
    def initialize(@expected : T)
      @p = Satisfy(T).new { |x| x == @expected }
    end

    def parse(tokens : TokenStream(T)) : Result(T, T)
      @p.parse(tokens)
    rescue ex : ParserException
      raise ParserException.new("Token <#{@expected}>: #{ex.message}")
    end
  end

  class Alt(T, V) < Parser(T, V)
    def initialize(@p1 : Parser(T, V), @p2 : Parser(T, V))
    end

    def parse(tokens : TokenStream(T)) : Result(T, V)
      @p1.parse(tokens)
    rescue ParserException
      @p2.parse(tokens)
    end
  end

  class Map(T, V, U) < Parser(T, U)
    def initialize(@p : Parser(T, V), &block : V -> U)
      @f = block
    end

    def parse(tokens : TokenStream(T)) : Result(T, U)
      result = @p.parse(tokens)
      Result.new(result.tokens, @f.call(result.value))
    end
  end

  class Phrase(T, V)
    @p : Map(T, {V, Nil}, V)

    def initialize(p : Parser(T, V))
      @p = (p + Eof(T).new).map &.first
    end

    def parse(tokens : TokenStream(T)) : Result(T, V)
      @p.parse(tokens)
    end
  end

  class Plus(T, V, U) < Parser(T, {V, U})
    def initialize(@p1 : Parser(T, V), @p2 : Parser(T, U))
    end

    def parse(tokens : TokenStream(T)) : Result(T, {V, U})
      r1 = @p1.parse(tokens)
      r2 = @p2.parse(r1.tokens)
      Result.new(r2.tokens, {r1.value, r2.value})
    end
  end

  class Recover(T, V) < Parser(T, V)
    @p : Map(T, V?, V)

    def initialize(p : Parser(T, V), default : V)
      @p = Optional.new(p).map { |x| x.nil? ? default : x }
    end

    def parse(tokens : TokenStream(T)) : Result(T, V)
      @p.parse(tokens)
    end
  end

  class Optional(T, V) < Parser(T, V?)
    def initialize(@p : Parser(T, V))
    end

    def parse(tokens : TokenStream(T)) : Result(T, V?)
      r = @p.parse?(tokens)

      new_tokens = r.nil? ? tokens : r.tokens
      new_value = r.nil? ? nil : r.value
      Result.new(new_tokens, new_value)
    end
  end

  class Sequence(T, V) < Parser(T, Array(V))
    def initialize(@ps : Iterable(Parser(T, V)))
    end

    # TODO: this can probably be optimised more for Arrays
    # TODO: might be better to use #zip
    def parse(tokens : TokenStream(T)) : Result(T, Array(V))
      parsed = [] of V

      @ps.each do |p|
        r = p.parse(tokens)
        parsed << r.value
        tokens = r.tokens
      end

      Result.new(tokens, parsed)
    end
  end

  class Tokens(T) < Parser(T, Array(T))
    @p : Sequence(T, T)

    def initialize(expected : Iterable(T))
      ps = [] of Parser(T, T)
      expected.each { |t| ps << Token.new(t) }

      @p = Sequence.new(ps)
    end

    def parse(tokens : TokenStream(T)) : Result(T, Array(T))
      @p.parse(tokens)
    end
  end

  class Many(T, V) < Parser(T, Array(V))
    def initialize(@p : Parser(T, V))
    end

    def parse(tokens : TokenStream(T)) : Result(T, Array(V))
      parsed = [] of V

      loop do
        result = @p.parse?(tokens)
        if result.nil?
          break
        else
          parsed << result.value
          tokens = result.tokens
        end
      end

      Result.new(tokens, parsed)
    end
  end

  class Some(T, V) < Parser(T, Array(V))
    @p : Assert(T, Array(V))

    def initialize(p : Parser(T, V))
      @p = Many.new(p).assert { |arr| !arr.empty? }
    end

    def parse(tokens : TokenStream(T)) : Result(T, Array(V))
      @p.parse(tokens)
    end
  end

  class Exactly
  end

  class AtLeast
  end

  class AtMost
  end

  class Between
  end

  class StopAt
  end

  class StopAfter
  end

  class StopIf
  end

  class FirstOf
  end

  class SepBy
  end
end