yaparc 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yaparc.rb +59 -41
- data/tests/test_parser.rb +34 -1
- data/tests/test_sql.rb +2 -2
- metadata +2 -2
data/lib/yaparc.rb
CHANGED
@@ -49,18 +49,6 @@ module Yaparc
|
|
49
49
|
result
|
50
50
|
end
|
51
51
|
end
|
52
|
-
# def parse(input, &block)
|
53
|
-
# result = @parser.call(input)
|
54
|
-
# if block_given?
|
55
|
-
# # @tree = yield result.value
|
56
|
-
# result.value = yield result.value
|
57
|
-
# @tree = result
|
58
|
-
# else
|
59
|
-
# # @tree = result.value
|
60
|
-
# @tree = result.parse(input)
|
61
|
-
# end
|
62
|
-
# result
|
63
|
-
# end
|
64
52
|
|
65
53
|
def eval
|
66
54
|
tree = parse(input)
|
@@ -96,18 +84,19 @@ module Yaparc
|
|
96
84
|
include Parsable
|
97
85
|
def initialize
|
98
86
|
@parser = lambda do |input|
|
99
|
-
Result::Fail.new
|
87
|
+
Result::Fail.new(:input => input)
|
100
88
|
end
|
101
89
|
end
|
102
90
|
end
|
103
91
|
|
104
92
|
|
93
|
+
|
105
94
|
class ItemParser
|
106
95
|
include Parsable
|
107
96
|
def initialize
|
108
97
|
@parser = lambda do |input|
|
109
98
|
if input.nil? or input.empty?
|
110
|
-
Result::Fail.new
|
99
|
+
Result::Fail.new(:input => input)
|
111
100
|
else
|
112
101
|
Result::OK.new(:value => input[0..0],:input => input[1..input.length])
|
113
102
|
end
|
@@ -122,14 +111,14 @@ module Yaparc
|
|
122
111
|
|
123
112
|
@parser = lambda do |input|
|
124
113
|
case result = ItemParser.new.parse(input)
|
125
|
-
when Result::
|
126
|
-
FailParser.new
|
127
|
-
else
|
114
|
+
when Result::OK
|
128
115
|
parser = if predicate.call(result.value)
|
129
116
|
SucceedParser.new(result.value, result.input)
|
130
117
|
else
|
131
118
|
FailParser.new
|
132
119
|
end
|
120
|
+
else # Result::Fail or Result::Error
|
121
|
+
FailParser.new
|
133
122
|
end
|
134
123
|
end
|
135
124
|
end
|
@@ -146,6 +135,27 @@ module Yaparc
|
|
146
135
|
end
|
147
136
|
end
|
148
137
|
|
138
|
+
|
139
|
+
class NoFail
|
140
|
+
# hutton92:_higher_order_funct_parsin,p.19
|
141
|
+
include Parsable
|
142
|
+
|
143
|
+
def initialize(parser, &block)
|
144
|
+
@parser = lambda do |input|
|
145
|
+
case result = parser.parse(input)
|
146
|
+
when Result::Fail
|
147
|
+
Result::Error.new(:value => result.value, :input => result.input)
|
148
|
+
when Result::OK
|
149
|
+
SucceedParser.new(result.value)
|
150
|
+
else
|
151
|
+
SucceedParser.new(result.value)
|
152
|
+
# Result::OK.new(:value => result.value,:input => result.input)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end # of NoFail
|
157
|
+
|
158
|
+
|
149
159
|
class SeqParser
|
150
160
|
include Parsable
|
151
161
|
def initialize(*parsers, &block)
|
@@ -155,23 +165,22 @@ module Yaparc
|
|
155
165
|
final_result = parsers.inject(initial_result) do |subsequent, parser|
|
156
166
|
case result = parser.parse(subsequent.input)
|
157
167
|
when Result::Fail
|
158
|
-
break Result::Fail.new
|
168
|
+
break Result::Fail.new(:input => subsequent.input)
|
159
169
|
else
|
160
170
|
args << result.value
|
161
|
-
# args << result.parse(input)
|
162
171
|
result
|
163
172
|
end
|
164
173
|
end
|
165
174
|
|
166
175
|
case final_result
|
167
176
|
when Result::Fail
|
168
|
-
Result::Fail.new
|
177
|
+
Result::Fail.new(:input => final_result.input)
|
169
178
|
when Result::OK
|
170
179
|
final_value = if block_given?
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
180
|
+
yield(*args)
|
181
|
+
else
|
182
|
+
args.last
|
183
|
+
end
|
175
184
|
Result::OK.new(:value => final_value, :input => final_result.input)
|
176
185
|
else
|
177
186
|
raise
|
@@ -186,11 +195,13 @@ module Yaparc
|
|
186
195
|
def initialize(*parsers)
|
187
196
|
@parser = lambda do |input|
|
188
197
|
initial_result = Result::OK.new(:input => input)
|
189
|
-
final_result = Result::Fail.new
|
198
|
+
final_result = Result::Fail.new(:input => input)
|
190
199
|
parsers.each do |parser|
|
191
200
|
case result = parser.parse(initial_result.input)
|
192
201
|
when Result::Fail
|
193
202
|
next
|
203
|
+
when Result::Error
|
204
|
+
return Result::Error.new(:value => result.value, :input => result.input)
|
194
205
|
when Result::OK
|
195
206
|
break final_result = result
|
196
207
|
else
|
@@ -211,10 +222,8 @@ module Yaparc
|
|
211
222
|
case result = parser.parse(input)
|
212
223
|
when Result::OK
|
213
224
|
SucceedParser.new(yield(result.value)).parse(result.input)
|
214
|
-
when Result::Fail
|
215
|
-
FailParser.new.parse(input)
|
216
225
|
else
|
217
|
-
|
226
|
+
FailParser.new.parse(input)
|
218
227
|
end
|
219
228
|
end
|
220
229
|
end # of initialize
|
@@ -226,8 +235,6 @@ module Yaparc
|
|
226
235
|
def initialize(string)
|
227
236
|
@parser = lambda do |input|
|
228
237
|
case result = ItemParser.new.parse(string)
|
229
|
-
when Result::Fail
|
230
|
-
SucceedParser.new(result) # Is it OK?
|
231
238
|
when Result::OK
|
232
239
|
SeqParser.new(
|
233
240
|
CharParser.new(result.value),
|
@@ -237,7 +244,7 @@ module Yaparc
|
|
237
244
|
succeed_result
|
238
245
|
end
|
239
246
|
else
|
240
|
-
|
247
|
+
SucceedParser.new(result) # Is it OK?
|
241
248
|
end
|
242
249
|
end
|
243
250
|
end
|
@@ -255,7 +262,7 @@ module Yaparc
|
|
255
262
|
if match = Regexp.new(regex).match(input)
|
256
263
|
Result::OK.new(:value => match[0], :input => match.post_match)
|
257
264
|
else
|
258
|
-
Result::Fail.new
|
265
|
+
Result::Fail.new(:input => input)
|
259
266
|
end
|
260
267
|
end
|
261
268
|
end
|
@@ -264,7 +271,8 @@ module Yaparc
|
|
264
271
|
# permits zero or more applications of parser.
|
265
272
|
class ManyParser
|
266
273
|
include Parsable
|
267
|
-
def initialize(parser, identity = "")
|
274
|
+
# def initialize(parser, identity = "")
|
275
|
+
def initialize(parser, identity = [])
|
268
276
|
@parser = lambda do |input|
|
269
277
|
# AltParser.new(ManyOneParser.new(parser, identity), SucceedParser.new(identity)).parse(input)
|
270
278
|
AltParser.new(ManyOneParser.new(parser, identity), SucceedParser.new(identity))
|
@@ -279,7 +287,7 @@ module Yaparc
|
|
279
287
|
# requires at least one successfull application of parser.
|
280
288
|
class ManyOneParser
|
281
289
|
include Parsable
|
282
|
-
def initialize(parser, identity =
|
290
|
+
def initialize(parser, identity = [])
|
283
291
|
@parser = lambda do |input|
|
284
292
|
SeqParser.new(
|
285
293
|
parser,
|
@@ -321,7 +329,7 @@ module Yaparc
|
|
321
329
|
def initialize
|
322
330
|
@parser = SeqParser.new(
|
323
331
|
SatisfyParser.new(IS_LOWER),
|
324
|
-
ManyParser.new(SatisfyParser.new(IS_ALPHANUM))
|
332
|
+
ManyParser.new(SatisfyParser.new(IS_ALPHANUM),"")
|
325
333
|
) do |head, tail|
|
326
334
|
head + tail
|
327
335
|
end
|
@@ -330,7 +338,7 @@ module Yaparc
|
|
330
338
|
|
331
339
|
class Nat < ParserBase
|
332
340
|
def initialize
|
333
|
-
@parser = SeqParser.new(ManyOneParser.new(SatisfyParser.new(IS_DIGIT))) do |vs|
|
341
|
+
@parser = SeqParser.new(ManyOneParser.new(SatisfyParser.new(IS_DIGIT),"")) do |vs|
|
334
342
|
if vs == ""
|
335
343
|
vs
|
336
344
|
else
|
@@ -343,7 +351,7 @@ module Yaparc
|
|
343
351
|
|
344
352
|
class Space < ParserBase
|
345
353
|
def initialize
|
346
|
-
@parser = SeqParser.new(ManyParser.new(SatisfyParser.new(IS_SPACE))) do |vs|
|
354
|
+
@parser = SeqParser.new(ManyParser.new(SatisfyParser.new(IS_SPACE),"")) do |vs|
|
347
355
|
SucceedParser.new([])
|
348
356
|
end
|
349
357
|
end
|
@@ -351,7 +359,7 @@ module Yaparc
|
|
351
359
|
|
352
360
|
class WhiteSpace < ParserBase
|
353
361
|
def initialize
|
354
|
-
@parser = SeqParser.new(ManyParser.new(SatisfyParser.new(IS_WHITESPACE))) do |vs|
|
362
|
+
@parser = SeqParser.new(ManyParser.new(SatisfyParser.new(IS_WHITESPACE),"")) do |vs|
|
355
363
|
SucceedParser.new([])
|
356
364
|
end
|
357
365
|
end
|
@@ -368,6 +376,18 @@ module Yaparc
|
|
368
376
|
end
|
369
377
|
end
|
370
378
|
|
379
|
+
class Tokenize < ParserBase
|
380
|
+
attr_accessor :prefix, :postfix
|
381
|
+
|
382
|
+
def initialize(parser, &block)
|
383
|
+
@prefix, @postfix = WhiteSpace.new,WhiteSpace.new
|
384
|
+
yield self
|
385
|
+
@parser = SeqParser.new(@prefix, parser, @postfix) do |_, vs, _|
|
386
|
+
vs
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
371
391
|
# Refer to http://www.cs.nott.ac.uk/~gmh/monparsing.pdf, p.23
|
372
392
|
class Identifier
|
373
393
|
include Parsable
|
@@ -382,10 +402,8 @@ module Yaparc
|
|
382
402
|
case result = AltParser.new(*keyword_parsers).parse(input)
|
383
403
|
when Result::OK
|
384
404
|
FailParser.new
|
385
|
-
|
405
|
+
else # Result::Fail or Result::Error
|
386
406
|
Token.new(Ident.new)
|
387
|
-
else
|
388
|
-
raise
|
389
407
|
end
|
390
408
|
end
|
391
409
|
end
|
data/tests/test_parser.rb
CHANGED
@@ -187,7 +187,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
187
187
|
|
188
188
|
def test_many_parse
|
189
189
|
is_digit = SatisfyParser.new(lambda {|i| i >= '0' and i <= '9'})
|
190
|
-
parser = ManyParser.new(is_digit)
|
190
|
+
parser = ManyParser.new(is_digit,"")
|
191
191
|
result = parser.parse("123abc")
|
192
192
|
assert_equal "123", result.value
|
193
193
|
assert_equal "abc", result.input
|
@@ -234,6 +234,39 @@ class YaparcTest < Test::Unit::TestCase
|
|
234
234
|
assert_equal "abc", result.input
|
235
235
|
end
|
236
236
|
|
237
|
+
# def test_token
|
238
|
+
# parser = Token.new
|
239
|
+
# result = parser.parse(" \n abc")
|
240
|
+
# assert_instance_of Result::OK, result
|
241
|
+
# assert_equal "abc", result.input
|
242
|
+
# end
|
243
|
+
|
244
|
+
def test_tokenize
|
245
|
+
parser = Tokenize.new(Ident.new) do |tokenize|
|
246
|
+
tokenize.prefix = Space.new
|
247
|
+
tokenize.postfix = Space.new
|
248
|
+
end
|
249
|
+
|
250
|
+
result = parser.parse(" abc")
|
251
|
+
assert_instance_of Result::OK, result
|
252
|
+
assert_equal "abc", result.value
|
253
|
+
assert_equal "", result.input
|
254
|
+
|
255
|
+
result = parser.parse(" \n abc")
|
256
|
+
assert_instance_of Result::Fail, result
|
257
|
+
assert_equal "\n abc", result.input
|
258
|
+
|
259
|
+
parser = Tokenize.new(Ident.new) do |tokenize|
|
260
|
+
tokenize.prefix = WhiteSpace.new
|
261
|
+
tokenize.postfix = WhiteSpace.new
|
262
|
+
end
|
263
|
+
|
264
|
+
result = parser.parse(" \n abc")
|
265
|
+
assert_instance_of Result::OK, result
|
266
|
+
assert_equal "abc", result.value
|
267
|
+
assert_equal "", result.input
|
268
|
+
end
|
269
|
+
|
237
270
|
def test_identifier
|
238
271
|
parser = Identifier.new
|
239
272
|
result = parser.parse(" abc ")
|
data/tests/test_sql.rb
CHANGED
@@ -315,10 +315,10 @@ class YaparcQueryTest < Test::Unit::TestCase
|
|
315
315
|
query = SQL::QueryBody.new
|
316
316
|
result = query.parse("select abc from /xyz")
|
317
317
|
assert_instance_of Result::OK, result
|
318
|
-
assert_equal Hash[:from=>["xyz"], :where=>
|
318
|
+
assert_equal Hash[:from=>["xyz"], :where=>[], :select=>["abc"]], result.value
|
319
319
|
result = query.parse("select abc from /xyz/fgh")
|
320
320
|
assert_instance_of Result::OK, result
|
321
|
-
assert_equal Hash[:from=>["xyz","fgh"], :where=>
|
321
|
+
assert_equal Hash[:from=>["xyz","fgh"], :where=>[], :select=>["abc"]], result.value
|
322
322
|
end
|
323
323
|
|
324
324
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaparc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akimichi Tatsukawa
|
@@ -9,7 +9,7 @@ autorequire: yaparc
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-02-
|
12
|
+
date: 2008-02-21 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|