sequitur 0.1.13 → 0.1.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.rubocop.yml +5 -4
- data/CHANGELOG.md +7 -2
- data/Rakefile +4 -6
- data/lib/sequitur.rb +0 -1
- data/lib/sequitur/constants.rb +1 -1
- data/lib/sequitur/digram.rb +0 -3
- data/lib/sequitur/dynamic_grammar.rb +0 -3
- data/lib/sequitur/formatter/base_formatter.rb +0 -3
- data/lib/sequitur/formatter/base_text.rb +0 -3
- data/lib/sequitur/formatter/debug.rb +0 -3
- data/lib/sequitur/grammar_visitor.rb +0 -4
- data/lib/sequitur/production.rb +0 -5
- data/lib/sequitur/production_ref.rb +0 -4
- data/lib/sequitur/sequitur_grammar.rb +0 -4
- data/lib/sequitur/symbol_sequence.rb +6 -8
- data/spec/sequitur/digram_spec.rb +0 -8
- data/spec/sequitur/dynamic_grammar_spec.rb +144 -159
- data/spec/sequitur/formatter/base_text_spec.rb +92 -95
- data/spec/sequitur/formatter/debug_spec.rb +111 -114
- data/spec/sequitur/grammar_visitor_spec.rb +80 -88
- data/spec/sequitur/production_ref_spec.rb +102 -111
- data/spec/sequitur/production_spec.rb +361 -376
- data/spec/sequitur/sequitur_grammar_spec.rb +1 -5
- data/spec/sequitur/symbol_sequence_spec.rb +115 -124
- metadata +2 -2
@@ -1,95 +1,92 @@
|
|
1
|
-
require_relative '../../spec_helper'
|
2
|
-
require 'stringio'
|
3
|
-
|
4
|
-
require_relative '../../../lib/sequitur/dynamic_grammar'
|
5
|
-
|
6
|
-
# Load the class under test
|
7
|
-
require_relative '../../../lib/sequitur/formatter/base_text'
|
8
|
-
|
9
|
-
module Sequitur # Re-open the module to get rid of qualified names
|
10
|
-
module Formatter
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
let(:
|
22
|
-
let(:
|
23
|
-
let(:
|
24
|
-
|
25
|
-
|
26
|
-
let(:
|
27
|
-
|
28
|
-
grm
|
29
|
-
grm.add_production(
|
30
|
-
grm.add_production(
|
31
|
-
grm.add_production(
|
32
|
-
grm
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
instance
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
instance
|
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
|
-
end #
|
91
|
-
|
92
|
-
|
93
|
-
end # module
|
94
|
-
|
95
|
-
# End of file
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
require_relative '../../../lib/sequitur/dynamic_grammar'
|
5
|
+
|
6
|
+
# Load the class under test
|
7
|
+
require_relative '../../../lib/sequitur/formatter/base_text'
|
8
|
+
|
9
|
+
module Sequitur # Re-open the module to get rid of qualified names
|
10
|
+
module Formatter
|
11
|
+
describe BaseText do
|
12
|
+
# Factory method. Build a production with the given sequence
|
13
|
+
# of symbols as its rhs.
|
14
|
+
def build_production(*symbols)
|
15
|
+
prod = Production.new
|
16
|
+
symbols.each { |symb| prod.append_symbol(symb) }
|
17
|
+
return prod
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:p_a) { build_production(:a) }
|
21
|
+
let(:p_b) { build_production(:b) }
|
22
|
+
let(:p_c) { build_production(:c) }
|
23
|
+
let(:p_bc) { build_production(p_b, p_c) }
|
24
|
+
|
25
|
+
let(:empty_grammar) { DynamicGrammar.new }
|
26
|
+
let(:sample_grammar) do
|
27
|
+
grm = DynamicGrammar.new
|
28
|
+
grm.add_production(p_a)
|
29
|
+
grm.add_production(p_b)
|
30
|
+
grm.add_production(p_c)
|
31
|
+
grm.add_production(p_bc)
|
32
|
+
grm
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:destination) { StringIO.new('', 'w') }
|
36
|
+
|
37
|
+
context 'Standard creation & initialization:' do
|
38
|
+
it 'should be initialized with an IO argument' do
|
39
|
+
expect { BaseText.new(StringIO.new('', 'w')) }.not_to raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should know its output destination' do
|
43
|
+
instance = BaseText.new(destination)
|
44
|
+
expect(instance.output).to eq(destination)
|
45
|
+
end
|
46
|
+
end # context
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
context 'Formatting events:' do
|
51
|
+
it 'should support events of an empty grammar' do
|
52
|
+
instance = BaseText.new(destination)
|
53
|
+
a_visitor = empty_grammar.visitor
|
54
|
+
instance.render(a_visitor)
|
55
|
+
expectations = <<-SNIPPET
|
56
|
+
start :.
|
57
|
+
SNIPPET
|
58
|
+
expect(destination.string).to eq(expectations)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should support visit events with an explicit visitor' do
|
62
|
+
instance = BaseText.new(destination)
|
63
|
+
a_visitor = sample_grammar.visitor # Use visitor explicitly
|
64
|
+
instance.render(a_visitor)
|
65
|
+
expectations = <<-SNIPPET
|
66
|
+
start :.
|
67
|
+
P1 : a.
|
68
|
+
P2 : b.
|
69
|
+
P3 : c.
|
70
|
+
P4 : P2 P3.
|
71
|
+
SNIPPET
|
72
|
+
expect(destination.string).to eq(expectations)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should support visit events without an explicit visitor' do
|
76
|
+
instance = BaseText.new(destination)
|
77
|
+
instance.render(sample_grammar)
|
78
|
+
expectations = <<-SNIPPET
|
79
|
+
start :.
|
80
|
+
P1 : a.
|
81
|
+
P2 : b.
|
82
|
+
P3 : c.
|
83
|
+
P4 : P2 P3.
|
84
|
+
SNIPPET
|
85
|
+
expect(destination.string).to eq(expectations)
|
86
|
+
end
|
87
|
+
end # context
|
88
|
+
end # describe
|
89
|
+
end # module
|
90
|
+
end # module
|
91
|
+
|
92
|
+
# End of file
|
@@ -1,114 +1,111 @@
|
|
1
|
-
require_relative '../../spec_helper'
|
2
|
-
require 'stringio'
|
3
|
-
|
4
|
-
require_relative '../../../lib/sequitur/dynamic_grammar'
|
5
|
-
|
6
|
-
# Load the class under test
|
7
|
-
require_relative '../../../lib/sequitur/formatter/debug'
|
8
|
-
|
9
|
-
module Sequitur # Re-open the module to get rid of qualified names
|
10
|
-
module Formatter
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
let(:
|
22
|
-
let(:
|
23
|
-
let(:
|
24
|
-
|
25
|
-
|
26
|
-
let(:
|
27
|
-
|
28
|
-
grm
|
29
|
-
grm.add_production(
|
30
|
-
grm.add_production(
|
31
|
-
grm.add_production(
|
32
|
-
grm
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
instance
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
instance
|
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
|
-
before_non_terminal
|
99
|
-
after_non_terminal
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
end #
|
110
|
-
|
111
|
-
|
112
|
-
end # module
|
113
|
-
|
114
|
-
# End of file
|
1
|
+
require_relative '../../spec_helper'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
require_relative '../../../lib/sequitur/dynamic_grammar'
|
5
|
+
|
6
|
+
# Load the class under test
|
7
|
+
require_relative '../../../lib/sequitur/formatter/debug'
|
8
|
+
|
9
|
+
module Sequitur # Re-open the module to get rid of qualified names
|
10
|
+
module Formatter
|
11
|
+
describe Debug do
|
12
|
+
# Factory method. Build a production with the given sequence
|
13
|
+
# of symbols as its rhs.
|
14
|
+
def build_production(*symbols)
|
15
|
+
prod = Production.new
|
16
|
+
symbols.each { |symb| prod.append_symbol(symb) }
|
17
|
+
return prod
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:p_a) { build_production(:a) }
|
21
|
+
let(:p_b) { build_production(:b) }
|
22
|
+
let(:p_c) { build_production(:c) }
|
23
|
+
let(:p_bc) { build_production(p_b, p_c) }
|
24
|
+
|
25
|
+
let(:empty_grammar) { DynamicGrammar.new }
|
26
|
+
let(:sample_grammar) do
|
27
|
+
grm = DynamicGrammar.new
|
28
|
+
grm.add_production(p_a)
|
29
|
+
grm.add_production(p_b)
|
30
|
+
grm.add_production(p_c)
|
31
|
+
grm.add_production(p_bc)
|
32
|
+
grm
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:destination) { StringIO.new('', 'w') }
|
36
|
+
|
37
|
+
context 'Standard creation & initialization:' do
|
38
|
+
it 'should be initialized with an IO argument' do
|
39
|
+
expect { Debug.new(StringIO.new('', 'w')) }.not_to raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should know its output destination' do
|
43
|
+
instance = Debug.new(destination)
|
44
|
+
expect(instance.output).to eq(destination)
|
45
|
+
end
|
46
|
+
end # context
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
context 'Formatting events:' do
|
51
|
+
it 'should support events of an empty grammar' do
|
52
|
+
instance = Debug.new(destination)
|
53
|
+
a_visitor = empty_grammar.visitor
|
54
|
+
instance.render(a_visitor)
|
55
|
+
expectations = <<-SNIPPET
|
56
|
+
before_grammar
|
57
|
+
before_production
|
58
|
+
before_rhs
|
59
|
+
after_rhs
|
60
|
+
after_production
|
61
|
+
after_grammar
|
62
|
+
SNIPPET
|
63
|
+
expect(destination.string).to eq(expectations)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should support events of a non-empty grammar' do
|
67
|
+
instance = Debug.new(destination)
|
68
|
+
a_visitor = sample_grammar.visitor
|
69
|
+
instance.render(a_visitor)
|
70
|
+
expectations = <<-SNIPPET
|
71
|
+
before_grammar
|
72
|
+
before_production
|
73
|
+
before_rhs
|
74
|
+
after_rhs
|
75
|
+
after_production
|
76
|
+
before_production
|
77
|
+
before_rhs
|
78
|
+
before_terminal
|
79
|
+
after_terminal
|
80
|
+
after_rhs
|
81
|
+
after_production
|
82
|
+
before_production
|
83
|
+
before_rhs
|
84
|
+
before_terminal
|
85
|
+
after_terminal
|
86
|
+
after_rhs
|
87
|
+
after_production
|
88
|
+
before_production
|
89
|
+
before_rhs
|
90
|
+
before_terminal
|
91
|
+
after_terminal
|
92
|
+
after_rhs
|
93
|
+
after_production
|
94
|
+
before_production
|
95
|
+
before_rhs
|
96
|
+
before_non_terminal
|
97
|
+
after_non_terminal
|
98
|
+
before_non_terminal
|
99
|
+
after_non_terminal
|
100
|
+
after_rhs
|
101
|
+
after_production
|
102
|
+
after_grammar
|
103
|
+
SNIPPET
|
104
|
+
expect(destination.string).to eq(expectations)
|
105
|
+
end
|
106
|
+
end # context
|
107
|
+
end # describe
|
108
|
+
end # module
|
109
|
+
end # module
|
110
|
+
|
111
|
+
# End of file
|
@@ -1,88 +1,80 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
|
3
|
-
# Load the class under test
|
4
|
-
require_relative '../../lib/sequitur/grammar_visitor'
|
5
|
-
|
6
|
-
module Sequitur # Re-open the module to get rid of qualified names
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
expect(subject.subscribers).to eq(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
subject.
|
51
|
-
subject.
|
52
|
-
subject.unsubscribe(
|
53
|
-
expect(subject.subscribers
|
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
|
-
end # context
|
83
|
-
|
84
|
-
end # describe
|
85
|
-
|
86
|
-
end # module
|
87
|
-
|
88
|
-
# End of file
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
# Load the class under test
|
4
|
+
require_relative '../../lib/sequitur/grammar_visitor'
|
5
|
+
|
6
|
+
module Sequitur # Re-open the module to get rid of qualified names
|
7
|
+
describe GrammarVisitor do
|
8
|
+
# Use a double(mock) as a grammar
|
9
|
+
let(:fake) { double('fake-grammar') }
|
10
|
+
|
11
|
+
context 'Standard creation & initialization:' do
|
12
|
+
# Default instantiation rule
|
13
|
+
subject { GrammarVisitor.new(fake) }
|
14
|
+
|
15
|
+
it 'should be initialized with a grammar argument' do
|
16
|
+
expect { GrammarVisitor.new(fake) }.not_to raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should know the grammar to visit' do
|
20
|
+
expect(subject.grammar).to eq(fake)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "shouldn't have subscribers at start" do
|
24
|
+
expect(subject.subscribers).to be_empty
|
25
|
+
end
|
26
|
+
end # context
|
27
|
+
|
28
|
+
|
29
|
+
context 'Subscribing:' do
|
30
|
+
# Default instantiation rule
|
31
|
+
subject { GrammarVisitor.new(fake) }
|
32
|
+
|
33
|
+
let(:listener1) { double('fake-formatter1') }
|
34
|
+
let(:listener2) { double('fake-formatter2') }
|
35
|
+
|
36
|
+
it 'should allow subscriptions' do
|
37
|
+
subject.subscribe(listener1)
|
38
|
+
expect(subject.subscribers.size).to eq(1)
|
39
|
+
expect(subject.subscribers).to eq([listener1])
|
40
|
+
|
41
|
+
subject.subscribe(listener2)
|
42
|
+
expect(subject.subscribers.size).to eq(2)
|
43
|
+
expect(subject.subscribers).to eq([listener1, listener2])
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should allow un-subcriptions' do
|
47
|
+
subject.subscribe(listener1)
|
48
|
+
subject.subscribe(listener2)
|
49
|
+
subject.unsubscribe(listener2)
|
50
|
+
expect(subject.subscribers.size).to eq(1)
|
51
|
+
expect(subject.subscribers).to eq([listener1])
|
52
|
+
subject.unsubscribe(listener1)
|
53
|
+
expect(subject.subscribers).to be_empty
|
54
|
+
end
|
55
|
+
end # context
|
56
|
+
|
57
|
+
context 'Notifying visit events:' do
|
58
|
+
# Default instantiation rule
|
59
|
+
subject do
|
60
|
+
instance = GrammarVisitor.new(fake)
|
61
|
+
instance.subscribe(listener1)
|
62
|
+
instance
|
63
|
+
end
|
64
|
+
|
65
|
+
# Use doubles/mocks to simulate formatters
|
66
|
+
let(:listener1) { double('fake-formatter1') }
|
67
|
+
let(:listener2) { double('fake-formatter2') }
|
68
|
+
let(:mock_production) { double('fake-production') }
|
69
|
+
|
70
|
+
it 'should react to the start_visit_grammar message' do
|
71
|
+
# Notify subscribers when start the visit of the grammar
|
72
|
+
expect(listener1).to receive(:before_grammar).with(fake)
|
73
|
+
|
74
|
+
subject.start_visit_grammar(fake)
|
75
|
+
end
|
76
|
+
end # context
|
77
|
+
end # describe
|
78
|
+
end # module
|
79
|
+
|
80
|
+
# End of file
|