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,30 @@
1
+ require 'spec_helper'
2
+
3
+ BENCHMARK = false
4
+ METAGRAMMAR_PATH = File.expand_path('../../../lib/treetop/compiler/metagrammar.treetop', __FILE__)
5
+
6
+ module CircularCompilationSpec
7
+ describe "a parser for the metagrammar" do
8
+ attr_reader :parser
9
+
10
+ before do
11
+ @parser = Treetop::Compiler::MetagrammarParser.new
12
+ end
13
+
14
+ it "can parse the metagrammar.treetop whence it was generated" do
15
+ File.open(METAGRAMMAR_PATH, 'r') do |f|
16
+ metagrammar_source = f.read
17
+ result = parser.parse(metagrammar_source)
18
+ result.should_not be_nil
19
+
20
+ # generated_parser = result.compile
21
+ # Object.class_eval(generated_parser)
22
+ # parser_2 = Treetop::Compiler::MetagrammarParser.new
23
+ # optionally_benchmark do
24
+ # result = parser_2.parse(metagrammar_source)
25
+ # result.should_not be_nil
26
+ # end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe "An expression for braces surrounding zero or more letters followed by semicolons" do
4
+ testing_expression "'{' ([a-z] ';')* '}'"
5
+
6
+ it "parses matching input successfully" do
7
+ parse('{a;b;c;}').should_not be_nil
8
+ end
9
+
10
+ it "fails to parse input with an expression that is missing a semicolon, reporting the terminal failure occurring at the maximum index" do
11
+ parse('{a;b;c}') do |result|
12
+ result.should be_nil
13
+
14
+ terminal_failures = parser.terminal_failures
15
+ terminal_failures.size.should == 1
16
+ failure = terminal_failures[0]
17
+ failure.index.should == 6
18
+ failure.expected_string.should == ';'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'tmpdir'
3
+
4
+ describe Compiler::GrammarCompiler do
5
+ attr_reader :compiler, :source_path_with_treetop_extension, :source_path_with_tt_extension, :target_path, :alternate_target_path
6
+ before do
7
+ @compiler = Compiler::GrammarCompiler.new
8
+
9
+ dir = File.dirname(__FILE__)
10
+ @tmpdir = Dir.tmpdir
11
+
12
+ @source_path_with_treetop_extension = "#{dir}/test_grammar.treetop"
13
+ @source_path_with_do = "#{dir}/test_grammar_do.treetop"
14
+ @source_path_with_tt_extension = "#{dir}/test_grammar.tt"
15
+ @target_path = "#{@tmpdir}/test_grammar.rb"
16
+ @target_path_with_do = "#{@tmpdir}/test_grammar_do.rb"
17
+ @alternate_target_path = "#{@tmpdir}/test_grammar_alt.rb"
18
+ delete_target_files
19
+ end
20
+
21
+ after do
22
+ delete_target_files
23
+ Object.class_eval do
24
+ remove_const(:Test) if const_defined?(:Test)
25
+ end
26
+ end
27
+
28
+ specify "compilation of a single file to a default file name" do
29
+ src_copy = "#{@tmpdir}/test_grammar.treetop"
30
+ File.open(source_path_with_treetop_extension) { |f| File.open(src_copy,'w'){|o|o.write(f.read)} }
31
+ File.exists?(target_path).should be_false
32
+ compiler.compile(src_copy)
33
+ File.exists?(target_path).should be_true
34
+ require target_path
35
+ Test::GrammarParser.new.parse('foo').should_not be_nil
36
+ end
37
+
38
+ specify "compilation of a single file to an explicit file name" do
39
+ File.exists?(alternate_target_path).should be_false
40
+ compiler.compile(source_path_with_treetop_extension, alternate_target_path)
41
+ File.exists?(alternate_target_path).should be_true
42
+ require alternate_target_path
43
+ Test::GrammarParser.new.parse('foo').should_not be_nil
44
+ end
45
+
46
+ specify "compilation of a single file without writing it to an output file" do
47
+ compiler.ruby_source(source_path_with_treetop_extension).should_not be_nil
48
+ end
49
+
50
+ specify "ruby_source_from_string compiles a grammar stored in string" do
51
+ compiler.ruby_source_from_string(File.read(source_path_with_treetop_extension)).should_not be_nil
52
+ end
53
+
54
+ specify "Treetop.load_from_string compiles and evaluates a source grammar stored in string" do
55
+ Treetop.load_from_string File.read(source_path_with_treetop_extension)
56
+ Test::GrammarParser.new.parse('foo').should_not be_nil
57
+ end
58
+
59
+ specify "Treetop.load compiles and evaluates a source grammar with a .treetop extension" do
60
+ Treetop.load source_path_with_treetop_extension
61
+ Test::GrammarParser.new.parse('foo').should_not be_nil
62
+ end
63
+
64
+ specify "Treetop.load compiles and evaluates a source grammar with a .tt extension" do
65
+ path_without_extension = source_path_with_tt_extension
66
+ Treetop.load path_without_extension
67
+ Test::GrammarParser.new.parse('foo').should_not be_nil
68
+ end
69
+
70
+
71
+ specify "Treetop.load compiles and evaluates source grammar with no extension" do
72
+ path_without_extension = source_path_with_treetop_extension.gsub(/\.treetop\Z/, '')
73
+ Treetop.load path_without_extension
74
+ Test::GrammarParser.new.parse('foo').should_not be_nil
75
+ end
76
+
77
+ specify "grammars with 'do' compile" do
78
+ src_copy = "#{@tmpdir}/test_grammar_do.treetop"
79
+ File.open(@source_path_with_do) { |f| File.open(src_copy,'w'){|o|o.write(f.read)} }
80
+ compiler.compile(src_copy)
81
+ require @target_path_with_do
82
+ Test::GrammarParser.new.parse('foo').should_not be_nil
83
+ end
84
+
85
+ def delete_target_files
86
+ File.delete(target_path) if File.exists?(target_path)
87
+ File.delete(@target_path_with_do) if File.exists?(@target_path_with_do)
88
+ File.delete(alternate_target_path) if File.exists?(alternate_target_path)
89
+ end
90
+ end
91
+
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module GrammarSpec
4
+ module Bar
5
+ end
6
+
7
+ describe "a grammar" do
8
+ testing_grammar %{
9
+ grammar Foo
10
+ # This comment should not cause a syntax error, nor should the following empty one
11
+ #
12
+ include GrammarSpec::Bar
13
+
14
+ rule foo
15
+ bar / baz
16
+ end
17
+
18
+ rule bar
19
+ 'bar' 'bar'
20
+ end
21
+
22
+ rule baz
23
+ 'baz' 'baz'
24
+ end
25
+ end
26
+ }
27
+
28
+ it "parses matching input" do
29
+ parse('barbar').should_not be_nil
30
+ parse('bazbaz').should_not be_nil
31
+ end
32
+
33
+ it "fails if it does not parse all input" do
34
+ parse('barbarbazbaz').should be_nil
35
+ end
36
+
37
+ it "mixes in included modules" do
38
+ self.class.const_get(:Foo).ancestors.should include(GrammarSpec::Bar)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ #!ruby19
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ module MultibyteCharsSpec
7
+ describe "an anything symbol" do
8
+ testing_expression '.'
9
+ it "matches an UTF-8 character" do
10
+ parse_multibyte("ø").should_not be_nil
11
+ end
12
+ end
13
+
14
+ describe "A character class containing UTF-8 characters" do
15
+ testing_expression "[æøå]"
16
+ it "recognizes the UTF-8 characters" do
17
+ parse_multibyte("ø").should_not be_nil
18
+ end
19
+ end
20
+
21
+ describe "a character class repetition containing UTF-8 characters mixed with other expressions" do
22
+ testing_expression '[æøå]+ "a"'
23
+ it "lazily instantiates a node for the character" do
24
+ result = parse_multibyte('æøåa')
25
+ pending "Multibyte support is not supported in Ruby 1.8.6" if RUBY_VERSION =~ /^1\.8.6/
26
+ result.elements[0].instance_variable_get("@elements").should include(true)
27
+ result.elements[0].elements.should_not include(true)
28
+ result.elements[0].elements.size.should == 3
29
+ result.elements.size.should == 2
30
+ result.elements[0].text_value.should == "æøå"
31
+ result.elements[0].elements[0].text_value.should == "æ"
32
+ result.elements[0].elements[1].text_value.should == "ø"
33
+ result.elements[0].elements[2].text_value.should == "å"
34
+ result.elements[1].text_value == "a"
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ module NonterminalSymbolSpec
4
+ describe "A nonterminal symbol followed by a block" do
5
+ testing_expression 'foo { def a_method; end }'
6
+
7
+ parser_class_under_test.class_eval do
8
+ def _nt_foo
9
+ '_nt_foo called'
10
+ end
11
+ end
12
+
13
+ it "compiles to a method call, extending its results with the anonymous module for the block" do
14
+ result = parse('')
15
+ result.should == '_nt_foo called'
16
+ result.should respond_to(:a_method)
17
+ end
18
+ end
19
+
20
+ module TestModule
21
+ def a_method
22
+ end
23
+ end
24
+
25
+ describe "a non-terminal followed by a module declaration" do
26
+ testing_expression 'foo <NonterminalSymbolSpec::TestModule>'
27
+
28
+ parser_class_under_test.class_eval do
29
+ def _nt_foo
30
+ '_nt_foo called'
31
+ end
32
+ end
33
+
34
+ it "compiles to a method call, extending its results with the anonymous module for the block" do
35
+ result = parse('')
36
+ result.should == '_nt_foo called'
37
+ result.should respond_to(:a_method)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module NotPredicateSpec
4
+ describe "A !-predicated terminal symbol" do
5
+ testing_expression '!"foo"'
6
+
7
+ it "fails to parse input matching the terminal symbol" do
8
+ parse('foo').should be_nil
9
+ end
10
+ end
11
+
12
+ describe "A sequence of a terminal and an and another !-predicated terminal" do
13
+ testing_expression '"foo" !"bar"'
14
+
15
+ it "fails to match input matching both terminals" do
16
+ parse('foobar').should be_nil
17
+ end
18
+
19
+ it "successfully parses input matching the first terminal and not the second, reporting the parse failure of the second terminal" do
20
+ parse('foo') do |result|
21
+ result.should_not be_nil
22
+ terminal_failures = parser.terminal_failures
23
+ terminal_failures.size.should == 1
24
+ failure = terminal_failures.first
25
+ failure.index.should == 3
26
+ failure.expected_string.should == 'bar'
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "A !-predicated sequence" do
32
+ testing_expression '!("a" "b" "c")'
33
+
34
+ it "fails to parse matching input" do
35
+ parse('abc').should be_nil
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,191 @@
1
+ require 'spec_helper'
2
+ require 'ruby-debug'
3
+ Debugger.start
4
+
5
+ module OccurrenceRangeSpec
6
+ class Foo < Treetop::Runtime::SyntaxNode
7
+ end
8
+
9
+ describe "zero to two of a terminal symbol followed by a node class declaration and a block" do
10
+ testing_expression '"foo"..2 <OccurrenceRangeSpec::Foo> { def a_method; end }'
11
+
12
+ it "successfully parses epsilon, reporting a failure" do
13
+ parse('') do |result|
14
+ result.should_not be_nil
15
+ result.should be_an_instance_of(Foo)
16
+ result.should respond_to(:a_method)
17
+
18
+ terminal_failures = parser.terminal_failures
19
+ terminal_failures.size.should == 1
20
+ failure = terminal_failures.first
21
+ failure.index.should == 0
22
+ failure.expected_string.should == 'foo'
23
+ end
24
+ end
25
+
26
+ it "successfully parses epsilon, returning an instance declared node class and recording a terminal failure" do
27
+ parse('') do |result|
28
+ result.should_not be_nil
29
+ result.should be_an_instance_of(Foo)
30
+ result.should respond_to(:a_method)
31
+
32
+ terminal_failures = parser.terminal_failures
33
+ terminal_failures.size.should == 1
34
+ failure = terminal_failures.first
35
+ failure.index.should == 0
36
+ failure.expected_string.should == 'foo'
37
+ end
38
+ end
39
+
40
+ it "successfully parses one of that terminal, returning an instance of the declared node class and recording a terminal failure" do
41
+ parse("foo") do |result|
42
+ result.should_not be_nil
43
+ result.should be_an_instance_of(Foo)
44
+ result.should respond_to(:a_method)
45
+
46
+ terminal_failures = parser.terminal_failures
47
+ terminal_failures.size.should == 1
48
+ failure = terminal_failures.first
49
+ failure.index.should == 3
50
+ failure.expected_string.should == 'foo'
51
+ end
52
+ end
53
+
54
+ it "successfully parses two of that terminal, returning an instance of the declared node class and reporting no failure" do
55
+ parse("foofoo") do |result|
56
+ result.should_not be_nil
57
+ result.should be_an_instance_of(Foo)
58
+ result.should respond_to(:a_method)
59
+
60
+ terminal_failures = parser.terminal_failures
61
+ terminal_failures.size.should == 0
62
+ end
63
+ end
64
+
65
+ it "fails to parses three of that terminal, returning an instance of the declared node class and reporting no failure" do
66
+ parse("foofoofoo") do |result|
67
+ result.should be_nil
68
+
69
+ terminal_failures = parser.terminal_failures
70
+ terminal_failures.size.should == 0
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "two to four of a terminal symbol followed by a node class declaration and a block" do
76
+ testing_expression '"foo" 2..4 <OccurrenceRangeSpec::Foo> { def a_method; end }'
77
+
78
+ it "fails to parse epsilon, reporting a failure" do
79
+ parse('') do |result|
80
+ result.should be_nil
81
+ terminal_failures = parser.terminal_failures
82
+ terminal_failures.size.should == 1
83
+ failure = terminal_failures.first
84
+ failure.index.should == 0
85
+ failure.expected_string.should == 'foo'
86
+ end
87
+ end
88
+
89
+ it "fails to parse one of that terminal, returning an instance of the declared node class and recording a terminal failure" do
90
+ parse("foo") do |result|
91
+ result.should be_nil
92
+
93
+ terminal_failures = parser.terminal_failures
94
+ terminal_failures.size.should == 1
95
+ failure = terminal_failures.first
96
+ failure.index.should == 3
97
+ failure.expected_string.should == 'foo'
98
+ end
99
+ end
100
+
101
+ it "successfully parses two of that terminal, returning an instance of the declared node class and reporting no failure" do
102
+ parse("foofoo") do |result|
103
+ result.should_not be_nil
104
+ result.should be_an_instance_of(Foo)
105
+ result.should respond_to(:a_method)
106
+
107
+ terminal_failures = parser.terminal_failures
108
+ terminal_failures.size.should == 1
109
+ failure = terminal_failures.first
110
+ failure.index.should == 6
111
+ failure.expected_string.should == 'foo'
112
+ end
113
+ end
114
+
115
+ it "successfully parses four of that terminal, returning an instance of the declared node class and reporting no failure" do
116
+ parse("foofoofoofoo") do |result|
117
+ result.should_not be_nil
118
+ result.should be_an_instance_of(Foo)
119
+ result.should respond_to(:a_method)
120
+
121
+ terminal_failures = parser.terminal_failures
122
+ terminal_failures.size.should == 0
123
+ end
124
+ end
125
+
126
+ it "fails to parses five of that terminal, returning an instance of the declared node class and reporting no failure" do
127
+ parse("foofoofoofoofoo") do |result|
128
+ result.should be_nil
129
+
130
+ terminal_failures = parser.terminal_failures
131
+ terminal_failures.size.should == 0
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "two to any number of a terminal symbol followed by a node class declaration and a block" do
137
+ testing_expression '"foo" 2.. <OccurrenceRangeSpec::Foo> { def a_method; end }'
138
+
139
+ it "fails to parse epsilon, reporting a failure" do
140
+ parse('') do |result|
141
+ result.should be_nil
142
+ terminal_failures = parser.terminal_failures
143
+ terminal_failures.size.should == 1
144
+ failure = terminal_failures.first
145
+ failure.index.should == 0
146
+ failure.expected_string.should == 'foo'
147
+ end
148
+ end
149
+
150
+ it "fails to parse one of that terminal, returning an instance of the declared node class and recording a terminal failure" do
151
+ parse("foo") do |result|
152
+ result.should be_nil
153
+
154
+ terminal_failures = parser.terminal_failures
155
+ terminal_failures.size.should == 1
156
+ failure = terminal_failures.first
157
+ failure.index.should == 3
158
+ failure.expected_string.should == 'foo'
159
+ end
160
+ end
161
+
162
+ it "successfully parses two of that terminal, returning an instance of the declared node class and reporting no failure" do
163
+ parse("foofoo") do |result|
164
+ result.should_not be_nil
165
+ result.should be_an_instance_of(Foo)
166
+ result.should respond_to(:a_method)
167
+
168
+ terminal_failures = parser.terminal_failures
169
+ terminal_failures.size.should == 1
170
+ failure = terminal_failures.first
171
+ failure.index.should == 6
172
+ failure.expected_string.should == 'foo'
173
+ end
174
+ end
175
+
176
+ it "successfully parses four of that terminal, returning an instance of the declared node class and reporting a failure on the fifth" do
177
+ parse("foofoofoofoo") do |result|
178
+ result.should_not be_nil
179
+ result.should be_an_instance_of(Foo)
180
+ result.should respond_to(:a_method)
181
+
182
+ terminal_failures = parser.terminal_failures
183
+ terminal_failures.size.should == 1
184
+ failure = terminal_failures.first
185
+ failure.index.should == 12
186
+ failure.expected_string.should == 'foo'
187
+ end
188
+ end
189
+ end
190
+
191
+ end