functional_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b47c35eaea8d03f51311f1718b79c5d3e70729c
4
+ data.tar.gz: 0b740be01743b668e8ac37cb5f77f566d3705cdc
5
+ SHA512:
6
+ metadata.gz: 397c8502ff4799d894d7f64b3151570100341e1b59f9d46fee169ce64e0adebf6122f2feb84f20ee2a5e83a40697cc6645a839717fa28e9628dc34d252fa3e61
7
+ data.tar.gz: d715b861ff57f0f44b7c278105e86c5207b9a88dea8c8a394c54c4087f2f777794777e20ec0d2adf188b169d3ad93f9556123ffedc75c86fd15ae4949c3fa77f
@@ -0,0 +1,23 @@
1
+ *.*~
2
+ *#
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ test
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
21
+ test.rb
22
+ test2.rb
23
+ test3.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in functional_parser.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # FunctionalParser
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'functional_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install functional_parser
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/functional_parser/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ # coding: utf-8
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'functional_parser/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "functional_parser"
10
+ spec.version = FunctionalParser::VERSION
11
+ spec.authors = ["sawaken"]
12
+ spec.email = ["sasasawada@gmail.com"]
13
+ spec.summary = %q{Yet another functional text-parser}
14
+ spec.description = %q{}
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,170 @@
1
+ require "functional_parser/version"
2
+
3
+ class FunctionalParser
4
+ class Succeeded
5
+ attr_reader :parsed, :rest
6
+ def initialize(parsed, rest)
7
+ @parsed, @rest = parsed, rest
8
+ end
9
+ end
10
+
11
+ class Failed
12
+ end
13
+
14
+ def initialize(&proc)
15
+ @f = proc
16
+ end
17
+
18
+ def parse(inp)
19
+ @f.call(inp)
20
+ end
21
+
22
+ def >>(proc)
23
+ FunctionalParser.so(self, &proc)
24
+ end
25
+
26
+ def |(other)
27
+ FunctionalParser.either(self, other)
28
+ end
29
+
30
+ def +(other)
31
+ FunctionalParser.append(self, other)
32
+ end
33
+
34
+ def self.ret(something)
35
+ new{|inp| Succeeded.new(something, inp)}
36
+ end
37
+
38
+ def self.failure
39
+ new{|inp| Failed.new}
40
+ end
41
+
42
+ def self.item
43
+ new{|inp| inp.size == 0 ? Failed.new : Succeeded.new(inp[0], inp[1, inp.size - 1])}
44
+ end
45
+
46
+ def self.so(parser, &proc)
47
+ new{|inp|
48
+ case result = parser.parse(inp)
49
+ when Failed
50
+ result
51
+ when Succeeded
52
+ proc.call(result.parsed).parse(result.rest)
53
+ else
54
+ raise "error."
55
+ end
56
+ }
57
+ end
58
+
59
+ def self.either(parser1, parser2)
60
+ new{|inp|
61
+ case result = parser1.parse(inp)
62
+ when Failed
63
+ parser2.parse(inp)
64
+ when Succeeded
65
+ result
66
+ else
67
+ raise "error."
68
+ end
69
+ }
70
+ end
71
+
72
+ def append(parser1, parser2)
73
+ parser1 >> proc{|x|
74
+ parer2 >> proc{|y|
75
+ ret(x + y)
76
+ }
77
+ }
78
+ end
79
+
80
+ def self.sat(&proc)
81
+ item >> proc{|c|
82
+ proc.call(c) ? ret(c) : failure
83
+ }
84
+ end
85
+
86
+ def self.digit
87
+ sat{|c| "0" <= c && c <= "9"}
88
+ end
89
+
90
+ def self.lower
91
+ sat{|c| "a" <= c && c <= "z"}
92
+ end
93
+
94
+ def self.upper
95
+ sat{|c| "A" <= c && c <= "Z"}
96
+ end
97
+
98
+ def self.alpha
99
+ lower | upper
100
+ end
101
+
102
+ def self.alphanum
103
+ alpha | digit
104
+ end
105
+
106
+ def self.char(char)
107
+ sat{|c| c == char}
108
+ end
109
+
110
+ def self.string(str)
111
+ if str.size == 0
112
+ ret(str)
113
+ else
114
+ char(str[0]) >> proc{|c|
115
+ string(str[1, str.size - 1]) >> proc{
116
+ ret(str)
117
+ }
118
+ }
119
+ end
120
+ end
121
+
122
+ def self.many(parser)
123
+ many1(parser) | ret([])
124
+ end
125
+
126
+ def self.many1(parser)
127
+ parser >> proc{|x|
128
+ many(parser) >> proc{|xs|
129
+ ret([x] + xs)
130
+ }
131
+ }
132
+ end
133
+
134
+ def self.ident
135
+ many1(alphanum) >> proc{|cs|
136
+ ret(cs.inject(&:+))
137
+ }
138
+ end
139
+
140
+ def self.nat
141
+ many1(digit) >> proc{|cs|
142
+ ret(cs.inject(&:+).to_i)
143
+ }
144
+ end
145
+
146
+ def self.whitespace
147
+ many(char("\s") | char("\n") | char("\t")) >> proc{
148
+ ret(nil)
149
+ }
150
+ end
151
+
152
+ def self.token(parser)
153
+ whitespace >> proc{
154
+ parser >> proc{|x|
155
+ whitespace >> proc{
156
+ ret(x)
157
+ }
158
+ }
159
+ }
160
+ end
161
+
162
+ def self.identifier
163
+ token(ident)
164
+ end
165
+
166
+ def self.natural
167
+ token(nat)
168
+ end
169
+ end
170
+
@@ -0,0 +1,3 @@
1
+ class FunctionalParser
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: functional_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sawaken
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ''
42
+ email:
43
+ - sasasawada@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - functional_parser.gemspec
54
+ - lib/functional_parser.rb
55
+ - lib/functional_parser/version.rb
56
+ homepage: ''
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Yet another functional text-parser
80
+ test_files: []