raabro 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +13 -0
- data/README.md +3 -0
- data/lib/raabro.rb +292 -74
- data/spec/all_spec.rb +54 -0
- data/spec/alt_spec.rb +92 -12
- data/spec/eseq_spec.rb +112 -0
- data/spec/jseq_spec.rb +67 -0
- data/spec/ren_spec.rb +43 -0
- data/spec/rep_spec.rb +28 -16
- data/spec/rex_spec.rb +22 -11
- data/spec/sample_xel_spec.rb +110 -0
- data/spec/seq_spec.rb +165 -187
- data/spec/spec_helper.rb +57 -0
- data/spec/str_spec.rb +20 -16
- data/spec/tree_spec.rb +47 -0
- metadata +9 -2
data/spec/all_spec.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# specifying raabro
|
4
|
+
#
|
5
|
+
# Mon Sep 21 05:56:18 JST 2015
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
|
11
|
+
describe Raabro do
|
12
|
+
|
13
|
+
describe '.all' do
|
14
|
+
|
15
|
+
it 'fails when not all the input is consumed' do
|
16
|
+
|
17
|
+
i = Raabro::Input.new('tototota')
|
18
|
+
|
19
|
+
t = Raabro.all(nil, i, :to_plus)
|
20
|
+
|
21
|
+
expect(t.to_a(:leaves => true)).to eq(
|
22
|
+
[ nil, 0, 0, 0, nil, :all, [
|
23
|
+
[ :tos, 1, 0, 6, nil, :rep, [
|
24
|
+
[ nil, 1, 0, 2, nil, :str, 'to' ],
|
25
|
+
[ nil, 1, 2, 2, nil, :str, 'to' ],
|
26
|
+
[ nil, 1, 4, 2, nil, :str, 'to' ],
|
27
|
+
[ nil, 0, 6, 0, nil, :str, [] ]
|
28
|
+
] ]
|
29
|
+
] ]
|
30
|
+
)
|
31
|
+
expect(i.offset).to eq(0)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'succeeds when all the input is consumed' do
|
35
|
+
|
36
|
+
i = Raabro::Input.new('tototo')
|
37
|
+
|
38
|
+
t = Raabro.all(nil, i, :to_plus)
|
39
|
+
|
40
|
+
expect(t.to_a(:leaves => true)).to eq(
|
41
|
+
[ nil, 1, 0, 6, nil, :all, [
|
42
|
+
[ :tos, 1, 0, 6, nil, :rep, [
|
43
|
+
[ nil, 1, 0, 2, nil, :str, 'to' ],
|
44
|
+
[ nil, 1, 2, 2, nil, :str, 'to' ],
|
45
|
+
[ nil, 1, 4, 2, nil, :str, 'to' ],
|
46
|
+
[ nil, 0, 6, 0, nil, :str, [] ]
|
47
|
+
] ]
|
48
|
+
] ]
|
49
|
+
)
|
50
|
+
expect(i.offset).to eq(6)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/spec/alt_spec.rb
CHANGED
@@ -12,16 +12,11 @@ describe Raabro do
|
|
12
12
|
|
13
13
|
describe '.alt' do
|
14
14
|
|
15
|
-
before :each do
|
16
|
-
|
17
|
-
@input = Raabro::Input.new('tato')
|
18
|
-
end
|
19
|
-
|
20
15
|
it "returns a tree with result == 0 in case of failure" do
|
21
16
|
|
22
|
-
|
17
|
+
i = Raabro::Input.new('tutu')
|
23
18
|
|
24
|
-
t = Raabro.alt(nil,
|
19
|
+
t = Raabro.alt(nil, i, :ta, :to)
|
25
20
|
|
26
21
|
expect(t.to_a(:leaves => true)).to eq(
|
27
22
|
[ nil, 0, 0, 0, nil, :alt, [
|
@@ -29,24 +24,28 @@ describe Raabro do
|
|
29
24
|
[ nil, 0, 0, 0, nil, :str, [] ]
|
30
25
|
] ]
|
31
26
|
)
|
32
|
-
expect(
|
27
|
+
expect(i.offset).to eq(0)
|
33
28
|
end
|
34
29
|
|
35
30
|
it "succeeds (1st alternative)" do
|
36
31
|
|
37
|
-
|
32
|
+
i = Raabro::Input.new('tato')
|
33
|
+
|
34
|
+
t = Raabro.alt(nil, i, :ta, :to)
|
38
35
|
|
39
36
|
expect(t.to_a(:leaves => true)).to eq(
|
40
37
|
[ nil, 1, 0, 2, nil, :alt, [
|
41
38
|
[ nil, 1, 0, 2, nil, :str, 'ta' ]
|
42
39
|
] ]
|
43
40
|
)
|
44
|
-
expect(
|
41
|
+
expect(i.offset).to eq(2)
|
45
42
|
end
|
46
43
|
|
47
44
|
it "succeeds (2nd alternative)" do
|
48
45
|
|
49
|
-
|
46
|
+
i = Raabro::Input.new('tato')
|
47
|
+
|
48
|
+
t = Raabro.alt(nil, i, :to, :ta)
|
50
49
|
|
51
50
|
expect(t.to_a(:leaves => true)).to eq(
|
52
51
|
[ nil, 1, 0, 2, nil, :alt, [
|
@@ -54,7 +53,88 @@ describe Raabro do
|
|
54
53
|
[ nil, 1, 0, 2, nil, :str, 'ta' ]
|
55
54
|
] ]
|
56
55
|
)
|
57
|
-
expect(
|
56
|
+
expect(i.offset).to eq(2)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'prunes' do
|
60
|
+
|
61
|
+
i = Raabro::Input.new('tato', :prune => true)
|
62
|
+
|
63
|
+
t = Raabro.alt(nil, i, :to, :ta)
|
64
|
+
|
65
|
+
expect(t.to_a(:leaves => true)).to eq(
|
66
|
+
[ nil, 1, 0, 2, nil, :alt, [
|
67
|
+
[ nil, 1, 0, 2, nil, :str, 'ta' ]
|
68
|
+
] ]
|
69
|
+
)
|
70
|
+
expect(i.offset).to eq(2)
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when not greedy (default)' do
|
74
|
+
|
75
|
+
it 'goes with the first successful result' do
|
76
|
+
|
77
|
+
i = Raabro::Input.new('xx')
|
78
|
+
|
79
|
+
t = Raabro.alt(nil, i, :onex, :twox)
|
80
|
+
|
81
|
+
expect(t.to_a(:leaves => true)).to eq(
|
82
|
+
[ nil, 1, 0, 1, nil, :alt, [
|
83
|
+
[ :onex, 1, 0, 1, nil, :str, 'x' ]
|
84
|
+
] ]
|
85
|
+
)
|
86
|
+
expect(i.offset).to eq(1)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'when greedy (last argument set to true)' do
|
91
|
+
|
92
|
+
it 'takes the longest successful result' do
|
93
|
+
|
94
|
+
i = Raabro::Input.new('xx')
|
95
|
+
|
96
|
+
t = Raabro.alt(nil, i, :onex, :twox, true)
|
97
|
+
|
98
|
+
expect(t.to_a(:leaves => true)).to eq(
|
99
|
+
[ nil, 1, 0, 2, nil, :altg, [
|
100
|
+
[ :onex, 0, 0, 1, nil, :str, [] ],
|
101
|
+
[ :twox, 1, 0, 2, nil, :str, 'xx' ]
|
102
|
+
] ]
|
103
|
+
)
|
104
|
+
expect(i.offset).to eq(2)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '.altg' do
|
110
|
+
|
111
|
+
it 'is greedy, always' do
|
112
|
+
|
113
|
+
i = Raabro::Input.new('xx')
|
114
|
+
|
115
|
+
t = Raabro.altg(nil, i, :onex, :twox)
|
116
|
+
|
117
|
+
expect(t.to_a(:leaves => true)).to eq(
|
118
|
+
[ nil, 1, 0, 2, nil, :altg, [
|
119
|
+
[ :onex, 0, 0, 1, nil, :str, [] ],
|
120
|
+
[ :twox, 1, 0, 2, nil, :str, 'xx' ]
|
121
|
+
] ]
|
122
|
+
)
|
123
|
+
expect(i.offset).to eq(2)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'prunes' do
|
127
|
+
|
128
|
+
i = Raabro::Input.new('xx', :prune => true)
|
129
|
+
|
130
|
+
t = Raabro.altg(nil, i, :onex, :twox)
|
131
|
+
|
132
|
+
expect(t.to_a(:leaves => true)).to eq(
|
133
|
+
[ nil, 1, 0, 2, nil, :altg, [
|
134
|
+
[ :twox, 1, 0, 2, nil, :str, 'xx' ]
|
135
|
+
] ]
|
136
|
+
)
|
137
|
+
expect(i.offset).to eq(2)
|
58
138
|
end
|
59
139
|
end
|
60
140
|
end
|
data/spec/eseq_spec.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# specifying raabro
|
4
|
+
#
|
5
|
+
# Mon Sep 21 10:15:35 JST 2015
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
|
11
|
+
describe Raabro do
|
12
|
+
|
13
|
+
describe '.eseq' do
|
14
|
+
|
15
|
+
it 'parses successfully' do
|
16
|
+
|
17
|
+
i = Raabro::Input.new('<a,b>')
|
18
|
+
|
19
|
+
t = Raabro.eseq(:list, i, :lt, :cha, :com, :gt)
|
20
|
+
|
21
|
+
expect(t.to_a(:leaves => true)).to eq(
|
22
|
+
[ :list, 1, 0, 5, nil, :eseq, [
|
23
|
+
[ nil, 1, 0, 1, nil, :str, '<' ],
|
24
|
+
[ nil, 1, 1, 1, nil, :rex, 'a' ],
|
25
|
+
[ nil, 1, 2, 1, nil, :str, ',' ],
|
26
|
+
[ nil, 1, 3, 1, nil, :rex, 'b' ],
|
27
|
+
[ nil, 0, 4, 0, nil, :str, [] ],
|
28
|
+
[ nil, 1, 4, 1, nil, :str, '>' ]
|
29
|
+
] ]
|
30
|
+
)
|
31
|
+
expect(i.offset).to eq(5)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'prunes' do
|
35
|
+
|
36
|
+
i = Raabro::Input.new('<a,b>', :prune => true)
|
37
|
+
|
38
|
+
t = Raabro.eseq(:list, i, :lt, :cha, :com, :gt)
|
39
|
+
|
40
|
+
expect(t.to_a(:leaves => true)).to eq(
|
41
|
+
[ :list, 1, 0, 5, nil, :eseq, [
|
42
|
+
[ nil, 1, 0, 1, nil, :str, '<' ],
|
43
|
+
[ nil, 1, 1, 1, nil, :rex, 'a' ],
|
44
|
+
[ nil, 1, 2, 1, nil, :str, ',' ],
|
45
|
+
[ nil, 1, 3, 1, nil, :rex, 'b' ],
|
46
|
+
[ nil, 1, 4, 1, nil, :str, '>' ]
|
47
|
+
] ]
|
48
|
+
)
|
49
|
+
expect(i.offset).to eq(5)
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'no start parser' do
|
53
|
+
|
54
|
+
it 'parses successfully' do
|
55
|
+
|
56
|
+
i = Raabro::Input.new('a,b>')
|
57
|
+
|
58
|
+
t = Raabro.eseq(:list, i, nil, :cha, :com, :gt)
|
59
|
+
|
60
|
+
expect(t.to_a(:leaves => true)).to eq(
|
61
|
+
[ :list, 1, 0, 4, nil, :eseq, [
|
62
|
+
[ nil, 1, 0, 1, nil, :rex, 'a' ],
|
63
|
+
[ nil, 1, 1, 1, nil, :str, ',' ],
|
64
|
+
[ nil, 1, 2, 1, nil, :rex, 'b' ],
|
65
|
+
[ nil, 0, 3, 0, nil, :str, [] ],
|
66
|
+
[ nil, 1, 3, 1, nil, :str, '>' ]
|
67
|
+
] ]
|
68
|
+
)
|
69
|
+
expect(i.offset).to eq(4)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'no end parser' do
|
74
|
+
|
75
|
+
it 'parses successfully' do
|
76
|
+
|
77
|
+
i = Raabro::Input.new('<a,b')
|
78
|
+
|
79
|
+
t = Raabro.eseq(:list, i, :lt, :cha, :com, nil)
|
80
|
+
|
81
|
+
expect(t.to_a(:leaves => true)).to eq(
|
82
|
+
[ :list, 1, 0, 4, nil, :eseq, [
|
83
|
+
[ nil, 1, 0, 1, nil, :str, '<' ],
|
84
|
+
[ nil, 1, 1, 1, nil, :rex, 'a' ],
|
85
|
+
[ nil, 1, 2, 1, nil, :str, ',' ],
|
86
|
+
[ nil, 1, 3, 1, nil, :rex, 'b' ],
|
87
|
+
[ nil, 0, 4, 0, nil, :str, [] ],
|
88
|
+
] ]
|
89
|
+
)
|
90
|
+
expect(i.offset).to eq(4)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'prunes' do
|
94
|
+
|
95
|
+
i = Raabro::Input.new('<a,b', :prune => true)
|
96
|
+
|
97
|
+
t = Raabro.eseq(:list, i, :lt, :cha, :com, nil)
|
98
|
+
|
99
|
+
expect(t.to_a(:leaves => true)).to eq(
|
100
|
+
[ :list, 1, 0, 4, nil, :eseq, [
|
101
|
+
[ nil, 1, 0, 1, nil, :str, '<' ],
|
102
|
+
[ nil, 1, 1, 1, nil, :rex, 'a' ],
|
103
|
+
[ nil, 1, 2, 1, nil, :str, ',' ],
|
104
|
+
[ nil, 1, 3, 1, nil, :rex, 'b' ]
|
105
|
+
] ]
|
106
|
+
)
|
107
|
+
expect(i.offset).to eq(4)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
data/spec/jseq_spec.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# specifying raabro
|
4
|
+
#
|
5
|
+
# Mon Sep 21 06:55:35 JST 2015
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
|
11
|
+
describe Raabro do
|
12
|
+
|
13
|
+
describe '.jseq' do
|
14
|
+
|
15
|
+
it 'parses elts joined by a separator' do
|
16
|
+
|
17
|
+
i = Raabro::Input.new('a,b,c')
|
18
|
+
|
19
|
+
t = Raabro.jseq(:j, i, :cha, :com)
|
20
|
+
|
21
|
+
expect(t.to_a(:leaves => true)).to eq(
|
22
|
+
[ :j, 1, 0, 5, nil, :jseq, [
|
23
|
+
[ nil, 1, 0, 1, nil, :rex, 'a' ],
|
24
|
+
[ nil, 1, 1, 1, nil, :str, ',' ],
|
25
|
+
[ nil, 1, 2, 1, nil, :rex, 'b' ],
|
26
|
+
[ nil, 1, 3, 1, nil, :str, ',' ],
|
27
|
+
[ nil, 1, 4, 1, nil, :rex, 'c' ],
|
28
|
+
[ nil, 0, 5, 0, nil, :str, [] ]
|
29
|
+
] ]
|
30
|
+
)
|
31
|
+
expect(i.offset).to eq(5)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'prunes' do
|
35
|
+
|
36
|
+
i = Raabro::Input.new('a,b,c', :prune => true)
|
37
|
+
|
38
|
+
t = Raabro.jseq(:j, i, :cha, :com)
|
39
|
+
|
40
|
+
expect(t.to_a(:leaves => true)).to eq(
|
41
|
+
[ :j, 1, 0, 5, nil, :jseq, [
|
42
|
+
[ nil, 1, 0, 1, nil, :rex, 'a' ],
|
43
|
+
[ nil, 1, 1, 1, nil, :str, ',' ],
|
44
|
+
[ nil, 1, 2, 1, nil, :rex, 'b' ],
|
45
|
+
[ nil, 1, 3, 1, nil, :str, ',' ],
|
46
|
+
[ nil, 1, 4, 1, nil, :rex, 'c' ]
|
47
|
+
] ]
|
48
|
+
)
|
49
|
+
expect(i.offset).to eq(5)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'fails when zero elements' do
|
53
|
+
|
54
|
+
i = Raabro::Input.new('')
|
55
|
+
|
56
|
+
t = Raabro.jseq(:j, i, :cha, :com)
|
57
|
+
|
58
|
+
expect(t.to_a(:leaves => true)).to eq(
|
59
|
+
[ :j, 0, 0, 0, nil, :jseq, [
|
60
|
+
[ nil, 0, 0, 0, nil, :rex, [] ]
|
61
|
+
] ]
|
62
|
+
)
|
63
|
+
expect(i.offset).to eq(0)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/spec/ren_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# specifying raabro
|
4
|
+
#
|
5
|
+
# Mon Sep 21 05:46:00 JST 2015
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
|
11
|
+
describe Raabro do
|
12
|
+
|
13
|
+
describe '.ren' do
|
14
|
+
|
15
|
+
it 'returns the tree coming from the wrapped parser' do
|
16
|
+
|
17
|
+
i = Raabro::Input.new('ta')
|
18
|
+
|
19
|
+
t = Raabro.ren('renamed', i, :nta)
|
20
|
+
|
21
|
+
expect(t.to_a(:leaves => true)).to eq(
|
22
|
+
[ 'renamed', 1, 0, 2, nil, :str, 'ta' ]
|
23
|
+
)
|
24
|
+
expect(i.offset).to eq(2)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '.rename' do
|
29
|
+
|
30
|
+
it 'is an alias to .ren' do
|
31
|
+
|
32
|
+
i = Raabro::Input.new('ta')
|
33
|
+
|
34
|
+
t = Raabro.rename('autre', i, :nta)
|
35
|
+
|
36
|
+
expect(t.to_a(:leaves => true)).to eq(
|
37
|
+
[ 'autre', 1, 0, 2, nil, :str, 'ta' ]
|
38
|
+
)
|
39
|
+
expect(i.offset).to eq(2)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
data/spec/rep_spec.rb
CHANGED
@@ -10,16 +10,13 @@ require 'spec_helper'
|
|
10
10
|
|
11
11
|
describe Raabro do
|
12
12
|
|
13
|
-
before :each do
|
14
|
-
|
15
|
-
@input = Raabro::Input.new('toto')
|
16
|
-
end
|
17
|
-
|
18
13
|
describe '.rep' do
|
19
14
|
|
20
15
|
it 'returns a tree with result == 0 in case of failure' do
|
21
16
|
|
22
|
-
|
17
|
+
i = Raabro::Input.new('toto')
|
18
|
+
|
19
|
+
t = Raabro.rep(:x, i, :to, 3, 4)
|
23
20
|
|
24
21
|
expect(t.to_a(:leaves => true)).to eq(
|
25
22
|
[ :x, 0, 0, 0, nil, :rep, [
|
@@ -28,14 +25,29 @@ describe Raabro do
|
|
28
25
|
[ nil, 0, 4, 0, nil, :str, [] ]
|
29
26
|
] ]
|
30
27
|
)
|
31
|
-
expect(
|
28
|
+
expect(i.offset).to eq(0)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'prunes' do
|
32
|
+
|
33
|
+
i = Raabro::Input.new('toto', :prune => true)
|
34
|
+
|
35
|
+
t = Raabro.rep(:x, i, :to, 3, 4)
|
36
|
+
|
37
|
+
expect(t.to_a(:leaves => true)).to eq(
|
38
|
+
[ :x, 0, 0, 0, nil, :rep, [
|
39
|
+
[ nil, 1, 0, 2, nil, :str, 'to' ],
|
40
|
+
[ nil, 1, 2, 2, nil, :str, 'to' ]
|
41
|
+
] ]
|
42
|
+
)
|
43
|
+
expect(i.offset).to eq(0)
|
32
44
|
end
|
33
45
|
|
34
46
|
it "fails (min not reached)" do
|
35
47
|
|
36
|
-
|
48
|
+
i = Raabro::Input.new('toto')
|
37
49
|
|
38
|
-
t = Raabro.rep(:x,
|
50
|
+
t = Raabro.rep(:x, i, :to, 3)
|
39
51
|
|
40
52
|
expect(t.to_a(:leaves => true)).to eq(
|
41
53
|
[ :x, 0, 0, 0, nil, :rep, [
|
@@ -44,14 +56,14 @@ describe Raabro do
|
|
44
56
|
[ nil, 0, 4, 0, nil, :str, [] ]
|
45
57
|
] ]
|
46
58
|
)
|
47
|
-
expect(
|
59
|
+
expect(i.offset).to eq(0)
|
48
60
|
end
|
49
61
|
|
50
62
|
it "succeeds (max set)" do
|
51
63
|
|
52
|
-
|
64
|
+
i = Raabro::Input.new('tototo')
|
53
65
|
|
54
|
-
t = Raabro.rep(:x,
|
66
|
+
t = Raabro.rep(:x, i, :to, 1, 2)
|
55
67
|
|
56
68
|
expect(t.to_a(:leaves => true)).to eq(
|
57
69
|
[ :x, 1, 0, 4, nil, :rep, [
|
@@ -59,14 +71,14 @@ describe Raabro do
|
|
59
71
|
[ nil, 1, 2, 2, nil, :str, 'to' ]
|
60
72
|
] ]
|
61
73
|
)
|
62
|
-
expect(
|
74
|
+
expect(i.offset).to eq(4)
|
63
75
|
end
|
64
76
|
|
65
77
|
it "succeeds (max not set)" do
|
66
78
|
|
67
|
-
|
79
|
+
i = Raabro::Input.new('toto')
|
68
80
|
|
69
|
-
t = Raabro.rep(:x,
|
81
|
+
t = Raabro.rep(:x, i, :to, 1)
|
70
82
|
|
71
83
|
expect(t.to_a(:leaves => true)).to eq(
|
72
84
|
[ :x, 1, 0, 4, nil, :rep, [
|
@@ -75,7 +87,7 @@ describe Raabro do
|
|
75
87
|
[ nil, 0, 4, 0, nil, :str, [] ]
|
76
88
|
] ]
|
77
89
|
)
|
78
|
-
expect(
|
90
|
+
expect(i.offset).to eq(4)
|
79
91
|
end
|
80
92
|
end
|
81
93
|
end
|