antelope 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +25 -25
- data/.rspec +3 -3
- data/.travis.yml +10 -10
- data/.yardopts +7 -7
- data/CONTRIBUTING.md +50 -38
- data/GENERATORS.md +180 -124
- data/Gemfile +7 -7
- data/LICENSE.txt +22 -22
- data/README.md +240 -104
- data/Rakefile +2 -2
- data/TODO.md +58 -58
- data/antelope.gemspec +29 -28
- data/bin/antelope +7 -7
- data/examples/deterministic.ace +35 -35
- data/examples/example.ace +52 -51
- data/examples/example.ace.err +192 -192
- data/examples/example.ace.inf +432 -432
- data/examples/example.ate +70 -70
- data/examples/example.ate.err +192 -192
- data/examples/example.ate.inf +432 -432
- data/examples/liquidscript.ace +233 -233
- data/examples/simple.ace +22 -22
- data/lib/antelope/ace/compiler.rb +334 -334
- data/lib/antelope/ace/errors.rb +30 -30
- data/lib/antelope/ace/scanner/argument.rb +57 -57
- data/lib/antelope/ace/scanner/first.rb +89 -89
- data/lib/antelope/ace/scanner/second.rb +178 -178
- data/lib/antelope/ace/scanner/third.rb +27 -27
- data/lib/antelope/ace/scanner.rb +144 -144
- data/lib/antelope/ace.rb +47 -47
- data/lib/antelope/cli.rb +60 -60
- data/lib/antelope/errors.rb +25 -25
- data/lib/antelope/generation/constructor/first.rb +86 -86
- data/lib/antelope/generation/constructor/follow.rb +105 -105
- data/lib/antelope/generation/constructor/nullable.rb +64 -64
- data/lib/antelope/generation/constructor.rb +127 -127
- data/lib/antelope/generation/errors.rb +17 -17
- data/lib/antelope/generation/null.rb +13 -13
- data/lib/antelope/generation/recognizer/rule.rb +216 -216
- data/lib/antelope/generation/recognizer/state.rb +129 -129
- data/lib/antelope/generation/recognizer.rb +177 -177
- data/lib/antelope/generation/tableizer.rb +176 -176
- data/lib/antelope/generation.rb +15 -15
- data/lib/antelope/generator/base/coerce.rb +115 -0
- data/lib/antelope/generator/base/extra.rb +50 -0
- data/lib/antelope/generator/base.rb +134 -264
- data/lib/antelope/generator/c.rb +11 -11
- data/lib/antelope/generator/c_header.rb +105 -105
- data/lib/antelope/generator/c_source.rb +39 -39
- data/lib/antelope/generator/error.rb +34 -34
- data/lib/antelope/generator/group.rb +60 -57
- data/lib/antelope/generator/html.rb +51 -51
- data/lib/antelope/generator/info.rb +47 -47
- data/lib/antelope/generator/null.rb +18 -18
- data/lib/antelope/generator/output.rb +17 -17
- data/lib/antelope/generator/ruby.rb +112 -79
- data/lib/antelope/generator/templates/c_header.ant +36 -36
- data/lib/antelope/generator/templates/c_source.ant +202 -202
- data/lib/antelope/generator/templates/error.erb +40 -0
- data/lib/antelope/generator/templates/html/antelope.css +53 -1
- data/lib/antelope/generator/templates/html/antelope.html +82 -1
- data/lib/antelope/generator/templates/html/antelope.js +9 -1
- data/lib/antelope/generator/templates/html/css.ant +53 -53
- data/lib/antelope/generator/templates/html/html.ant +82 -82
- data/lib/antelope/generator/templates/html/js.ant +9 -9
- data/lib/antelope/generator/templates/info.erb +61 -0
- data/lib/antelope/generator/templates/{ruby.ant → ruby.erb} +171 -178
- data/lib/antelope/generator.rb +62 -66
- data/lib/antelope/grammar/generation.rb +76 -76
- data/lib/antelope/grammar/loading.rb +84 -84
- data/lib/antelope/grammar/precedence.rb +59 -59
- data/lib/antelope/grammar/precedences.rb +64 -64
- data/lib/antelope/grammar/production.rb +56 -56
- data/lib/antelope/grammar/productions.rb +154 -154
- data/lib/antelope/grammar/symbols.rb +64 -64
- data/lib/antelope/grammar/token/epsilon.rb +23 -23
- data/lib/antelope/grammar/token/error.rb +24 -24
- data/lib/antelope/grammar/token/nonterminal.rb +15 -15
- data/lib/antelope/grammar/token/terminal.rb +15 -15
- data/lib/antelope/grammar/token.rb +231 -231
- data/lib/antelope/grammar.rb +68 -68
- data/lib/antelope/version.rb +6 -6
- data/lib/antelope.rb +18 -19
- data/optimizations.txt +42 -42
- data/spec/antelope/ace/compiler_spec.rb +60 -60
- data/spec/antelope/ace/scanner_spec.rb +27 -27
- data/spec/antelope/generation/constructor_spec.rb +131 -131
- data/spec/fixtures/simple.ace +22 -22
- data/spec/spec_helper.rb +39 -39
- data/spec/support/benchmark_helper.rb +5 -5
- data/spec/support/grammar_helper.rb +14 -14
- data/subl/Ace (Ruby).JSON-tmLanguage +94 -94
- data/subl/Ace (Ruby).tmLanguage +153 -153
- metadata +22 -11
- data/lib/antelope/generator/templates/error.ant +0 -34
- data/lib/antelope/generator/templates/info.ant +0 -53
- data/lib/antelope/template/compiler.rb +0 -78
- data/lib/antelope/template/errors.rb +0 -9
- data/lib/antelope/template/scanner.rb +0 -109
- data/lib/antelope/template.rb +0 -64
- data/spec/antelope/template_spec.rb +0 -50
data/optimizations.txt
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
[0;31;49mlib/antelope/ace/grammar/precedences.rb[0m
|
2
|
-
Array#select.first is slower than Array#detect. Occured at lines: 31, 35.
|
3
|
-
Using each_with_index is slower than while loop. Occured at lines: 51.
|
4
|
-
|
5
|
-
[0;31;49mlib/antelope/ace/grammar/productions.rb[0m
|
6
|
-
Using each_with_index is slower than while loop. Occured at lines: 71, 106.
|
7
|
-
|
8
|
-
[0;31;49mlib/antelope/ace/grammar/symbols.rb[0m
|
9
|
-
Hash#fetch with second argument is slower than Hash#fetch with block. Occured at lines: 17.
|
10
|
-
|
11
|
-
[0;31;49mlib/antelope/generation/constructor/first.rb[0m
|
12
|
-
Using each_with_index is slower than while loop. Occured at lines: 63.
|
13
|
-
|
14
|
-
[0;31;49mlib/antelope/generation/constructor/follow.rb[0m
|
15
|
-
Using each_with_index is slower than while loop. Occured at lines: 78.
|
16
|
-
|
17
|
-
[0;31;49mlib/antelope/generation/constructor.rb[0m
|
18
|
-
Using each_with_index is slower than while loop. Occured at lines: 71.
|
19
|
-
|
20
|
-
[0;31;49mlib/antelope/generation/recognizer/rule.rb[0m
|
21
|
-
Parallel assignment is slower than sequential assignment. Occured at lines: 77.
|
22
|
-
Using each_with_index is slower than while loop. Occured at lines: 178.
|
23
|
-
|
24
|
-
[0;31;49mlib/antelope/generation/recognizer/state.rb[0m
|
25
|
-
Using each_with_index is slower than while loop. Occured at lines: 123.
|
26
|
-
|
27
|
-
[0;31;49mlib/antelope/generation/recognizer.rb[0m
|
28
|
-
Using each_with_index is slower than while loop. Occured at lines: 138.
|
29
|
-
|
30
|
-
[0;31;49mlib/antelope/generation/tableizer.rb[0m
|
31
|
-
Using each_with_index is slower than while loop. Occured at lines: 90, 138.
|
32
|
-
Parallel assignment is slower than sequential assignment. Occured at lines: 99.
|
33
|
-
|
34
|
-
[0;31;49mlib/antelope/generator/base.rb[0m
|
35
|
-
Using each_with_index is slower than while loop. Occured at lines: 124.
|
36
|
-
|
37
|
-
[0;31;49mlib/antelope/generator/c_header.rb[0m
|
38
|
-
Using each_with_index is slower than while loop. Occured at lines: 91.
|
39
|
-
|
40
|
-
[0;31;49mlib/antelope/template/compiler.rb[0m
|
41
|
-
Don't rescue NoMethodError, rather check with respond_to?. Occured at lines: 40.
|
42
|
-
|
1
|
+
[0;31;49mlib/antelope/ace/grammar/precedences.rb[0m
|
2
|
+
Array#select.first is slower than Array#detect. Occured at lines: 31, 35.
|
3
|
+
Using each_with_index is slower than while loop. Occured at lines: 51.
|
4
|
+
|
5
|
+
[0;31;49mlib/antelope/ace/grammar/productions.rb[0m
|
6
|
+
Using each_with_index is slower than while loop. Occured at lines: 71, 106.
|
7
|
+
|
8
|
+
[0;31;49mlib/antelope/ace/grammar/symbols.rb[0m
|
9
|
+
Hash#fetch with second argument is slower than Hash#fetch with block. Occured at lines: 17.
|
10
|
+
|
11
|
+
[0;31;49mlib/antelope/generation/constructor/first.rb[0m
|
12
|
+
Using each_with_index is slower than while loop. Occured at lines: 63.
|
13
|
+
|
14
|
+
[0;31;49mlib/antelope/generation/constructor/follow.rb[0m
|
15
|
+
Using each_with_index is slower than while loop. Occured at lines: 78.
|
16
|
+
|
17
|
+
[0;31;49mlib/antelope/generation/constructor.rb[0m
|
18
|
+
Using each_with_index is slower than while loop. Occured at lines: 71.
|
19
|
+
|
20
|
+
[0;31;49mlib/antelope/generation/recognizer/rule.rb[0m
|
21
|
+
Parallel assignment is slower than sequential assignment. Occured at lines: 77.
|
22
|
+
Using each_with_index is slower than while loop. Occured at lines: 178.
|
23
|
+
|
24
|
+
[0;31;49mlib/antelope/generation/recognizer/state.rb[0m
|
25
|
+
Using each_with_index is slower than while loop. Occured at lines: 123.
|
26
|
+
|
27
|
+
[0;31;49mlib/antelope/generation/recognizer.rb[0m
|
28
|
+
Using each_with_index is slower than while loop. Occured at lines: 138.
|
29
|
+
|
30
|
+
[0;31;49mlib/antelope/generation/tableizer.rb[0m
|
31
|
+
Using each_with_index is slower than while loop. Occured at lines: 90, 138.
|
32
|
+
Parallel assignment is slower than sequential assignment. Occured at lines: 99.
|
33
|
+
|
34
|
+
[0;31;49mlib/antelope/generator/base.rb[0m
|
35
|
+
Using each_with_index is slower than while loop. Occured at lines: 124.
|
36
|
+
|
37
|
+
[0;31;49mlib/antelope/generator/c_header.rb[0m
|
38
|
+
Using each_with_index is slower than while loop. Occured at lines: 91.
|
39
|
+
|
40
|
+
[0;31;49mlib/antelope/template/compiler.rb[0m
|
41
|
+
Don't rescue NoMethodError, rather check with respond_to?. Occured at lines: 40.
|
42
|
+
|
@@ -1,60 +1,60 @@
|
|
1
|
-
describe Ace::Compiler do
|
2
|
-
let :file do
|
3
|
-
<<-DOC
|
4
|
-
%{
|
5
|
-
test
|
6
|
-
%}
|
7
|
-
|
8
|
-
%require "#{VERSION}"
|
9
|
-
%language "ruby"
|
10
|
-
|
11
|
-
%terminal NUMBER
|
12
|
-
%terminal SEMICOLON ";"
|
13
|
-
%terminal ADD "+"
|
14
|
-
%terminal LPAREN "("
|
15
|
-
%terminal RPAREN ")"
|
16
|
-
|
17
|
-
%%
|
18
|
-
|
19
|
-
s: e
|
20
|
-
e: t ADD e
|
21
|
-
t: NUMBER | LPAREN e RPAREN
|
22
|
-
|
23
|
-
%%
|
24
|
-
|
25
|
-
hello
|
26
|
-
DOC
|
27
|
-
end
|
28
|
-
|
29
|
-
let :tokens do
|
30
|
-
Ace::Scanner.scan(file)
|
31
|
-
end
|
32
|
-
|
33
|
-
let :compiler do
|
34
|
-
Ace::Compiler.new(tokens)
|
35
|
-
end
|
36
|
-
|
37
|
-
subject do
|
38
|
-
compiler.compile
|
39
|
-
compiler
|
40
|
-
end
|
41
|
-
|
42
|
-
its(:body) { should =~ /test/ }
|
43
|
-
its(:body) { should =~ /hello/ }
|
44
|
-
its(:options) { should have_key :type }
|
45
|
-
|
46
|
-
it 'has the proper terminals' do
|
47
|
-
expect(subject.options[:terminals].map(&:first)).to eq [:NUMBER,
|
48
|
-
:SEMICOLON, :ADD, :LPAREN, :RPAREN]
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'with an unmatched version' do
|
52
|
-
let(:file) { "%require \"0.0.0\"\n%%\n%%\n" }
|
53
|
-
|
54
|
-
it 'raises an error' do
|
55
|
-
expect do
|
56
|
-
subject
|
57
|
-
end.to raise_error(IncompatibleVersionError)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
1
|
+
describe Ace::Compiler do
|
2
|
+
let :file do
|
3
|
+
<<-DOC
|
4
|
+
%{
|
5
|
+
test
|
6
|
+
%}
|
7
|
+
|
8
|
+
%require "#{VERSION}"
|
9
|
+
%language "ruby"
|
10
|
+
|
11
|
+
%terminal NUMBER
|
12
|
+
%terminal SEMICOLON ";"
|
13
|
+
%terminal ADD "+"
|
14
|
+
%terminal LPAREN "("
|
15
|
+
%terminal RPAREN ")"
|
16
|
+
|
17
|
+
%%
|
18
|
+
|
19
|
+
s: e
|
20
|
+
e: t ADD e
|
21
|
+
t: NUMBER | LPAREN e RPAREN
|
22
|
+
|
23
|
+
%%
|
24
|
+
|
25
|
+
hello
|
26
|
+
DOC
|
27
|
+
end
|
28
|
+
|
29
|
+
let :tokens do
|
30
|
+
Ace::Scanner.scan(file)
|
31
|
+
end
|
32
|
+
|
33
|
+
let :compiler do
|
34
|
+
Ace::Compiler.new(tokens)
|
35
|
+
end
|
36
|
+
|
37
|
+
subject do
|
38
|
+
compiler.compile
|
39
|
+
compiler
|
40
|
+
end
|
41
|
+
|
42
|
+
its(:body) { should =~ /test/ }
|
43
|
+
its(:body) { should =~ /hello/ }
|
44
|
+
its(:options) { should have_key :type }
|
45
|
+
|
46
|
+
it 'has the proper terminals' do
|
47
|
+
expect(subject.options[:terminals].map(&:first)).to eq [:NUMBER,
|
48
|
+
:SEMICOLON, :ADD, :LPAREN, :RPAREN]
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with an unmatched version' do
|
52
|
+
let(:file) { "%require \"0.0.0\"\n%%\n%%\n" }
|
53
|
+
|
54
|
+
it 'raises an error' do
|
55
|
+
expect do
|
56
|
+
subject
|
57
|
+
end.to raise_error(IncompatibleVersionError)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -1,27 +1,27 @@
|
|
1
|
-
describe Ace::Scanner do
|
2
|
-
|
3
|
-
it 'properly scans' do
|
4
|
-
expect(scan("%test \"a\" hi\n%%\nt: d { { } }\n%%\nhi\n")).to eq [
|
5
|
-
[:directive, 'test', %w(a hi)],
|
6
|
-
[:second],
|
7
|
-
[:label, 't', nil],
|
8
|
-
[:part, 'd', nil],
|
9
|
-
[:block, '{ { } }'],
|
10
|
-
[:third],
|
11
|
-
[:copy, "\nhi\n"]
|
12
|
-
]
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'throws an error' do
|
16
|
-
expect {
|
17
|
-
scan('% %% %% ')
|
18
|
-
}.to raise_error(Ace::SyntaxError)
|
19
|
-
end
|
20
|
-
|
21
|
-
def scan(source)
|
22
|
-
Timeout.timeout(5) do
|
23
|
-
Ace::Scanner.scan(source)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
1
|
+
describe Ace::Scanner do
|
2
|
+
|
3
|
+
it 'properly scans' do
|
4
|
+
expect(scan("%test \"a\" hi\n%%\nt: d { { } }\n%%\nhi\n")).to eq [
|
5
|
+
[:directive, 'test', %w(a hi)],
|
6
|
+
[:second],
|
7
|
+
[:label, 't', nil],
|
8
|
+
[:part, 'd', nil],
|
9
|
+
[:block, '{ { } }'],
|
10
|
+
[:third],
|
11
|
+
[:copy, "\nhi\n"]
|
12
|
+
]
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'throws an error' do
|
16
|
+
expect {
|
17
|
+
scan('% %% %% ')
|
18
|
+
}.to raise_error(Ace::SyntaxError)
|
19
|
+
end
|
20
|
+
|
21
|
+
def scan(source)
|
22
|
+
Timeout.timeout(5) do
|
23
|
+
Ace::Scanner.scan(source)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -1,131 +1,131 @@
|
|
1
|
-
describe Generation::Constructor do
|
2
|
-
let(:grammar) { double('grammar') }
|
3
|
-
let(:terminal) { token(:TERMINAL) }
|
4
|
-
let(:epsilon) { token(:epsilon) }
|
5
|
-
|
6
|
-
subject { described_class.new(grammar) }
|
7
|
-
|
8
|
-
context '#nullable?' do
|
9
|
-
context 'when given an epsilon token' do
|
10
|
-
it 'returns true' do
|
11
|
-
expect(subject.nullable?(epsilon)).to be true
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'when given a terminal' do
|
16
|
-
it 'returns false' do
|
17
|
-
expect(subject.nullable?(terminal)).to be false
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when given an array' do
|
22
|
-
context 'with one of the elements not nullable' do
|
23
|
-
it 'returns false' do
|
24
|
-
expect(subject.nullable?([terminal, epsilon])).to be false
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'with all of the elements nullable' do
|
29
|
-
it 'returns true' do
|
30
|
-
expect(subject.nullable?([epsilon, epsilon])).to be true
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'when given a nonterminal' do
|
36
|
-
let(:grammar) { with_recognizer }
|
37
|
-
|
38
|
-
context 'with no nullable productions' do
|
39
|
-
let(:nonterminal) { Grammar::Token::Nonterminal.new(:l) }
|
40
|
-
|
41
|
-
it 'returns false' do
|
42
|
-
expect(subject.nullable?(nonterminal)).to be false
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'with a nullable production' do
|
47
|
-
let(:nonterminal) { Grammar::Token::Nonterminal.new(:e) }
|
48
|
-
|
49
|
-
it 'returns true' do
|
50
|
-
expect(subject.nullable?(nonterminal)).to be true
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context 'when given a bad argument' do
|
56
|
-
it 'raises an error' do
|
57
|
-
expect { subject.nullable?(nil) }.to raise_error(ArgumentError)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context '#first' do
|
63
|
-
context 'when given an epsilon token' do
|
64
|
-
it 'generates an empty set' do
|
65
|
-
expect(subject.first(epsilon)).to eq Set.new
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context 'when given a terminal' do
|
70
|
-
it 'generates a set' do
|
71
|
-
expect(subject.first(terminal)).to eq [terminal].to_set
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context 'when given an array' do
|
76
|
-
let(:terminal2) { token(:terminal, :TERMINAL2) }
|
77
|
-
|
78
|
-
it 'generates a set' do
|
79
|
-
expect(subject.first([epsilon, terminal]))
|
80
|
-
.to eq [terminal].to_set
|
81
|
-
expect(subject.first([terminal, terminal2]))
|
82
|
-
.to eq [terminal].to_set
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context 'when given a nonterminal' do
|
87
|
-
let(:grammar) { with_recognizer }
|
88
|
-
let(:nonterminal) { token(:nonterminal, :e) }
|
89
|
-
|
90
|
-
it 'generates a set' do
|
91
|
-
expect(subject.first(nonterminal))
|
92
|
-
.to eq [token(:terminal, :IDENT), token(:terminal, :STAR, '*')].to_set
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context 'when given a bad argument' do
|
97
|
-
it 'raises an error' do
|
98
|
-
expect { subject.first(nil) }.to raise_error(ArgumentError)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context '#follow' do
|
104
|
-
context 'when given a bad argument' do
|
105
|
-
it 'raises an error' do
|
106
|
-
expect { subject.follow(nil) }.to raise_error(ArgumentError)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
context 'when given a nonterminal' do
|
111
|
-
let(:grammar) { with_recognizer }
|
112
|
-
let(:nonterminal) { token(:nonterminal, :l) }
|
113
|
-
|
114
|
-
before do
|
115
|
-
subject.productions.merge grammar.productions.values.flatten
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'generates a set' do
|
119
|
-
expect(subject.follow(nonterminal)).to eq [
|
120
|
-
token(:terminal, :EQUALS, '='),
|
121
|
-
token(:terminal, :'$end')
|
122
|
-
].to_set
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def token(type, name = nil, value = nil, ttype = nil, id = nil)
|
128
|
-
type = Grammar::Token.const_get(type.to_s.capitalize)
|
129
|
-
type.new(name, ttype, id, value)
|
130
|
-
end
|
131
|
-
end
|
1
|
+
describe Generation::Constructor do
|
2
|
+
let(:grammar) { double('grammar') }
|
3
|
+
let(:terminal) { token(:TERMINAL) }
|
4
|
+
let(:epsilon) { token(:epsilon) }
|
5
|
+
|
6
|
+
subject { described_class.new(grammar) }
|
7
|
+
|
8
|
+
context '#nullable?' do
|
9
|
+
context 'when given an epsilon token' do
|
10
|
+
it 'returns true' do
|
11
|
+
expect(subject.nullable?(epsilon)).to be true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when given a terminal' do
|
16
|
+
it 'returns false' do
|
17
|
+
expect(subject.nullable?(terminal)).to be false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when given an array' do
|
22
|
+
context 'with one of the elements not nullable' do
|
23
|
+
it 'returns false' do
|
24
|
+
expect(subject.nullable?([terminal, epsilon])).to be false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with all of the elements nullable' do
|
29
|
+
it 'returns true' do
|
30
|
+
expect(subject.nullable?([epsilon, epsilon])).to be true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when given a nonterminal' do
|
36
|
+
let(:grammar) { with_recognizer }
|
37
|
+
|
38
|
+
context 'with no nullable productions' do
|
39
|
+
let(:nonterminal) { Grammar::Token::Nonterminal.new(:l) }
|
40
|
+
|
41
|
+
it 'returns false' do
|
42
|
+
expect(subject.nullable?(nonterminal)).to be false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'with a nullable production' do
|
47
|
+
let(:nonterminal) { Grammar::Token::Nonterminal.new(:e) }
|
48
|
+
|
49
|
+
it 'returns true' do
|
50
|
+
expect(subject.nullable?(nonterminal)).to be true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when given a bad argument' do
|
56
|
+
it 'raises an error' do
|
57
|
+
expect { subject.nullable?(nil) }.to raise_error(ArgumentError)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '#first' do
|
63
|
+
context 'when given an epsilon token' do
|
64
|
+
it 'generates an empty set' do
|
65
|
+
expect(subject.first(epsilon)).to eq Set.new
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when given a terminal' do
|
70
|
+
it 'generates a set' do
|
71
|
+
expect(subject.first(terminal)).to eq [terminal].to_set
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when given an array' do
|
76
|
+
let(:terminal2) { token(:terminal, :TERMINAL2) }
|
77
|
+
|
78
|
+
it 'generates a set' do
|
79
|
+
expect(subject.first([epsilon, terminal]))
|
80
|
+
.to eq [terminal].to_set
|
81
|
+
expect(subject.first([terminal, terminal2]))
|
82
|
+
.to eq [terminal].to_set
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when given a nonterminal' do
|
87
|
+
let(:grammar) { with_recognizer }
|
88
|
+
let(:nonterminal) { token(:nonterminal, :e) }
|
89
|
+
|
90
|
+
it 'generates a set' do
|
91
|
+
expect(subject.first(nonterminal))
|
92
|
+
.to eq [token(:terminal, :IDENT), token(:terminal, :STAR, '*')].to_set
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when given a bad argument' do
|
97
|
+
it 'raises an error' do
|
98
|
+
expect { subject.first(nil) }.to raise_error(ArgumentError)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context '#follow' do
|
104
|
+
context 'when given a bad argument' do
|
105
|
+
it 'raises an error' do
|
106
|
+
expect { subject.follow(nil) }.to raise_error(ArgumentError)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'when given a nonterminal' do
|
111
|
+
let(:grammar) { with_recognizer }
|
112
|
+
let(:nonterminal) { token(:nonterminal, :l) }
|
113
|
+
|
114
|
+
before do
|
115
|
+
subject.productions.merge grammar.productions.values.flatten
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'generates a set' do
|
119
|
+
expect(subject.follow(nonterminal)).to eq [
|
120
|
+
token(:terminal, :EQUALS, '='),
|
121
|
+
token(:terminal, :'$end')
|
122
|
+
].to_set
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def token(type, name = nil, value = nil, ttype = nil, id = nil)
|
128
|
+
type = Grammar::Token.const_get(type.to_s.capitalize)
|
129
|
+
type.new(name, ttype, id, value)
|
130
|
+
end
|
131
|
+
end
|
data/spec/fixtures/simple.ace
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
%language <ruby>
|
2
|
-
|
3
|
-
%terminal IDENT
|
4
|
-
%terminal STAR "*"
|
5
|
-
%terminal EQUALS "="
|
6
|
-
|
7
|
-
%%
|
8
|
-
|
9
|
-
e: l EQUALS r
|
10
|
-
| r
|
11
|
-
| nothing
|
12
|
-
|
13
|
-
l: IDENT
|
14
|
-
| STAR r
|
15
|
-
|
16
|
-
r: l
|
17
|
-
|
18
|
-
%%
|
19
|
-
|
20
|
-
class SimpleParser
|
21
|
-
%{write}
|
22
|
-
end
|
1
|
+
%language <ruby>
|
2
|
+
|
3
|
+
%terminal IDENT
|
4
|
+
%terminal STAR "*"
|
5
|
+
%terminal EQUALS "="
|
6
|
+
|
7
|
+
%%
|
8
|
+
|
9
|
+
e: l EQUALS r
|
10
|
+
| r
|
11
|
+
| nothing
|
12
|
+
|
13
|
+
l: IDENT
|
14
|
+
| STAR r
|
15
|
+
|
16
|
+
r: l
|
17
|
+
|
18
|
+
%%
|
19
|
+
|
20
|
+
class SimpleParser
|
21
|
+
%{write}
|
22
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,39 +1,39 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "bundler/setup"
|
3
|
-
Bundler.setup
|
4
|
-
|
5
|
-
require "timeout"
|
6
|
-
require "benchmark"
|
7
|
-
require "simplecov"
|
8
|
-
require "coveralls"
|
9
|
-
|
10
|
-
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
11
|
-
SimpleCov.start do
|
12
|
-
add_filter 'spec'
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
require "rspec/its"
|
17
|
-
require "antelope"
|
18
|
-
|
19
|
-
include Antelope
|
20
|
-
|
21
|
-
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
22
|
-
|
23
|
-
# This file was generated by the `rspec --init` command. Conventionally, all
|
24
|
-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
25
|
-
# Require this file using `require "spec_helper"` to ensure that it is only
|
26
|
-
# loaded once.
|
27
|
-
#
|
28
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
29
|
-
RSpec.configure do |config|
|
30
|
-
config.run_all_when_everything_filtered = true
|
31
|
-
|
32
|
-
# Run specs in random order to surface order dependencies. If you find an
|
33
|
-
# order dependency and want to debug it, you can fix the order by providing
|
34
|
-
# the seed, which is printed after each run.
|
35
|
-
# --seed 1234
|
36
|
-
config.order = 'random'
|
37
|
-
config.include BenchmarkHelper
|
38
|
-
config.include GrammarHelper
|
39
|
-
end
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
Bundler.setup
|
4
|
+
|
5
|
+
require "timeout"
|
6
|
+
require "benchmark"
|
7
|
+
require "simplecov"
|
8
|
+
require "coveralls"
|
9
|
+
|
10
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter 'spec'
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
require "rspec/its"
|
17
|
+
require "antelope"
|
18
|
+
|
19
|
+
include Antelope
|
20
|
+
|
21
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
22
|
+
|
23
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
24
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
25
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
26
|
+
# loaded once.
|
27
|
+
#
|
28
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
29
|
+
RSpec.configure do |config|
|
30
|
+
config.run_all_when_everything_filtered = true
|
31
|
+
|
32
|
+
# Run specs in random order to surface order dependencies. If you find an
|
33
|
+
# order dependency and want to debug it, you can fix the order by providing
|
34
|
+
# the seed, which is printed after each run.
|
35
|
+
# --seed 1234
|
36
|
+
config.order = 'random'
|
37
|
+
config.include BenchmarkHelper
|
38
|
+
config.include GrammarHelper
|
39
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
module BenchmarkHelper
|
2
|
-
def benchmark
|
3
|
-
Benchmark.realtime(&Proc.new)
|
4
|
-
end
|
5
|
-
end
|
1
|
+
module BenchmarkHelper
|
2
|
+
def benchmark
|
3
|
+
Benchmark.realtime(&Proc.new)
|
4
|
+
end
|
5
|
+
end
|