rparsec 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/rparsec.rb +2 -2
- data/rparsec/context.rb +81 -66
- data/rparsec/error.rb +22 -19
- data/rparsec/expressions.rb +179 -136
- data/rparsec/functors.rb +270 -249
- data/rparsec/id_monad.rb +13 -11
- data/rparsec/keywords.rb +101 -89
- data/rparsec/locator.rb +36 -32
- data/rparsec/misc.rb +109 -102
- data/rparsec/monad.rb +57 -51
- data/rparsec/operators.rb +116 -109
- data/rparsec/parser.rb +892 -794
- data/rparsec/parser_monad.rb +19 -16
- data/rparsec/parsers.rb +614 -603
- data/rparsec/token.rb +23 -20
- data/test/src/simple_monad_test.rb +21 -21
- metadata +49 -41
data/rparsec/functors.rb
CHANGED
@@ -1,249 +1,270 @@
|
|
1
|
-
#
|
2
|
-
# This module provides frequently used functors.
|
3
|
-
#
|
4
|
-
module Functors
|
5
|
-
Id = proc {|x|x}
|
6
|
-
Idn = proc {|*x|x}
|
7
|
-
Neg = proc {|x|-x}
|
8
|
-
Inc = proc {|x|x+1}
|
9
|
-
Dec = proc {|x|x-1}
|
10
|
-
Plus = proc {|x,y|x+y}
|
11
|
-
Minus = proc {|x,y|x-y}
|
12
|
-
Mul = proc {|x,y|x*y}
|
13
|
-
Div = proc {|x,y|x/y}
|
14
|
-
Mod = proc {|x,y|x%y}
|
15
|
-
Power = proc {|x,y|x**y}
|
16
|
-
Not = proc {|x,y|!x}
|
17
|
-
And = proc {|x,y|x&&y}
|
18
|
-
Or = proc {|x,y|x||y}
|
19
|
-
Xor = proc {|x,y|x^y}
|
20
|
-
BitAnd = proc {|x,y|x&y}
|
21
|
-
Union = proc {|x,y|x|y}
|
22
|
-
Match = proc {|x,y|x=~y}
|
23
|
-
Eq = proc {|x,y|x==y}
|
24
|
-
Ne = proc {|x,y|x!=y}
|
25
|
-
Lt = proc {|x,y|x<y}
|
26
|
-
Gt = proc {|x,y|x>y}
|
27
|
-
Le = proc {|x,y|x<=y}
|
28
|
-
Ge = proc {|x,y|x>=y}
|
29
|
-
Compare = proc {|x,y|x<=>y}
|
30
|
-
Call = proc {|x,y|x.call(y)}
|
31
|
-
Feed = proc {|x,y|y.call(x)}
|
32
|
-
Fst = proc {|x,_|x}
|
33
|
-
Snd = proc {|_, x|x}
|
34
|
-
At = proc {|x,y|x[y]}
|
35
|
-
To_a = proc {|x|x.to_a}
|
36
|
-
To_s = proc {|x|x.to_s}
|
37
|
-
To_i = proc {|x|x.to_i}
|
38
|
-
To_sym = proc {|x|x.to_sym}
|
39
|
-
To_f = proc {|x|x.to_f}
|
40
|
-
|
41
|
-
#
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
#
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
#
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
#
|
197
|
-
#
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
#
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
#
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
#
|
243
|
-
#
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
1
|
+
#
|
2
|
+
# This module provides frequently used functors.
|
3
|
+
#
|
4
|
+
module Functors
|
5
|
+
Id = proc {|x|x}
|
6
|
+
Idn = proc {|*x|x}
|
7
|
+
Neg = proc {|x|-x}
|
8
|
+
Inc = proc {|x|x+1}
|
9
|
+
Dec = proc {|x|x-1}
|
10
|
+
Plus = proc {|x,y|x+y}
|
11
|
+
Minus = proc {|x,y|x-y}
|
12
|
+
Mul = proc {|x,y|x*y}
|
13
|
+
Div = proc {|x,y|x/y}
|
14
|
+
Mod = proc {|x,y|x%y}
|
15
|
+
Power = proc {|x,y|x**y}
|
16
|
+
Not = proc {|x,y|!x}
|
17
|
+
And = proc {|x,y|x&&y}
|
18
|
+
Or = proc {|x,y|x||y}
|
19
|
+
Xor = proc {|x,y|x^y}
|
20
|
+
BitAnd = proc {|x,y|x&y}
|
21
|
+
Union = proc {|x,y|x|y}
|
22
|
+
Match = proc {|x,y|x=~y}
|
23
|
+
Eq = proc {|x,y|x==y}
|
24
|
+
Ne = proc {|x,y|x!=y}
|
25
|
+
Lt = proc {|x,y|x<y}
|
26
|
+
Gt = proc {|x,y|x>y}
|
27
|
+
Le = proc {|x,y|x<=y}
|
28
|
+
Ge = proc {|x,y|x>=y}
|
29
|
+
Compare = proc {|x,y|x<=>y}
|
30
|
+
Call = proc {|x,y|x.call(y)}
|
31
|
+
Feed = proc {|x,y|y.call(x)}
|
32
|
+
Fst = proc {|x,_|x}
|
33
|
+
Snd = proc {|_, x|x}
|
34
|
+
At = proc {|x,y|x[y]}
|
35
|
+
To_a = proc {|x|x.to_a}
|
36
|
+
To_s = proc {|x|x.to_s}
|
37
|
+
To_i = proc {|x|x.to_i}
|
38
|
+
To_sym = proc {|x|x.to_sym}
|
39
|
+
To_f = proc {|x|x.to_f}
|
40
|
+
|
41
|
+
#
|
42
|
+
# Get a Proc, when called, always return the given value.
|
43
|
+
#
|
44
|
+
def const(v)
|
45
|
+
proc {|_|v}
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Get a Proc, when called, return the nth parameter.
|
50
|
+
#
|
51
|
+
def nth(n)
|
52
|
+
proc {|*args|args[n]}
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Create a Proc, which expects the two parameters
|
57
|
+
# in the reverse order of _block_.
|
58
|
+
#
|
59
|
+
def flip(&block)
|
60
|
+
proc {|x,y|block.call(y,x)}
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Create a Proc, when called, the parameter is
|
65
|
+
# first passed into _f2_, _f1_ is called in turn
|
66
|
+
# with the return value from _other_.
|
67
|
+
#
|
68
|
+
def compose(f1, f2)
|
69
|
+
proc {|*x|f1.call(f2.call(*x))}
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
# Create a Proc that's curriable.
|
74
|
+
# When curried, parameters are passed in from left to right.
|
75
|
+
# i.e. curry(closure).call(a).call(b) is quivalent to closure.call(a,b) .
|
76
|
+
# _block_ is encapsulated under the hood to perform the actual
|
77
|
+
# job when currying is done.
|
78
|
+
# arity explicitly specifies the number of parameters to curry.
|
79
|
+
#
|
80
|
+
def curry(arity, &block)
|
81
|
+
fail "cannot curry for unknown arity" if arity < 0
|
82
|
+
Functors.make_curry(arity, &block)
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# Create a Proc that's curriable.
|
87
|
+
# When curried, parameters are passed in from right to left.
|
88
|
+
# i.e. reverse_curry(closure).call(a).call(b) is quivalent to closure.call(b,a) .
|
89
|
+
# _block_ is encapsulated under the hood to perform the actual
|
90
|
+
# job when currying is done.
|
91
|
+
# arity explicitly specifies the number of parameters to curry.
|
92
|
+
#
|
93
|
+
def reverse_curry(arity, &block)
|
94
|
+
fail "cannot curry for unknown arity" if arity < 0
|
95
|
+
Functors.make_reverse_curry(arity, &block)
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Uncurry a curried closure.
|
100
|
+
#
|
101
|
+
def uncurry(&block)
|
102
|
+
return block unless block.arity == 1
|
103
|
+
proc do |*args|
|
104
|
+
result = block
|
105
|
+
args.each do |a|
|
106
|
+
result = result.call(a)
|
107
|
+
end
|
108
|
+
result
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
#
|
113
|
+
# Uncurry a reverse curried closure.
|
114
|
+
#
|
115
|
+
def reverse_uncurry(&block)
|
116
|
+
return block unless block.arity == 1
|
117
|
+
proc do |*args|
|
118
|
+
result = block
|
119
|
+
args.reverse_each do |a|
|
120
|
+
result = result.call(a)
|
121
|
+
end
|
122
|
+
result
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Create a Proc, when called,
|
128
|
+
# repeatedly call _block_ for _n_ times.
|
129
|
+
# The same arguments are passed to each invocation.
|
130
|
+
#
|
131
|
+
def repeat(n, &block)
|
132
|
+
proc do |*args|
|
133
|
+
result = nil
|
134
|
+
n.times {result = block.call(*args)}
|
135
|
+
result
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
#
|
140
|
+
# Create a Proc, when called,
|
141
|
+
# repeatedly call _block_ for _n_ times.
|
142
|
+
# At each iteration, return value from the previous iteration
|
143
|
+
# is used as parameter.
|
144
|
+
#
|
145
|
+
def power(n, &block)
|
146
|
+
return const(nil) if n<=0
|
147
|
+
return block if n==1
|
148
|
+
proc do |*args|
|
149
|
+
result = block.call(*args)
|
150
|
+
(n-1).times {result = block.call(result)}
|
151
|
+
result
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
extend self
|
156
|
+
|
157
|
+
private_class_method
|
158
|
+
|
159
|
+
def self.make_curry(arity, &block)
|
160
|
+
return block if arity<=1
|
161
|
+
proc do |x|
|
162
|
+
make_curry(arity-1) do |*rest|
|
163
|
+
block.call(*rest.insert(0, x))
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.make_reverse_curry(arity, &block)
|
169
|
+
return block if arity <= 1
|
170
|
+
proc do |x|
|
171
|
+
make_reverse_curry(arity-1) do |*rest|
|
172
|
+
block.call(*rest << x)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
#
|
180
|
+
# This module provides instance methods that
|
181
|
+
# manipulate closures in a functional style.
|
182
|
+
# It is typically included in Proc and Method.
|
183
|
+
#
|
184
|
+
module FunctorMixin
|
185
|
+
#
|
186
|
+
# Create a Proc, which expects the two parameters
|
187
|
+
# in the reverse order of _self_.
|
188
|
+
#
|
189
|
+
def flip
|
190
|
+
Functors.flip(&self)
|
191
|
+
end
|
192
|
+
|
193
|
+
#
|
194
|
+
# Create a Proc, when called, the parameter is
|
195
|
+
# first passed into _other_, _self_ is called in turn
|
196
|
+
# with the return value from _other_.
|
197
|
+
#
|
198
|
+
def compose(other)
|
199
|
+
Functors.compose(self, other)
|
200
|
+
end
|
201
|
+
|
202
|
+
alias << compose
|
203
|
+
|
204
|
+
#
|
205
|
+
# a >> b is equivalent to b << a
|
206
|
+
#
|
207
|
+
def >> (other)
|
208
|
+
other << self
|
209
|
+
end
|
210
|
+
|
211
|
+
#
|
212
|
+
# Create a Proc that's curriable.
|
213
|
+
# When curried, parameters are passed in from left to right.
|
214
|
+
# i.e. closure.curry.call(a).call(b) is quivalent to closure.call(a,b) .
|
215
|
+
# _self_ is encapsulated under the hood to perform the actual
|
216
|
+
# job when currying is done.
|
217
|
+
# _ary_ explicitly specifies the number of parameters to curry.
|
218
|
+
#
|
219
|
+
def curry(ary=arity)
|
220
|
+
Functors.curry(ary, &self)
|
221
|
+
end
|
222
|
+
|
223
|
+
#
|
224
|
+
# Create a Proc that's curriable.
|
225
|
+
# When curried, parameters are passed in from right to left.
|
226
|
+
# i.e. closure.reverse_curry.call(a).call(b) is quivalent to closure.call(b,a) .
|
227
|
+
# _self_ is encapsulated under the hood to perform the actual
|
228
|
+
# job when currying is done.
|
229
|
+
# _ary_ explicitly specifies the number of parameters to curry.
|
230
|
+
#
|
231
|
+
def reverse_curry(ary=arity)
|
232
|
+
Functors.reverse_curry(ary, &self)
|
233
|
+
end
|
234
|
+
|
235
|
+
#
|
236
|
+
# Uncurry a curried closure.
|
237
|
+
#
|
238
|
+
def uncurry
|
239
|
+
Functors.uncurry(&self)
|
240
|
+
end
|
241
|
+
|
242
|
+
#
|
243
|
+
# Uncurry a reverse curried closure.
|
244
|
+
#
|
245
|
+
def reverse_uncurry
|
246
|
+
Functors.reverse_uncurry(&self)
|
247
|
+
end
|
248
|
+
|
249
|
+
#
|
250
|
+
# Create a Proc, when called,
|
251
|
+
# repeatedly call _self_ for _n_ times.
|
252
|
+
# The same arguments are passed to each invocation.
|
253
|
+
#
|
254
|
+
def repeat(n)
|
255
|
+
Functors.repeat(n, &self)
|
256
|
+
end
|
257
|
+
#
|
258
|
+
|
259
|
+
# Create a Proc, when called,
|
260
|
+
# repeatedly call _self_ for _n_ times.
|
261
|
+
# At each iteration, return value from the previous iteration
|
262
|
+
# is used as parameter.
|
263
|
+
#
|
264
|
+
def power(n)
|
265
|
+
Functors.power(n, &self)
|
266
|
+
end
|
267
|
+
|
268
|
+
alias ** power
|
269
|
+
alias * repeat
|
270
|
+
end
|