functional_parser 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b47c35eaea8d03f51311f1718b79c5d3e70729c
4
- data.tar.gz: 0b740be01743b668e8ac37cb5f77f566d3705cdc
3
+ metadata.gz: 3f0239c77b5a66e4fbbdf3cb1e9f2f6de6ecd4db
4
+ data.tar.gz: 500d228375743ff5269423b0ae49f26ac5ffc116
5
5
  SHA512:
6
- metadata.gz: 397c8502ff4799d894d7f64b3151570100341e1b59f9d46fee169ce64e0adebf6122f2feb84f20ee2a5e83a40697cc6645a839717fa28e9628dc34d252fa3e61
7
- data.tar.gz: d715b861ff57f0f44b7c278105e86c5207b9a88dea8c8a394c54c4087f2f777794777e20ec0d2adf188b169d3ad93f9556123ffedc75c86fd15ae4949c3fa77f
6
+ metadata.gz: 82bf4200becd1955faf6290d8e146c875136e26ef85b01d87c26ca2c6e756acb0d055049290d08630bfb96c9c93dcbf40e54cf591b77643d5d71bc6592541b03
7
+ data.tar.gz: 60a07a5ee75212a6962eafad70f34323e9a8946d03a3cb372aa7690ac54650b58adc1a75db5e2148099301db93773005d57f3dc41c449392f9bca9f3d79fd9b9
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FunctionalParser
2
2
 
3
- TODO: Write a gem description
3
+ Yet another functional library for text-parsing by Ruby.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,14 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ ```ruby
24
+ require 'functional_parser'
25
+
26
+ P = FunctionalParser
27
+ p P.many(P.identifier).parse("hoge fuga piyo")
28
+ #=> #<FunctionalParser::Succeeded:0x007f86277d0a00 @parsed=["hoge", "fuga", "piyo"], @rest="">
29
+ ```
30
+
24
31
 
25
32
  ## Contributing
26
33
 
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |test|
5
+ test.test_files = Dir["unit_test/**/test_*.rb"]
6
+ test.verbose = true
7
+ end
2
8
 
@@ -69,9 +69,9 @@ class FunctionalParser
69
69
  }
70
70
  end
71
71
 
72
- def append(parser1, parser2)
72
+ def self.append(parser1, parser2)
73
73
  parser1 >> proc{|x|
74
- parer2 >> proc{|y|
74
+ parser2 >> proc{|y|
75
75
  ret(x + y)
76
76
  }
77
77
  }
@@ -1,3 +1,3 @@
1
1
  class FunctionalParser
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,127 @@
1
+ $LIBRARY_ROOT_PATH = File.dirname(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ class FunctionalParser
4
+ module UnitTest
5
+ require 'test/unit'
6
+ require $LIBRARY_ROOT_PATH + '/lib/functional_parser.rb'
7
+
8
+ class FunctionalParserTest < Test::Unit::TestCase
9
+
10
+ P = FunctionalParser
11
+
12
+ def test_ret
13
+ result = P.ret("hoge").parse("fuga")
14
+ assert_kind_of(Succeeded, result)
15
+ assert_equal("hoge", result.parsed)
16
+ assert_equal("fuga", result.rest)
17
+ end
18
+
19
+ def test_failure
20
+ assert_kind_of(Failed, P.failure.parse("hoge"))
21
+ end
22
+
23
+ def test_item_suc
24
+ result = P.item.parse("hoge")
25
+ assert_kind_of(Succeeded, result)
26
+ assert_equal("h", result.parsed)
27
+ assert_equal("oge", result.rest)
28
+ end
29
+
30
+ def test_item_fai
31
+ assert_kind_of(Failed, P.item.parse(""))
32
+ end
33
+
34
+ def test_so_suc
35
+ result = P.so(P.item){|c| P.ret(c.next)}.parse("hoge")
36
+ assert_kind_of(Succeeded, result)
37
+ assert_equal("i", result.parsed)
38
+ assert_equal("oge", result.rest)
39
+ end
40
+
41
+ def test_so_fai
42
+ assert_kind_of(Failed, P.so(P.item){|c| P.ret(c.next)}.parse(""))
43
+ end
44
+
45
+ def test_either_suc_suc
46
+ result = P.either(P.ret("a"), P.ret("b")).parse("hoge")
47
+ assert_kind_of(Succeeded, result)
48
+ assert_equal("a", result.parsed)
49
+ assert_equal("hoge", result.rest)
50
+ end
51
+
52
+ def test_either_suc_fai
53
+ result = P.either(P.ret("a"), P.failure).parse("hoge")
54
+ assert_kind_of(Succeeded, result)
55
+ assert_equal("a", result.parsed)
56
+ assert_equal("hoge", result.rest)
57
+ end
58
+
59
+ def test_either_fai_suc
60
+ result = P.either(P.failure, P.ret("b")).parse("hoge")
61
+ assert_kind_of(Succeeded, result)
62
+ assert_equal("b", result.parsed)
63
+ assert_equal("hoge", result.rest)
64
+ end
65
+
66
+ def test_either_fai_fai
67
+ result = P.either(P.failure, P.failure).parse("hoge")
68
+ assert_kind_of(Failed, result)
69
+ end
70
+
71
+ def test_append_suc_suc
72
+ result = P.append(P.ret("a"), P.ret("b")).parse("hoge")
73
+ assert_kind_of(Succeeded, result)
74
+ assert_equal("ab", result.parsed)
75
+ assert_equal("hoge", result.rest)
76
+ end
77
+
78
+ def test_append_suc_fai
79
+ assert_kind_of(Failed, P.append(P.ret("a"), P.failure).parse("hoge"))
80
+ end
81
+
82
+ def test_append_fai_suc
83
+ assert_kind_of(Failed, P.append(P.failure, P.ret("b")).parse("hoge"))
84
+ end
85
+
86
+ def test_append_fai_fai
87
+ assert_kind_of(Failed, P.append(P.failure, P.failure).parse("hoge"))
88
+ end
89
+
90
+ def test_sat_suc
91
+ result = P.sat{|c| c == "a"}.parse("age")
92
+ assert_kind_of(Succeeded, result)
93
+ assert_equal("a", result.parsed)
94
+ assert_equal("ge", result.rest)
95
+ end
96
+
97
+ def test_sat_fai
98
+ assert_kind_of(Failed, P.sat{|c| c == "a"}.parse("sage"))
99
+ end
100
+
101
+ def test_string_suc
102
+ result = P.string("ho").parse("hoge")
103
+ assert_kind_of(Succeeded, result)
104
+ assert_equal("ho", result.parsed)
105
+ assert_equal("ge", result.rest)
106
+ end
107
+
108
+ def test_string_fai
109
+ assert_kind_of(Failed, P.string("ho").parse("hage"))
110
+ end
111
+
112
+ def test_many
113
+ result = P.many(P.string("hoge")).parse("hogehogefuga")
114
+ assert_kind_of(Succeeded, result)
115
+ assert_equal(["hoge", "hoge"], result.parsed)
116
+ assert_equal("fuga", result.rest)
117
+ end
118
+
119
+ def test_many_0times
120
+ result = P.many(P.string("hoge")).parse("fugafugafuga")
121
+ assert_kind_of(Succeeded, result)
122
+ assert_equal([], result.parsed)
123
+ assert_equal("fugafugafuga", result.rest)
124
+ end
125
+ end
126
+ end
127
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: functional_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawaken
@@ -53,6 +53,7 @@ files:
53
53
  - functional_parser.gemspec
54
54
  - lib/functional_parser.rb
55
55
  - lib/functional_parser/version.rb
56
+ - unit_test/test_functional_parser.rb
56
57
  homepage: ''
57
58
  licenses:
58
59
  - MIT