antelope 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -0
  3. data/examples/deterministic.output +53 -139
  4. data/examples/example.output +159 -347
  5. data/examples/simple.output +50 -124
  6. data/lib/antelope/ace/compiler.rb +10 -1
  7. data/lib/antelope/ace/errors.rb +8 -0
  8. data/lib/antelope/ace/grammar/generation.rb +6 -1
  9. data/lib/antelope/ace/grammar/loading.rb +2 -0
  10. data/lib/antelope/ace/grammar/precedences.rb +3 -1
  11. data/lib/antelope/ace/grammar/production.rb +2 -0
  12. data/lib/antelope/ace/grammar/productions.rb +2 -0
  13. data/lib/antelope/ace/grammar/terminals.rb +2 -0
  14. data/lib/antelope/ace/grammar.rb +2 -0
  15. data/lib/antelope/ace/precedence.rb +2 -0
  16. data/lib/antelope/ace/scanner/first.rb +2 -0
  17. data/lib/antelope/ace/scanner/second.rb +3 -1
  18. data/lib/antelope/ace/scanner/third.rb +2 -0
  19. data/lib/antelope/ace/scanner.rb +2 -0
  20. data/lib/antelope/ace/token/epsilon.rb +4 -2
  21. data/lib/antelope/ace/token/error.rb +2 -0
  22. data/lib/antelope/ace/token/nonterminal.rb +2 -0
  23. data/lib/antelope/ace/token/terminal.rb +2 -0
  24. data/lib/antelope/ace/token.rb +2 -0
  25. data/lib/antelope/ace.rb +2 -0
  26. data/lib/antelope/cli.rb +2 -0
  27. data/lib/antelope/errors.rb +2 -0
  28. data/lib/antelope/generation/constructor/first.rb +2 -0
  29. data/lib/antelope/generation/constructor/follow.rb +5 -0
  30. data/lib/antelope/generation/constructor/nullable.rb +2 -0
  31. data/lib/antelope/generation/constructor.rb +2 -0
  32. data/lib/antelope/generation/errors.rb +2 -0
  33. data/lib/antelope/generation/recognizer/rule.rb +9 -0
  34. data/lib/antelope/generation/recognizer/state.rb +2 -0
  35. data/lib/antelope/generation/recognizer.rb +7 -2
  36. data/lib/antelope/generation/tableizer.rb +3 -2
  37. data/lib/antelope/generation.rb +2 -0
  38. data/lib/antelope/generator/output.rb +2 -0
  39. data/lib/antelope/generator/ruby.rb +2 -0
  40. data/lib/antelope/generator/templates/output.erb +21 -25
  41. data/lib/antelope/generator/templates/ruby.erb +1 -1
  42. data/lib/antelope/generator.rb +2 -0
  43. data/lib/antelope/version.rb +3 -1
  44. data/lib/antelope.rb +2 -0
  45. data/spec/antelope/ace/compiler_spec.rb +11 -1
  46. data/spec/antelope/constructor_spec.rb +127 -0
  47. data/spec/fixtures/simple.ace +22 -0
  48. data/spec/spec_helper.rb +1 -0
  49. data/spec/support/grammar_helper.rb +15 -0
  50. metadata +9 -4
  51. data/spec/antelope/automaton_spec.rb +0 -29
@@ -1,4 +1,6 @@
1
+ # encoding: utf-8
2
+
1
3
  module Antelope
2
4
  # The current running version of antelope.
3
- VERSION = "0.1.1".freeze
5
+ VERSION = "0.1.2".freeze
4
6
  end
data/lib/antelope.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "antelope/errors"
2
4
  require "antelope/generation"
3
5
  require "antelope/generator"
@@ -5,7 +5,7 @@ describe Ace::Compiler do
5
5
  test
6
6
  %}
7
7
 
8
- %require "0.0.1"
8
+ %require "#{VERSION}"
9
9
  %type "ruby"
10
10
 
11
11
  %terminal NUMBER
@@ -47,4 +47,14 @@ describe Ace::Compiler do
47
47
  expect(subject.options[:terminals].map(&:first)).to eq [:NUMBER,
48
48
  :SEMICOLON, :ADD, :LPAREN, :RPAREN]
49
49
  end
50
+
51
+ context "with an unmatched version" do
52
+ let :file do "%require \"0.0.0\"\n%%\n%%\n" end
53
+
54
+ it "should raise an error" do
55
+ expect {
56
+ subject
57
+ }.to raise_error(Ace::IncompatibleVersionError)
58
+ end
59
+ end
50
60
  end
@@ -0,0 +1,127 @@
1
+ describe Generation::Constructor do
2
+ let(:grammar) { double("grammar") }
3
+ let(:terminal) { token(:TERMINAL) }
4
+ let(:epsilon) { token(nil, nil, :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) { Ace::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) { Ace::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(: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(:e, nil, :nonterminal) }
89
+
90
+ it "generates a set" do
91
+ expect(subject.first(nonterminal)).
92
+ to eq [token(:IDENT), token(: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(:l, nil, :nonterminal) }
113
+
114
+ it "generates a set" do
115
+ expect(subject.follow(nonterminal)).to eq [
116
+ token(:EQUALS, "="),
117
+ token(:"$")
118
+ ].to_set
119
+ end
120
+ end
121
+ end
122
+
123
+ def token(name = nil, value = nil, type = :terminal)
124
+ type = Ace::Token.const_get(type.to_s.capitalize)
125
+ type.new(name, value)
126
+ end
127
+ end
@@ -0,0 +1,22 @@
1
+ %type "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
@@ -35,4 +35,5 @@ RSpec.configure do |config|
35
35
  # --seed 1234
36
36
  config.order = 'random'
37
37
  config.include BenchmarkHelper
38
+ config.include GrammarHelper
38
39
  end
@@ -0,0 +1,15 @@
1
+ module GrammarHelper
2
+
3
+ def grammar_for(grammar_file = "simple")
4
+ source_path = Pathname.new("../../fixtures").expand_path(__FILE__)
5
+ Ace::Grammar.from_file(source_path.children.select(&:file?)
6
+ .find { |x| x.to_s =~ /#{Regexp.escape(grammar_file)}\..*\z/ }.to_s)
7
+ end
8
+
9
+ def with_recognizer(grammar = simple_grammar)
10
+ recognizer = Generation::Recognizer.new(grammar).call
11
+ grammar
12
+ end
13
+
14
+ alias_method :simple_grammar, :grammar_for
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antelope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Rodi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-18 00:00:00.000000000 Z
11
+ date: 2014-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -104,6 +104,7 @@ extra_rdoc_files: []
104
104
  files:
105
105
  - ".gitignore"
106
106
  - ".rspec"
107
+ - ".travis.yml"
107
108
  - ".yardopts"
108
109
  - Gemfile
109
110
  - LICENSE.txt
@@ -158,9 +159,11 @@ files:
158
159
  - lib/antelope/version.rb
159
160
  - spec/antelope/ace/compiler_spec.rb
160
161
  - spec/antelope/ace/scanner_spec.rb
161
- - spec/antelope/automaton_spec.rb
162
+ - spec/antelope/constructor_spec.rb
163
+ - spec/fixtures/simple.ace
162
164
  - spec/spec_helper.rb
163
165
  - spec/support/benchmark_helper.rb
166
+ - spec/support/grammar_helper.rb
164
167
  homepage: https://github.com/medcat/antelope
165
168
  licenses:
166
169
  - MIT
@@ -188,7 +191,9 @@ summary: A compiler compiler, written in ruby.
188
191
  test_files:
189
192
  - spec/antelope/ace/compiler_spec.rb
190
193
  - spec/antelope/ace/scanner_spec.rb
191
- - spec/antelope/automaton_spec.rb
194
+ - spec/antelope/constructor_spec.rb
195
+ - spec/fixtures/simple.ace
192
196
  - spec/spec_helper.rb
193
197
  - spec/support/benchmark_helper.rb
198
+ - spec/support/grammar_helper.rb
194
199
  has_rdoc:
@@ -1,29 +0,0 @@
1
- describe Automaton do
2
- context "with a simple table" do
3
- let(:states) { [ :s1, :s2 ] }
4
- let(:alphabet) { [0, 1] }
5
- let(:accept) { [ :s1 ] }
6
- let(:transitions) { {
7
- :s1 => { 0 => :s2, 1 => :s1 },
8
- :s2 => { 0 => :s1, 1 => :s2 }
9
- } }
10
-
11
- subject do
12
- Automaton.new(states, alphabet, :s1, accept, transitions)
13
- end
14
-
15
- it "basically runs" do
16
- expect(subject.run([1, 1])).to be_truthy
17
- end
18
-
19
- it "yields for transitions" do
20
- expect { |b| subject.run([1], &b) }.to yield_control
21
- end
22
-
23
- it "runs fast" do
24
- expect(benchmark do
25
- subject.run([1, 1])
26
- end).to be < 5e-5
27
- end
28
- end
29
- end