yaparc 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +3 -0
- data/.rdoc_options +1 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README +105 -85
- data/Rakefile +21 -0
- data/lib/yaparc.rb +7 -2
- data/manifest.scm +1 -0
- data/sig/yaparc.gen.rbs +220 -0
- data/sig/yaparc.rbs +4 -0
- data/yaparc.gemspec +36 -0
- metadata +91 -58
- data/test/n3-report.html +0 -169
- data/test/n3.bnf +0 -129
- data/test/test_abc.rb.bak +0 -112
- data/test/test_calc.rb +0 -112
- data/test/test_lambda.rb +0 -87
- data/test/test_metric.rb +0 -617
- data/test/test_owl.rb +0 -1039
- data/test/test_parser.rb +0 -460
- data/test/test_prolog.rb +0 -287
- data/test/test_sql.rb +0 -317
- data/test/test_uri.rb +0 -752
data/test/test_parser.rb
DELETED
@@ -1,460 +0,0 @@
|
|
1
|
-
require 'lib/yaparc.rb'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'pp'
|
4
|
-
|
5
|
-
class YaparcTest < Test::Unit::TestCase
|
6
|
-
include ::Yaparc
|
7
|
-
|
8
|
-
def test_directory_structure
|
9
|
-
parser = Yaparc::Seq.new(Yaparc::Many.new(Yaparc::Regex.new(/\A[a-z]/),''),
|
10
|
-
Yaparc::Many.new(Yaparc::Seq.new(Yaparc::Symbol.new('/'),
|
11
|
-
Yaparc::Regex.new(/\A[a-z]*/)) do |_,path|
|
12
|
-
path
|
13
|
-
end,'')
|
14
|
-
) do |head, rest|
|
15
|
-
{:head => head,:rest => rest }
|
16
|
-
end
|
17
|
-
result = parser.parse("root/user")
|
18
|
-
assert_instance_of Result::OK, result
|
19
|
-
assert_equal Hash[:head=>"root", :rest=>"user"], result.value
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_seq_alt_parse
|
23
|
-
parser = Yaparc::Seq.new(Yaparc::Symbol.new('scheme'),
|
24
|
-
Yaparc::Symbol.new(':'),
|
25
|
-
Yaparc::Alt.new(Natural.new,
|
26
|
-
Identifier.new))
|
27
|
-
result = parser.parse("scheme:124")
|
28
|
-
assert_instance_of Result::OK, result
|
29
|
-
result = parser.parse("scheme:identifier")
|
30
|
-
assert_instance_of Result::OK, result
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_succeed_parse
|
34
|
-
parser = ::Yaparc::Succeed.new(1)
|
35
|
-
result = parser.parse("abs")
|
36
|
-
assert_equal 1, result.value
|
37
|
-
assert_equal "abs", result.input
|
38
|
-
# assert_equal 1, parser.tree
|
39
|
-
result = parser.parse("abs") do |answer|
|
40
|
-
answer
|
41
|
-
end
|
42
|
-
assert_equal 1, result.value
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_fail_parse
|
46
|
-
parser = ::Yaparc::Fail.new
|
47
|
-
result = parser.parse("abc")
|
48
|
-
assert_instance_of Result::Fail, result
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_item_parse
|
52
|
-
parser = ::Yaparc::Item.new
|
53
|
-
result = parser.parse("")
|
54
|
-
assert_instance_of Result::Fail, result
|
55
|
-
result = parser.parse("abc")
|
56
|
-
assert_equal "a", result.value
|
57
|
-
assert_equal "bc", result.input
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_satisfy_parse
|
61
|
-
is_integer = lambda do |i|
|
62
|
-
begin
|
63
|
-
Integer(i)
|
64
|
-
true
|
65
|
-
rescue
|
66
|
-
false
|
67
|
-
end
|
68
|
-
end
|
69
|
-
parser = Satisfy.new(is_integer)
|
70
|
-
result = parser.parse("123")
|
71
|
-
assert_equal "1", result.value
|
72
|
-
assert_equal "23", result.input
|
73
|
-
|
74
|
-
parser = Satisfy.new(is_integer)
|
75
|
-
result = parser.parse("abc")
|
76
|
-
assert_instance_of Result::Fail, result
|
77
|
-
|
78
|
-
is_char = lambda do |i|
|
79
|
-
begin
|
80
|
-
String(i)
|
81
|
-
true
|
82
|
-
rescue
|
83
|
-
false
|
84
|
-
end
|
85
|
-
end
|
86
|
-
parser = Satisfy.new(is_char)
|
87
|
-
result = parser.parse("abc")
|
88
|
-
assert_equal "a", result.value
|
89
|
-
assert_equal "bc", result.input
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_seq_parse
|
93
|
-
parser = Seq.new(Item.new, Item.new) do |item1, item2|
|
94
|
-
[item1, item2]
|
95
|
-
end
|
96
|
-
result = parser.parse("abcdef")
|
97
|
-
assert_equal ["a", "b"], result.value
|
98
|
-
assert_equal "cdef", result.input
|
99
|
-
|
100
|
-
parser = Seq.new(Item.new, Item.new, Item.new) do |item1, item2, item3|
|
101
|
-
[item1, item3]
|
102
|
-
end
|
103
|
-
result = parser.parse("ABCDEF")
|
104
|
-
assert_equal ["A", "C"], result.value
|
105
|
-
assert_equal "DEF", result.input
|
106
|
-
|
107
|
-
parser = Seq.new(Item.new, Item.new, Item.new) do |item1, item2, item3|
|
108
|
-
[item2]
|
109
|
-
end
|
110
|
-
result = parser.parse("ABCDEF")
|
111
|
-
assert_equal ["B"], result.value
|
112
|
-
assert_equal "DEF", result.input
|
113
|
-
|
114
|
-
parser = Seq.new(Digit.new, Many.new(Char.new('a'),''),Many.new(Digit.new,'')) do |digit, _,digits|
|
115
|
-
digit + digits
|
116
|
-
end
|
117
|
-
result = parser.parse("123abc")
|
118
|
-
assert_equal "123", result.value
|
119
|
-
assert_equal "abc", result.input
|
120
|
-
|
121
|
-
parser = Seq.new(
|
122
|
-
Yaparc::Apply.new(Yaparc::Tokenize.new(Digit.new)){|digit| digit}, Many.new(Char.new('a'),''),Many.new(Digit.new,'')) do |digit, _,digits|
|
123
|
-
digit + digits
|
124
|
-
end
|
125
|
-
result = parser.parse("123abc")
|
126
|
-
assert_equal "123", result.value
|
127
|
-
assert_equal "abc", result.input
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_seq_parse_without_block
|
131
|
-
parser = Seq.new(Item.new, Item.new)
|
132
|
-
result = parser.parse("abcdef")
|
133
|
-
assert_equal "b", result.value
|
134
|
-
assert_equal "cdef", result.input
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_alt_parse
|
138
|
-
parser = Alt.new(Item.new, Succeed.new('d'))
|
139
|
-
result = parser.parse("abc")
|
140
|
-
assert_equal "a", result.value
|
141
|
-
assert_equal "bc", result.input
|
142
|
-
|
143
|
-
parser = Alt.new(Fail.new, Succeed.new('d'))
|
144
|
-
result = parser.parse("abc")
|
145
|
-
assert_equal "d", result.value
|
146
|
-
assert_equal "abc", result.input
|
147
|
-
|
148
|
-
parser = Alt.new(Fail.new, Fail.new)
|
149
|
-
result = parser.parse("abc")
|
150
|
-
assert_instance_of Result::Fail, result
|
151
|
-
|
152
|
-
parser = Alt.new(Natural.new, Ident.new)
|
153
|
-
result = parser.parse("abc")
|
154
|
-
assert_instance_of Result::OK, result
|
155
|
-
result = parser.parse("124")
|
156
|
-
assert_instance_of Result::OK, result
|
157
|
-
result = parser.parse("ABC124")
|
158
|
-
assert_instance_of Result::Fail, result
|
159
|
-
result = parser.parse("abc124")
|
160
|
-
assert_instance_of Result::OK, result
|
161
|
-
assert_equal 'abc124', result.value
|
162
|
-
end
|
163
|
-
|
164
|
-
def test_apply_parse
|
165
|
-
is_digit = lambda {|i| i >= '0' and i <= '9'}
|
166
|
-
parser = Apply.new(Satisfy.new(is_digit)) do |digit|
|
167
|
-
digit.to_i - '0'.to_i
|
168
|
-
end
|
169
|
-
result = parser.parse('7')
|
170
|
-
assert_equal 7, result.value
|
171
|
-
assert_equal "", result.input
|
172
|
-
|
173
|
-
parser = Yaparc::Apply.new(Yaparc::Regex.new(/\d+/)) do |match|
|
174
|
-
Integer(match)
|
175
|
-
end
|
176
|
-
result = parser.parse('7')
|
177
|
-
assert_equal 7, result.value
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_char_parse
|
181
|
-
parser = Char.new("a")
|
182
|
-
result = parser.parse("abc")
|
183
|
-
assert_equal "a", result.value
|
184
|
-
assert_equal "bc", result.input
|
185
|
-
|
186
|
-
parser = Char.new("a")
|
187
|
-
result = parser.parse("123")
|
188
|
-
assert_instance_of Result::Fail, result
|
189
|
-
|
190
|
-
parser = Char.new("a", false)
|
191
|
-
result = parser.parse("a")
|
192
|
-
assert_instance_of Result::OK, result
|
193
|
-
result = parser.parse("A")
|
194
|
-
assert_instance_of Result::OK, result
|
195
|
-
result = parser.parse("!")
|
196
|
-
assert_instance_of Result::Fail, result
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_string_parse
|
200
|
-
parser = Yaparc::String.new("abc")
|
201
|
-
result = parser.parse("abcdef")
|
202
|
-
assert_equal "abc", result.value
|
203
|
-
assert_equal "def", result.input
|
204
|
-
|
205
|
-
parser = Yaparc::String.new("abc")
|
206
|
-
result = parser.parse("ab1234")
|
207
|
-
assert_instance_of Result::Fail, result
|
208
|
-
|
209
|
-
parser = Yaparc::String.new("abc", false)
|
210
|
-
result = parser.parse("abc")
|
211
|
-
assert_instance_of Result::OK, result
|
212
|
-
result = parser.parse("aBc")
|
213
|
-
assert_instance_of Result::OK, result
|
214
|
-
end
|
215
|
-
|
216
|
-
def test_regex_parse
|
217
|
-
parser = Regex.new(/\A[a-z]/)
|
218
|
-
result = parser.parse("abcdef")
|
219
|
-
assert_equal "a", result.value
|
220
|
-
assert_equal "bcdef", result.input
|
221
|
-
|
222
|
-
parser = Regex.new(/\A[0-9]+/)
|
223
|
-
result = parser.parse("1234ab")
|
224
|
-
assert_equal "1234", result.value
|
225
|
-
assert_equal "ab", result.input
|
226
|
-
|
227
|
-
parser = Regex.new(/\A[0-9]+/)
|
228
|
-
result = parser.parse("1234ab")
|
229
|
-
# result = parser.parse("1234ab") do |match|
|
230
|
-
# Integer(match)
|
231
|
-
# end
|
232
|
-
# assert_equal 1234, result.value
|
233
|
-
assert_equal '1234', result.value
|
234
|
-
|
235
|
-
# parser = Regex.new(/([0-9]+):([a-z]+)/)
|
236
|
-
# result = parser.parse_with_parameter("1234:ab") do |match1, match2|
|
237
|
-
# assert_equal 'ab', match2
|
238
|
-
# end
|
239
|
-
|
240
|
-
parser = Regex.new(/([0-9]+):([a-z]+)/) do |match1, match2|
|
241
|
-
[match2,match1]
|
242
|
-
end
|
243
|
-
result = parser.parse("1234:ab")
|
244
|
-
assert_equal ["ab", "1234"], result.value
|
245
|
-
end
|
246
|
-
|
247
|
-
def test_zero_one_parse
|
248
|
-
parser = ZeroOne.new(Yaparc::String.new("abc"))
|
249
|
-
result = parser.parse("abc ")
|
250
|
-
assert_equal "abc", result.value
|
251
|
-
assert_equal " ", result.input
|
252
|
-
parser = ZeroOne.new(Yaparc::String.new("abc"))
|
253
|
-
result = parser.parse("123")
|
254
|
-
assert_equal [], result.value
|
255
|
-
assert_equal "123", result.input
|
256
|
-
end
|
257
|
-
|
258
|
-
def test_many_parse
|
259
|
-
is_digit = Satisfy.new(lambda {|i| i >= '0' and i <= '9'})
|
260
|
-
parser = Many.new(is_digit,"")
|
261
|
-
result = parser.parse("123abc")
|
262
|
-
assert_equal "123", result.value
|
263
|
-
assert_equal "abc", result.input
|
264
|
-
|
265
|
-
result = parser.parse("abcdef")
|
266
|
-
assert_equal "", result.value
|
267
|
-
assert_equal "abcdef", result.input
|
268
|
-
|
269
|
-
parser = Many.new(Alt.new(Ident.new, Nat.new),'')
|
270
|
-
result = parser.parse("abc23def")
|
271
|
-
assert_equal "abc23def", result.value
|
272
|
-
|
273
|
-
parser = Many.new(Digit.new,0)
|
274
|
-
result = parser.parse("abc")
|
275
|
-
assert_instance_of Result::OK, result
|
276
|
-
end
|
277
|
-
|
278
|
-
def test_many_one_parse
|
279
|
-
is_digit = Satisfy.new(lambda {|i| i >= '0' and i <= '9'})
|
280
|
-
parser = ManyOne.new(is_digit,"")
|
281
|
-
result = parser.parse("123abc")
|
282
|
-
assert_equal "123", result.value
|
283
|
-
assert_equal "abc", result.input
|
284
|
-
|
285
|
-
result = parser.parse("abcdef")
|
286
|
-
assert_instance_of Result::Fail, result
|
287
|
-
assert_equal "abcdef", result.input
|
288
|
-
|
289
|
-
parser = ManyOne.new(Char.new('a'),'')
|
290
|
-
result = parser.parse("123abc")
|
291
|
-
assert_instance_of Result::Fail, result
|
292
|
-
end
|
293
|
-
|
294
|
-
def test_ident
|
295
|
-
parser = Ident.new
|
296
|
-
result = parser.parse("abc def")
|
297
|
-
assert_equal "abc", result.value
|
298
|
-
assert_equal " def", result.input
|
299
|
-
end
|
300
|
-
|
301
|
-
def test_digit
|
302
|
-
parser = Digit.new
|
303
|
-
result = parser.parse("123 abc")
|
304
|
-
assert_equal '1', result.value
|
305
|
-
assert_equal "23 abc", result.input
|
306
|
-
end
|
307
|
-
|
308
|
-
def test_nat
|
309
|
-
parser = Nat.new
|
310
|
-
result = parser.parse("123 abc")
|
311
|
-
assert_equal 123, result.value
|
312
|
-
assert_equal " abc", result.input
|
313
|
-
end
|
314
|
-
|
315
|
-
def test_nat_ident
|
316
|
-
parser = Seq.new(Nat.new, Ident.new) do |nat, ident|
|
317
|
-
[nat,ident]
|
318
|
-
end
|
319
|
-
result = parser.parse("123abc")
|
320
|
-
assert_equal [123, "abc"], result.value
|
321
|
-
assert_equal "", result.input
|
322
|
-
end
|
323
|
-
|
324
|
-
def test_space
|
325
|
-
parser = Space.new
|
326
|
-
result = parser.parse(" abc")
|
327
|
-
assert_instance_of Result::OK, result
|
328
|
-
assert_equal "abc", result.input
|
329
|
-
end
|
330
|
-
|
331
|
-
def test_whitespace
|
332
|
-
parser = WhiteSpace.new
|
333
|
-
result = parser.parse(" \n abc")
|
334
|
-
assert_instance_of Result::OK, result
|
335
|
-
assert_equal "abc", result.input
|
336
|
-
snip =<<-SNIP
|
337
|
-
|
338
|
-
abc
|
339
|
-
SNIP
|
340
|
-
result = parser.parse(snip)
|
341
|
-
assert_instance_of Result::OK, result
|
342
|
-
assert_equal "abc\n", result.input
|
343
|
-
end
|
344
|
-
|
345
|
-
# def test_token
|
346
|
-
# parser = Token.new
|
347
|
-
# result = parser.parse(" \n abc")
|
348
|
-
# assert_instance_of Result::OK, result
|
349
|
-
# assert_equal "abc", result.input
|
350
|
-
# end
|
351
|
-
|
352
|
-
def test_tokenize_with_block
|
353
|
-
parser = Tokenize.new(Ident.new) do |tokenize|
|
354
|
-
tokenize.prefix = Space.new
|
355
|
-
tokenize.postfix = Space.new
|
356
|
-
end
|
357
|
-
|
358
|
-
result = parser.parse(" abc")
|
359
|
-
assert_instance_of Result::OK, result
|
360
|
-
assert_equal "abc", result.value
|
361
|
-
assert_equal "", result.input
|
362
|
-
|
363
|
-
result = parser.parse(" \n abc")
|
364
|
-
assert_instance_of Result::Fail, result
|
365
|
-
assert_equal "\n abc", result.input
|
366
|
-
|
367
|
-
parser = Tokenize.new(Ident.new) do |tokenize|
|
368
|
-
tokenize.prefix = WhiteSpace.new
|
369
|
-
tokenize.postfix = WhiteSpace.new
|
370
|
-
end
|
371
|
-
|
372
|
-
result = parser.parse(" \n abc")
|
373
|
-
assert_instance_of Result::OK, result
|
374
|
-
assert_equal "abc", result.value
|
375
|
-
assert_equal "", result.input
|
376
|
-
end
|
377
|
-
|
378
|
-
def test_tokenize_without_block
|
379
|
-
parser = Tokenize.new(Ident.new, :prefix => WhiteSpace.new, :postfix => WhiteSpace.new)
|
380
|
-
|
381
|
-
result = parser.parse(" abc")
|
382
|
-
assert_instance_of Result::OK, result
|
383
|
-
assert_equal "abc", result.value
|
384
|
-
assert_equal "", result.input
|
385
|
-
|
386
|
-
parser = Tokenize.new(Ident.new, :prefix => Space.new, :postfix => Space.new)
|
387
|
-
result = parser.parse(" \n abc")
|
388
|
-
assert_instance_of Result::Fail, result
|
389
|
-
assert_equal "\n abc", result.input
|
390
|
-
end
|
391
|
-
|
392
|
-
|
393
|
-
def test_natural
|
394
|
-
parser = Natural.new
|
395
|
-
result = parser.parse(" 1234 ")
|
396
|
-
assert_equal 1234, result.value
|
397
|
-
assert_equal "", result.input
|
398
|
-
end
|
399
|
-
|
400
|
-
def test_symbol
|
401
|
-
parser = Symbol.new('%')
|
402
|
-
result = parser.parse(" % ")
|
403
|
-
assert_equal "%", result.value
|
404
|
-
assert_equal "", result.input
|
405
|
-
end
|
406
|
-
|
407
|
-
def test_literal_parser
|
408
|
-
parser = Literal.new('%')
|
409
|
-
result = parser.parse(" % ")
|
410
|
-
assert_equal "%", result.value
|
411
|
-
assert_equal "", result.input
|
412
|
-
|
413
|
-
parser = Literal.new('vgh', false)
|
414
|
-
result = parser.parse(" vgh ")
|
415
|
-
assert_instance_of Result::OK, result
|
416
|
-
result = parser.parse(" vgH ")
|
417
|
-
assert_instance_of Result::OK, result
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
|
422
|
-
class IdentifierParserTest < Test::Unit::TestCase
|
423
|
-
include ::Yaparc
|
424
|
-
|
425
|
-
def setup
|
426
|
-
@parser = ::Yaparc::Identifier.new
|
427
|
-
# @untokenized_parser = ::Yaparc::Identifier.new do |tokenize|
|
428
|
-
# tokenize.prefix = Space.new
|
429
|
-
# tokenize.postfix = Space.new
|
430
|
-
# end
|
431
|
-
end
|
432
|
-
|
433
|
-
def test_parse
|
434
|
-
result = @parser.parse("abc")
|
435
|
-
assert_equal "abc", result.value
|
436
|
-
assert_equal "", result.input
|
437
|
-
result = @parser.parse(" abc ")
|
438
|
-
assert_equal "abc", result.value
|
439
|
-
assert_equal "", result.input
|
440
|
-
result = @parser.parse(" _abc ")
|
441
|
-
assert_instance_of Result::OK, result
|
442
|
-
result = @parser.parse(" 0_abc ")
|
443
|
-
assert_instance_of Result::Fail, result
|
444
|
-
result = @parser.parse(" _00abc ")
|
445
|
-
assert_instance_of Result::OK, result
|
446
|
-
# result = @untokenized_parser.parse(" \n abc ")
|
447
|
-
# assert_instance_of Result::OK, result
|
448
|
-
# assert_equal "abc", result.value
|
449
|
-
# assert_equal "", result.input
|
450
|
-
end
|
451
|
-
|
452
|
-
def test_parse_with_keyword
|
453
|
-
parser_with_keyword = Identifier.new(:exclude => ["abc","efg"])
|
454
|
-
result = parser_with_keyword.parse("abc")
|
455
|
-
assert_instance_of Result::Fail, result
|
456
|
-
result = parser_with_keyword.parse(" xyz")
|
457
|
-
assert_equal "xyz", result.value
|
458
|
-
assert_equal "", result.input
|
459
|
-
end
|
460
|
-
end
|