raabro 1.1.1 → 1.1.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.
@@ -1,298 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Sun Sep 20 06:11:54 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Raabro do
12
-
13
- describe '.seq' do
14
-
15
- it "returns a tree with result == 0 in case of failure" do
16
-
17
- i = Raabro::Input.new('tato')
18
-
19
- t = Raabro.seq(nil, i, :to, :ta)
20
-
21
- expect(t.to_a).to eq(
22
- [ nil, 0, 0, 0, nil, :seq, [
23
- [ nil, 0, 0, 0, nil, :str, [] ]
24
- ] ]
25
- )
26
- expect(i.offset).to eq(0)
27
- end
28
-
29
- it "returns a tree with result == 0 in case of failure (at 2nd step)" do
30
-
31
- i = Raabro::Input.new('tato')
32
-
33
- t = Raabro.seq(nil, i, :ta, :ta)
34
-
35
- expect(
36
- t.to_a(:leaves => true)
37
- ).to eq(
38
- [ nil, 0, 0, 0, nil, :seq, [
39
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
40
- [ nil, 0, 2, 0, nil, :str, [] ]
41
- ] ]
42
- )
43
- expect(i.offset).to eq(0)
44
- end
45
-
46
- it "returns a tree with result == 1 in case of success" do
47
-
48
- i = Raabro::Input.new('tato')
49
-
50
- t = Raabro.seq(nil, i, :ta, :to)
51
-
52
- expect(
53
- t.to_a(:leaves => true)
54
- ).to eq(
55
- [ nil, 1, 0, 4, nil, :seq, [
56
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
57
- [ nil, 1, 2, 2, nil, :str, 'to' ]
58
- ] ]
59
- )
60
- expect(i.offset).to eq(4)
61
- end
62
-
63
- it "names the result if there is a name" do
64
-
65
- i = Raabro::Input.new('tato')
66
-
67
- t = Raabro.seq(:x, i, :ta, :to)
68
-
69
- expect(
70
- t.to_a(:leaves => true)
71
- ).to eq(
72
- [ :x, 1, 0, 4, nil, :seq, [
73
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
74
- [ nil, 1, 2, 2, nil, :str, 'to' ]
75
- ] ]
76
- )
77
- expect(i.offset).to eq(4)
78
- end
79
-
80
- it "names in case of failure as well" do
81
-
82
- i = Raabro::Input.new('tato')
83
-
84
- t = Raabro.seq(:y, i, :ta, :ta)
85
-
86
- expect(
87
- t.to_a(:leaves => true)
88
- ).to eq(
89
- [ :y, 0, 0, 0, nil, :seq, [
90
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
91
- [ nil, 0, 2, 0, nil, :str, [] ]
92
- ] ]
93
- )
94
- expect(i.offset).to eq(0)
95
- end
96
-
97
- it "fails when the input string ends" do
98
-
99
- i = Raabro::Input.new('to')
100
-
101
- t = Raabro.seq(:z, i, :to, :ta)
102
-
103
- expect(
104
- t.to_a(:leaves => true)
105
- ).to eq(
106
- [ :z, 0, 0, 0, nil, :seq, [
107
- [ nil, 1, 0, 2, nil, :str, 'to' ],
108
- [ nil, 0, 2, 0, nil, :str, [] ]
109
- ] ]
110
- )
111
- expect(i.offset).to eq(0)
112
- end
113
-
114
- it "accepts an empty input" do
115
-
116
- i = Raabro::Input.new('tato', 4)
117
-
118
- t = Raabro.seq(nil, i, :to, :ta)
119
-
120
- expect(t.to_a).to eq(
121
- [ nil, 0, 4, 0, nil, :seq, [
122
- [ nil, 0, 4, 0, nil, :str, [] ]
123
- ] ]
124
- )
125
- expect(i.offset).to eq(4)
126
- end
127
- end
128
-
129
- describe 'seq and quantifiers' do
130
-
131
- describe 'a lonely quantifier' do
132
-
133
- it 'raises an ArgumentError' do
134
-
135
- i = Raabro::Input.new('tato')
136
-
137
- expect {
138
- t = Raabro.seq(nil, i, '?')
139
- }.to raise_error(ArgumentError, 'lone quantifier ?')
140
- end
141
- end
142
-
143
- describe 'the question mark quantifier' do
144
-
145
- it 'lets optional elements appear in sequences (miss)' do
146
-
147
- i = Raabro::Input.new('tato')
148
-
149
- t = Raabro.seq(nil, i, :ta, :tu, '?', :to)
150
-
151
- expect(t.to_a(:leaves => true)).to eq(
152
- [ nil, 1, 0, 4, nil, :seq, [
153
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
154
- [ nil, 0, 2, 0, nil, :str, [] ],
155
- [ nil, 1, 2, 2, nil, :str, 'to' ]
156
- ] ]
157
- )
158
- expect(i.offset).to eq(4)
159
- end
160
-
161
- it 'lets optional elements appear in sequences (hit)' do
162
-
163
- i = Raabro::Input.new('tatuto')
164
-
165
- t = Raabro.seq(nil, i, :ta, :tu, '?', :to)
166
-
167
- expect(t.to_a(:leaves => true)).to eq(
168
- [ nil, 1, 0, 6, nil, :seq, [
169
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
170
- [ nil, 1, 2, 2, nil, :str, 'tu' ],
171
- [ nil, 1, 4, 2, nil, :str, 'to' ]
172
- ] ]
173
- )
174
- expect(i.offset).to eq(6)
175
- end
176
-
177
- it 'lets optional elements appear in sequences (fail)' do
178
-
179
- i = Raabro::Input.new('tatututo')
180
-
181
- t = Raabro.seq(nil, i, :ta, :tu, '?', :to)
182
-
183
- expect(t.to_a(:leaves => true)).to eq(
184
- [ nil, 0, 0, 0, nil, :seq, [
185
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
186
- [ nil, 1, 2, 2, nil, :str, 'tu' ],
187
- [ nil, 0, 4, 0, nil, :str, [] ]
188
- ] ]
189
- )
190
- expect(i.offset).to eq(0)
191
- end
192
- end
193
-
194
- describe 'the star quantifier' do
195
-
196
- it 'lets optional elements recur in sequences (hit zero)' do
197
-
198
- i = Raabro::Input.new('tato')
199
-
200
- t = Raabro.seq(nil, i, :ta, :tu, '*', :to)
201
-
202
- expect(t.to_a(:leaves => true)).to eq(
203
- [ nil, 1, 0, 4, nil, :seq, [
204
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
205
- [ nil, 0, 2, 0, nil, :str, [] ],
206
- [ nil, 1, 2, 2, nil, :str, 'to' ]
207
- ] ]
208
- )
209
- expect(i.offset).to eq(4)
210
- end
211
-
212
- it 'lets optional elements recur in sequences (hit)' do
213
-
214
- i = Raabro::Input.new('tatututo')
215
-
216
- t = Raabro.seq(nil, i, :ta, :tu, '*', :to)
217
-
218
- expect(t.to_a(:leaves => true)).to eq(
219
- [ nil, 1, 0, 8, nil, :seq, [
220
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
221
- [ nil, 1, 2, 2, nil, :str, 'tu' ],
222
- [ nil, 1, 4, 2, nil, :str, 'tu' ],
223
- [ nil, 0, 6, 0, nil, :str, [] ],
224
- [ nil, 1, 6, 2, nil, :str, 'to' ]
225
- ] ]
226
- )
227
- expect(i.offset).to eq(8)
228
- end
229
-
230
- it 'stops when there is no progress' do
231
-
232
- i = Raabro::Input.new('abc')
233
-
234
- t = Raabro.seq(nil, i, :to_star, '*');
235
-
236
- expect(t.to_a(:leaves => true)).to eq(
237
- [ nil, 1, 0, 0, nil, :seq, [
238
- [ nil, 1, 0, 0, nil, :rep, [
239
- [ nil, 0, 0, 0, nil, :str, [] ]
240
- ] ]
241
- ] ]
242
- )
243
- end
244
- end
245
-
246
- describe 'the plus quantifier' do
247
-
248
- it 'lets elements recur in sequences (hit)' do
249
-
250
- i = Raabro::Input.new('tatututo')
251
-
252
- t = Raabro.seq(nil, i, :ta, :tu, '+', :to)
253
-
254
- expect(t.to_a(:leaves => true)).to eq(
255
- [ nil, 1, 0, 8, nil, :seq, [
256
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
257
- [ nil, 1, 2, 2, nil, :str, 'tu' ],
258
- [ nil, 1, 4, 2, nil, :str, 'tu' ],
259
- [ nil, 0, 6, 0, nil, :str, [] ],
260
- [ nil, 1, 6, 2, nil, :str, 'to' ]
261
- ] ]
262
- )
263
- expect(i.offset).to eq(8)
264
- end
265
-
266
- it 'lets elements recur in sequences (fail)' do
267
-
268
- i = Raabro::Input.new('tato')
269
-
270
- t = Raabro.seq(nil, i, :ta, :tu, '+', :to)
271
-
272
- expect(t.to_a(:leaves => true)).to eq(
273
- [ nil, 0, 0, 0, nil, :seq, [
274
- [ nil, 1, 0, 2, nil, :str, 'ta' ],
275
- [ nil, 0, 2, 0, nil, :str, [] ]
276
- ] ]
277
- )
278
- expect(i.offset).to eq(0)
279
- end
280
-
281
- it 'stops when there is no progress' do
282
-
283
- i = Raabro::Input.new('abc')
284
-
285
- t = Raabro.seq(nil, i, :to_star, '+');
286
-
287
- expect(t.to_a(:leaves => true)).to eq(
288
- [ nil, 1, 0, 0, nil, :seq, [
289
- [ nil, 1, 0, 0, nil, :rep, [
290
- [ nil, 0, 0, 0, nil, :str, [] ]
291
- ] ]
292
- ] ]
293
- )
294
- end
295
- end
296
- end
297
- end
298
-
@@ -1,68 +0,0 @@
1
-
2
- #
3
- # Specifying raabro
4
- #
5
- # Sat Sep 19 21:12:35 JST 2015
6
- #
7
-
8
- require 'pp'
9
-
10
- require 'raabro'
11
-
12
- #
13
- # parsers
14
-
15
- def ta(i); Raabro.str(nil, i, 'ta'); end
16
- def to(i); Raabro.str(nil, i, 'to'); end
17
- def tu(i); Raabro.str(nil, i, 'tu'); end
18
-
19
- def to_plus(input); Raabro.rep(:tos, input, :to, 1); end
20
- def to_star(input); Raabro.rep(nil, input, :to, 0); end
21
- def to_qmark(input); Raabro.rep(nil, input, :to, 0, 1); end
22
-
23
- def nta(i); Raabro.str('the-ta', i, 'ta'); end
24
-
25
- def cha(i); Raabro.rex(nil, i, /\A[a-z]/); end
26
- def com(i); Raabro.str(nil, i, ','); end
27
-
28
- def lt(i); Raabro.str(nil, i, '<'); end
29
- def gt(i); Raabro.str(nil, i, '>'); end
30
-
31
- def onex(i); Raabro.str(:onex, i, 'x'); end
32
- def twox(i); Raabro.str(:twox, i, 'xx'); end
33
- def deux(i); Raabro.str(:deux, i, 'xx'); end
34
-
35
- # testing eseq...
36
- def acom(i); Raabro.rex(nil, i, /,?/); end
37
- def aval(i); Raabro.rex(nil, i, /[a-z]?/); end
38
- def arr(i); Raabro.eseq(nil, i, :lt, :aval, :acom, :gt); end
39
-
40
-
41
- #
42
- # test modules
43
-
44
- module Sample; end
45
-
46
- module Sample::Cal include Raabro
47
-
48
- def sp(i); rex(nil, i, /\s+/); end
49
-
50
- def num(i); rex(:num, i, /-?[0-9]+/); end
51
- def op(i); rex(:op, i, /[+\-*\/]/); end
52
- def item(i); alt(:item, i, :num, :op); end
53
-
54
- def suite(i); jseq(nil, i, :item, :sp); end
55
- end
56
-
57
- module Sample::Arith include Raabro
58
-
59
- def number(i); rex(:number, i, /-?[0-9]+\s*/); end
60
- def plus(i); rex(:plus, i, /\+\s*/); end
61
- def minus(i); rex(:minus, i, /-\s*/); end
62
-
63
- def addition(i); seq(:addition, i, :number, :plus, :op_or_num); end
64
- def substraction(i); seq(:substraction, i, :number, :minus, :op_or_num); end
65
-
66
- def op_or_num(i); alt(nil, i, :addition, :substraction, :number); end
67
- end
68
-
@@ -1,77 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Sun Sep 20 06:11:54 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Raabro do
12
-
13
- describe '.str' do
14
-
15
- it 'returns a tree with result == 0 in case of failure' do
16
-
17
- i = Raabro::Input.new('toto')
18
-
19
- t = Raabro.str(nil, i, 'nada')
20
-
21
- expect(t.to_a).to eq(
22
- [ nil, 0, 0, 0, nil, :str, [] ]
23
- )
24
- expect(i.offset).to eq(0)
25
- end
26
-
27
- it "returns a tree with result == 1 in case of success" do
28
-
29
- i = Raabro::Input.new('toto')
30
-
31
- t = Raabro.str(nil, i, 'toto')
32
-
33
- expect(t.to_a).to eq(
34
- [ nil, 1, 0, 4, nil, :str, [] ]
35
- )
36
- expect(i.offset).to eq(4)
37
- end
38
-
39
- it "names the result if there is a name" do
40
-
41
- i = Raabro::Input.new('toto')
42
-
43
- t = Raabro.str(:x, i, 'toto')
44
-
45
- expect(t.to_a).to eq(
46
- [ :x, 1, 0, 4, nil, :str, [] ]
47
- )
48
- expect(i.offset).to eq(4)
49
- end
50
-
51
- it "names in case of failure as well" do
52
-
53
- i = Raabro::Input.new('toto')
54
-
55
- t = Raabro.str(:y, i, 'nada')
56
-
57
- expect(t.to_a).to eq(
58
- [ :y, 0, 0, 0, nil, :str, [] ]
59
- )
60
- expect(i.offset).to eq(0)
61
- end
62
-
63
- it "accepts an empty input" do
64
-
65
- i = Raabro::Input.new('toto')
66
- i.offset = 4
67
-
68
- t = Raabro.str(nil, i, 'nada')
69
-
70
- expect(t.to_a).to eq(
71
- [ nil, 0, 4, 0, nil, :str, [] ]
72
- )
73
- expect(i.offset).to eq(4)
74
- end
75
- end
76
- end
77
-
@@ -1,126 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Tue Sep 22 07:55:52 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Raabro::Tree do
12
-
13
- describe '.lookup' do
14
-
15
- it 'returns the first node with the given name' do
16
-
17
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
18
-
19
- expect(
20
- t.lookup('item').to_a(:leaves)
21
- ).to eq(
22
- [ :item, 1, 0, 1, nil, :alt, [
23
- [ :num, 1, 0, 1, nil, :rex, '4' ]
24
- ] ]
25
- )
26
- end
27
-
28
- it 'returns the first named node if the given name is nil' do
29
-
30
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
31
-
32
- expect(
33
- t.lookup(nil).to_a(:leaves)
34
- ).to eq(
35
- [ :item, 1, 0, 1, nil, :alt, [
36
- [ :num, 1, 0, 1, nil, :rex, '4' ]
37
- ] ]
38
- )
39
- end
40
- end
41
-
42
- describe '.sublookup' do
43
-
44
- it 'skips the callee node' do
45
-
46
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
47
- t = t.children[0]
48
-
49
- expect(
50
- t.sublookup(nil).to_a(:leaves)
51
- ).to eq(
52
- [ :num, 1, 0, 1, nil, :rex, '4' ]
53
- )
54
- end
55
- end
56
-
57
- describe '.gather' do
58
-
59
- it 'returns all the nodes with a given name' do
60
-
61
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
62
-
63
- expect(
64
- t.gather('op').collect { |n| n.to_a(:leaves) }
65
- ).to eq(
66
- [
67
- [ :op, 1, 6, 1, nil, :rex, '+' ],
68
- [ :op, 1, 14, 1, nil, :rex, '*' ],
69
- [ :op, 1, 16, 1, nil, :rex, '+' ]
70
- ]
71
- )
72
- end
73
-
74
- it 'returns all the nodes with a name if the given name is nil' do
75
-
76
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
77
-
78
- expect(
79
- t.gather(nil).collect { |n| n.to_a(:leaves) }
80
- ).to eq([
81
- [ :item, 1, 0, 1, nil, :alt, [
82
- [ :num, 1, 0, 1, nil, :rex, '4' ]
83
- ] ],
84
- [ :item, 1, 2, 1, nil, :alt, [
85
- [ :num, 1, 2, 1, nil, :rex, '5' ]
86
- ] ],
87
- [ :item, 1, 4, 1, nil, :alt, [
88
- [ :num, 1, 4, 1, nil, :rex, '6' ]
89
- ] ],
90
- [ :item, 1, 6, 1, nil, :alt, [
91
- [ :op, 1, 6, 1, nil, :rex, '+' ]
92
- ] ],
93
- [ :item, 1, 8, 1, nil, :alt, [
94
- [ :num, 1, 8, 1, nil, :rex, '1' ]
95
- ] ],
96
- [ :item, 1, 10, 1, nil, :alt, [
97
- [ :num, 1, 10, 1, nil, :rex, '2' ]
98
- ] ],
99
- [ :item, 1, 12, 1, nil, :alt, [
100
- [ :num, 1, 12, 1, nil, :rex, '3' ]
101
- ] ],
102
- [ :item, 1, 14, 1, nil, :alt, [
103
- [ :op, 1, 14, 1, nil, :rex, '*' ]
104
- ] ],
105
- [ :item, 1, 16, 1, nil, :alt, [
106
- [ :op, 1, 16, 1, nil, :rex, '+' ]
107
- ] ]
108
- ])
109
- end
110
- end
111
-
112
- describe '.subgather' do
113
-
114
- it 'skips the callee node' do
115
-
116
- t = Sample::Cal.parse('4 5 6 + 1 2 3 * +', rewrite: false)
117
-
118
- expect(
119
- t.children[0].subgather(nil).collect { |n| n.to_a(:leaves) }
120
- ).to eq([
121
- [ :num, 1, 0, 1, nil, :rex, '4' ]
122
- ])
123
- end
124
- end
125
- end
126
-