parser_combinator 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb0d5aae337910defc3d0eb3a61be9a37d99f5b5
4
- data.tar.gz: 0513a0c7ca661716f31c7a1dc8561c74d6330cce
3
+ metadata.gz: d456efcd3b6e21d8ab0d1c0622247276f86a8ddf
4
+ data.tar.gz: 7a565356e08db305a2a0c020e549de9df6bcded9
5
5
  SHA512:
6
- metadata.gz: 21b5c9876e6d8a7bb88b17fe07ab59422b7e8381a6b66a47bc66aa105084fe924c22f7e532cd6dba09288a79b7f6b9002bcb462eacfc739a21a167e1c542bd0f
7
- data.tar.gz: c209e1110f96a141e70b20c97b1ba944b58a303e43443e186d32376c2d1ac3650f0d35f571551c25936ee3e85f2c2582c648705e20cfb1ae741858c9dc02bcc9
6
+ metadata.gz: a9ed18d16f6bfc022f518cf70f41d93fa6a591945022b008f0282f69210efe2b98bf80bb07d49d1cce7b7880ecb1ddb7a98b26e0969509a32191059aecb3f923
7
+ data.tar.gz: db4e7c2760fea42ab14e77136b871864254a5e30e99e82b50986f2af6e95ca7836b410e09c43ef14b1b74721337de39496fbbb59ce42dc47d8a6755ad6f432d1
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ task :examples do
4
+ $LOAD_PATH.unshift File.expand_path('../examples', __FILE__)
5
+ require 'basic'
6
+ require 'error_handling'
7
+ require 'recursive'
8
+ end
9
+
@@ -1,3 +1,3 @@
1
1
  class ParserCombinator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -97,9 +97,8 @@ class ParserCombinator
97
97
  end
98
98
  end
99
99
 
100
- attr_accessor :parser_proc
101
- attr_reader :parser_name
102
- def initialize(status_handler=nil, name=nil &proc)
100
+ attr_reader :status_handler, :parser_name, :parser_proc
101
+ def initialize(status_handler=proc{nil}, name=nil, &proc)
103
102
  @status_handler = status_handler
104
103
  @parser_name = name
105
104
  @parser_proc = proc
@@ -107,10 +106,17 @@ class ParserCombinator
107
106
 
108
107
  def parse(items)
109
108
  result = @parser_proc.call(items)
110
- if result.is_a?(Fail) && @status_handler
111
- result.class.new(@status_handler.call(items, result.status))
112
- else
109
+ case result
110
+ when Fail
111
+ if @status_handler.call(items, result.status) != nil
112
+ result.class.new(@status_handler.call(items, result.status))
113
+ else
114
+ result
115
+ end
116
+ when Ok
113
117
  result
118
+ else
119
+ raise "parsed object is #{result.inspect}/#{result.class}"
114
120
  end
115
121
  end
116
122
 
@@ -125,6 +131,13 @@ class ParserCombinator
125
131
  end
126
132
  end
127
133
 
134
+ def onparse(&proc)
135
+ self.class.new(@status_handler, @parser_name) do |*args|
136
+ proc.call(*args)
137
+ @parser_proc.call(*args)
138
+ end
139
+ end
140
+
128
141
  def name(new_name)
129
142
  self.class.new(@status_handler, new_name, &parser_proc)
130
143
  end
@@ -171,6 +184,8 @@ class ParserCombinator
171
184
  result
172
185
  when Ok
173
186
  continuation_proc.call(result.parsed).parse(result.rest)
187
+ else
188
+ raise "error"
174
189
  end
175
190
  }
176
191
  end
@@ -182,6 +197,8 @@ class ParserCombinator
182
197
  parser2.parse(i)
183
198
  when Ok
184
199
  result1
200
+ else
201
+ raise "error"
185
202
  end
186
203
  }
187
204
  end
@@ -197,6 +214,8 @@ class ParserCombinator
197
214
  end
198
215
  when Ok
199
216
  result1
217
+ else
218
+ raise "error"
200
219
  end
201
220
  }
202
221
  end
@@ -305,8 +324,12 @@ class ParserCombinator
305
324
  if @cache[key]
306
325
  return @cache[key]
307
326
  else
308
- @cache[key] = self.new{}
309
- @cache[key].parser_proc = proc.call(*args).parser_proc
327
+ status_handler = proc{ raise "this is never-called proc (status_handler)" }
328
+ parser_proc = proc{ raise "this is never-called proc (parser_proc)" }
329
+ @cache[key] = self.new(proc{|*args| status_handler.call(*args)}){|*args| parser_proc.call(*args)}
330
+ generated_parser = proc.call(*args)
331
+ parser_proc = generated_parser.parser_proc
332
+ status_handler = generated_parser.status_handler
310
333
  return @cache[key]
311
334
  end
312
335
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parser_combinator
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
  - Kensuke Sawada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler