rley 0.1.08 → 0.1.09

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +8 -8
  2. data/.rubocop.yml +81 -74
  3. data/CHANGELOG.md +4 -0
  4. data/Rakefile +4 -6
  5. data/examples/grammars/grammar_L0.rb +31 -31
  6. data/examples/grammars/grammar_abc.rb +26 -26
  7. data/lib/rley/constants.rb +1 -1
  8. data/lib/rley/formatter/base_formatter.rb +1 -3
  9. data/lib/rley/formatter/debug.rb +1 -3
  10. data/lib/rley/formatter/json.rb +2 -5
  11. data/lib/rley/parse_tree_visitor.rb +0 -2
  12. data/lib/rley/parser/dotted_item.rb +3 -1
  13. data/lib/rley/parser/earley_parser.rb +4 -4
  14. data/lib/rley/parser/parsing.rb +10 -9
  15. data/lib/rley/parser/state_set.rb +1 -3
  16. data/lib/rley/ptree/non_terminal_node.rb +1 -1
  17. data/lib/rley/ptree/parse_tree.rb +32 -34
  18. data/lib/rley/ptree/parse_tree_node.rb +1 -3
  19. data/lib/rley/ptree/terminal_node.rb +1 -2
  20. data/lib/rley/ptree/token_range.rb +14 -14
  21. data/spec/rley/formatter/debug_spec.rb +65 -68
  22. data/spec/rley/formatter/json_spec.rb +69 -72
  23. data/spec/rley/parse_tree_visitor_spec.rb +5 -7
  24. data/spec/rley/parser/chart_spec.rb +0 -4
  25. data/spec/rley/parser/dotted_item_spec.rb +0 -3
  26. data/spec/rley/parser/earley_parser_spec.rb +0 -1
  27. data/spec/rley/parser/parse_state_spec.rb +0 -5
  28. data/spec/rley/parser/parsing_spec.rb +0 -3
  29. data/spec/rley/parser/state_set_spec.rb +0 -4
  30. data/spec/rley/parser/token_spec.rb +0 -4
  31. data/spec/rley/ptree/non_terminal_node_spec.rb +0 -1
  32. data/spec/rley/ptree/parse_tree_node_spec.rb +4 -4
  33. data/spec/rley/ptree/parse_tree_spec.rb +2 -3
  34. data/spec/rley/ptree/token_range_spec.rb +16 -17
  35. data/spec/rley/support/grammar_abc_helper.rb +0 -2
  36. data/spec/rley/syntax/grammar_builder_spec.rb +1 -4
  37. data/spec/rley/syntax/grammar_spec.rb +0 -9
  38. data/spec/rley/syntax/grm_symbol_spec.rb +0 -1
  39. data/spec/rley/syntax/literal_spec.rb +0 -1
  40. data/spec/rley/syntax/non_terminal_spec.rb +0 -1
  41. data/spec/rley/syntax/production_spec.rb +0 -2
  42. data/spec/rley/syntax/symbol_seq_spec.rb +0 -1
  43. data/spec/rley/syntax/terminal_spec.rb +0 -1
  44. data/spec/rley/syntax/verbatim_symbol_spec.rb +0 -1
  45. metadata +2 -2
@@ -57,7 +57,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
57
57
 
58
58
 
59
59
  context 'Standard creation & initialization:' do
60
-
61
60
  it 'should be initialized with a parse tree argument' do
62
61
  expect { ParseTreeVisitor.new(grm_abc_ptree1) }.not_to raise_error
63
62
  end
@@ -95,7 +94,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
95
94
  subject.unsubscribe(listener1)
96
95
  expect(subject.subscribers).to be_empty
97
96
  end
98
-
99
97
  end # context
100
98
 
101
99
 
@@ -136,7 +134,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
136
134
  it 'should react to the visit_children message' do
137
135
  # Notify subscribers when start the visit of children nodes
138
136
  children = nterm_node.children
139
- expect(listener1).to receive(:before_children).with(nterm_node, children)
137
+ args = [nterm_node, children]
138
+ expect(listener1).to receive(:before_children).with(*args)
140
139
  expect(listener1).to receive(:before_terminal).with(children[0])
141
140
  expect(listener1).to receive(:after_terminal).with(children[0])
142
141
  expect(listener1).to receive(:after_children).with(nterm_node, children)
@@ -185,7 +184,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
185
184
  [:before_ptree, [grm_abc_ptree1]],
186
185
  [:before_non_terminal, [root]],
187
186
  [:before_children, [root, children]],
188
- [:before_non_terminal , [big_a_1]],
187
+ [:before_non_terminal, [big_a_1]],
189
188
  [:before_children, [big_a_1, big_a_1_children]],
190
189
  [:before_terminal, [big_a_1_children[0]]],
191
190
  [:after_terminal, [big_a_1_children[0]]],
@@ -204,8 +203,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
204
203
  [:before_terminal, [big_a_1_children[2]]],
205
204
  [:after_terminal, [big_a_1_children[2]]],
206
205
  [:after_children, [big_a_1, big_a_1_children]],
207
- [:after_children , [root, children]],
208
- [:after_ptree , [grm_abc_ptree1]]
206
+ [:after_children, [root, children]],
207
+ [:after_ptree, [grm_abc_ptree1]]
209
208
  ]
210
209
  expectations.each do |(msg, args)|
211
210
  allow(listener1).to receive(msg).with(*args).ordered
@@ -214,7 +213,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
214
213
  # Here we go...
215
214
  subject.start
216
215
  end
217
-
218
216
  end # context
219
217
  end # describe
220
218
  end # module
@@ -7,12 +7,10 @@ require_relative '../../../lib/rley/parser/chart'
7
7
  module Rley # Open this namespace to avoid module qualifier prefixes
8
8
  module Parser # Open this namespace to avoid module qualifier prefixes
9
9
  describe Chart do
10
-
11
10
  let(:count_token) { 20 }
12
11
  let(:dotted_rule) { double('fake-dotted-item') }
13
12
 
14
13
  context 'Initialization:' do
15
-
16
14
  # Default instantiation rule
17
15
  subject { Chart.new(dotted_rule, count_token) }
18
16
 
@@ -35,9 +33,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
35
33
  it 'should the start dotted rule' do
36
34
  expect(subject.start_dotted_rule).to eq(dotted_rule)
37
35
  end
38
-
39
36
  end # context
40
-
41
37
  end # describe
42
38
  end # module
43
39
  end # module
@@ -10,7 +10,6 @@ require_relative '../../../lib/rley/parser/dotted_item'
10
10
  module Rley # Open this namespace to avoid module qualifier prefixes
11
11
  module Parser # Open this namespace to avoid module qualifier prefixes
12
12
  describe DottedItem do
13
-
14
13
  # Factory method. Builds a production with given left-hand side (LHS)
15
14
  # and given RHS (right-hand side)
16
15
  def build_prod(theLHS, *theRHSSymbols)
@@ -65,7 +64,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
65
64
  instance4 = DottedItem.new(build_prod(nt_sentence), 0)
66
65
  expect(instance4.position).to eq(-2)
67
66
  end
68
-
69
67
  end # context
70
68
 
71
69
  context 'Provided service:' do
@@ -128,7 +126,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
128
126
  expect(subject.to_s).to eq(expectation)
129
127
  end
130
128
  end
131
-
132
129
  end # describe
133
130
  end # module
134
131
  end # module
@@ -587,7 +587,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
587
587
  compare_state_texts(parse_result.chart[4], expected)
588
588
  end
589
589
  end # context
590
-
591
590
  end # describe
592
591
  end # module
593
592
  end # module
@@ -11,7 +11,6 @@ require_relative '../../../lib/rley/parser/parse_state'
11
11
  module Rley # Open this namespace to avoid module qualifier prefixes
12
12
  module Parser # Open this namespace to avoid module qualifier prefixes
13
13
  describe ParseState do
14
-
15
14
  let(:t_a) { Syntax::Terminal.new('A') }
16
15
  let(:t_b) { Syntax::Terminal.new('B') }
17
16
  let(:t_c) { Syntax::Terminal.new('C') }
@@ -37,7 +36,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
37
36
  subject { ParseState.new(dotted_rule, origin_val) }
38
37
 
39
38
  context 'Initialization:' do
40
-
41
39
  it 'should be created with a dotted item and a origin position' do
42
40
  args = [dotted_rule, origin_val]
43
41
  expect { ParseState.new(*args) }.not_to raise_error
@@ -56,8 +54,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
56
54
  it 'should know the origin value' do
57
55
  expect(subject.origin).to eq(origin_val)
58
56
  end
59
-
60
-
61
57
  end # context
62
58
 
63
59
  context 'Provided services:' do
@@ -107,7 +103,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
107
103
  expect(subject.to_s).to eq(expected)
108
104
  end
109
105
  end # context
110
-
111
106
  end # describe
112
107
  end # module
113
108
  end # module
@@ -13,7 +13,6 @@ require_relative '../../../lib/rley/parser/parsing'
13
13
  module Rley # Open this namespace to avoid module qualifier prefixes
14
14
  module Parser # Open this namespace to avoid module qualifier prefixes
15
15
  describe Parsing do
16
-
17
16
  # Grammar 1: A very simple language
18
17
  # S ::= A.
19
18
  # A ::= "a" A "c".
@@ -47,7 +46,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
47
46
  subject { Parsing.new(start_dotted_rule, grm1_tokens) }
48
47
 
49
48
  context 'Initialization:' do
50
-
51
49
  it 'should be created with list of tokens and start dotted rule' do
52
50
  start_rule = start_dotted_rule
53
51
  tokens = grm1_tokens
@@ -61,7 +59,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
61
59
  it 'should know its chart object' do
62
60
  expect(subject.chart).to be_kind_of(Chart)
63
61
  end
64
-
65
62
  end # context
66
63
 
67
64
  context 'Parsing:' do
@@ -14,14 +14,12 @@ module Rley # Open this namespace to avoid module qualifier prefixes
14
14
  let(:state2) { ParseState.new(dotted_rule2, 5) }
15
15
 
16
16
  context 'Initialization:' do
17
-
18
17
  it 'should be created without argument' do
19
18
  expect { StateSet.new }.not_to raise_error
20
19
  end
21
20
  end # context
22
21
 
23
22
  context 'Provided services:' do
24
-
25
23
  it 'should push a state' do
26
24
  expect(subject.states).to be_empty
27
25
  expect { subject.push_state(state1) }.not_to raise_error
@@ -80,9 +78,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
80
78
  err = StandardError
81
79
  expect { subject.predecessor_state(state1) }.to raise_error(err)
82
80
  end
83
-
84
81
  end # context
85
-
86
82
  end # describe
87
83
  end # module
88
84
  end # module
@@ -8,13 +8,10 @@ require_relative '../../../lib/rley/parser/token'
8
8
  module Rley # Open this namespace to avoid module qualifier prefixes
9
9
  module Parser # Open this namespace to avoid module qualifier prefixes
10
10
  describe Token do
11
-
12
11
  let(:lexeme) { '"some text"' }
13
12
  let(:sample_terminal) { Syntax::Terminal.new('if') }
14
13
 
15
14
  context 'Initialization:' do
16
-
17
-
18
15
  # Default instantiation rule
19
16
  subject { Token.new(lexeme, sample_terminal) }
20
17
 
@@ -30,7 +27,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
30
27
  expect(subject.terminal).to eq(sample_terminal)
31
28
  end
32
29
  end # context
33
-
34
30
  end # describe
35
31
  end # module
36
32
  end # module
@@ -28,7 +28,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
28
28
  expect(subject.children).to eq([child1, child2, child3])
29
29
  end
30
30
  end # context
31
-
32
31
  end # describe
33
32
  end # module
34
33
  end # module
@@ -7,24 +7,24 @@ module Rley # Open this namespace to avoid module qualifier prefixes
7
7
  module PTree # Open this namespace to avoid module qualifier prefixes
8
8
  describe ParseTreeNode do
9
9
  let(:sample_symbol) { double('fake-symbol') }
10
- let(:sample_range) { {low: 0, high: 5} }
10
+ let(:sample_range) { { low: 0, high: 5 } }
11
11
 
12
12
  subject { ParseTreeNode.new(sample_symbol, sample_range) }
13
13
 
14
14
  context 'Initialization:' do
15
15
  it 'should be created with a symbol and a range' do
16
- expect { ParseTreeNode.new(sample_symbol, sample_range) }.not_to raise_error
16
+ args = [ sample_symbol, sample_range ]
17
+ expect { ParseTreeNode.new(*args) }.not_to raise_error
17
18
  end
18
19
 
19
20
  it 'should know its symbol' do
20
21
  expect(subject.symbol).to eq(sample_symbol)
21
22
  end
22
23
 
23
- it "should know its range" do
24
+ it 'should know its range' do
24
25
  expect(subject.range).to eq(sample_range)
25
26
  end
26
27
  end # context
27
-
28
28
  end # describe
29
29
  end # module
30
30
  end # module
@@ -16,7 +16,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
16
16
  end
17
17
 
18
18
  let(:sample_prod) { sample_grammar.rules[0] }
19
- let(:sample_range) { {low:0, high:5} }
19
+ let(:sample_range) { { low: 0, high: 5 } }
20
20
  subject { ParseTree.new(sample_prod, sample_range) }
21
21
 
22
22
  context 'Initialization:' do
@@ -86,9 +86,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
86
86
  expect(path[4].range.high).to eq(4)
87
87
  end
88
88
  end
89
-
90
89
  end # describe
91
90
  end # module
92
91
  end # module
93
92
 
94
- # End of file
93
+ # End of file
@@ -6,8 +6,7 @@ require_relative '../../../lib/rley/ptree/token_range'
6
6
  module Rley # Open this namespace to avoid module qualifier prefixes
7
7
  module PTree # Open this namespace to avoid module qualifier prefixes
8
8
  describe TokenRange do
9
-
10
- let(:sample_range) { {low: 0, high: 5} }
9
+ let(:sample_range) { { low: 0, high: 5 } }
11
10
 
12
11
  # Default instantiation rule
13
12
  subject { TokenRange.new(sample_range) }
@@ -18,26 +17,26 @@ module Rley # Open this namespace to avoid module qualifier prefixes
18
17
  expect { TokenRange.new({}) }.not_to raise_error
19
18
 
20
19
  # Low bound provided
21
- expect { TokenRange.new({low: 0}) }.not_to raise_error
20
+ expect { TokenRange.new(low: 0) }.not_to raise_error
22
21
 
23
22
  # High bound provided
24
- expect { TokenRange.new({high: 5}) }.not_to raise_error
23
+ expect { TokenRange.new(high: 5) }.not_to raise_error
25
24
 
26
25
  # Both bounds provided
27
- expect { TokenRange.new({low: 0, high: 5}) }.not_to raise_error
26
+ expect { TokenRange.new(low: 0, high: 5) }.not_to raise_error
28
27
  end
29
28
 
30
29
  it 'could be created with another TokenRange' do
31
30
  # Low bound provided
32
- instance = TokenRange.new({low: 0})
31
+ instance = TokenRange.new(low: 0)
33
32
  expect { TokenRange.new(instance) }.not_to raise_error
34
33
 
35
34
  # High bound provided
36
- instance = TokenRange.new({high: 5})
35
+ instance = TokenRange.new(high: 5)
37
36
  expect { TokenRange.new(instance) }.not_to raise_error
38
37
 
39
38
  # Both bounds provided
40
- instance = TokenRange.new({low: 0, high: 5})
39
+ instance = TokenRange.new(low: 0, high: 5)
41
40
  expect { TokenRange.new(instance) }.not_to raise_error
42
41
  end
43
42
 
@@ -52,8 +51,9 @@ module Rley # Open this namespace to avoid module qualifier prefixes
52
51
 
53
52
  context 'Provided services:' do
54
53
  it 'should compare to another range' do
55
- expect(subject == subject).to eq(true)
56
- equal = TokenRange.new({low: 0, high: 5})
54
+ me = subject
55
+ expect(subject == me).to eq(true)
56
+ equal = TokenRange.new(low: 0, high: 5)
57
57
  expect(subject == equal).to eq(true)
58
58
  end
59
59
 
@@ -62,11 +62,11 @@ module Rley # Open this namespace to avoid module qualifier prefixes
62
62
  expect(subject).to be_bounded
63
63
 
64
64
  # Case: only low bound is set
65
- instance = TokenRange.new({low: 0})
65
+ instance = TokenRange.new(low: 0)
66
66
  expect(instance).not_to be_bounded
67
67
 
68
68
  # Case: only upper bound is set
69
- instance = TokenRange.new({high: 5})
69
+ instance = TokenRange.new(high: 5)
70
70
  expect(instance).not_to be_bounded
71
71
 
72
72
  # No bound is set
@@ -75,7 +75,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
75
75
  end
76
76
 
77
77
  it 'should assign its open bounds' do
78
- some_range = {low: 1, high: 4}
78
+ some_range = { low: 1, high: 4 }
79
79
 
80
80
  ###########
81
81
  # Case of bounded token range...
@@ -87,7 +87,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
87
87
 
88
88
  ###########
89
89
  # Case: only low bound is set
90
- instance = TokenRange.new({low: 0})
90
+ instance = TokenRange.new(low: 0)
91
91
  instance.assign(some_range)
92
92
 
93
93
  # Expectation: high is assigned the new value
@@ -97,7 +97,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
97
97
 
98
98
  ###########
99
99
  # Case: only high bound is set
100
- instance = TokenRange.new({high: 5})
100
+ instance = TokenRange.new(high: 5)
101
101
  instance.assign(some_range)
102
102
 
103
103
  # Expectation: low is assigned the new value
@@ -116,9 +116,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
116
116
  expect(instance.high).to eq(4)
117
117
  end
118
118
  end
119
-
120
119
  end # describe
121
120
  end # module
122
121
  end # module
123
122
 
124
- # End of file
123
+ # End of file
@@ -3,7 +3,6 @@ require_relative '../../../lib/rley/syntax/grammar_builder'
3
3
 
4
4
 
5
5
  module GrammarABCHelper
6
-
7
6
  # Factory method. Creates a grammar builder for a simple grammar.
8
7
  # (based on example in N. Wirth "Compiler Construction" book, p. 6)
9
8
  def grammar_abc_builder()
@@ -16,4 +15,3 @@ module GrammarABCHelper
16
15
  return builder
17
16
  end
18
17
  end # module
19
-
@@ -6,7 +6,6 @@ require_relative '../../../lib/rley/syntax/grammar_builder'
6
6
  module Rley # Open this namespace to avoid module qualifier prefixes
7
7
  module Syntax # Open this namespace to avoid module qualifier prefixes
8
8
  describe GrammarBuilder do
9
-
10
9
  context 'Initialization:' do
11
10
  it 'should be created without argument' do
12
11
  expect { GrammarBuilder.new }.not_to raise_error
@@ -19,7 +18,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
19
18
  it 'should have no productions at start' do
20
19
  expect(subject.productions).to be_empty
21
20
  end
22
-
23
21
  end # context
24
22
 
25
23
  context 'Adding symbols:' do
@@ -156,8 +154,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
156
154
  expect { builder.grammar }.not_to raise_error
157
155
  expect(builder.productions.last).to be_empty
158
156
  end
159
- end
160
-
157
+ end # context
161
158
  end # describe
162
159
  end # module
163
160
  end # module
@@ -10,7 +10,6 @@ require_relative '../../../lib/rley/syntax/grammar'
10
10
  module Rley # Open this namespace to avoid module qualifier prefixes
11
11
  module Syntax # Open this namespace to avoid module qualifier prefixes
12
12
  describe Grammar do
13
-
14
13
  # Factory method. Builds a list of productions
15
14
  # having same lhs and the symbols sequence
16
15
  # in their rhs.
@@ -123,7 +122,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
123
122
  end
124
123
 
125
124
  context 'Initialization:' do
126
-
127
125
  it 'should be created with a list of productions' do
128
126
  expect { Grammar.new([prod_S, prod_A1, prod_A2]) }.not_to raise_error
129
127
  end
@@ -145,12 +143,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
145
143
  end
146
144
  end # context
147
145
 
148
- # let(:nt_S) { NonTerminal.new('S') }
149
- # let(:nt_A) { NonTerminal.new('A') }
150
- # let(:a_) { VerbatimSymbol.new('a') }
151
- # let(:b_) { VerbatimSymbol.new('b') }
152
- # let(:c_) { VerbatimSymbol.new('c') }
153
-
154
146
  context 'Provided services:' do
155
147
  it 'should retrieve its symbols from their name' do
156
148
  expect(subject.name2symbol['S']).to eq(nt_S)
@@ -187,7 +179,6 @@ module Rley # Open this namespace to avoid module qualifier prefixes
187
179
  end
188
180
 
189
181
  end # context
190
-
191
182
  end # describe
192
183
  end # module
193
184
  end # module