raabro 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,67 +0,0 @@
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
-
@@ -1,43 +0,0 @@
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
-
@@ -1,94 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Sun Sep 20 09:36:16 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Raabro do
12
-
13
- describe '.rep' 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.rep(:x, i, :to, 3, 4)
20
-
21
- expect(t.to_a(:leaves => true)).to eq(
22
- [ :x, 0, 0, 0, nil, :rep, [
23
- [ nil, 1, 0, 2, nil, :str, 'to' ],
24
- [ nil, 1, 2, 2, nil, :str, 'to' ],
25
- [ nil, 0, 4, 0, nil, :str, [] ]
26
- ] ]
27
- )
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)
44
- end
45
-
46
- it "fails (min not reached)" do
47
-
48
- i = Raabro::Input.new('toto')
49
-
50
- t = Raabro.rep(:x, i, :to, 3)
51
-
52
- expect(t.to_a(:leaves => true)).to eq(
53
- [ :x, 0, 0, 0, nil, :rep, [
54
- [ nil, 1, 0, 2, nil, :str, 'to' ],
55
- [ nil, 1, 2, 2, nil, :str, 'to' ],
56
- [ nil, 0, 4, 0, nil, :str, [] ]
57
- ] ]
58
- )
59
- expect(i.offset).to eq(0)
60
- end
61
-
62
- it "succeeds (max set)" do
63
-
64
- i = Raabro::Input.new('tototo')
65
-
66
- t = Raabro.rep(:x, i, :to, 1, 2)
67
-
68
- expect(t.to_a(:leaves => true)).to eq(
69
- [ :x, 1, 0, 4, nil, :rep, [
70
- [ nil, 1, 0, 2, nil, :str, 'to' ],
71
- [ nil, 1, 2, 2, nil, :str, 'to' ]
72
- ] ]
73
- )
74
- expect(i.offset).to eq(4)
75
- end
76
-
77
- it "succeeds (max not set)" do
78
-
79
- i = Raabro::Input.new('toto')
80
-
81
- t = Raabro.rep(:x, i, :to, 1)
82
-
83
- expect(t.to_a(:leaves => true)).to eq(
84
- [ :x, 1, 0, 4, nil, :rep, [
85
- [ nil, 1, 0, 2, nil, :str, 'to' ],
86
- [ nil, 1, 2, 2, nil, :str, 'to' ],
87
- [ nil, 0, 4, 0, nil, :str, [] ]
88
- ] ]
89
- )
90
- expect(i.offset).to eq(4)
91
- end
92
- end
93
- end
94
-
@@ -1,52 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Sun Sep 20 07:12:35 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Raabro do
12
-
13
- describe '.rex' do
14
-
15
- it 'hits' do
16
-
17
- i = Raabro::Input.new('toto')
18
-
19
- t = Raabro.rex(nil, i, /t[ua]/)
20
-
21
- expect(t.to_a).to eq(
22
- [ nil, 0, 0, 0, nil, :rex, [] ]
23
- )
24
- expect(i.offset).to eq(0)
25
- end
26
-
27
- it 'misses' do
28
-
29
- i = Raabro::Input.new('toto')
30
-
31
- t = Raabro.rex(nil, i, /(to)+/)
32
-
33
- expect(t.to_a(:leaves => true)).to eq(
34
- [ nil, 1, 0, 4, nil, :rex, 'toto' ]
35
- )
36
- expect(i.offset).to eq(4)
37
- end
38
-
39
- it 'misses if the match is not at the current input offset' do
40
-
41
- i = Raabro::Input.new('tato')
42
-
43
- t = Raabro.rex(:biga, i, /(to)+/)
44
-
45
- expect(t.to_a(:leaves => true)).to eq(
46
- [ :biga, 0, 0, 0, nil, :rex, [] ]
47
- )
48
- expect(i.offset).to eq(0)
49
- end
50
- end
51
- end
52
-
@@ -1,43 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Sun Oct 11 04:24:32 SGT 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- module Sample::OwnRewrite include Raabro
12
-
13
- # parse
14
-
15
- def hello(i); str(:hello, i, 'hello'); end
16
-
17
- #alias root exp
18
- # not necessary since Raabro takes the last defined parser as the root
19
-
20
- # rewrite
21
-
22
- def rewrite(t)
23
-
24
- [ :ok, t.string ]
25
- end
26
- end
27
-
28
-
29
- describe Raabro do
30
-
31
- describe Sample::OwnRewrite do
32
-
33
- it 'uses its own rewrite' do
34
-
35
- expect(
36
- Sample::OwnRewrite.parse('hello')
37
- ).to eq(
38
- [ :ok, 'hello' ]
39
- )
40
- end
41
- end
42
- end
43
-
@@ -1,118 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Mon Sep 21 16:58:01 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- module Sample::Xel include Raabro
12
-
13
- # parse
14
-
15
- def pa(i); str(nil, i, '('); end
16
- def pz(i); str(nil, i, ')'); end
17
- def com(i); str(nil, i, ','); end
18
-
19
- def num(i); rex(:num, i, /-?[0-9]+/); end
20
-
21
- def args(i); eseq(:args, i, :pa, :exp, :com, :pz); end
22
- def funame(i); rex(:funame, i, /[A-Z][A-Z0-9]*/); end
23
- def fun(i); seq(:fun, i, :funame, :args); end
24
-
25
- def exp(i); alt(:exp, i, :fun, :num); end
26
-
27
- #alias root exp
28
- # not necessary since Raabro takes the last defined parser as the root
29
-
30
- # rewrite
31
-
32
- def rewrite_exp(t); rewrite(t.children[0]); end
33
- def rewrite_num(t); t.string.to_i; end
34
-
35
- def rewrite_fun(t)
36
-
37
- #[ t.children[0].string ] +
38
- #t.children[1].children.inject([]) { |a, e| a << rewrite(e) if e.name; a }
39
- [ t.children[0].string ] + t.children[1].odd_children.map { |c| rewrite(c) }
40
- end
41
- end
42
-
43
-
44
- describe Raabro do
45
-
46
- describe Sample::Xel do
47
-
48
- describe '.funame' do
49
-
50
- it 'hits' do
51
-
52
- i = Raabro::Input.new('NADA')
53
-
54
- t = Sample::Xel.funame(i)
55
-
56
- expect(t.to_a(:leaves => true)).to eq(
57
- [ :funame, 1, 0, 4, nil, :rex, 'NADA' ]
58
- )
59
- end
60
- end
61
-
62
- describe '.fun' do
63
-
64
- it 'parses a function call' do
65
-
66
- i = Raabro::Input.new('SUM(1,MUL(4,5))', :prune => true)
67
-
68
- t = Sample::Xel.fun(i)
69
-
70
- expect(t.result).to eq(1)
71
-
72
- expect(
73
- Sample::Xel.rewrite(t)
74
- ).to eq(
75
- [ 'SUM', 1, [ 'MUL', 4, 5 ] ]
76
- )
77
- end
78
- end
79
-
80
- describe '.parse' do
81
-
82
- it 'parses (success)' do
83
-
84
- expect(
85
- Sample::Xel.parse('MUL(7,-3)')
86
- ).to eq(
87
- [ 'MUL', 7, -3 ]
88
- )
89
- end
90
-
91
- it 'parses (rewrite: false, success)' do
92
-
93
- expect(
94
- Sample::Xel.parse('MUL(7,-3)', rewrite: false).to_s
95
- ).to eq(%{
96
- 1 :exp 0,9
97
- 1 :fun 0,9
98
- 1 :funame 0,3 "MUL"
99
- 1 :args 3,6
100
- 1 nil 3,1 "("
101
- 1 :exp 4,1
102
- 1 :num 4,1 "7"
103
- 1 nil 5,1 ","
104
- 1 :exp 6,2
105
- 1 :num 6,2 "-3"
106
- 1 nil 8,1 ")"
107
- }.strip)
108
- end
109
-
110
- it 'parses (miss)' do
111
-
112
- expect(Sample::Xel.parse('MUL(7,3) ')).to eq(nil)
113
- expect(Sample::Xel.parse('MUL(7,3')).to eq(nil)
114
- end
115
- end
116
- end
117
- end
118
-
@@ -1,92 +0,0 @@
1
-
2
- #
3
- # specifying raabro
4
- #
5
- # Sun Dec 13 06:10:00 JST 2015
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- module Sample::Xell include Raabro
12
-
13
- # parse
14
-
15
- def pa(i); str(nil, i, '('); end
16
- def pz(i); str(nil, i, ')'); end
17
- def com(i); str(nil, i, ','); end
18
-
19
- def num(i); rex(:num, i, /-?[0-9]+/); end
20
-
21
- def args(i); eseq(nil, i, :pa, :exp, :com, :pz); end
22
- def funame(i); rex(nil, i, /[A-Z][A-Z0-9]*/); end
23
- def fun(i); seq(:fun, i, :funame, :args); end
24
-
25
- def exp(i); alt(nil, i, :fun, :num); end
26
-
27
- # rewrite
28
-
29
- #def rewrite_(t)
30
- #
31
- # c = t.children.find { |c| c.length > 0 || c.name }
32
- # c ? rewrite(c) : nil
33
- #end
34
- #
35
- # part of aabro now
36
-
37
- def rewrite_num(t); t.string.to_i; end
38
-
39
- def rewrite_fun(t)
40
-
41
- #as = []
42
- #t.children[1].children.each_with_index { |e, i| as << e if i.odd? }
43
- #[ t.children[0].string ] + as.collect { |a| rewrite(a) }
44
- [ t.children[0].string ] +
45
- t.children[1].odd_children.collect { |a| rewrite(a) }
46
- end
47
- end
48
-
49
-
50
- describe Raabro do
51
-
52
- describe Sample::Xell do
53
-
54
- describe '.parse' do
55
-
56
- it 'parses (success)' do
57
-
58
- expect(
59
- Sample::Xell.parse('MUL(7,-3)')
60
- ).to eq(
61
- [ 'MUL', 7, -3 ]
62
- )
63
- end
64
-
65
- it 'parses (rewrite: false, success)' do
66
-
67
- expect(
68
- Sample::Xell.parse('MUL(7,-3)', rewrite: false).to_s
69
- ).to eq(%{
70
- 1 nil 0,9
71
- 1 :fun 0,9
72
- 1 nil 0,3 "MUL"
73
- 1 nil 3,6
74
- 1 nil 3,1 "("
75
- 1 nil 4,1
76
- 1 :num 4,1 "7"
77
- 1 nil 5,1 ","
78
- 1 nil 6,2
79
- 1 :num 6,2 "-3"
80
- 1 nil 8,1 ")"
81
- }.strip)
82
- end
83
-
84
- it 'parses (miss)' do
85
-
86
- expect(Sample::Xell.parse('MUL(7,3) ')).to eq(nil)
87
- expect(Sample::Xell.parse('MUL(7,3')).to eq(nil)
88
- end
89
- end
90
- end
91
- end
92
-