yaparc 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yaparc.rb +44 -42
- data/tests/test_calc.rb +2 -2
- data/tests/test_parser.rb +7 -7
- metadata +2 -2
data/lib/yaparc.rb
CHANGED
@@ -6,9 +6,27 @@ end
|
|
6
6
|
|
7
7
|
|
8
8
|
module Yaparc
|
9
|
-
module
|
10
|
-
|
9
|
+
module Result
|
10
|
+
class Base
|
11
|
+
attr_accessor :message, :input, :value
|
12
|
+
def initialize(options = {})
|
13
|
+
@message = options[:message] if options[:message]
|
14
|
+
@input = options[:input] if options[:input]
|
15
|
+
@value = options[:value] if options[:value]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class OK < Base
|
20
|
+
end
|
21
|
+
|
22
|
+
class Fail < Base
|
23
|
+
end
|
24
|
+
|
25
|
+
class Error < Base
|
26
|
+
end
|
27
|
+
end # of module Result
|
11
28
|
|
29
|
+
module Parsable
|
12
30
|
attr_accessor :tree
|
13
31
|
|
14
32
|
IS_LOWER = lambda {|c| c >= 'a' and c <= 'z'}
|
@@ -66,23 +84,7 @@ module Yaparc
|
|
66
84
|
end
|
67
85
|
end # of Module Parsable
|
68
86
|
|
69
|
-
class Result
|
70
|
-
attr_accessor :message, :input, :value
|
71
|
-
def initialize(options = {})
|
72
|
-
@message = options[:message] if options[:message]
|
73
|
-
@input = options[:input] if options[:input]
|
74
|
-
@value = options[:value] if options[:value]
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
class OK < Result
|
79
|
-
end
|
80
|
-
|
81
|
-
class Fail < Result
|
82
|
-
end
|
83
87
|
|
84
|
-
class Error < Result
|
85
|
-
end
|
86
88
|
|
87
89
|
class SucceedParser
|
88
90
|
include Parsable
|
@@ -90,7 +92,7 @@ module Yaparc
|
|
90
92
|
def initialize(value, remaining = nil)
|
91
93
|
@parser = lambda do |input|
|
92
94
|
# [[value, input]]
|
93
|
-
OK.new(:value => value, :input => input)
|
95
|
+
Result::OK.new(:value => value, :input => input)
|
94
96
|
end
|
95
97
|
@remaining = remaining
|
96
98
|
end
|
@@ -100,7 +102,7 @@ module Yaparc
|
|
100
102
|
include Parsable
|
101
103
|
def initialize
|
102
104
|
@parser = lambda do |input|
|
103
|
-
Fail.new
|
105
|
+
Result::Fail.new
|
104
106
|
end
|
105
107
|
end
|
106
108
|
end
|
@@ -111,10 +113,10 @@ module Yaparc
|
|
111
113
|
def initialize
|
112
114
|
@parser = lambda do |input|
|
113
115
|
if input.nil? or input.empty?
|
114
|
-
Fail.new
|
116
|
+
Result::Fail.new
|
115
117
|
else
|
116
118
|
# [[input[0..0],input[1..input.length]]]
|
117
|
-
OK.new(:value => input[0..0],:input => input[1..input.length])
|
119
|
+
Result::OK.new(:value => input[0..0],:input => input[1..input.length])
|
118
120
|
end
|
119
121
|
end
|
120
122
|
end
|
@@ -127,7 +129,7 @@ module Yaparc
|
|
127
129
|
|
128
130
|
@parser = lambda do |input|
|
129
131
|
case result = ItemParser.new.parse(input)
|
130
|
-
when Fail
|
132
|
+
when Result::Fail
|
131
133
|
FailParser.new
|
132
134
|
else
|
133
135
|
parser = if predicate.call(result.value)
|
@@ -158,11 +160,11 @@ module Yaparc
|
|
158
160
|
def initialize(*parsers, &block)
|
159
161
|
@parser = lambda do |input|
|
160
162
|
args = []
|
161
|
-
initial_result = OK.new(:input => input)
|
163
|
+
initial_result = Result::OK.new(:input => input)
|
162
164
|
final_result = parsers.inject(initial_result) do |subsequent, parser|
|
163
165
|
case result = parser.parse(subsequent.input)
|
164
|
-
when Fail
|
165
|
-
break Fail.new
|
166
|
+
when Result::Fail
|
167
|
+
break Result::Fail.new
|
166
168
|
else
|
167
169
|
args << result.value
|
168
170
|
result
|
@@ -170,15 +172,15 @@ module Yaparc
|
|
170
172
|
end
|
171
173
|
|
172
174
|
case final_result
|
173
|
-
when Fail
|
174
|
-
Fail.new
|
175
|
-
when OK
|
175
|
+
when Result::Fail
|
176
|
+
Result::Fail.new
|
177
|
+
when Result::OK
|
176
178
|
final_value = if block_given?
|
177
179
|
yield(*args)
|
178
180
|
else
|
179
181
|
args.last
|
180
182
|
end
|
181
|
-
OK.new(:value => final_value, :input => final_result.input)
|
183
|
+
Result::OK.new(:value => final_value, :input => final_result.input)
|
182
184
|
else
|
183
185
|
raise
|
184
186
|
end
|
@@ -191,14 +193,14 @@ module Yaparc
|
|
191
193
|
include Parsable
|
192
194
|
def initialize(*parsers)
|
193
195
|
@parser = lambda do |input|
|
194
|
-
initial_result = OK.new(:input => input)
|
195
|
-
final_result = Fail.new
|
196
|
+
initial_result = Result::OK.new(:input => input)
|
197
|
+
final_result = Result::Fail.new
|
196
198
|
# final_result = Error.new
|
197
199
|
parsers.each do |parser|
|
198
200
|
case result = parser.parse(initial_result.input)
|
199
|
-
when Fail
|
201
|
+
when Result::Fail
|
200
202
|
next
|
201
|
-
when OK
|
203
|
+
when Result::OK
|
202
204
|
break final_result = result
|
203
205
|
else
|
204
206
|
raise
|
@@ -216,9 +218,9 @@ module Yaparc
|
|
216
218
|
def initialize(parser, &block)
|
217
219
|
@parser = lambda do |input|
|
218
220
|
case result = parser.parse(input)
|
219
|
-
when OK
|
221
|
+
when Result::OK
|
220
222
|
SucceedParser.new(yield(result.value)).parse(result.input)
|
221
|
-
when Fail
|
223
|
+
when Result::Fail
|
222
224
|
FailParser.new.parse(input)
|
223
225
|
else
|
224
226
|
raise
|
@@ -233,9 +235,9 @@ module Yaparc
|
|
233
235
|
def initialize(string)
|
234
236
|
@parser = lambda do |input|
|
235
237
|
case result = ItemParser.new.parse(string)
|
236
|
-
when Fail
|
238
|
+
when Result::Fail
|
237
239
|
SucceedParser.new(result) # Is it OK?
|
238
|
-
when OK
|
240
|
+
when Result::OK
|
239
241
|
SeqParser.new(
|
240
242
|
CharParser.new(result.value),
|
241
243
|
StringParser.new(result.input),
|
@@ -260,9 +262,9 @@ module Yaparc
|
|
260
262
|
def initialize(regex)
|
261
263
|
@parser = lambda do |input|
|
262
264
|
if match = Regexp.new(regex).match(input)
|
263
|
-
OK.new(:value => match[0], :input => match.post_match)
|
265
|
+
Result::OK.new(:value => match[0], :input => match.post_match)
|
264
266
|
else
|
265
|
-
Fail.new
|
267
|
+
Result::Fail.new
|
266
268
|
end
|
267
269
|
end
|
268
270
|
end
|
@@ -378,9 +380,9 @@ module Yaparc
|
|
378
380
|
@parser = lambda do |input|
|
379
381
|
keyword_parsers = keywords.map {|keyword| StringParser.new(keyword)}
|
380
382
|
case result = AltParser.new(*keyword_parsers).parse(input)
|
381
|
-
when OK
|
383
|
+
when Result::OK
|
382
384
|
FailParser.new
|
383
|
-
when Fail
|
385
|
+
when Result::Fail
|
384
386
|
Token.new(Ident.new)
|
385
387
|
else
|
386
388
|
raise
|
data/tests/test_calc.rb
CHANGED
@@ -87,7 +87,7 @@ class Factor < Yaparc::AbstractParser
|
|
87
87
|
|
88
88
|
def initialize
|
89
89
|
@parser = lambda do
|
90
|
-
AltParser.new(
|
90
|
+
Yaparc::AltParser.new(
|
91
91
|
Yaparc::SeqParser.new(
|
92
92
|
Yaparc::Symbol.new('('),
|
93
93
|
Expr.new,
|
@@ -125,7 +125,7 @@ class YaparcCalcTest < Test::Unit::TestCase
|
|
125
125
|
|
126
126
|
def test_expr
|
127
127
|
result = @expr.parse("1 + 2 ")
|
128
|
-
assert_instance_of OK, result
|
128
|
+
assert_instance_of Result::OK, result
|
129
129
|
assert_equal ["+", 1, 2], result.value
|
130
130
|
assert_equal "", result.input
|
131
131
|
assert_equal 3, @expr.evaluate("1 + 2 ")
|
data/tests/test_parser.rb
CHANGED
@@ -38,7 +38,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
38
38
|
def test_fail_parse
|
39
39
|
parser = ::Yaparc::FailParser.new
|
40
40
|
result = parser.parse("abc")
|
41
|
-
assert_instance_of Fail, result
|
41
|
+
assert_instance_of Result::Fail, result
|
42
42
|
end
|
43
43
|
|
44
44
|
# def test_fail_parse
|
@@ -50,7 +50,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
50
50
|
def test_item_parse
|
51
51
|
parser = ::Yaparc::ItemParser.new
|
52
52
|
result = parser.parse("")
|
53
|
-
assert_instance_of Fail, result
|
53
|
+
assert_instance_of Result::Fail, result
|
54
54
|
result = parser.parse("abc")
|
55
55
|
assert_equal "a", result.value
|
56
56
|
assert_equal "bc", result.input
|
@@ -80,7 +80,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
80
80
|
|
81
81
|
parser = SatisfyParser.new(is_integer)
|
82
82
|
result = parser.parse("abc")
|
83
|
-
assert_instance_of Fail, result
|
83
|
+
assert_instance_of Result::Fail, result
|
84
84
|
|
85
85
|
is_char = lambda do |i|
|
86
86
|
begin
|
@@ -196,7 +196,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
196
196
|
|
197
197
|
parser = AltParser.new(FailParser.new, FailParser.new)
|
198
198
|
result = parser.parse("abc")
|
199
|
-
assert_instance_of Fail, result
|
199
|
+
assert_instance_of Result::Fail, result
|
200
200
|
end
|
201
201
|
|
202
202
|
|
@@ -233,7 +233,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
233
233
|
|
234
234
|
parser = CharParser.new("a")
|
235
235
|
result = parser.parse("123")
|
236
|
-
assert_instance_of Fail, result
|
236
|
+
assert_instance_of Result::Fail, result
|
237
237
|
end
|
238
238
|
|
239
239
|
# def test_char_parse
|
@@ -254,7 +254,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
254
254
|
|
255
255
|
parser = StringParser.new("abc")
|
256
256
|
result = parser.parse("ab1234")
|
257
|
-
assert_instance_of
|
257
|
+
assert_instance_of Result::Fail, result
|
258
258
|
end
|
259
259
|
|
260
260
|
# def test_string_parse
|
@@ -397,7 +397,7 @@ class YaparcTest < Test::Unit::TestCase
|
|
397
397
|
|
398
398
|
parser_with_keyword = Identifier.new("abc","efg")
|
399
399
|
result = parser_with_keyword.parse("abc")
|
400
|
-
assert_instance_of Fail, result
|
400
|
+
assert_instance_of Result::Fail, result
|
401
401
|
result = parser_with_keyword.parse(" xyz")
|
402
402
|
assert_equal "xyz", result.value
|
403
403
|
assert_equal "", result.input
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.3
|
|
3
3
|
specification_version: 1
|
4
4
|
name: yaparc
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0
|
7
|
-
date: 2008-01-
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2008-01-16 00:00:00 +09:00
|
8
8
|
summary: Yet Another Combinator Parser Library
|
9
9
|
require_paths:
|
10
10
|
- lib
|