walrus 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/bin/walrus +29 -14
  2. data/ext/extconf.rb +16 -0
  3. data/ext/{jindex/jindex.c → jindex.c} +27 -14
  4. data/lib/walrus/additions/string.rb +11 -5
  5. data/lib/walrus/compile_error.rb +11 -5
  6. data/lib/walrus/compiler.rb +12 -6
  7. data/lib/walrus/contrib/spec/walruscloth_spec.rb +11 -5
  8. data/lib/walrus/contrib/walruscloth.rb +11 -5
  9. data/lib/walrus/diff.rb +11 -5
  10. data/lib/walrus/document.rb +11 -5
  11. data/lib/walrus/grammar/additions/proc.rb +11 -5
  12. data/lib/walrus/grammar/additions/regexp.rb +11 -5
  13. data/lib/walrus/grammar/additions/string.rb +11 -5
  14. data/lib/walrus/grammar/additions/symbol.rb +12 -5
  15. data/lib/walrus/grammar/and_predicate.rb +11 -5
  16. data/lib/walrus/grammar/array_result.rb +11 -5
  17. data/lib/walrus/grammar/continuation_wrapper_exception.rb +11 -5
  18. data/lib/walrus/grammar/left_recursion_exception.rb +11 -5
  19. data/lib/walrus/grammar/location_tracking.rb +19 -9
  20. data/lib/walrus/grammar/match_data_wrapper.rb +11 -5
  21. data/lib/walrus/grammar/memoizing.rb +11 -5
  22. data/lib/walrus/grammar/memoizing_cache.rb +18 -9
  23. data/lib/walrus/grammar/node.rb +11 -5
  24. data/lib/walrus/grammar/not_predicate.rb +11 -5
  25. data/lib/walrus/grammar/parse_error.rb +11 -5
  26. data/lib/walrus/grammar/parser_state.rb +11 -5
  27. data/lib/walrus/grammar/parslet.rb +11 -5
  28. data/lib/walrus/grammar/parslet_choice.rb +15 -7
  29. data/lib/walrus/grammar/parslet_combination.rb +11 -5
  30. data/lib/walrus/grammar/parslet_combining.rb +11 -5
  31. data/lib/walrus/grammar/parslet_merge.rb +11 -5
  32. data/lib/walrus/grammar/parslet_omission.rb +11 -5
  33. data/lib/walrus/grammar/parslet_repetition.rb +17 -8
  34. data/lib/walrus/grammar/parslet_repetition_default.rb +11 -5
  35. data/lib/walrus/grammar/parslet_sequence.rb +20 -8
  36. data/lib/walrus/grammar/predicate.rb +11 -5
  37. data/lib/walrus/grammar/proc_parslet.rb +11 -5
  38. data/lib/walrus/grammar/regexp_parslet.rb +11 -5
  39. data/lib/walrus/grammar/skipped_substring_exception.rb +11 -5
  40. data/lib/walrus/grammar/string_enumerator.rb +14 -6
  41. data/lib/walrus/grammar/string_parslet.rb +11 -5
  42. data/lib/walrus/grammar/string_result.rb +11 -5
  43. data/lib/walrus/grammar/symbol_parslet.rb +11 -5
  44. data/lib/walrus/grammar.rb +11 -5
  45. data/lib/walrus/no_parameter_marker.rb +11 -5
  46. data/lib/walrus/parser.rb +12 -6
  47. data/lib/walrus/runner.rb +60 -67
  48. data/lib/walrus/template.rb +15 -7
  49. data/lib/walrus/version.rb +3 -0
  50. data/lib/walrus/walrus_grammar/assignment_expression.rb +11 -5
  51. data/lib/walrus/walrus_grammar/block_directive.rb +11 -5
  52. data/lib/walrus/walrus_grammar/comment.rb +11 -5
  53. data/lib/walrus/walrus_grammar/def_directive.rb +16 -8
  54. data/lib/walrus/walrus_grammar/echo_directive.rb +11 -5
  55. data/lib/walrus/walrus_grammar/escape_sequence.rb +11 -5
  56. data/lib/walrus/walrus_grammar/import_directive.rb +11 -5
  57. data/lib/walrus/walrus_grammar/include_directive.rb +11 -5
  58. data/lib/walrus/walrus_grammar/instance_variable.rb +11 -5
  59. data/lib/walrus/walrus_grammar/literal.rb +11 -5
  60. data/lib/walrus/walrus_grammar/message_expression.rb +11 -5
  61. data/lib/walrus/walrus_grammar/multiline_comment.rb +11 -5
  62. data/lib/walrus/walrus_grammar/placeholder.rb +11 -5
  63. data/lib/walrus/walrus_grammar/raw_directive.rb +11 -5
  64. data/lib/walrus/walrus_grammar/raw_text.rb +11 -5
  65. data/lib/walrus/walrus_grammar/ruby_directive.rb +11 -5
  66. data/lib/walrus/walrus_grammar/ruby_expression.rb +11 -5
  67. data/lib/walrus/walrus_grammar/set_directive.rb +11 -5
  68. data/lib/walrus/walrus_grammar/silent_directive.rb +11 -5
  69. data/lib/walrus/walrus_grammar/slurp_directive.rb +11 -5
  70. data/lib/walrus/walrus_grammar/super_directive.rb +11 -5
  71. data/lib/walrus.rb +30 -16
  72. metadata +91 -211
  73. data/ext/jindex/extconf.rb +0 -11
  74. data/ext/mkdtemp/extconf.rb +0 -11
  75. data/ext/mkdtemp/mkdtemp.c +0 -41
  76. data/lib/walrus/additions/module.rb +0 -36
  77. data/lib/walrus/additions/test/unit/error_collector.rb +0 -62
  78. data/spec/acceptance/acceptance_spec.rb +0 -97
  79. data/spec/acceptance/block/basic_block.expected +0 -1
  80. data/spec/acceptance/block/basic_block.tmpl +0 -3
  81. data/spec/acceptance/block/nested_blocks.expected +0 -5
  82. data/spec/acceptance/block/nested_blocks.tmpl +0 -11
  83. data/spec/acceptance/comments/comments_and_text.expected +0 -3
  84. data/spec/acceptance/comments/comments_and_text.tmpl +0 -6
  85. data/spec/acceptance/comments/single_comment.expected +0 -0
  86. data/spec/acceptance/comments/single_comment.tmpl +0 -1
  87. data/spec/acceptance/def/alternative_def_calling_conventions.expected +0 -3
  88. data/spec/acceptance/def/alternative_def_calling_conventions.tmpl +0 -18
  89. data/spec/acceptance/def/basic_def_block_no_output.expected +0 -0
  90. data/spec/acceptance/def/basic_def_block_no_output.tmpl +0 -17
  91. data/spec/acceptance/def/defs_can_be_called_multiple_times.expected +0 -3
  92. data/spec/acceptance/def/defs_can_be_called_multiple_times.tmpl +0 -6
  93. data/spec/acceptance/def/defs_can_be_dynamic.expected +0 -4
  94. data/spec/acceptance/def/defs_can_be_dynamic.tmpl +0 -12
  95. data/spec/acceptance/echo/echo_directive_with_numeric_literal.expected +0 -1
  96. data/spec/acceptance/echo/echo_directive_with_numeric_literal.tmpl +0 -1
  97. data/spec/acceptance/echo/echo_expression_list.expected +0 -1
  98. data/spec/acceptance/echo/echo_expression_list.tmpl +0 -1
  99. data/spec/acceptance/echo/echo_short_notation.expected +0 -1
  100. data/spec/acceptance/echo/echo_short_notation.tmpl +0 -1
  101. data/spec/acceptance/echo/echo_simple_expression.expected +0 -1
  102. data/spec/acceptance/echo/echo_simple_expression.tmpl +0 -1
  103. data/spec/acceptance/echo/echo_single_quoted_string_literal.expected +0 -1
  104. data/spec/acceptance/echo/echo_single_quoted_string_literal.tmpl +0 -1
  105. data/spec/acceptance/echo/multiple_echo_statements.expected +0 -1
  106. data/spec/acceptance/echo/multiple_echo_statements.tmpl +0 -2
  107. data/spec/acceptance/includes/basic_included_file.txt +0 -1
  108. data/spec/acceptance/includes/basic_includer.complex +0 -3
  109. data/spec/acceptance/includes/basic_includer.expected +0 -3
  110. data/spec/acceptance/includes/basic_includer.rb +0 -38
  111. data/spec/acceptance/includes/complicated_included_file.txt +0 -3
  112. data/spec/acceptance/includes/complicated_includer.complex +0 -3
  113. data/spec/acceptance/includes/complicated_includer.expected +0 -3
  114. data/spec/acceptance/includes/complicated_includer.rb +0 -41
  115. data/spec/acceptance/includes/nested_include_1.txt +0 -3
  116. data/spec/acceptance/includes/nested_include_2.txt +0 -1
  117. data/spec/acceptance/includes/nested_includer.complex +0 -3
  118. data/spec/acceptance/includes/nested_includer.expected +0 -4
  119. data/spec/acceptance/includes/nested_includer.rb +0 -41
  120. data/spec/acceptance/inheritance/basic_child.complex +0 -10
  121. data/spec/acceptance/inheritance/basic_child.expected +0 -9
  122. data/spec/acceptance/inheritance/basic_child.rb +0 -54
  123. data/spec/acceptance/inheritance/basic_parent.complex +0 -5
  124. data/spec/acceptance/inheritance/basic_parent.expected +0 -3
  125. data/spec/acceptance/inheritance/basic_parent.rb +0 -41
  126. data/spec/acceptance/inheritance/importing_child.complex +0 -8
  127. data/spec/acceptance/inheritance/importing_child.expected +0 -7
  128. data/spec/acceptance/inheritance/importing_child.rb +0 -46
  129. data/spec/acceptance/inheritance/subdirectory/importing_child_in_subdirectory.complex +0 -8
  130. data/spec/acceptance/inheritance/subdirectory/importing_child_in_subdirectory.expected +0 -7
  131. data/spec/acceptance/inheritance/subdirectory/importing_child_in_subdirectory.rb +0 -44
  132. data/spec/acceptance/multiline_comments/multiline_comment_with_directives_inside.expected +0 -0
  133. data/spec/acceptance/multiline_comments/multiline_comment_with_directives_inside.tmpl +0 -15
  134. data/spec/acceptance/multiline_comments/simple_multiline_comment.expected +0 -2
  135. data/spec/acceptance/multiline_comments/simple_multiline_comment.tmpl +0 -4
  136. data/spec/acceptance/raw/complicated_raw_example.expected +0 -57
  137. data/spec/acceptance/raw/complicated_raw_example.tmpl +0 -79
  138. data/spec/acceptance/raw-text/UTF_8.expected +0 -12
  139. data/spec/acceptance/raw-text/UTF_8.tmpl +0 -12
  140. data/spec/acceptance/raw-text/empty_file.expected +0 -0
  141. data/spec/acceptance/raw-text/empty_file.tmpl +0 -0
  142. data/spec/acceptance/raw-text/multi_line.expected +0 -4
  143. data/spec/acceptance/raw-text/multi_line.tmpl +0 -4
  144. data/spec/acceptance/raw-text/single_line.expected +0 -1
  145. data/spec/acceptance/raw-text/single_line.tmpl +0 -1
  146. data/spec/acceptance/raw-text/single_line_whitespace.expected +0 -1
  147. data/spec/acceptance/raw-text/single_line_whitespace.tmpl +0 -1
  148. data/spec/acceptance/ruby/ruby_directive_is_just_like_silent.expected +0 -1
  149. data/spec/acceptance/ruby/ruby_directive_is_just_like_silent.tmpl +0 -4
  150. data/spec/acceptance/ruby/ruby_directive_using_here_doc.expected +0 -1
  151. data/spec/acceptance/ruby/ruby_directive_using_here_doc.tmpl +0 -4
  152. data/spec/acceptance/ruby/ruby_directive_using_here_doc_alt_syntax.expected +0 -1
  153. data/spec/acceptance/ruby/ruby_directive_using_here_doc_alt_syntax.tmpl +0 -4
  154. data/spec/acceptance/ruby/ruby_directive_with_accumulate.expected +0 -1
  155. data/spec/acceptance/ruby/ruby_directive_with_accumulate.tmpl +0 -4
  156. data/spec/acceptance/ruby/ruby_directive_with_accumulate_and_block.expected +0 -1
  157. data/spec/acceptance/ruby/ruby_directive_with_accumulate_and_block.tmpl +0 -6
  158. data/spec/acceptance/set/unused_set.expected +0 -0
  159. data/spec/acceptance/set/unused_set.tmpl +0 -1
  160. data/spec/acceptance/set/used_set.expected +0 -1
  161. data/spec/acceptance/set/used_set.tmpl +0 -2
  162. data/spec/acceptance/silent/silent_and_echo_combined.expected +0 -1
  163. data/spec/acceptance/silent/silent_and_echo_combined.tmpl +0 -2
  164. data/spec/acceptance/silent/silent_short_notation.expected +0 -1
  165. data/spec/acceptance/silent/silent_short_notation.tmpl +0 -1
  166. data/spec/acceptance/silent/simple_silent_directive.expected +0 -0
  167. data/spec/acceptance/silent/simple_silent_directive.tmpl +0 -1
  168. data/spec/acceptance/slurp/basic_slurp_demo.expected +0 -1
  169. data/spec/acceptance/slurp/basic_slurp_demo.tmpl +0 -4
  170. data/spec/acceptance/super/super_with_no_effect.expected +0 -4
  171. data/spec/acceptance/super/super_with_no_effect.tmpl +0 -5
  172. data/spec/additions/module_spec.rb +0 -126
  173. data/spec/additions/string_spec.rb +0 -99
  174. data/spec/compiler_spec.rb +0 -55
  175. data/spec/grammar/additions/proc_spec.rb +0 -25
  176. data/spec/grammar/additions/regexp_spec.rb +0 -37
  177. data/spec/grammar/additions/string_spec.rb +0 -106
  178. data/spec/grammar/and_predicate_spec.rb +0 -29
  179. data/spec/grammar/continuation_wrapper_exception_spec.rb +0 -23
  180. data/spec/grammar/match_data_wrapper_spec.rb +0 -41
  181. data/spec/grammar/memoizing_cache_spec.rb +0 -112
  182. data/spec/grammar/node_spec.rb +0 -126
  183. data/spec/grammar/not_predicate_spec.rb +0 -29
  184. data/spec/grammar/parser_state_spec.rb +0 -172
  185. data/spec/grammar/parslet_choice_spec.rb +0 -49
  186. data/spec/grammar/parslet_combining_spec.rb +0 -287
  187. data/spec/grammar/parslet_merge_spec.rb +0 -33
  188. data/spec/grammar/parslet_omission_spec.rb +0 -58
  189. data/spec/grammar/parslet_repetition_spec.rb +0 -77
  190. data/spec/grammar/parslet_sequence_spec.rb +0 -49
  191. data/spec/grammar/parslet_spec.rb +0 -23
  192. data/spec/grammar/predicate_spec.rb +0 -53
  193. data/spec/grammar/proc_parslet_spec.rb +0 -52
  194. data/spec/grammar/regexp_parslet_spec.rb +0 -347
  195. data/spec/grammar/string_enumerator_spec.rb +0 -94
  196. data/spec/grammar/string_parslet_spec.rb +0 -143
  197. data/spec/grammar/symbol_parslet_spec.rb +0 -30
  198. data/spec/grammar_spec.rb +0 -545
  199. data/spec/parser_spec.rb +0 -1418
  200. data/spec/spec_helper.rb +0 -34
  201. data/spec/walrus_grammar/comment_spec.rb +0 -39
  202. data/spec/walrus_grammar/echo_directive_spec.rb +0 -63
  203. data/spec/walrus_grammar/escape_sequence_spec.rb +0 -85
  204. data/spec/walrus_grammar/literal_spec.rb +0 -41
  205. data/spec/walrus_grammar/message_expression_spec.rb +0 -37
  206. data/spec/walrus_grammar/multiline_comment_spec.rb +0 -58
  207. data/spec/walrus_grammar/placeholder_spec.rb +0 -48
  208. data/spec/walrus_grammar/raw_directive_spec.rb +0 -81
  209. data/spec/walrus_grammar/raw_text_spec.rb +0 -65
  210. data/spec/walrus_grammar/silent_directive_spec.rb +0 -34
data/spec/grammar_spec.rb DELETED
@@ -1,545 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is distributed in the hope that it will be useful, but WITHOUT
3
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4
- # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5
- # in the accompanying file, "LICENSE.txt", for more details.
6
- #
7
- # $Id: /mirrors/Walrus/trunk/walrus/spec/grammar_spec.rb 6702 2007-04-09T15:04:40.448669Z wincent $
8
-
9
- require File.join(File.dirname(__FILE__), 'spec_helper.rb')
10
-
11
- module Walrus
12
- class Grammar
13
-
14
- describe 'defining a grammar subclass' do
15
-
16
- it 'should be able to create new Grammar subclasses on the fly' do
17
-
18
- # first create a new subclass and make sure the returned instance is non-nil
19
- Grammar.subclass('MyGrammar').should_not be_nil
20
-
21
- # the class constant should now be available for creating new instances
22
- MyGrammar.new.should_not be_nil
23
-
24
- end
25
-
26
- it 'should complain if an attempt is made to create the same subclass twice' do
27
- lambda { Grammar.subclass('FooGrammar') }.should_not raise_error
28
- lambda { Grammar.subclass('FooGrammar') }.should raise_error
29
- end
30
-
31
- it 'should complain if subclass name is nil' do
32
- lambda { Grammar.subclass(nil) }.should raise_error(ArgumentError)
33
- end
34
-
35
- it 'should be able to pass a block while defining a new subclass' do
36
-
37
- instance = Grammar.subclass('TestGrammar') do
38
- starting_symbol :foo
39
- end
40
- instance.instance_eval("@starting_symbol").should == :foo
41
-
42
- end
43
-
44
- end
45
-
46
- describe 'defining rules in a grammar' do
47
-
48
- it '"rules" method should complain if either parameter is nil' do
49
- lambda { Grammar.subclass('AxeGrammar') { rule nil, 'expression' } }.should raise_error(ArgumentError)
50
- lambda { Grammar.subclass('BoneGrammar') { rule :my_rule, nil } }.should raise_error(ArgumentError)
51
- lambda { Grammar.subclass('CatGrammar') { rule nil, nil } }.should raise_error(ArgumentError)
52
- end
53
-
54
- it '"rules" method should complain if an attempt is made to define a rule a second time' do
55
- lambda do
56
- Grammar.subclass('DogGrammar') do
57
- rule :my_rule, 'foo'
58
- rule :my_rule, 'bar'
59
- end
60
- end.should raise_error(ArgumentError)
61
- end
62
-
63
- it 'should be able to define rules in the block using the "rule" method' do
64
-
65
- end
66
-
67
- end
68
-
69
- describe 'defining productions in a grammar' do
70
-
71
- it '"node" method should complain if new class name is nil' do
72
- lambda do
73
- Grammar.subclass('NodeComplainingGrammar') { node nil }
74
- end.should raise_error(ArgumentError)
75
- end
76
-
77
- it 'should be able to define a simple Node subclass using the "node" function' do
78
-
79
- Grammar.subclass('NodeGrammar1') do
80
- node :my_node_subclass
81
- node :my_subclass_of_a_subclass, :my_node_subclass
82
- node :my_node_class_that_takes_params, :node, :foo, :bar
83
- end
84
-
85
- NodeGrammar1::MyNodeSubclass.to_s.should == 'Walrus::NodeGrammar1::MyNodeSubclass'
86
- NodeGrammar1::MyNodeSubclass.superclass.should == Node
87
- NodeGrammar1::MySubclassOfASubclass.to_s.should == 'Walrus::NodeGrammar1::MySubclassOfASubclass'
88
- NodeGrammar1::MySubclassOfASubclass.superclass.should == NodeGrammar1::MyNodeSubclass
89
- NodeGrammar1::MyNodeClassThatTakesParams.to_s.should == 'Walrus::NodeGrammar1::MyNodeClassThatTakesParams'
90
- NodeGrammar1::MyNodeClassThatTakesParams.superclass.should == Node
91
- node = NodeGrammar1::MyNodeClassThatTakesParams.new('hello', 'world')
92
- node.foo.should == 'hello'
93
- node.bar.should == 'world'
94
-
95
- end
96
-
97
- it 'should be able to use the "build" method to define production subclasses on the fly' do
98
-
99
- Grammar.subclass('HeMeansJavaRuntimeAPIs') do
100
- rule :foobar, 'foo' & 'bar'
101
- production :foobar.build(:node, :foo, :bar)
102
- end
103
-
104
- # try instantiating the newly created class
105
- node = HeMeansJavaRuntimeAPIs::Foobar.new('hello', 'world')
106
- node.class.should == HeMeansJavaRuntimeAPIs::Foobar
107
- node.foo.should == 'hello'
108
- node.bar.should == 'world'
109
-
110
- # try passing the wrong number of parameters
111
- lambda { HeMeansJavaRuntimeAPIs::Foobar.new }.should raise_error(ArgumentError) # no parameters
112
- lambda { HeMeansJavaRuntimeAPIs::Foobar.new('hi') }.should raise_error(ArgumentError) # one parameter too few
113
- lambda { HeMeansJavaRuntimeAPIs::Foobar.new('a', 'b', 'c') }.should raise_error(ArgumentError) # one parameter too many
114
-
115
- end
116
-
117
- it 'should complain if an attempt is made to create the same production class twice' do
118
- lambda do
119
- Grammar.subclass('HowToGetControlOfJavaAwayFromSun') do
120
- rule :foo, 'foo'
121
- production :foo
122
- production :foo
123
- end
124
- end.should raise_error(ArgumentError)
125
- end
126
-
127
- it 'should complain if an attempt is made to create a production for a rule that does not exist yet' do
128
- lambda do
129
- Grammar.subclass('GettingControlOfJavaAwayFromSun') { production :foo }
130
- end.should raise_error(ArgumentError)
131
- end
132
-
133
- end
134
-
135
- describe 'parsing using a grammar' do
136
-
137
- it 'should complain if asked to parse a nil string' do
138
- lambda { Grammar.subclass('BobGrammar').parse(nil) }.should raise_error(ArgumentError)
139
- end
140
-
141
- it 'should complain if trying to parse without first defining a start symbol' do
142
- lambda { Grammar.subclass('RoyalGrammar').parse('foo') }.should raise_error
143
- end
144
-
145
- it 'should parse starting with the start symbol' do
146
- grammar = Grammar.subclass('AliceGrammar') do
147
- rule :expr, /\w+/
148
- starting_symbol :expr
149
- end
150
-
151
- grammar.parse('foo').should == 'foo'
152
- lambda { grammar.parse('') }.should raise_error(ParseError)
153
-
154
- end
155
-
156
- it 'should complain if reference is made to an undefined symbol' do
157
- grammar = Grammar.subclass('RoyGrammar') { starting_symbol :expr } # :expr is not defined
158
- lambda { grammar.parse('foo') }.should raise_error
159
- end
160
-
161
- it 'should be able to parse using a simple grammar (one rule)' do
162
- grammar = Grammar.subclass('SimpleGrammar') do
163
- starting_symbol :foo
164
- rule :foo, 'foo!'
165
- end
166
- grammar.parse('foo!').should == 'foo!'
167
- lambda { grammar.parse('---') }.should raise_error(ParseError)
168
- end
169
-
170
- it 'should be able to parse using a simple grammar (two rules)' do
171
- grammar = Grammar.subclass('AlmostAsSimpleGrammar') do
172
- starting_symbol :foo
173
- rule :foo, 'foo!' | :bar
174
- rule :bar, /bar/
175
- end
176
- grammar.parse('foo!').should == 'foo!'
177
- grammar.parse('bar').should == 'bar'
178
- lambda { grammar.parse('---') }.should raise_error(ParseError)
179
- end
180
-
181
- it 'should be able to parse using a simple grammar (three rules)' do
182
-
183
- # a basic version written using intermediary parslets (really two parslets and one rule)
184
- grammar = Grammar.subclass('MacGrammar') do
185
- starting_symbol :comment
186
-
187
- # parslets
188
- comment_marker = '##'
189
- comment_body = /.+/
190
-
191
- # rules
192
- rule :comment, comment_marker & comment_body.optional
193
- end
194
- grammar.parse('## hello!').should == ['##', ' hello!']
195
- grammar.parse('##').should == '##'
196
- lambda { grammar.parse('foobar') }.should raise_error(ParseError)
197
-
198
- # the same grammar rewritten without intermediary parslets (three rules, no standalone parslets)
199
- grammar = Grammar.subclass('MacAltGrammar') do
200
- starting_symbol :comment
201
- rule :comment, :comment_marker & :comment_body.optional
202
- rule :comment_marker, '##'
203
- rule :comment_body, /.+/
204
- end
205
- grammar.parse('## hello!').should == ['##', ' hello!']
206
- grammar.parse('##').should == '##'
207
- lambda { grammar.parse('foobar') }.should raise_error(ParseError)
208
- end
209
-
210
- it 'should be able to parse using recursive rules (nested parentheses)' do
211
-
212
- # basic example
213
- grammar = Grammar.subclass('NestedGrammar') do
214
- starting_symbol :bracket_expression
215
- rule :left_bracket, '('
216
- rule :right_bracket, ')'
217
- rule :bracket_content, (/[^()]+/ | :bracket_expression).zero_or_more
218
- rule :bracket_expression, :left_bracket & :bracket_content.optional & :right_bracket
219
- end
220
- grammar.parse('()').should == ['(', ')']
221
- grammar.parse('(content)').should == ['(', 'content', ')']
222
- grammar.parse('(content (and more content))').should == ['(', ['content ', ['(', 'and more content', ')']], ')']
223
- lambda { grammar.parse('(') }.should raise_error(ParseError)
224
-
225
- # same example but automatically skipping the delimiting braces for clearer output
226
- grammar = Grammar.subclass('NestedSkippingGrammar') do
227
- starting_symbol :bracket_expression
228
- rule :bracket_expression, '('.skip & (/[^()]+/ | :bracket_expression).zero_or_more & ')'.skip
229
- end
230
- grammar.parse('()').should == []
231
- grammar.parse('(content)').should == 'content'
232
- grammar.parse('(content (and more content))').should == ['content ', 'and more content']
233
- grammar.parse('(content (and more content)(and more))').should == ['content ', 'and more content', 'and more']
234
- grammar.parse('(content (and more content)(and more)(more still))').should == ['content ', 'and more content', 'and more', 'more still']
235
- grammar.parse('(content (and more content)(and more(more still)))').should == ['content ', 'and more content', ['and more', 'more still']]
236
- lambda { grammar.parse('(') }.should raise_error(ParseError)
237
-
238
- # note that this confusing (possible even misleading) nesting goes away if you use a proper AST
239
- grammar = Grammar.subclass('NestedBracketsWithAST') do
240
- starting_symbol :bracket_expression
241
- rule :text_expression, /[^()]+/
242
- rule :bracket_expression, '('.skip & (:text_expression | :bracket_expression).zero_or_more & ')'.skip
243
- production :bracket_expression.build(:node, :children)
244
- end
245
-
246
- # simple tests
247
- grammar.parse('()').children.should == []
248
- grammar.parse('(content)').children.to_s.should == 'content'
249
-
250
- # nested test: two expressions at the first level, one of them nested
251
- results = grammar.parse('(content (and more content))')
252
- results.children[0].should == 'content '
253
- results.children[1].children.to_s.should == 'and more content'
254
-
255
- # nested test: three expressions at first level, two of them nested
256
- results = grammar.parse('(content (and more content)(and more))')#.should == ['content ', 'and more content', 'and more']
257
- results.children[0].should == 'content '
258
- results.children[1].children.should == 'and more content'
259
- results.children[2].children.should == 'and more'
260
-
261
- # nested test: four expressions at the first level, three of them nested
262
- results = grammar.parse('(content (and more content)(and more)(more still))')
263
- results.children[0].should == 'content '
264
- results.children[1].children.should == 'and more content'
265
- results.children[2].children.should == 'and more'
266
- results.children[3].children.should == 'more still'
267
-
268
- # nested test: three expressions at the first level, one nested and another not only nested but containing another level of nesting
269
- results = grammar.parse('(content (and more content)(and more(more still)))')
270
- results.children[0].should == 'content '
271
- results.children[1].children.should == 'and more content'
272
- results.children[2].children[0].should == 'and more'
273
- results.children[2].children[1].children.should == 'more still'
274
-
275
- # bad input case
276
- lambda { grammar.parse('(') }.should raise_error(ParseError)
277
-
278
- end
279
-
280
- it 'should be able to parse using recursive rules (nested comments)' do
281
- grammar = Grammar.subclass('NestedCommentsGrammar') do
282
- starting_symbol :comment
283
- rule :comment_start, '/*'
284
- rule :comment_end, '*/'
285
- rule :comment_content, (:comment | /\/+/ | ('*' & '/'.not!) | /[^*\/]+/).zero_or_more
286
- rule :comment, '/*' & :comment_content.optional & '*/'
287
- end
288
- grammar.parse('/**/').should == ['/*', '*/']
289
- grammar.parse('/*comment*/').should == ['/*', 'comment', '*/']
290
- grammar.parse('/* comment /* nested */*/').should == ['/*', [' comment ', ['/*', ' nested ', '*/']], '*/']
291
- lambda { grammar.parse('/*') }.should raise_error(ParseError)
292
- end
293
-
294
- it 'should be able to write a grammar that produces an AST for a simple language that supports addition and assignment' do
295
-
296
- grammar = Grammar.subclass('SimpleASTLanguage') do
297
-
298
- starting_symbol :expression
299
-
300
- # terminal tokens
301
- rule :identifier, /[a-zA-Z_][a-zA-Z0-9_]*/
302
- production :identifier.build(:node)
303
- rule :integer_literal, /[0-9]+/
304
- production :integer_literal.build(:node)
305
-
306
- # expressions
307
- rule :expression, :assignment_expression | :addition_expression | :identifier | :integer_literal
308
- node :expression
309
- rule :assignment_expression, :identifier & '='.skip & :expression
310
- production :assignment_expression.build(:expression, :target, :value)
311
- rule :addition_expression, (:identifier | :integer_literal) & '+'.skip & :expression
312
- production :addition_expression.build(:expression, :summee, :summor)
313
-
314
- end
315
-
316
- results = grammar.parse('hello')
317
- results.should be_kind_of(SimpleASTLanguage::Identifier)
318
- results.lexeme.should == 'hello'
319
-
320
- results = grammar.parse('1234')
321
- results.should be_kind_of(SimpleASTLanguage::IntegerLiteral)
322
- results.lexeme.should == '1234'
323
-
324
- results = grammar.parse('foo=bar')
325
- results.should be_kind_of(SimpleASTLanguage::Expression)
326
- results.should be_kind_of(SimpleASTLanguage::AssignmentExpression)
327
- results.target.should be_kind_of(SimpleASTLanguage::Identifier)
328
- results.target.lexeme.should == 'foo'
329
- results.value.should be_kind_of(SimpleASTLanguage::Identifier)
330
- results.value.lexeme.should == 'bar'
331
-
332
- results = grammar.parse('baz+123')
333
- results.should be_kind_of(SimpleASTLanguage::Expression)
334
- results.should be_kind_of(SimpleASTLanguage::AdditionExpression)
335
- results.summee.should be_kind_of(SimpleASTLanguage::Identifier)
336
- results.summee.lexeme.should == 'baz'
337
- results.summor.should be_kind_of(SimpleASTLanguage::IntegerLiteral)
338
- results.summor.lexeme.should == '123'
339
-
340
- results = grammar.parse('foo=abc+123')
341
- results.should be_kind_of(SimpleASTLanguage::Expression)
342
- results.should be_kind_of(SimpleASTLanguage::AssignmentExpression)
343
- results.target.should be_kind_of(SimpleASTLanguage::Identifier)
344
- results.target.lexeme.should == 'foo'
345
- results.value.should be_kind_of(SimpleASTLanguage::AdditionExpression)
346
- results.value.summee.should be_kind_of(SimpleASTLanguage::Identifier)
347
- results.value.summee.lexeme.should == 'abc'
348
- results.value.summor.should be_kind_of(SimpleASTLanguage::IntegerLiteral)
349
- results.value.summor.lexeme.should == '123'
350
-
351
- results = grammar.parse('a+b+2')
352
- results.should be_kind_of(SimpleASTLanguage::Expression)
353
- results.should be_kind_of(SimpleASTLanguage::AdditionExpression)
354
- results.summee.should be_kind_of(SimpleASTLanguage::Identifier)
355
- results.summee.lexeme.should == 'a'
356
- results.summor.should be_kind_of(SimpleASTLanguage::AdditionExpression)
357
- results.summor.summee.should be_kind_of(SimpleASTLanguage::Identifier)
358
- results.summor.summee.lexeme.should == 'b'
359
- results.summor.summor.should be_kind_of(SimpleASTLanguage::IntegerLiteral)
360
- results.summor.summor.lexeme.should == '2'
361
-
362
- end
363
-
364
- it 'should be able to write a grammar that complains if all the input is not consumed' do
365
- grammar = Grammar.subclass('ComplainingGrammar') do
366
- starting_symbol :translation_unit
367
- rule :translation_unit, :word_list & :end_of_string.and? | :end_of_string
368
- rule :end_of_string, /\z/
369
- rule :whitespace, /\s+/
370
- rule :word, /[a-z]+/
371
- rule :word_list, :word >> (:whitespace.skip & :word).zero_or_more
372
-
373
- end
374
-
375
- grammar.parse('').should == ''
376
- grammar.parse('foo').should == 'foo'
377
- grammar.parse('foo bar').should == ['foo', 'bar']
378
- lambda { grammar.parse('...') }.should raise_error(ParseError)
379
- lambda { grammar.parse('foo...') }.should raise_error(ParseError)
380
- lambda { grammar.parse('foo bar...') }.should raise_error(ParseError)
381
-
382
- end
383
-
384
- it 'should be able to define a default parslet for intertoken skipping' do
385
-
386
- # simple example
387
- grammar = Grammar.subclass('SkippingGrammar') do
388
- starting_symbol :translation_unit
389
- skipping :whitespace_and_newlines
390
- rule :whitespace_and_newlines, /[\s\n\r]+/
391
- rule :translation_unit, :word_list & :end_of_string.and? | :end_of_string
392
- rule :end_of_string, /\z/
393
- rule :word_list, :word.zero_or_more
394
- rule :word, /[a-z0-9_]+/
395
- end
396
-
397
- # not sure if I can justify the difference in behaviour here compared with the previous grammar
398
- # if I catch these throws at the grammar level I can return nil
399
- # but note that the previous grammar returns an empty array, which to_s is just ""
400
- lambda { grammar.parse('') }.should throw_symbol(:AndPredicateSuccess)
401
-
402
- grammar.parse('foo').should == 'foo'
403
- grammar.parse('foo bar').should == ['foo', 'bar'] # intervening whitespace
404
- grammar.parse('foo bar ').should == ['foo', 'bar'] # trailing whitespace
405
- grammar.parse(' foo bar').should == ['foo', 'bar'] # leading whitespace
406
-
407
- # additional example, this time involving the ">>" pseudo-operator
408
- grammar = Grammar.subclass('SkippingAndMergingGrammar') do
409
- starting_symbol :translation_unit
410
- skipping :whitespace_and_newlines
411
- rule :whitespace_and_newlines, /[\s\n\r]+/
412
- rule :translation_unit, :word_list & :end_of_string.and? | :end_of_string
413
- rule :end_of_string, /\z/
414
- rule :word_list, :word >> (','.skip & :word).zero_or_more
415
- rule :word, /[a-z0-9_]+/
416
- end
417
-
418
- # one word
419
- grammar.parse('foo').should == 'foo'
420
-
421
- # two words
422
- grammar.parse('foo,bar').should == ['foo', 'bar'] # no whitespace
423
- grammar.parse('foo, bar').should == ['foo', 'bar'] # whitespace after
424
- grammar.parse('foo ,bar').should == ['foo', 'bar'] # whitespace before
425
- grammar.parse('foo , bar').should == ['foo', 'bar'] # whitespace before and after
426
- grammar.parse('foo , bar ').should == ['foo', 'bar'] # trailing and embedded whitespace
427
- grammar.parse(' foo , bar').should == ['foo', 'bar'] # leading and embedded whitespace
428
-
429
- # three or four words
430
- grammar.parse('foo , bar, baz').should == ['foo', 'bar', 'baz']
431
- grammar.parse(' foo , bar, baz ,bin').should == ['foo', 'bar', 'baz', 'bin']
432
-
433
- end
434
-
435
- it 'should complain if trying to set default skipping parslet more than once' do
436
- lambda do
437
- Grammar.subclass('SetSkipperTwice') do
438
- skipping :first # fine
439
- skipping :again # should raise here
440
- end
441
- end.should raise_error
442
- end
443
-
444
- it 'should complain if passed nil' do
445
- lambda do
446
- Grammar.subclass('PassNilToSkipping') { skipping nil }
447
- end.should raise_error(ArgumentError)
448
- end
449
-
450
- it 'should be able to override default skipping parslet on a per-rule basis' do
451
-
452
- # the example grammar parses word lists and number lists
453
- grammar = Grammar.subclass('OverrideDefaultSkippingParslet') do
454
- starting_symbol :translation_unit
455
- skipping :whitespace_and_newlines
456
- rule :whitespace_and_newlines, /\s+/ # any whitespace including newlines
457
- rule :whitespace, /[ \t\v]+/ # literally only spaces, tabs, not newlines etc
458
- rule :translation_unit, :component.one_or_more & :end_of_string.and? | :end_of_string
459
- rule :end_of_string, /\z/
460
- rule :component, :word_list | :number_list
461
- rule :word_list, :word.one_or_more
462
- rule :word, /[a-z]+/
463
- rule :number, /[0-9]+/
464
-
465
- # the interesting bit: we override the skipping rule for number lists
466
- rule :number_list, :number.one_or_more
467
- skipping :number_list, :whitespace # only whitespace, no newlines
468
- end
469
-
470
- # words in word lists can be separated by whitespace or newlines
471
- grammar.parse('hello world').should == ['hello', 'world']
472
- grammar.parse("hello\nworld").should == ['hello', 'world']
473
- grammar.parse("hello world\nworld hello").should == ['hello', 'world', 'world', 'hello']
474
-
475
- # numbers in number lists may be separated only by whitespace, not newlines
476
- grammar.parse('123 456').should == ['123', '456']
477
- grammar.parse("123\n456").should == ['123', '456'] # this succeeds because parser treats them as two separate number lists
478
- grammar.parse("123 456\n456 123").should == [['123', '456'], ['456', '123']]
479
-
480
- # intermixing word lists and number lists
481
- grammar.parse("bar\n123").should == ['bar', '123']
482
- grammar.parse("123\n456\nbar").should == ['123', '456', 'bar']
483
-
484
- # these were buggy at one point: "123\n456" was getting mashed into "123456" due to misguided use of String#delete! to delete first newline
485
- grammar.parse("\n123\n456").should == ['123', '456']
486
- grammar.parse("bar\n123\n456").should == ['bar', '123', '456']
487
- grammar.parse("baz bar\n123\n456").should == [['baz', 'bar'], '123', '456']
488
- grammar.parse("hello world\nfoo\n123 456 baz bar\n123\n456").should == [['hello', 'world', 'foo'], ['123', '456'], ['baz', 'bar'], '123', '456']
489
-
490
- end
491
-
492
- it 'should complain if trying to override the default for the same rule twice' do
493
- lambda do
494
- Grammar.subclass('OverrideSameRuleTwice') do
495
- rule :the_rule, 'foo'
496
- skipping :the_rule, :the_override # fine
497
- skipping :the_rule, :the_override # should raise
498
- end
499
- end.should raise_error(ArgumentError)
500
- end
501
-
502
- it "should complain if trying to set an override for a rule that hasn't been defined yet" do
503
- lambda do
504
- Grammar.subclass('OverrideUndefinedRule') { skipping :non_existent_rule, :the_override }
505
- end.should raise_error(ArgumentError)
506
- end
507
-
508
- it 'use of the "skipping" directive should play nicely with predicates' do
509
-
510
- # example 1: word + predicate
511
- grammar = Grammar.subclass('NicePlayer') do
512
- starting_symbol :foo
513
- skipping :whitespace
514
- rule :whitespace, /[ \t\v]+/
515
- rule :foo, 'hello' & 'world'.and?
516
- end
517
-
518
- grammar.parse('hello world').should == 'hello'
519
- grammar.parse('hello world').should == 'hello'
520
- grammar.parse('helloworld').should == 'hello'
521
- lambda { grammar.parse('hello') }.should raise_error(ParseError)
522
- lambda { grammar.parse('hello buddy') }.should raise_error(ParseError)
523
- lambda { grammar.parse("hello\nbuddy") }.should raise_error(ParseError)
524
-
525
- # example 2: word + predicate + other word
526
- grammar = Grammar.subclass('NicePlayer2') do
527
- starting_symbol :foo
528
- skipping :whitespace
529
- rule :whitespace, /[ \t\v]+/
530
- rule :foo, /hel../ & 'world'.and? & /\w+/
531
- end
532
-
533
- grammar.parse('hello world').should == ['hello', 'world']
534
- grammar.parse('hello world').should == ['hello', 'world']
535
- grammar.parse('helloworld').should == ['hello', 'world']
536
- lambda { grammar.parse('hello') }.should raise_error(ParseError)
537
- lambda { grammar.parse('hello buddy') }.should raise_error(ParseError)
538
- lambda { grammar.parse("hello\nbuddy") }.should raise_error(ParseError)
539
-
540
- end
541
-
542
- end
543
- end # class Grammar
544
- end # module Walrus
545
-