File: test_kpeg.rb

package info (click to toggle)
ruby-kpeg 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: ruby: 11,839; makefile: 10
file content (416 lines) | stat: -rw-r--r-- 9,419 bytes parent folder | download
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
require 'minitest/autorun'
require 'kpeg'
require 'stringio'

class TestKPeg < Minitest::Test
  def assert_match(m, str)
    assert_kind_of KPeg::MatchString, m
    assert_equal str, m.string
  end

  def test_dot
    gram = KPeg.grammar do |g|
      g.root = g.dot
    end

    assert_match KPeg.match("q", gram), "q"
  end

  def test_str
    gram = KPeg.grammar do |g|
      g.root = g.str("hello")
    end

    assert_match KPeg.match("hello", gram), "hello"
    assert_nil KPeg.match("vador", gram)
  end

  def test_reg
    gram = KPeg.grammar do |g|
      g.root = g.reg(/[0-9]/)
    end

    assert_match KPeg.match("3", gram), "3"
  end

  def test_char_range
    gram = KPeg.grammar do |g|
      g.root = g.range('0', '9')
    end

    assert_match KPeg.match("3", gram), "3"
  end

  def test_any
    gram = KPeg.grammar do |g|
      g.root = g.any g.str("hello"), g.str("chicken")
    end

    assert_match KPeg.match("hello", gram), "hello"
    assert_match KPeg.match("chicken", gram), "chicken"
    assert_nil KPeg.match("vador", gram)
  end

  def test_maybe
    gram = KPeg.grammar do |g|
      g.root = g.maybe g.str("hello")
    end

    m = KPeg.match "hello", gram
    assert_kind_of KPeg::Match, m
    assert_equal 1, m.matches.size
    assert_match m.matches[0], "hello"

    m = KPeg.match "surprise", gram
    assert_kind_of KPeg::Match, m
    assert_equal 0, m.matches.size
  end

  def test_many
    gram = KPeg.grammar do |g|
      g.root = g.many g.str("run")
    end

    m = KPeg.match "runrunrun", gram
    assert_kind_of KPeg::Match, m
    assert_equal 3, m.matches.size
    m.matches.each do |sm|
      assert_match sm, "run"
    end

    assert_nil KPeg.match("vador", gram)
  end

  def test_kleene
    gram = KPeg.grammar do |g|
      g.root = g.kleene g.str("run")
    end

    m = KPeg.match "runrunrun", gram
    assert_kind_of KPeg::Match, m
    assert_equal 3, m.matches.size
    m.matches.each do |sm|
      assert_match sm, "run"
    end

    m = KPeg.match "chicken", gram
    assert_kind_of KPeg::Match, m
    assert_equal 0, m.matches.size
  end

  def test_multiple
    gram = KPeg.grammar do |g|
      g.root = g.multiple g.str("run"), 2, 4
    end

    m = KPeg.match "runrun", gram
    assert_kind_of KPeg::Match, m
    assert_equal 2, m.matches.size
    m.matches.each do |sm|
      assert_match sm, "run"
    end

    m = KPeg.match "runrunrun", gram
    assert_kind_of KPeg::Match, m
    assert_equal 3, m.matches.size
    m.matches.each do |sm|
      assert_match sm, "run"
    end

    m = KPeg.match "runrunrunrun", gram
    assert_kind_of KPeg::Match, m
    assert_equal 4, m.matches.size
    m.matches.each do |sm|
      assert_match sm, "run"
    end

    assert_nil KPeg.match("run", gram) 
    assert_nil KPeg.match("runrunrunrunrun", gram) 
    assert_nil KPeg.match("vador", gram) 
  end

  def test_seq
    gram = KPeg.grammar do |g|
      g.root = g.seq g.str("hello"), g.str(", world")
    end

    m = KPeg.match "hello, world", gram
    assert_kind_of KPeg::Match, m
    assert_match m.matches[0], "hello"
    assert_match m.matches[1], ", world"

    assert_equal m.value, ["hello", ", world"]

    assert_nil KPeg.match("vador", gram)
    assert_nil KPeg.match("hello, vador", gram)
  end

  def test_andp
    gram = KPeg.grammar do |g|
      g.root = g.seq g.andp(g.str("h")), g.str("hello")
    end

    m = KPeg.match "hello", gram
    assert_equal m.matches.size, 2
    assert_match m.matches[0], ""
    assert_match m.matches[1], "hello"
  end

  def test_notp
    gram = KPeg.grammar do |g|
      g.root = g.seq g.notp(g.str("g")), g.str("hello")
    end

    m = KPeg.match "hello", gram
    assert_equal m.matches.size, 2
    assert_match m.matches[0], ""
    assert_match m.matches[1], "hello"
  end

  def test_ref
    gram = KPeg.grammar do |g|
      g.greeting = g.str("hello")
      g.root = g.ref "greeting"
    end

    m = KPeg.match "hello", gram
    assert_match m, "hello"
  end

  def test_invoke
    gram = KPeg.grammar do |g|
      g.greeting = g.str("hello")
      g.root = g.invoke "greeting"
    end

    m = KPeg.match "hello", gram
    assert_match m, "hello"
  end

  def test_foreign_ref
    g1 = KPeg.grammar do |g|
      g.greeting = "hello"
    end

    g2 = KPeg.grammar do |g|
      g.root = g.ref("greeting", g1)
    end

    m = KPeg.match "hello", g2
    assert_match m, "hello"
  end

  def test_foreign_ref_with_ref
    g1 = KPeg.grammar do |g|
      g.name = ", evan"
      g.greeting = g.seq("hello", :name)
    end

    g2 = KPeg.grammar do |g|
      g.root = g.ref("greeting", g1)
    end

    m = KPeg.match "hello, evan", g2
    assert_match m.matches[0], "hello"
    assert_match m.matches[1], ", evan"
  end

  def test_tag_with_name
    gram = KPeg.grammar do |g|
      g.root = g.seq(" ", g.t("hello", "greeting"))
    end

    m = KPeg.match " hello", gram

    assert_equal 2, m.matches.size
    tag = m.matches[1]
    assert_kind_of KPeg::Tag, tag.op
    assert_equal 1, tag.matches.size
    assert_match tag.matches[0], "hello"

    # show that tag influences the value of the sequence
    assert_equal m.value, "hello"
  end

  def test_tag_without_name
    gram = KPeg.grammar do |g|
      g.root = g.seq(" ", g.t("hello"))
    end

    m = KPeg.match " hello", gram
    assert_equal m.value, "hello"
  end

  def test_action
    gram = KPeg.grammar do |g|
      g.root = g.seq("hello", g.action("b + c"))
    end

    m = KPeg.match "hello", gram
    assert_equal 2, m.matches.size
    assert_match m.matches[0], "hello"

    action = m.matches[1]
    assert_equal action.op.action, "b + c"
  end

  def test_naming
    gram = KPeg.grammar do |g|
      g.greeting = g.str("hello")
      g.root = g.greeting
    end

    m = KPeg.match "hello", gram
    assert_match m, "hello"
  end

  def test_matching_curly
    gram = KPeg.grammar do |g|
      g.curly = g.seq("{", g.kleene(g.any(/[^{}]+/, :curly)), "}")
      g.root = :curly
    end

    m = KPeg.match "{ hello }", gram
    assert_match m.matches[0], "{"
    assert_match m.matches[1].matches[0], " hello "
    assert_match m.matches[2], "}"

    parc = KPeg::Parser.new "{ foo { bar } }", gram
    m = parc.parse
    assert_equal "{ foo { bar } }", m.total_string

    parc = KPeg::Parser.new "{ foo {\nbar }\n }", gram
    m = parc.parse
    assert_equal "{ foo {\nbar }\n }", m.total_string
  end

  def test_collect
    gram = KPeg.grammar do |g|
      g.root = g.collect(g.many(/[a-z]/))
    end

    m = KPeg.match "hellomatch", gram
    assert_equal "hellomatch", m.value
  end

  def test_memoization
    gram = KPeg.grammar do |g|
      g.one = g.str("1")
      g.two = g.str("2")

      g.root = g.any(
        [:one, "-", :two],
        [:one, "+", :two]
        )
    end

    parser = KPeg::Parser.new "1+2", gram
    m = parser.parse

    assert_equal 3, m.matches.size
    assert_match m.matches[0], "1"
    assert_match m.matches[1], "+"
    assert_match m.matches[2], "2"

    one = gram.find("one")
    two = gram.find("two")

    # We try 1 twice
    assert_equal 2, parser.memoizations[one][0].uses

    # but we only get as far as 2 once
    assert_equal 1, parser.memoizations[two][2].uses
  end

  def test_left_recursion
    gram = KPeg.grammar do |g|
      g.num  = g.reg(/[0-9]/)
      g.expr = g.any [:expr, "-", :num], :num

      g.root = g.expr
    end

    parser = KPeg::Parser.new "1-2-3", gram

    m = parser.parse
    assert_equal 3, m.matches.size

    left = m.matches[0]
    assert_equal 3, left.matches.size
    assert_match left.matches[0], "1"
    assert_match left.matches[1], "-"
    assert_match left.matches[2], "2"
    assert_match m.matches[1], "-"
    assert_match m.matches[2], "3"

    parser = KPeg::Parser.new "hello", gram
    m = parser.parse

    assert_nil m
  end

  def test_math_grammar
    gram = KPeg.grammar do |g|
      g.num = '0'..'9'
      g.term = g.seq(:term, "+", :term) \
             | g.seq(:term, "-", :term) \
             | :fact

      g.fact = g.seq(:fact, "*", :fact) \
             | g.seq(:fact, "/", :fact) \
             | :num

      g.root = g.term
    end

    sub = KPeg.match "4*3-8/9", gram
    mul = sub.matches[0]
    div = sub.matches[2]

    assert_match mul.matches[0], "4"
    assert_match mul.matches[1], "*"
    assert_match mul.matches[2], "3"

    assert_match sub.matches[1], "-"

    assert_match div.matches[0], "8"
    assert_match div.matches[1], "/"
    assert_match div.matches[2], "9"
  end

  def test_calc
    vars = {}
    gram = KPeg.grammar do |g|
      g.spaces = /\s*/
      g.var = 'a'..'z'
      g.num = g.lit(/[0-9]+/) { |i| i.to_i }

      g.pri = g.seq(:spaces, :var)   { |s,v| vars[v] } \
            | g.seq(:spaces, :num)   { |s,n| n }       \
            | g.seq('(', :expr, ')') { |_,e,_| e }

      g.mul = g.seq(:mul, "*", :pri) { |x,_,y| x * y } \
            | g.seq(:mul, "/", :pri) { |x,_,y| x / y } \
            | :pri

      g.add = g.seq(:add, "+", :mul) { |x,_,y| x + y } \
            | g.seq(:add, "-", :mul) { |x,_,y| x - y } \
            | :mul

      g.expr = g.seq(:var, "=", :expr) { |v,_,e| vars[v] = e } \
             | :add

      g.root = g.seq(g.kleene(:expr), :spaces) { |e,_| e }
    end

    m = KPeg.match "3+4*5", gram
    assert_equal 23, m.value

    m = KPeg.match "x=2", gram
    assert_equal 2, m.value

    m = KPeg.match "x=x*7", gram
    assert_equal 14, m.value
  end

end