treetop 1.4.5 → 1.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/README.md +44 -20
  2. data/lib/treetop/compiler/metagrammar.rb +126 -33
  3. data/lib/treetop/compiler/metagrammar.treetop +46 -42
  4. data/lib/treetop/compiler/node_classes/repetition.rb +39 -5
  5. data/lib/treetop/version.rb +1 -1
  6. data/spec/compiler/and_predicate_spec.rb +36 -0
  7. data/spec/compiler/anything_symbol_spec.rb +44 -0
  8. data/spec/compiler/character_class_spec.rb +276 -0
  9. data/spec/compiler/choice_spec.rb +80 -0
  10. data/spec/compiler/circular_compilation_spec.rb +30 -0
  11. data/spec/compiler/failure_propagation_functional_spec.rb +21 -0
  12. data/spec/compiler/grammar_compiler_spec.rb +91 -0
  13. data/spec/compiler/grammar_spec.rb +41 -0
  14. data/spec/compiler/multibyte_chars_spec.rb +38 -0
  15. data/spec/compiler/nonterminal_symbol_spec.rb +40 -0
  16. data/spec/compiler/not_predicate_spec.rb +38 -0
  17. data/spec/compiler/occurrence_range_spec.rb +191 -0
  18. data/spec/compiler/one_or_more_spec.rb +35 -0
  19. data/spec/compiler/optional_spec.rb +37 -0
  20. data/spec/compiler/parenthesized_expression_spec.rb +19 -0
  21. data/spec/compiler/parsing_rule_spec.rb +61 -0
  22. data/spec/compiler/repeated_subrule_spec.rb +29 -0
  23. data/spec/compiler/semantic_predicate_spec.rb +175 -0
  24. data/spec/compiler/sequence_spec.rb +115 -0
  25. data/spec/compiler/terminal_spec.rb +81 -0
  26. data/spec/compiler/terminal_symbol_spec.rb +37 -0
  27. data/spec/compiler/test_grammar.treetop +7 -0
  28. data/spec/compiler/test_grammar.tt +7 -0
  29. data/spec/compiler/test_grammar_do.treetop +7 -0
  30. data/spec/compiler/tt_compiler_spec.rb +215 -0
  31. data/spec/compiler/zero_or_more_spec.rb +56 -0
  32. data/spec/composition/a.treetop +11 -0
  33. data/spec/composition/b.treetop +11 -0
  34. data/spec/composition/c.treetop +10 -0
  35. data/spec/composition/d.treetop +10 -0
  36. data/spec/composition/f.treetop +17 -0
  37. data/spec/composition/grammar_composition_spec.rb +40 -0
  38. data/spec/composition/subfolder/e_includes_c.treetop +15 -0
  39. data/spec/ruby_extensions/string_spec.rb +32 -0
  40. data/spec/runtime/compiled_parser_spec.rb +101 -0
  41. data/spec/runtime/interval_skip_list/delete_spec.rb +147 -0
  42. data/spec/runtime/interval_skip_list/expire_range_spec.rb +349 -0
  43. data/spec/runtime/interval_skip_list/insert_and_delete_node.rb +385 -0
  44. data/spec/runtime/interval_skip_list/insert_spec.rb +660 -0
  45. data/spec/runtime/interval_skip_list/interval_skip_list_spec.graffle +6175 -0
  46. data/spec/runtime/interval_skip_list/interval_skip_list_spec.rb +58 -0
  47. data/spec/runtime/interval_skip_list/palindromic_fixture.rb +23 -0
  48. data/spec/runtime/interval_skip_list/palindromic_fixture_spec.rb +163 -0
  49. data/spec/runtime/interval_skip_list/spec_helper.rb +84 -0
  50. data/spec/runtime/syntax_node_spec.rb +77 -0
  51. data/spec/spec_helper.rb +110 -0
  52. data/treetop.gemspec +18 -0
  53. metadata +70 -9
@@ -0,0 +1,10 @@
1
+ module Test
2
+ grammar D
3
+ include A
4
+ include B
5
+
6
+ rule inherit
7
+ super 'works'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+
2
+ require "a"
3
+
4
+ require File.dirname(__FILE__) + "/b"
5
+ require File.dirname(__FILE__) + "/subfolder/e_includes_c"
6
+
7
+ module Test
8
+ grammar F
9
+ include A
10
+ include B
11
+ include E
12
+
13
+ rule f
14
+ c e 'f'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ module GrammarCompositionSpec
4
+ describe "several composed grammars" do
5
+ before do
6
+ dir = File.dirname(__FILE__)
7
+ Treetop.load File.join(dir, 'a')
8
+ Treetop.load File.join(dir, 'b')
9
+ Treetop.load File.join(dir, 'c')
10
+ # Check that polyglot finds d.treetop and loads it:
11
+ $: << dir
12
+ require 'd'
13
+
14
+ @c = ::Test::CParser.new
15
+ @d = ::Test::DParser.new
16
+ end
17
+
18
+ specify "rules in C have access to rules defined in A and B" do
19
+ @c.parse('abc').should_not be_nil
20
+ end
21
+
22
+ specify "rules in C can override rules in A and B with super semantics" do
23
+ @d.parse('superkeywordworks').should_not be_nil
24
+ end
25
+ end
26
+
27
+ describe "composed grammar chaining with require" do
28
+ before do
29
+ # Load f with polyglot without using the load path:
30
+ require File.dirname(__FILE__) + '/f'
31
+
32
+ @f = ::Test::FParser.new
33
+ end
34
+
35
+ specify "rules in F have access to rule defined in E" do
36
+ @f.parse('abcef').should_not be_nil
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../c"
2
+
3
+ module Test
4
+ grammar E
5
+ include C
6
+
7
+ rule e
8
+ 'e'
9
+ end
10
+
11
+ rule inherit
12
+ 'super'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+ before do
5
+ @string = %{
6
+ 0123456789
7
+ 012345
8
+ 01234567
9
+ 0123
10
+ }.tabto(0).strip
11
+ end
12
+
13
+ it "can translate indices to column numbers" do
14
+ @string.column_of(0).should == 1
15
+ @string.column_of(5).should == 6
16
+ @string.column_of(10).should == 11
17
+ @string.column_of(11).should == 1
18
+ @string.column_of(17).should == 7
19
+ @string.column_of(18).should == 1
20
+ @string.column_of(24).should == 7
21
+ end
22
+
23
+ it "can translate indices to line numbers" do
24
+ @string.line_of(0).should == 1
25
+ @string.line_of(5).should == 1
26
+ @string.line_of(10).should == 1
27
+ @string.line_of(11).should == 2
28
+ @string.line_of(17).should == 2
29
+ @string.line_of(18).should == 3
30
+ @string.line_of(24).should == 3
31
+ end
32
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ module CompiledParserSpec
4
+ describe Runtime::CompiledParser, "for a grammar with two rules" do
5
+ attr_reader :parser
6
+
7
+ testing_grammar %{
8
+ grammar TwoRules
9
+ rule a
10
+ 'a'
11
+ end
12
+
13
+ rule b
14
+ 'b'
15
+ end
16
+ end
17
+ }
18
+
19
+ before do
20
+ @parser = parser_class_under_test.new
21
+ end
22
+
23
+ it "allows its root to be specified" do
24
+ parser.parse('a').should_not be_nil
25
+ parser.parse('b').should be_nil
26
+
27
+ parser.root = :b
28
+ parser.parse('b').should_not be_nil
29
+ parser.parse('a').should be_nil
30
+ end
31
+
32
+ it "allows the requirement that all input be consumed to be disabled" do
33
+ parser.parse('ab').should be_nil
34
+ parser.consume_all_input = false
35
+ result = parser.parse('ab')
36
+ result.should_not be_nil
37
+ result.interval.should == (0...1)
38
+ end
39
+
40
+ it "allows input to be parsed at a given index" do
41
+ parser.parse('ba').should be_nil
42
+ parser.parse('ba', :index => 1).should_not be_nil
43
+ end
44
+
45
+ end
46
+
47
+ describe Runtime::CompiledParser, "for a grammar with a choice between terminals" do
48
+ attr_reader :parser
49
+
50
+ testing_grammar %{
51
+ grammar Choice
52
+ rule choice
53
+ 'a' / 'b' / 'c'
54
+ end
55
+ end
56
+ }
57
+
58
+ before do
59
+ @parser = parser_class_under_test.new
60
+ end
61
+
62
+ it "provides #failure_reason, #failure_column, and #failure_line when there is a parse failure" do
63
+ parser.parse('z').should be_nil
64
+ parser.failure_reason.should == "Expected one of a, b, c at line 1, column 1 (byte 1) after "
65
+ parser.failure_line.should == 1
66
+ parser.failure_column.should == 1
67
+ end
68
+ end
69
+
70
+ describe Runtime::CompiledParser, "#terminal_failures" do
71
+ attr_reader:parser
72
+
73
+ testing_grammar %{
74
+ grammar SequenceOfTerminals
75
+ rule foo
76
+ 'a' 'b' 'c'
77
+ end
78
+ end
79
+ }
80
+
81
+ before do
82
+ @parser = parser_class_under_test.new
83
+ end
84
+
85
+ it "is reset between parses" do
86
+ parser.parse('ac')
87
+ terminal_failures = parser.terminal_failures
88
+ terminal_failures.size.should == 1
89
+ failure = terminal_failures.first
90
+ failure.index.should == 1
91
+ failure.expected_string.should == 'b'
92
+
93
+ parser.parse('b')
94
+ terminal_failures = parser.terminal_failures
95
+ terminal_failures.size.should == 1
96
+ failure = terminal_failures.first
97
+ failure.index.should == 0
98
+ failure.expected_string.should == 'a'
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,147 @@
1
+ require 'runtime/interval_skip_list/spec_helper'
2
+
3
+ class IntervalSkipList
4
+ public :insert_node, :delete_node, :head, :nodes
5
+ end
6
+
7
+ describe IntervalSkipList do
8
+ it_should_behave_like "the palindromic fixture"
9
+
10
+ describe " when :c is deleted" do
11
+ before do
12
+ list.delete(:c)
13
+ end
14
+
15
+ describe "[0]" do
16
+ before do
17
+ @node = list.nodes[0]
18
+ end
19
+
20
+ it "has a key of 1 and a height of 3" do
21
+ node.key.should == 1
22
+ node.height.should == 3
23
+ end
24
+
25
+ it "has :f as its only forward marker at level 2" do
26
+ node.forward_markers[2].should have_markers(:f)
27
+ end
28
+
29
+ it "has :a, :b, :d, and :e as its only forward markers at level 1" do
30
+ node.forward_markers[1].should have_markers(:a, :b, :d, :e)
31
+ end
32
+
33
+ it "has no forward markers at level 0" do
34
+ node.forward_markers[0].should be_empty
35
+ end
36
+
37
+ it "has no markers" do
38
+ node.markers.should be_empty
39
+ end
40
+ end
41
+
42
+ describe "[1]" do
43
+ before do
44
+ @node = list.nodes[1]
45
+ end
46
+
47
+ it "has a key of 3 and a height of 2" do
48
+ node.key.should == 3
49
+ node.height.should == 2
50
+ end
51
+
52
+ it "has :e as its only forward marker at level 1" do
53
+ node.forward_markers[1].should have_markers(:e)
54
+ end
55
+
56
+ it "has :b and :d as its only forward markers at level 0" do
57
+ node.forward_markers[0].should have_markers(:b, :d)
58
+ end
59
+
60
+ it "has :a, :b, :d, and :e as its only markers" do
61
+ node.markers.should have_markers(:a, :b, :d, :e)
62
+ end
63
+ end
64
+
65
+ describe "[2]" do
66
+ before do
67
+ @node = list.nodes[2]
68
+ end
69
+
70
+ it "has a key of 5 and a height of 1" do
71
+ node.key.should == 5
72
+ node.height.should == 1
73
+ end
74
+
75
+ it "has :d and :g as its only forward markers at level 0" do
76
+ node.forward_markers[0].should have_markers(:d, :g)
77
+ end
78
+
79
+ it "has :b and :d as its only markers" do
80
+ node.markers.should have_markers(:b, :d)
81
+ end
82
+ end
83
+
84
+ describe "[3]" do
85
+ before do
86
+ @node = list.nodes[3]
87
+ end
88
+
89
+ it "has a key of 9 and a height of 1" do
90
+ node.key.should == 9
91
+ node.height.should == 1
92
+ end
93
+
94
+ it "has :g as its only forward marker at level 0" do
95
+ node.forward_markers[0].should have_markers(:g)
96
+ end
97
+
98
+ it "has :d and :g as its only markers" do
99
+ node.markers.should have_markers(:d, :g)
100
+ end
101
+ end
102
+
103
+ describe "[4]" do
104
+ before do
105
+ @node = list.nodes[4]
106
+ end
107
+
108
+ it "has a key of 11 and a height of 2" do
109
+ node.key.should == 11
110
+ node.height.should == 2
111
+ end
112
+
113
+ it "has :g as its only forward marker at level 1" do
114
+ node.forward_markers[1].should have_markers(:g)
115
+ end
116
+
117
+ it "has no forward markers at level 0" do
118
+ node.forward_markers[0].should be_empty
119
+ end
120
+
121
+ it "has :e and :g as its only markers" do
122
+ node.markers.should have_markers(:e, :g)
123
+ end
124
+ end
125
+
126
+ describe "[5]" do
127
+ before do
128
+ @node = list.nodes[5]
129
+ end
130
+
131
+ it "has a key of 13 and a height of 3" do
132
+ node.key.should == 13
133
+ node.height.should == 3
134
+ end
135
+
136
+ it "has no forward markers at any level" do
137
+ node.forward_markers[0].should be_empty
138
+ node.forward_markers[1].should be_empty
139
+ node.forward_markers[2].should be_empty
140
+ end
141
+
142
+ it "has :f and :g as its only markers" do
143
+ node.markers.should have_markers(:f, :g)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,349 @@
1
+ require 'runtime/interval_skip_list/spec_helper'
2
+
3
+ describe IntervalSkipList do
4
+ it_should_behave_like "the palindromic fixture"
5
+
6
+ describe "#overlapping" do
7
+ it "returns intervals :d, :e, :f, and :g for 7..9" do
8
+ list.overlapping(7..9)[0].should have_markers(:d, :e, :f, :g)
9
+ end
10
+
11
+ it "returns intervals :b, :c, :d, :e, :f, and :g for 3..7" do
12
+ list.overlapping(3..7)[0].should have_markers(:b, :c, :d, :e, :f, :g )
13
+ end
14
+
15
+ it "returns intervals :b, :c, :d, :e, :f, and :g for 3..6" do
16
+ list.overlapping(3..6)[0].should have_markers(:b, :c, :d, :e, :f, :g )
17
+ end
18
+
19
+ describe ", when :x is inserted on 3..7" do
20
+ before do
21
+ list.insert(3..7, :x)
22
+ end
23
+
24
+ it "returns intervals :b, :c, :d, :e, :f, :x for 3..5" do
25
+ list.overlapping(3..5)[0].should have_markers(:b, :c, :d, :e, :f, :x)
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ describe "when 7..7 is expired with a length change of 0" do
32
+ before do
33
+ list.expire(7..7, 0)
34
+ end
35
+
36
+ describe " #nodes" do
37
+ attr_reader :nodes, :node
38
+
39
+ before do
40
+ @nodes = list.nodes
41
+ end
42
+
43
+ it "has a size of 4" do
44
+ nodes.size.should == 4
45
+ end
46
+
47
+ describe "[0]" do
48
+ before do
49
+ @node = nodes[0]
50
+ end
51
+
52
+ it "has a key of 1 and a height of 3" do
53
+ node.key.should == 1
54
+ node.height.should == 3
55
+ end
56
+
57
+ it "has no forward markers at level 0" do
58
+ node.forward_markers[0].should be_empty
59
+ end
60
+
61
+ it "has :a and :b as its only forward markers on level 1" do
62
+ node.forward_markers[1].should have_markers(:a, :b)
63
+ end
64
+
65
+ it "has :c as its only forward marker on level 2" do
66
+ node.forward_markers[2].should have_markers(:c)
67
+ end
68
+
69
+ it "has no markers" do
70
+ node.markers.should be_empty
71
+ end
72
+ end
73
+
74
+ describe "[1]" do
75
+ before do
76
+ @node = nodes[1]
77
+ end
78
+
79
+ it "has a key of 3 and a height of 2" do
80
+ node.key.should == 3
81
+ node.height.should == 2
82
+ end
83
+
84
+ it "has :b as its only forward marker on level 0" do
85
+ node.forward_markers[0].should have_markers(:b)
86
+ end
87
+
88
+ it "has no forward markers on level 1" do
89
+ node.forward_markers[1].should be_empty
90
+ end
91
+
92
+ it "has :a and :b as its only markers" do
93
+ node.markers.should have_markers(:a, :b)
94
+ end
95
+ end
96
+
97
+ describe "[2]" do
98
+ before do
99
+ @node = nodes[2]
100
+ end
101
+
102
+ it "has a key of 5 and a height of 1" do
103
+ node.key.should == 5
104
+ node.height.should == 1
105
+ end
106
+
107
+ it "has no forward markers on level 0" do
108
+ node.forward_markers[0].should be_empty
109
+ end
110
+
111
+ it "has :b as its only marker" do
112
+ node.markers.should have_markers(:b)
113
+ end
114
+ end
115
+
116
+ describe "[3]" do
117
+ before do
118
+ @node = nodes[3]
119
+ end
120
+
121
+ it "has a key of 7 and a height of 3" do
122
+ node.key.should == 7
123
+ node.height.should == 3
124
+ end
125
+
126
+ it "has no forward markers at any level" do
127
+ node.forward_markers[0].should be_empty
128
+ node.forward_markers[1].should be_empty
129
+ node.forward_markers[2].should be_empty
130
+ end
131
+
132
+ it "has :c as its only marker" do
133
+ node.markers.should have_markers(:c)
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ describe "when 4..4 is expired with a length change of 2" do
140
+ before do
141
+ list.expire(4..4, 2)
142
+ end
143
+
144
+ describe " #nodes" do
145
+ attr_reader :nodes, :node
146
+
147
+ before do
148
+ @nodes = list.nodes
149
+ end
150
+
151
+ it "has a size of 4" do
152
+ nodes.size.should == 4
153
+ end
154
+
155
+ describe "[0]" do
156
+ before do
157
+ @node = nodes[0]
158
+ end
159
+
160
+ it "has a key of 1 and a height of 3" do
161
+ node.key.should == 1
162
+ node.height.should == 3
163
+ end
164
+
165
+ it "has no forward markers at level 0 and 2" do
166
+ node.forward_markers[0].should be_empty
167
+ node.forward_markers[2].should be_empty
168
+ end
169
+
170
+ it "has :a as its only forward marker on level 1" do
171
+ node.forward_markers[1].should have_markers(:a)
172
+ end
173
+
174
+ it "has no markers" do
175
+ node.markers.should be_empty
176
+ end
177
+ end
178
+
179
+ describe "[1]" do
180
+ before do
181
+ @node = nodes[1]
182
+ end
183
+
184
+ it "has a key of 3 and a height of 2" do
185
+ node.key.should == 3
186
+ node.height.should == 2
187
+ end
188
+
189
+ it "has no forward markers at any level" do
190
+ node.forward_markers[0].should be_empty
191
+ node.forward_markers[1].should be_empty
192
+ end
193
+
194
+ it "has :a as its only marker" do
195
+ node.markers.should have_markers(:a)
196
+ end
197
+ end
198
+
199
+ describe "[2]" do
200
+ before do
201
+ @node = nodes[2]
202
+ end
203
+
204
+ it "has a key of 7 and a height of 1" do
205
+ node.key.should == 7
206
+ node.height.should == 1
207
+ end
208
+
209
+ it "has :g as its only forward marker at level 0" do
210
+ node.forward_markers[0].should have_markers(:g)
211
+ end
212
+
213
+ it "has no markers" do
214
+ node.markers.should be_empty
215
+ end
216
+ end
217
+
218
+ describe "[3]" do
219
+ before do
220
+ @node = nodes[3]
221
+ end
222
+
223
+ it "has a key of 15 and a height of 3" do
224
+ node.key.should == 15
225
+ node.height.should == 3
226
+ end
227
+
228
+ it "has no forward markers at any level" do
229
+ node.forward_markers[0].should be_empty
230
+ node.forward_markers[1].should be_empty
231
+ node.forward_markers[2].should be_empty
232
+ end
233
+
234
+ it "has :g as its only marker" do
235
+ node.markers.should have_markers(:g)
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ describe "when :x is inserted on 1..5, :y on 7..11, and :z on 9..13" do
242
+ before do
243
+ list.insert(1..5, :x)
244
+ list.insert(7..11, :y)
245
+ list.insert(9..13, :z)
246
+ end
247
+
248
+ describe "when 4..8 is expired with a length change of -3" do
249
+ before do
250
+ list.expire(4..8, -3)
251
+ end
252
+
253
+ describe "#nodes" do
254
+ attr_reader :nodes, :node
255
+ before do
256
+ @nodes = list.nodes
257
+ end
258
+
259
+ it "has a size of 4" do
260
+ nodes.size.should == 4
261
+ end
262
+
263
+ describe "[0]" do
264
+ before do
265
+ @node = nodes[0]
266
+ end
267
+
268
+ it "has a key of 1 and height of 3" do
269
+ node.key.should == 1
270
+ node.height.should == 3
271
+ end
272
+
273
+ it "has :a as its only forward marker on level 1" do
274
+ node.forward_markers[1].should have_markers(:a)
275
+ end
276
+
277
+ it "has no forward markers at level 0 and 2" do
278
+ node.forward_markers[0].should be_empty
279
+ node.forward_markers[2].should be_empty
280
+ end
281
+
282
+ it "has no markers" do
283
+ node.markers.should be_empty
284
+ end
285
+ end
286
+
287
+ describe "[1]" do
288
+ before do
289
+ @node = nodes[1]
290
+ end
291
+
292
+ it "has a key of 3 and height of 2" do
293
+ node.key.should == 3
294
+ node.height.should == 2
295
+ end
296
+
297
+ it "has no forward markers" do
298
+ node.forward_markers[0].should be_empty
299
+ node.forward_markers[1].should be_empty
300
+ end
301
+
302
+ it "has :a as its only marker" do
303
+ node.markers.should have_markers(:a)
304
+ end
305
+ end
306
+
307
+ describe "[2]" do
308
+ before do
309
+ @node = nodes[2]
310
+ end
311
+
312
+ it "has a key of 6 and a height of 1" do
313
+ node.key.should == 6
314
+ node.height.should == 1
315
+ end
316
+
317
+ it "has :z as its only forward marker at level 0" do
318
+ node.forward_markers[0].should have_markers(:z)
319
+ end
320
+
321
+ it "has no markers" do
322
+ node.markers.should be_empty
323
+ end
324
+ end
325
+
326
+ describe "[3]" do
327
+ before do
328
+ @node = nodes[3]
329
+ end
330
+
331
+ it "has a key of 10 and height of 3" do
332
+ node.key.should == 10
333
+ node.height.should == 3
334
+ end
335
+
336
+ it "has no forward markers at any level" do
337
+ node.forward_markers[0].should be_empty
338
+ node.forward_markers[1].should be_empty
339
+ node.forward_markers[2].should be_empty
340
+ end
341
+
342
+ it "has :z as its only marker" do
343
+ node.markers.should have_markers(:z)
344
+ end
345
+ end
346
+ end
347
+ end
348
+ end
349
+ end