rley 0.8.14 → 0.8.15

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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -2
  3. data/CHANGELOG.md +3 -0
  4. data/examples/general/calc_iter1/spec/calculator_spec.rb +9 -9
  5. data/examples/general/calc_iter2/spec/calculator_spec.rb +39 -39
  6. data/examples/general/recursive_right.rb +2 -2
  7. data/lib/rley/constants.rb +1 -1
  8. data/lib/rley/gfg/grm_flow_graph.rb +0 -1
  9. data/lib/rley/parser/parse_entry_set.rb +0 -1
  10. data/lib/rley/parser/parse_walker_factory.rb +0 -1
  11. data/lib/rley/rgn/grammar_builder.rb +0 -2
  12. data/lib/rley/rgn/tokenizer.rb +1 -1
  13. data/lib/rley/syntax/base_grammar_builder.rb +0 -1
  14. data/lib/rley/syntax/grammar.rb +0 -1
  15. data/spec/rley/base/dotted_item_spec.rb +46 -46
  16. data/spec/rley/base/grm_items_builder_spec.rb +1 -1
  17. data/spec/rley/engine_spec.rb +50 -50
  18. data/spec/rley/formatter/asciitree_spec.rb +8 -8
  19. data/spec/rley/formatter/bracket_notation_spec.rb +10 -10
  20. data/spec/rley/formatter/debug_spec.rb +10 -10
  21. data/spec/rley/formatter/json_spec.rb +6 -7
  22. data/spec/rley/gfg/call_edge_spec.rb +6 -6
  23. data/spec/rley/gfg/edge_spec.rb +8 -7
  24. data/spec/rley/gfg/end_vertex_spec.rb +8 -7
  25. data/spec/rley/gfg/epsilon_edge_spec.rb +5 -4
  26. data/spec/rley/gfg/grm_flow_graph_spec.rb +33 -34
  27. data/spec/rley/gfg/item_vertex_spec.rb +34 -36
  28. data/spec/rley/gfg/non_terminal_vertex_spec.rb +12 -12
  29. data/spec/rley/gfg/return_edge_spec.rb +6 -6
  30. data/spec/rley/gfg/scan_edge_spec.rb +7 -6
  31. data/spec/rley/gfg/shortcut_edge_spec.rb +15 -15
  32. data/spec/rley/gfg/start_vertex_spec.rb +8 -8
  33. data/spec/rley/gfg/vertex_spec.rb +18 -18
  34. data/spec/rley/lexical/literal_spec.rb +5 -5
  35. data/spec/rley/lexical/token_range_spec.rb +55 -55
  36. data/spec/rley/lexical/token_spec.rb +17 -16
  37. data/spec/rley/parse_forest_visitor_spec.rb +30 -32
  38. data/spec/rley/parse_rep/ambiguous_parse_spec.rb +2 -2
  39. data/spec/rley/parse_rep/ast_builder_spec.rb +30 -30
  40. data/spec/rley/parse_rep/cst_builder_spec.rb +85 -85
  41. data/spec/rley/parse_rep/groucho_spec.rb +23 -23
  42. data/spec/rley/parse_rep/parse_forest_builder_spec.rb +42 -42
  43. data/spec/rley/parse_rep/parse_forest_factory_spec.rb +10 -12
  44. data/spec/rley/parse_rep/parse_tree_factory_spec.rb +10 -15
  45. data/spec/rley/parse_tree_visitor_spec.rb +43 -46
  46. data/spec/rley/parser/dangling_else_spec.rb +12 -12
  47. data/spec/rley/parser/error_reason_spec.rb +37 -37
  48. data/spec/rley/parser/gfg_chart_spec.rb +27 -29
  49. data/spec/rley/parser/gfg_earley_parser_spec.rb +55 -56
  50. data/spec/rley/parser/gfg_parsing_spec.rb +106 -103
  51. data/spec/rley/parser/parse_entry_set_spec.rb +63 -61
  52. data/spec/rley/parser/parse_entry_spec.rb +73 -71
  53. data/spec/rley/parser/parse_walker_factory_spec.rb +14 -15
  54. data/spec/rley/ptree/non_terminal_node_spec.rb +16 -16
  55. data/spec/rley/ptree/parse_tree_node_spec.rb +11 -11
  56. data/spec/rley/ptree/parse_tree_spec.rb +6 -8
  57. data/spec/rley/ptree/terminal_node_spec.rb +6 -6
  58. data/spec/rley/rgn/grammar_builder_spec.rb +69 -67
  59. data/spec/rley/rgn/parser_spec.rb +63 -63
  60. data/spec/rley/rgn/repetition_node_spec.rb +15 -15
  61. data/spec/rley/rgn/sequence_node_spec.rb +10 -10
  62. data/spec/rley/rgn/symbol_node_spec.rb +5 -6
  63. data/spec/rley/rgn/tokenizer_spec.rb +68 -67
  64. data/spec/rley/sppf/alternative_node_spec.rb +16 -16
  65. data/spec/rley/sppf/non_terminal_node_spec.rb +20 -20
  66. data/spec/rley/sppf/token_node_spec.rb +13 -13
  67. data/spec/rley/syntax/base_grammar_builder_spec.rb +76 -86
  68. data/spec/rley/syntax/grammar_spec.rb +40 -78
  69. data/spec/rley/syntax/grm_symbol_spec.rb +7 -7
  70. data/spec/rley/syntax/match_closest_spec.rb +8 -8
  71. data/spec/rley/syntax/non_terminal_spec.rb +25 -25
  72. data/spec/rley/syntax/production_spec.rb +33 -33
  73. data/spec/rley/syntax/symbol_seq_spec.rb +27 -27
  74. data/spec/rley/syntax/terminal_spec.rb +12 -11
  75. data/spec/support/base_tokenizer_spec.rb +9 -8
  76. metadata +2 -2
@@ -10,39 +10,40 @@ require_relative '../../../lib/rley/lexical/token'
10
10
  module Rley # Open this namespace to avoid module qualifier prefixes
11
11
  module Lexical # Open this namespace to avoid module qualifier prefixes
12
12
  describe Token do
13
+ # Default instantiation rule
14
+ subject(:a_token) { described_class.new(lexeme, a_terminal, a_pos) }
15
+
13
16
  let(:lexeme) { '"some text"' }
14
17
  let(:a_terminal) { Syntax::Terminal.new('if') }
15
18
  let(:a_pos) { Position.new(3, 4) }
16
- # Default instantiation rule
17
- subject { Token.new(lexeme, a_terminal, a_pos) }
18
19
 
19
20
  context 'Initialization:' do
20
- it 'could be created with a lexeme and a terminal ' do
21
- expect { Token.new(lexeme, a_terminal) }.not_to raise_error
21
+ it 'is created with a lexeme and a terminal' do
22
+ expect { described_class.new(lexeme, a_terminal) }.not_to raise_error
22
23
  end
23
24
 
24
- it 'could be created with a lexeme, a terminal and position' do
25
- expect { Token.new(lexeme, a_terminal, a_pos) }.not_to raise_error
25
+ it 'is created with a lexeme, a terminal and position' do
26
+ expect { described_class.new(lexeme, a_terminal, a_pos) }.not_to raise_error
26
27
  end
27
28
 
28
- it 'should know its lexeme' do
29
- expect(subject.lexeme).to eq(lexeme)
29
+ it 'knows its lexeme' do
30
+ expect(a_token.lexeme).to eq(lexeme)
30
31
  end
31
32
 
32
- it 'should know its terminal' do
33
- expect(subject.terminal).to eq(a_terminal)
33
+ it 'knows its terminal' do
34
+ expect(a_token.terminal).to eq(a_terminal)
34
35
  end
35
36
 
36
- it 'should know its position' do
37
+ it 'knows its position' do
37
38
  new_pos = Position.new(5, 7)
38
- subject.position = new_pos
39
- expect(subject.position).to eq(new_pos)
39
+ a_token.position = new_pos
40
+ expect(a_token.position).to eq(new_pos)
40
41
  end
41
42
  end # context
42
43
 
43
- context 'Initialization:' do
44
- it 'should accept a new position' do
45
- expect(subject.position).to eq(a_pos)
44
+ context 'Provided services:' do
45
+ it 'accepts a new position' do
46
+ expect(a_token.position).to eq(a_pos)
46
47
  end
47
48
  end # context
48
49
  end # describe
@@ -25,6 +25,9 @@ module Rley # Open this namespace to avoid module qualifier prefixes
25
25
  return Rley::SPPF::NonTerminalNode.new(a_vertex.non_terminal, aRange)
26
26
  end
27
27
 
28
+ # Default instantiation rule
29
+ subject(:a_visitor) { described_class.new(forest_root) }
30
+
28
31
  let(:grammar_sppf) do
29
32
  builder = grammar_sppf_builder
30
33
  builder.grammar
@@ -53,22 +56,17 @@ module Rley # Open this namespace to avoid module qualifier prefixes
53
56
  factory.create
54
57
  end
55
58
 
56
-
57
- # Default instantiation rule
58
- subject { ParseForestVisitor.new(forest_root) }
59
-
60
-
61
59
  context 'Standard creation & initialization:' do
62
- it 'should be initialized with a parse forest argument' do
63
- expect { ParseForestVisitor.new(forest_root) }.not_to raise_error
60
+ it 'is initialized with a parse forest argument' do
61
+ expect { described_class.new(forest_root) }.not_to raise_error
64
62
  end
65
63
 
66
- it 'should know the parse forest to visit' do
67
- expect(subject.pforest).to eq(forest_root)
64
+ it 'knows the parse forest to visit' do
65
+ expect(a_visitor.pforest).to eq(forest_root)
68
66
  end
69
67
 
70
- it "shouldn't have subscribers at start" do
71
- expect(subject.subscribers).to be_empty
68
+ it "doesn't have subscribers at start" do
69
+ expect(a_visitor.subscribers).to be_empty
72
70
  end
73
71
  end # context
74
72
 
@@ -77,24 +75,24 @@ module Rley # Open this namespace to avoid module qualifier prefixes
77
75
  let(:listener1) { double('fake-subscriber1') }
78
76
  let(:listener2) { double('fake-subscriber2') }
79
77
 
80
- it 'should allow subscriptions' do
81
- subject.subscribe(listener1)
82
- expect(subject.subscribers.size).to eq(1)
83
- expect(subject.subscribers).to eq([listener1])
78
+ it 'allows subscriptions' do
79
+ a_visitor.subscribe(listener1)
80
+ expect(a_visitor.subscribers.size).to eq(1)
81
+ expect(a_visitor.subscribers).to eq([listener1])
84
82
 
85
- subject.subscribe(listener2)
86
- expect(subject.subscribers.size).to eq(2)
87
- expect(subject.subscribers).to eq([listener1, listener2])
83
+ a_visitor.subscribe(listener2)
84
+ expect(a_visitor.subscribers.size).to eq(2)
85
+ expect(a_visitor.subscribers).to eq([listener1, listener2])
88
86
  end
89
87
 
90
- it 'should allow un-subcriptions' do
91
- subject.subscribe(listener1)
92
- subject.subscribe(listener2)
93
- subject.unsubscribe(listener2)
94
- expect(subject.subscribers.size).to eq(1)
95
- expect(subject.subscribers).to eq([listener1])
96
- subject.unsubscribe(listener1)
97
- expect(subject.subscribers).to be_empty
88
+ it 'allows un-subcriptions' do
89
+ a_visitor.subscribe(listener1)
90
+ a_visitor.subscribe(listener2)
91
+ a_visitor.unsubscribe(listener2)
92
+ expect(a_visitor.subscribers.size).to eq(1)
93
+ expect(a_visitor.subscribers).to eq([listener1])
94
+ a_visitor.unsubscribe(listener1)
95
+ expect(a_visitor.subscribers).to be_empty
98
96
  end
99
97
  end # context
100
98
 
@@ -140,13 +138,13 @@ module Rley # Open this namespace to avoid module qualifier prefixes
140
138
  end
141
139
 
142
140
  def check_legs(expectations)
143
- (parent, path_signature) = subject.legs[-1]
141
+ (parent, path_signature) = a_visitor.legs[-1]
144
142
  expect(parent.to_string(0)).to eq(expectations[0])
145
143
  expect(path_signature).to eq(expectations[1])
146
144
  end
147
145
 
148
146
  def check_node_accesses(node, paths)
149
- actual_paths = subject.node_accesses.fetch(node)
147
+ actual_paths = a_visitor.node_accesses.fetch(node)
150
148
  expect(actual_paths).to eq(paths)
151
149
  end
152
150
 
@@ -154,12 +152,12 @@ module Rley # Open this namespace to avoid module qualifier prefixes
154
152
 
155
153
  # Default instantiation rule
156
154
  subject do
157
- instance = ParseForestVisitor.new(grm_sppf_pforest1)
155
+ instance = described_class.new(grm_sppf_pforest1)
158
156
  instance.subscribe(checker)
159
157
  instance
160
158
  end
161
159
 
162
- it 'should react to the start_visit_pforest message' do
160
+ it 'reacts to the start_visit_pforest message' do
163
161
  # Notify subscribers when start the visit of the pforest
164
162
  # expect(listener1).to receive(:before_pforest).with(forest_root)
165
163
  checker.expectations = [
@@ -267,7 +265,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
267
265
  check_event(event, item, [:before_alternative, prediction])
268
266
  check_legs(['Alt(A => a .)[0, 1]', 7130]) # 2 * 5 * 23 * 31
269
267
  check_node_accesses(item, [7130])
270
- # p(subject.legs)
268
+ # p(a_visitor.legs)
271
269
  end,
272
270
  lambda do |event, parent, children|
273
271
  prediction = 'Alt(A => a .)[0, 1]'
@@ -487,7 +485,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
487
485
  check_event(event, item, [:after_pforest, grm_sppf_pforest1])
488
486
  end
489
487
  ]
490
- subject.start
488
+ a_visitor.start
491
489
  end
492
490
  end # context
493
491
  end # describe
@@ -46,7 +46,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
46
46
  expect(child.to_string(0)).to eq(anExpectation)
47
47
  end
48
48
 
49
- before(:each) do
49
+ before do
50
50
  factory = Parser::ParseWalkerFactory.new
51
51
  accept_entry = sentence_result.accepting_entry
52
52
  accept_index = sentence_result.chart.last_index
@@ -73,7 +73,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
73
73
 
74
74
  subject { ParseForestBuilder.new(expr_tokens) }
75
75
 
76
- it 'should handle walker events' do
76
+ it 'handles walker events' do
77
77
  next_event(:visit, 'P. | 0') # Event 1
78
78
  expected_curr_path('P[0, 5]')
79
79
 
@@ -99,7 +99,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
99
99
  arr_int_tokenizer('[2 , 3, 5 ]')
100
100
  end
101
101
 
102
- subject { ASTBuilder.new(sample_tokens) }
102
+ subject(:a_builder) { described_class.new(sample_tokens) }
103
103
 
104
104
  def init_walker(theParser, theTokens)
105
105
  result = theParser.parse(theTokens)
@@ -112,7 +112,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
112
112
  def skip_events(count)
113
113
  count.times do
114
114
  event = @walker.next
115
- subject.receive_event(*event)
115
+ a_builder.receive_event(*event)
116
116
  end
117
117
  end
118
118
 
@@ -125,20 +125,20 @@ module Rley # Open this namespace to avoid module qualifier prefixes
125
125
  end
126
126
 
127
127
  context 'Initialization:' do
128
- it 'should be created with a sequence of tokens' do
129
- expect { ASTBuilder.new(sample_tokens) }.not_to raise_error
128
+ it 'is created with a sequence of tokens' do
129
+ expect { described_class.new(sample_tokens) }.not_to raise_error
130
130
  end
131
131
 
132
- it 'should know the input tokens' do
133
- expect(subject.tokens).to eq(sample_tokens)
132
+ it 'knows the input tokens' do
133
+ expect(a_builder.tokens).to eq(sample_tokens)
134
134
  end
135
135
 
136
- it "shouldn't know the result yet" do
137
- expect(subject.result).to be_nil
136
+ it "doesn't know the result yet" do
137
+ expect(a_builder.result).to be_nil
138
138
  end
139
139
 
140
- it 'should have an empty stack' do
141
- expect(subject.send(:stack)).to be_empty
140
+ it 'has an empty stack' do
141
+ expect(a_builder.send(:stack)).to be_empty
142
142
  end
143
143
  end # context
144
144
 
@@ -148,11 +148,11 @@ module Rley # Open this namespace to avoid module qualifier prefixes
148
148
  (ev_type, entry, index) = event
149
149
  actual = "#{ev_type} #{entry} #{index}"
150
150
  expect(actual).to eq(expectation)
151
- subject.receive_event(*event)
151
+ a_builder.receive_event(*event)
152
152
  end
153
153
 
154
154
 
155
- before(:each) do
155
+ before do
156
156
  @parser = Parser::GFGEarleyParser.new(sample_grammar)
157
157
  init_walker(@parser, sample_tokens)
158
158
  end
@@ -189,8 +189,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
189
189
  # Event: visit P => . arr | 0 0
190
190
  # Event: visit .P | 0 0
191
191
 
192
- it 'should accept a first visit event' do
193
- stack = get_stack(subject)
192
+ it 'accepts a first visit event' do
193
+ stack = get_stack(a_builder)
194
194
 
195
195
  next_event('visit P. | 0 7')
196
196
  expect(stack.size).to eq(1)
@@ -199,8 +199,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
199
199
  expect(stack.last.children).to be_nil
200
200
  end
201
201
 
202
- it 'should build a tree for an empty array' do
203
- stack = get_stack(subject)
202
+ it 'builds a tree for an empty array' do
203
+ stack = get_stack(a_builder)
204
204
 
205
205
  next_event('visit P. | 0 7')
206
206
  next_event('visit P => arr . | 0 7')
@@ -216,7 +216,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
216
216
  next_event('visit arr => [ sequence ] . | 0 7')
217
217
  # stack: [P[0, 7], arr[0, 7]]
218
218
  rbracket = stack.last.children[-1]
219
- expect(rbracket).to be_kind_of(PTree::TerminalNode)
219
+ expect(rbracket).to be_a(PTree::TerminalNode)
220
220
  expect(rbracket.to_s).to eq("][6, 7]: ']'")
221
221
 
222
222
  next_event('visit arr => [ sequence . ] | 0 6')
@@ -238,12 +238,12 @@ module Rley # Open this namespace to avoid module qualifier prefixes
238
238
 
239
239
  next_event('visit list => list , integer . | 1 6')
240
240
  intval = stack.last.children[-1]
241
- expect(intval).to be_kind_of(IntegerNode)
241
+ expect(intval).to be_a(IntegerNode)
242
242
  expect(intval.value).to eq(5)
243
243
 
244
244
  next_event('visit list => list , . integer | 1 5')
245
245
  comma = stack.last.children[-2]
246
- expect(comma).to be_kind_of(PTree::TerminalNode)
246
+ expect(comma).to be_a(PTree::TerminalNode)
247
247
  expect(comma.to_s).to eq(",[4, 5]: ','")
248
248
 
249
249
  next_event('visit list => list . , integer | 1 4')
@@ -256,12 +256,12 @@ module Rley # Open this namespace to avoid module qualifier prefixes
256
256
 
257
257
  next_event('visit list => list , integer . | 1 4')
258
258
  intval = stack.last.children[-1]
259
- expect(intval).to be_kind_of(IntegerNode)
259
+ expect(intval).to be_a(IntegerNode)
260
260
  expect(intval.value).to eq(3)
261
261
 
262
262
  next_event('visit list => list , . integer | 1 3')
263
263
  comma = stack.last.children[-2]
264
- expect(comma).to be_kind_of(PTree::TerminalNode)
264
+ expect(comma).to be_a(PTree::TerminalNode)
265
265
  expect(comma.to_s).to eq(",[2, 3]: ','")
266
266
 
267
267
  next_event('visit list => list . , integer | 1 2')
@@ -275,14 +275,14 @@ module Rley # Open this namespace to avoid module qualifier prefixes
275
275
 
276
276
  next_event('visit list => integer . | 1 2')
277
277
  intval = stack.last.children[-1]
278
- expect(intval).to be_kind_of(IntegerNode)
278
+ expect(intval).to be_a(IntegerNode)
279
279
  expect(intval.value).to eq(2)
280
280
 
281
281
  next_event('visit list => . integer | 1 1')
282
282
  expect(stack.size).to eq(5)
283
283
  # stack: [P[0, 7], arr[0, 7], sequence[1, 6], list[1, 6], list[1, 4]
284
284
  list_node = stack.last.children[0]
285
- expect(list_node).to be_kind_of(ArrayNode)
285
+ expect(list_node).to be_a(ArrayNode)
286
286
  expect(list_node.children.size).to eq(1)
287
287
  expect(list_node.children.last.value).to eq(2)
288
288
 
@@ -292,7 +292,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
292
292
  expect(stack.size).to eq(4)
293
293
  # stack: [P[0, 7], arr[0, 7], sequence[1, 6], list[1, 6]
294
294
  list_node = stack.last.children[0]
295
- expect(list_node).to be_kind_of(ArrayNode)
295
+ expect(list_node).to be_a(ArrayNode)
296
296
  expect(list_node.children.size).to eq(2)
297
297
  expect(list_node.children.last.value).to eq(3)
298
298
 
@@ -301,7 +301,7 @@ module Rley # Open this namespace to avoid module qualifier prefixes
301
301
  next_event('revisit list => . list , integer | 1 1')
302
302
  # stack: [P[0, 7], arr[0, 7], sequence[1, 6]
303
303
  list_node = stack.last.children.last
304
- expect(list_node).to be_kind_of(ArrayNode)
304
+ expect(list_node).to be_a(ArrayNode)
305
305
  expect(list_node.children.size).to eq(3)
306
306
  expect(list_node.children.last.value).to eq(5)
307
307
 
@@ -311,21 +311,21 @@ module Rley # Open this namespace to avoid module qualifier prefixes
311
311
  expect(stack.size).to eq(2)
312
312
  # stack: [P[0, 7], arr[0, 7]
313
313
  list_node = stack.last.children[1]
314
- expect(list_node).to be_kind_of(ArrayNode)
314
+ expect(list_node).to be_a(ArrayNode)
315
315
  expect(list_node.children.size).to eq(3)
316
316
 
317
317
  next_event('visit .sequence | 1 1')
318
318
 
319
319
  next_event('visit arr => [ . sequence ] | 0 1')
320
320
  lbracket = stack.last.children[0]
321
- expect(lbracket).to be_kind_of(PTree::TerminalNode)
321
+ expect(lbracket).to be_a(PTree::TerminalNode)
322
322
  expect(lbracket.to_s).to eq("[[0, 1]: '['")
323
323
 
324
324
  next_event('visit arr => . [ sequence ] | 0 0')
325
325
  expect(stack.size).to eq(1)
326
326
  # stack: [P[0, 7]
327
327
  array_node = stack.last.children[0]
328
- expect(array_node).to be_kind_of(ArrayNode)
328
+ expect(array_node).to be_a(ArrayNode)
329
329
  expect(array_node.children.size).to eq(3)
330
330
 
331
331
 
@@ -333,8 +333,8 @@ module Rley # Open this namespace to avoid module qualifier prefixes
333
333
 
334
334
  next_event('visit P => . arr | 0 0')
335
335
  expect(stack.size).to eq(0)
336
- expect(subject.result).not_to be_nil
337
- root = subject.result.root
336
+ expect(a_builder.result).not_to be_nil
337
+ root = a_builder.result.root
338
338
  expect(root.interpret).to eq([2, 3, 5])
339
339
 
340
340
  next_event('visit .P | 0 0')