walrus 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. data/bin/walrus +44 -0
  2. data/ext/jindex/extconf.rb +11 -0
  3. data/ext/jindex/jindex.c +79 -0
  4. data/ext/mkdtemp/extconf.rb +11 -0
  5. data/ext/mkdtemp/mkdtemp.c +41 -0
  6. data/lib/walrus/additions/module.rb +36 -0
  7. data/lib/walrus/additions/string.rb +37 -0
  8. data/lib/walrus/additions/test/unit/error_collector.rb +62 -0
  9. data/lib/walrus/compile_error.rb +28 -0
  10. data/lib/walrus/compiler.rb +124 -0
  11. data/lib/walrus/contrib/spec/walruscloth_spec.rb +32 -0
  12. data/lib/walrus/contrib/walruscloth.rb +82 -0
  13. data/lib/walrus/diff.rb +89 -0
  14. data/lib/walrus/document.rb +98 -0
  15. data/lib/walrus/grammar/additions/proc.rb +20 -0
  16. data/lib/walrus/grammar/additions/regexp.rb +21 -0
  17. data/lib/walrus/grammar/additions/string.rb +52 -0
  18. data/lib/walrus/grammar/additions/symbol.rb +42 -0
  19. data/lib/walrus/grammar/and_predicate.rb +40 -0
  20. data/lib/walrus/grammar/array_result.rb +19 -0
  21. data/lib/walrus/grammar/continuation_wrapper_exception.rb +28 -0
  22. data/lib/walrus/grammar/left_recursion_exception.rb +27 -0
  23. data/lib/walrus/grammar/location_tracking.rb +105 -0
  24. data/lib/walrus/grammar/match_data_wrapper.rb +65 -0
  25. data/lib/walrus/grammar/memoizing.rb +41 -0
  26. data/lib/walrus/grammar/memoizing_cache.rb +94 -0
  27. data/lib/walrus/grammar/node.rb +60 -0
  28. data/lib/walrus/grammar/not_predicate.rb +40 -0
  29. data/lib/walrus/grammar/parse_error.rb +39 -0
  30. data/lib/walrus/grammar/parser_state.rb +181 -0
  31. data/lib/walrus/grammar/parslet.rb +28 -0
  32. data/lib/walrus/grammar/parslet_choice.rb +120 -0
  33. data/lib/walrus/grammar/parslet_combination.rb +26 -0
  34. data/lib/walrus/grammar/parslet_combining.rb +154 -0
  35. data/lib/walrus/grammar/parslet_merge.rb +88 -0
  36. data/lib/walrus/grammar/parslet_omission.rb +57 -0
  37. data/lib/walrus/grammar/parslet_repetition.rb +97 -0
  38. data/lib/walrus/grammar/parslet_repetition_default.rb +58 -0
  39. data/lib/walrus/grammar/parslet_sequence.rb +202 -0
  40. data/lib/walrus/grammar/predicate.rb +57 -0
  41. data/lib/walrus/grammar/proc_parslet.rb +52 -0
  42. data/lib/walrus/grammar/regexp_parslet.rb +73 -0
  43. data/lib/walrus/grammar/skipped_substring_exception.rb +36 -0
  44. data/lib/walrus/grammar/string_enumerator.rb +45 -0
  45. data/lib/walrus/grammar/string_parslet.rb +75 -0
  46. data/lib/walrus/grammar/string_result.rb +24 -0
  47. data/lib/walrus/grammar/symbol_parslet.rb +63 -0
  48. data/lib/walrus/grammar.rb +170 -0
  49. data/lib/walrus/no_parameter_marker.rb +19 -0
  50. data/lib/walrus/parser.rb +420 -0
  51. data/lib/walrus/runner.rb +356 -0
  52. data/lib/walrus/template.rb +75 -0
  53. data/lib/walrus/walrus_grammar/assignment_expression.rb +24 -0
  54. data/lib/walrus/walrus_grammar/block_directive.rb +28 -0
  55. data/lib/walrus/walrus_grammar/comment.rb +24 -0
  56. data/lib/walrus/walrus_grammar/def_directive.rb +64 -0
  57. data/lib/walrus/walrus_grammar/echo_directive.rb +44 -0
  58. data/lib/walrus/walrus_grammar/escape_sequence.rb +24 -0
  59. data/lib/walrus/walrus_grammar/import_directive.rb +44 -0
  60. data/lib/walrus/walrus_grammar/include_directive.rb +27 -0
  61. data/lib/walrus/walrus_grammar/instance_variable.rb +24 -0
  62. data/lib/walrus/walrus_grammar/literal.rb +24 -0
  63. data/lib/walrus/walrus_grammar/message_expression.rb +25 -0
  64. data/lib/walrus/walrus_grammar/multiline_comment.rb +54 -0
  65. data/lib/walrus/walrus_grammar/placeholder.rb +40 -0
  66. data/lib/walrus/walrus_grammar/raw_directive.rb +42 -0
  67. data/lib/walrus/walrus_grammar/raw_text.rb +45 -0
  68. data/lib/walrus/walrus_grammar/ruby_directive.rb +29 -0
  69. data/lib/walrus/walrus_grammar/ruby_expression.rb +31 -0
  70. data/lib/walrus/walrus_grammar/set_directive.rb +24 -0
  71. data/lib/walrus/walrus_grammar/silent_directive.rb +44 -0
  72. data/lib/walrus/walrus_grammar/slurp_directive.rb +25 -0
  73. data/lib/walrus/walrus_grammar/super_directive.rb +27 -0
  74. data/lib/walrus.rb +64 -0
  75. data/spec/acceptance/acceptance_spec.rb +97 -0
  76. data/spec/acceptance/block/basic_block.expected +1 -0
  77. data/spec/acceptance/block/basic_block.tmpl +3 -0
  78. data/spec/acceptance/block/nested_blocks.expected +5 -0
  79. data/spec/acceptance/block/nested_blocks.tmpl +11 -0
  80. data/spec/acceptance/comments/comments_and_text.expected +3 -0
  81. data/spec/acceptance/comments/comments_and_text.tmpl +6 -0
  82. data/spec/acceptance/comments/single_comment.expected +0 -0
  83. data/spec/acceptance/comments/single_comment.tmpl +1 -0
  84. data/spec/acceptance/def/alternative_def_calling_conventions.expected +3 -0
  85. data/spec/acceptance/def/alternative_def_calling_conventions.tmpl +18 -0
  86. data/spec/acceptance/def/basic_def_block_no_output.expected +0 -0
  87. data/spec/acceptance/def/basic_def_block_no_output.tmpl +17 -0
  88. data/spec/acceptance/def/defs_can_be_called_multiple_times.expected +3 -0
  89. data/spec/acceptance/def/defs_can_be_called_multiple_times.tmpl +6 -0
  90. data/spec/acceptance/def/defs_can_be_dynamic.expected +4 -0
  91. data/spec/acceptance/def/defs_can_be_dynamic.tmpl +12 -0
  92. data/spec/acceptance/echo/echo_directive_with_numeric_literal.expected +1 -0
  93. data/spec/acceptance/echo/echo_directive_with_numeric_literal.tmpl +1 -0
  94. data/spec/acceptance/echo/echo_expression_list.expected +1 -0
  95. data/spec/acceptance/echo/echo_expression_list.tmpl +1 -0
  96. data/spec/acceptance/echo/echo_short_notation.expected +1 -0
  97. data/spec/acceptance/echo/echo_short_notation.tmpl +1 -0
  98. data/spec/acceptance/echo/echo_simple_expression.expected +1 -0
  99. data/spec/acceptance/echo/echo_simple_expression.tmpl +1 -0
  100. data/spec/acceptance/echo/echo_single_quoted_string_literal.expected +1 -0
  101. data/spec/acceptance/echo/echo_single_quoted_string_literal.tmpl +1 -0
  102. data/spec/acceptance/echo/multiple_echo_statements.expected +1 -0
  103. data/spec/acceptance/echo/multiple_echo_statements.tmpl +2 -0
  104. data/spec/acceptance/includes/basic_included_file.txt +1 -0
  105. data/spec/acceptance/includes/basic_includer.complex +3 -0
  106. data/spec/acceptance/includes/basic_includer.expected +3 -0
  107. data/spec/acceptance/includes/basic_includer.rb +38 -0
  108. data/spec/acceptance/includes/complicated_included_file.txt +3 -0
  109. data/spec/acceptance/includes/complicated_includer.complex +3 -0
  110. data/spec/acceptance/includes/complicated_includer.expected +3 -0
  111. data/spec/acceptance/includes/complicated_includer.rb +41 -0
  112. data/spec/acceptance/includes/nested_include_1.txt +3 -0
  113. data/spec/acceptance/includes/nested_include_2.txt +1 -0
  114. data/spec/acceptance/includes/nested_includer.complex +3 -0
  115. data/spec/acceptance/includes/nested_includer.expected +4 -0
  116. data/spec/acceptance/includes/nested_includer.rb +41 -0
  117. data/spec/acceptance/inheritance/basic_child.complex +10 -0
  118. data/spec/acceptance/inheritance/basic_child.expected +9 -0
  119. data/spec/acceptance/inheritance/basic_child.rb +54 -0
  120. data/spec/acceptance/inheritance/basic_parent.complex +5 -0
  121. data/spec/acceptance/inheritance/basic_parent.expected +3 -0
  122. data/spec/acceptance/inheritance/basic_parent.rb +41 -0
  123. data/spec/acceptance/inheritance/importing_child.complex +8 -0
  124. data/spec/acceptance/inheritance/importing_child.expected +7 -0
  125. data/spec/acceptance/inheritance/importing_child.rb +46 -0
  126. data/spec/acceptance/inheritance/subdirectory/importing_child_in_subdirectory.complex +8 -0
  127. data/spec/acceptance/inheritance/subdirectory/importing_child_in_subdirectory.expected +7 -0
  128. data/spec/acceptance/inheritance/subdirectory/importing_child_in_subdirectory.rb +44 -0
  129. data/spec/acceptance/multiline_comments/multiline_comment_with_directives_inside.expected +0 -0
  130. data/spec/acceptance/multiline_comments/multiline_comment_with_directives_inside.tmpl +15 -0
  131. data/spec/acceptance/multiline_comments/simple_multiline_comment.expected +2 -0
  132. data/spec/acceptance/multiline_comments/simple_multiline_comment.tmpl +4 -0
  133. data/spec/acceptance/raw/complicated_raw_example.expected +57 -0
  134. data/spec/acceptance/raw/complicated_raw_example.tmpl +79 -0
  135. data/spec/acceptance/raw-text/UTF_8.expected +12 -0
  136. data/spec/acceptance/raw-text/UTF_8.tmpl +12 -0
  137. data/spec/acceptance/raw-text/empty_file.expected +0 -0
  138. data/spec/acceptance/raw-text/empty_file.tmpl +0 -0
  139. data/spec/acceptance/raw-text/multi_line.expected +4 -0
  140. data/spec/acceptance/raw-text/multi_line.tmpl +4 -0
  141. data/spec/acceptance/raw-text/single_line.expected +1 -0
  142. data/spec/acceptance/raw-text/single_line.tmpl +1 -0
  143. data/spec/acceptance/raw-text/single_line_whitespace.expected +1 -0
  144. data/spec/acceptance/raw-text/single_line_whitespace.tmpl +1 -0
  145. data/spec/acceptance/ruby/ruby_directive_is_just_like_silent.expected +1 -0
  146. data/spec/acceptance/ruby/ruby_directive_is_just_like_silent.tmpl +4 -0
  147. data/spec/acceptance/ruby/ruby_directive_using_here_doc.expected +1 -0
  148. data/spec/acceptance/ruby/ruby_directive_using_here_doc.tmpl +4 -0
  149. data/spec/acceptance/ruby/ruby_directive_using_here_doc_alt_syntax.expected +1 -0
  150. data/spec/acceptance/ruby/ruby_directive_using_here_doc_alt_syntax.tmpl +4 -0
  151. data/spec/acceptance/ruby/ruby_directive_with_accumulate.expected +1 -0
  152. data/spec/acceptance/ruby/ruby_directive_with_accumulate.tmpl +4 -0
  153. data/spec/acceptance/ruby/ruby_directive_with_accumulate_and_block.expected +1 -0
  154. data/spec/acceptance/ruby/ruby_directive_with_accumulate_and_block.tmpl +6 -0
  155. data/spec/acceptance/set/unused_set.expected +0 -0
  156. data/spec/acceptance/set/unused_set.tmpl +1 -0
  157. data/spec/acceptance/set/used_set.expected +1 -0
  158. data/spec/acceptance/set/used_set.tmpl +2 -0
  159. data/spec/acceptance/silent/silent_and_echo_combined.expected +1 -0
  160. data/spec/acceptance/silent/silent_and_echo_combined.tmpl +2 -0
  161. data/spec/acceptance/silent/silent_short_notation.expected +1 -0
  162. data/spec/acceptance/silent/silent_short_notation.tmpl +1 -0
  163. data/spec/acceptance/silent/simple_silent_directive.expected +0 -0
  164. data/spec/acceptance/silent/simple_silent_directive.tmpl +1 -0
  165. data/spec/acceptance/slurp/basic_slurp_demo.expected +1 -0
  166. data/spec/acceptance/slurp/basic_slurp_demo.tmpl +4 -0
  167. data/spec/acceptance/super/super_with_no_effect.expected +4 -0
  168. data/spec/acceptance/super/super_with_no_effect.tmpl +5 -0
  169. data/spec/additions/module_spec.rb +126 -0
  170. data/spec/additions/string_spec.rb +99 -0
  171. data/spec/compiler_spec.rb +55 -0
  172. data/spec/grammar/additions/proc_spec.rb +25 -0
  173. data/spec/grammar/additions/regexp_spec.rb +37 -0
  174. data/spec/grammar/additions/string_spec.rb +106 -0
  175. data/spec/grammar/and_predicate_spec.rb +29 -0
  176. data/spec/grammar/continuation_wrapper_exception_spec.rb +23 -0
  177. data/spec/grammar/match_data_wrapper_spec.rb +41 -0
  178. data/spec/grammar/memoizing_cache_spec.rb +112 -0
  179. data/spec/grammar/node_spec.rb +126 -0
  180. data/spec/grammar/not_predicate_spec.rb +29 -0
  181. data/spec/grammar/parser_state_spec.rb +172 -0
  182. data/spec/grammar/parslet_choice_spec.rb +49 -0
  183. data/spec/grammar/parslet_combining_spec.rb +287 -0
  184. data/spec/grammar/parslet_merge_spec.rb +33 -0
  185. data/spec/grammar/parslet_omission_spec.rb +58 -0
  186. data/spec/grammar/parslet_repetition_spec.rb +77 -0
  187. data/spec/grammar/parslet_sequence_spec.rb +49 -0
  188. data/spec/grammar/parslet_spec.rb +23 -0
  189. data/spec/grammar/predicate_spec.rb +53 -0
  190. data/spec/grammar/proc_parslet_spec.rb +52 -0
  191. data/spec/grammar/regexp_parslet_spec.rb +347 -0
  192. data/spec/grammar/string_enumerator_spec.rb +94 -0
  193. data/spec/grammar/string_parslet_spec.rb +143 -0
  194. data/spec/grammar/symbol_parslet_spec.rb +30 -0
  195. data/spec/grammar_spec.rb +545 -0
  196. data/spec/parser_spec.rb +1418 -0
  197. data/spec/spec_helper.rb +34 -0
  198. data/spec/walrus_grammar/comment_spec.rb +39 -0
  199. data/spec/walrus_grammar/echo_directive_spec.rb +63 -0
  200. data/spec/walrus_grammar/escape_sequence_spec.rb +85 -0
  201. data/spec/walrus_grammar/literal_spec.rb +41 -0
  202. data/spec/walrus_grammar/message_expression_spec.rb +37 -0
  203. data/spec/walrus_grammar/multiline_comment_spec.rb +58 -0
  204. data/spec/walrus_grammar/placeholder_spec.rb +48 -0
  205. data/spec/walrus_grammar/raw_directive_spec.rb +81 -0
  206. data/spec/walrus_grammar/raw_text_spec.rb +65 -0
  207. data/spec/walrus_grammar/silent_directive_spec.rb +34 -0
  208. metadata +291 -0
@@ -0,0 +1,27 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that the class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class IncludeDirective
15
+
16
+ def compile(options = {})
17
+ inner, outer = options[:compiler_instance].compile_subtree(subtree)
18
+ inner = [] if inner.nil?
19
+ inner.unshift "\# Include (start): #{file_name.to_s}:\n"
20
+ [inner, outer]
21
+ end
22
+
23
+ end # class IncludeDirective
24
+
25
+ end # class WalrusGrammar
26
+ end # Walrus
27
+
@@ -0,0 +1,24 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class InstanceVariable
15
+
16
+ def compile(options = {})
17
+ @lexeme.source_text
18
+ end
19
+
20
+ end # class InstanceVariable
21
+
22
+ end # class WalrusGrammar
23
+ end # Walrus
24
+
@@ -0,0 +1,24 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class Literal
15
+
16
+ def compile(options = {})
17
+ @lexeme.source_text
18
+ end
19
+
20
+ end # class Literal
21
+
22
+ end # class WalrusGrammar
23
+ end # Walrus
24
+
@@ -0,0 +1,25 @@
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/lib/walrus/walrus_grammar/message_expression.rb 6701 2007-04-09T14:55:55.352472Z wincent $
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class MessageExpression
15
+
16
+ def compile(options = {})
17
+ # simple case
18
+ @target.source_text + '.' + @message.source_text
19
+ end
20
+
21
+ end # class AssignmentExpression
22
+
23
+ end # class WalrusGrammar
24
+ end # Walrus
25
+
@@ -0,0 +1,54 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class MultilineComment
15
+
16
+ # Multiline comments may containing nested Comments/Multiline comments or normal text, so must compile recursively.
17
+ # TODO: anchor comments that appear immediately before #def and #block directives to their corresponding methods (for the timebeing should note in the documentation that if you want your comments to appear adjacent to the blocks which follow them then you must put your comments inside the blocks)
18
+ def compile(options = {})
19
+ compiled = ''
20
+ if @content.respond_to? :each
21
+ @content.each do |item|
22
+ if item.kind_of? Comment
23
+ compiled << '# (nested) ' + item.compile
24
+ else
25
+ first = true
26
+ item.to_s.each do |line|
27
+ if first
28
+ first = false
29
+ compiled << '# MultilineComment:' + line.to_s.chomp + "\n"
30
+ else
31
+ compiled << '# MultilineComment (continued):' + line.to_s.chomp + "\n"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ else # no nesting, just raw text, but still must check for multiple lines
37
+ first = true
38
+ @content.to_s.each do |line|
39
+ if first
40
+ first = false
41
+ compiled << '# MultilineComment:' + line.to_s.chomp + "\n"
42
+ else
43
+ compiled << '# MultilineComment (continued):' + line.to_s.chomp + "\n"
44
+ end
45
+ end
46
+ end
47
+ compiled
48
+ end
49
+
50
+ end
51
+
52
+ end # class WalrusGrammar
53
+ end # Walrus
54
+
@@ -0,0 +1,40 @@
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/lib/walrus/walrus_grammar/placeholder.rb 6721 2007-04-10T16:10:41.583472Z wincent $
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class Placeholder
15
+
16
+ def compile(options = {})
17
+
18
+ if options[:nest_placeholders] == true
19
+ method_name = "lookup_and_return_placeholder" # placeholder nested inside another placeholder
20
+ else
21
+ method_name = "lookup_and_accumulate_placeholder" # placeholder used in a literal text stream
22
+ end
23
+
24
+ if @params == []
25
+ "#{method_name}(#{@name.to_s.to_sym.inspect})\n"
26
+ else
27
+ options = options.clone
28
+ options[:nest_placeholders] = true
29
+ params = (@params.kind_of? Array) ? @params : [@params]
30
+ param_list = params.collect { |param| param.compile(options) }.join(', ').chomp
31
+ "#{method_name}(#{@name.to_s.to_sym.inspect}, #{param_list})\n"
32
+ end
33
+
34
+ end
35
+
36
+ end # class Placeholder
37
+
38
+ end # class WalrusGrammar
39
+ end # Walrus
40
+
@@ -0,0 +1,42 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class RawDirective
15
+
16
+ # Returns a string containing the compiled (Ruby) version of receiver.
17
+ def compile(options = {})
18
+ compiled = []
19
+ first = true
20
+ @content.to_s.to_source_string.each do |line|
21
+ newline = ''
22
+ if line =~ /(\r\n|\r|\n)\z/ # check for literal newline at end of line
23
+ line.chomp! # get rid of it
24
+ newline = ' + ' + $~[0].dump # include non-literal newline instead
25
+ end
26
+
27
+ if first
28
+ compiled << "accumulate('%s'%s) # RawDirective\n" % [ line, newline ]
29
+ first = false
30
+ else
31
+ compiled << "accumulate('%s'%s) # RawDirective (continued)\n" % [ line, newline ]
32
+ end
33
+
34
+ end
35
+ compiled.join
36
+ end
37
+
38
+ end # class RawDirective
39
+
40
+ end # class WalrusGrammar
41
+ end # Walrus
42
+
@@ -0,0 +1,45 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class RawText
15
+
16
+ # Returns a string containing the compiled (Ruby) version of receiver.
17
+ # If options[:slurping] is true, instructs the receiver to strip the leading carriage return/line feed from the @lexeme prior to emitting the compiled output.
18
+ def compile(options = {})
19
+ lexeme = options[:slurping] ? @lexeme.to_s.sub(/\A(\r\n|\r|\n)/, '') : @lexeme.to_s
20
+
21
+ compiled = ''
22
+ first = true
23
+ lexeme.to_source_string.each do |line|
24
+ newline = ''
25
+ if line =~ /(\r\n|\r|\n)\z/ # check for literal newline at end of line
26
+ line.chomp! # get rid of it
27
+ newline = ' + ' + $~[0].dump # include non-literal newline instead
28
+ end
29
+
30
+ if first
31
+ compiled << "accumulate('%s'%s) # RawText\n" % [ line, newline ]
32
+ first = false
33
+ else
34
+ compiled << "accumulate('%s'%s) # RawText (continued)\n" % [ line, newline ]
35
+ end
36
+
37
+ end
38
+ compiled
39
+ end
40
+
41
+ end # class RawText
42
+
43
+ end # class WalrusGrammar
44
+ end # Walrus
45
+
@@ -0,0 +1,29 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class RubyDirective
15
+
16
+ # TODO: could make a #rubyecho method that did an "accumulate do" instead of instance_eval
17
+ def compile(options = {})
18
+ # possible problem here is that the compiler will indent each line for us, possibly breaking here docs etc
19
+ # seeing as it is going to be indented anyway, I add some additional indenting here for pretty printing purposes
20
+ compiled = "instance_eval do # Ruby directive\n"
21
+ @content.to_s.each { |line| compiled << ' ' + line }
22
+ compiled << "end\n"
23
+ end
24
+
25
+ end # class RawDirective
26
+
27
+ end # class WalrusGrammar
28
+ end # Walrus
29
+
@@ -0,0 +1,31 @@
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/lib/walrus/walrus_grammar/ruby_expression.rb 6701 2007-04-09T14:55:55.352472Z wincent $
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class RubyExpression
15
+
16
+ # Rather than just compiling Ruby expressions back to text in a generic fashion it is desirable
17
+ # to compile the pieces separately (for an example, see the AssignmentExpression class) because
18
+ # this allows us to handle placeholders embedded inside Ruby expressions.
19
+ #
20
+ # Nevertheless, at an abstract level we here supply a default compilation method which just
21
+ # returns the source text, suitable for evaluation. Subclasses can then override this as new
22
+ # cases are discovered which require piece-by-piece compilation.
23
+ def compile(options = {})
24
+ source_text
25
+ end
26
+
27
+ end # class AssignmentExpression
28
+
29
+ end # class WalrusGrammar
30
+ end # Walrus
31
+
@@ -0,0 +1,24 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class SetDirective
15
+
16
+ def compile(options = {})
17
+ "set_value(%s, instance_eval { %s }) # Set directive \n" % [ @placeholder.to_s.dump, @expression.compile ]
18
+ end
19
+
20
+ end # class SetDirective
21
+
22
+ end # class WalrusGrammar
23
+ end # Walrus
24
+
@@ -0,0 +1,44 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that RawText class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class SilentDirective
15
+
16
+ def compile(options = {})
17
+
18
+ if @expression.respond_to? :each
19
+ expression = @expression
20
+ else
21
+ expression = [@expression]
22
+ end
23
+
24
+ # TODO: potentially include line, col and file name info in the comments generated by the compiler
25
+
26
+ compiled = ''
27
+ first = true
28
+ expression.each do |expr|
29
+ if first
30
+ compiled << "instance_eval { %s } # Silent directive\n" % expr.compile
31
+ first = false
32
+ else
33
+ compiled << "instance_eval { %s } # Silent directive (continued)\n" % expr.compile
34
+ end
35
+ end
36
+ compiled
37
+
38
+ end
39
+
40
+ end # class SilentDirective
41
+
42
+ end # class WalrusGrammar
43
+ end # Walrus
44
+
@@ -0,0 +1,25 @@
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$
8
+
9
+ require 'walrus/parser.rb' # make sure that the class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class SlurpDirective
15
+
16
+ # The slurp directive produces no meaningful output; but we leave a comment in the compiled template so that the location of the directive is visible in the source.
17
+ def compile(options = {})
18
+ "# Slurp directive\n"
19
+ end
20
+
21
+ end # class SlurpDirective
22
+
23
+ end # class WalrusGrammar
24
+ end # Walrus
25
+
@@ -0,0 +1,27 @@
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/lib/walrus/walrus_grammar/super_directive.rb 6707 2007-04-09T18:59:29.617466Z wincent $
8
+
9
+ require 'walrus/parser.rb' # make sure that the class has been defined prior to extending it
10
+
11
+ module Walrus
12
+ class WalrusGrammar
13
+
14
+ class SuperDirective
15
+
16
+ def compile(options = {})
17
+
18
+ # basic case, no explicit parameters
19
+ "super # Super directive\n"
20
+
21
+ end
22
+
23
+ end # class SuperDirective
24
+
25
+ end # class WalrusGrammar
26
+ end # Walrus
27
+
data/lib/walrus.rb ADDED
@@ -0,0 +1,64 @@
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$
8
+
9
+ require 'jcode' # jlength method
10
+ $KCODE = 'U' # UTF-8 (necessary for Unicode support)
11
+
12
+ module Walrus
13
+
14
+ VERSION = '0.1'
15
+
16
+ autoload(:CompileError, 'walrus/compile_error')
17
+ autoload(:Compiler, 'walrus/compiler')
18
+ autoload(:Grammar, 'walrus/grammar')
19
+ autoload(:Parser, 'walrus/parser')
20
+ autoload(:NoParameterMarker, 'walrus/no_parameter_marker')
21
+ autoload(:Template, 'walrus/template')
22
+
23
+ class Grammar
24
+
25
+ autoload(:AndPredicate, 'walrus/grammar/and_predicate')
26
+ autoload(:ArrayResult, 'walrus/grammar/array_result')
27
+ autoload(:ContinuationWrapperException, 'walrus/grammar/continuation_wrapper_exception')
28
+ autoload(:LeftRecursionException, 'walrus/grammar/left_recursion_exception')
29
+ autoload(:LocationTracking, 'walrus/grammar/location_tracking')
30
+ autoload(:MatchDataWrapper, 'walrus/grammar/match_data_wrapper')
31
+ autoload(:Memoizing, 'walrus/grammar/memoizing')
32
+ autoload(:MemoizingCache, 'walrus/grammar/memoizing_cache')
33
+ autoload(:NotPredicate, 'walrus/grammar/not_predicate')
34
+ autoload(:Node, 'walrus/grammar/node')
35
+ autoload(:ParseError, 'walrus/grammar/parse_error')
36
+ autoload(:ParserState, 'walrus/grammar/parser_state')
37
+ autoload(:Parslet, 'walrus/grammar/parslet')
38
+ autoload(:ParsletChoice, 'walrus/grammar/parslet_choice')
39
+ autoload(:ParsletCombination, 'walrus/grammar/parslet_combination')
40
+ autoload(:ParsletCombining, 'walrus/grammar/parslet_combining')
41
+ autoload(:ParsletMerge, 'walrus/grammar/parslet_merge')
42
+ autoload(:ParsletOmission, 'walrus/grammar/parslet_omission')
43
+ autoload(:ParsletRepetition, 'walrus/grammar/parslet_repetition')
44
+ autoload(:ParsletRepetitionDefault, 'walrus/grammar/parslet_repetition_default')
45
+ autoload(:ParsletSequence, 'walrus/grammar/parslet_sequence')
46
+ autoload(:Predicate, 'walrus/grammar/predicate')
47
+ autoload(:ProcParslet, 'walrus/grammar/proc_parslet')
48
+ autoload(:RegexpParslet, 'walrus/grammar/regexp_parslet')
49
+ autoload(:SkippedSubstringException, 'walrus/grammar/skipped_substring_exception')
50
+ autoload(:StringEnumerator, 'walrus/grammar/string_enumerator')
51
+ autoload(:StringParslet, 'walrus/grammar/string_parslet')
52
+ autoload(:StringResult, 'walrus/grammar/string_result')
53
+ autoload(:SymbolParslet, 'walrus/grammar/symbol_parslet')
54
+
55
+ end
56
+
57
+ end # module Walrus
58
+
59
+ require 'walrus/additions/module'
60
+ require 'walrus/additions/string'
61
+ require 'walrus/grammar/additions/proc'
62
+ require 'walrus/grammar/additions/regexp'
63
+ require 'walrus/grammar/additions/string'
64
+ require 'walrus/grammar/additions/symbol'
@@ -0,0 +1,97 @@
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/acceptance/acceptance_spec.rb 6739 2007-04-11T17:09:13.916915Z wincent $
8
+
9
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
10
+ require 'walrus/parser' # ensure that WalrusGrammar is defined before continuing
11
+ require 'mkdtemp'
12
+
13
+ module Walrus
14
+
15
+ class WalrusGrammar
16
+
17
+ # This spec performs high-level acceptance testing by running Walrus on the sample templates in the subdirectories of the "spec/acceptance/" directory and comparing them with the expected output.
18
+ describe 'processing test files with Walrus' do
19
+
20
+ # construct an array of absolute paths indicating the location of all testable templates.
21
+ template_paths = Dir[File.join(File.dirname(__FILE__), '**/*.tmpl')].collect { |template| Pathname.new(template).realpath }
22
+
23
+ # make temporary output dirs for storing compiled templates
24
+ manually_compiled_templates = Pathname.new(Dir.mkdtemp('/tmp/walrus.acceptance.XXXXXX'))
25
+ walrus_compiled_templates = Pathname.new(Dir.mkdtemp('/tmp/walrus.acceptance.XXXXXX'))
26
+ parser = Parser.new
27
+ search_additions = "#{ENV['RUBYLIB']}:#{Walrus::SpecHelper::LIBDIR}:#{Walrus::SpecHelper::EXTDIR}"
28
+
29
+ template_paths.each do |path|
30
+
31
+ template = Template.new(path)
32
+ compiled = nil
33
+ it 'template should compile (source file: #{path})' do
34
+ compiled = template.compile
35
+ end
36
+ next if compiled.nil? # compiled will be nil if the compilation spec failed
37
+
38
+ expected_output = IO.read(path.to_s.sub(/\.tmpl\z/i, ".expected"))
39
+
40
+ it "actual output should match expected output evaluating dynamically (source file: #{path})" do
41
+ actual_output = template.fill
42
+ actual_output.should == expected_output
43
+ end
44
+
45
+ it "actual output should match expected output running compiled file in subshell (source file: #{path})" do
46
+ target_path = manually_compiled_templates.join(path.basename(path.extname).to_s + '.rb')
47
+ File.open(target_path, 'w+') { |file| file.puts compiled }
48
+ actual_output = `ruby -I#{Walrus::SpecHelper::LIBDIR} -I#{Walrus::SpecHelper::EXTDIR} #{target_path}`
49
+ actual_output.should == expected_output
50
+ end
51
+
52
+ it "actual output should match expected output using 'walrus' commandline tool (source file: #{path})" do
53
+ `env RUBYLIB='#{search_additions}' #{Walrus::SpecHelper::TOOL} fill --output-dir '#{walrus_compiled_templates}' '#{path}'`
54
+ dir, base = path.split
55
+ dir = dir.to_s.sub(/\A\//, '') if dir.absolute? # and always will be absolute
56
+ base = base.basename(base.extname).to_s + '.html'
57
+ actual_output = IO.read(walrus_compiled_templates + dir + base)
58
+ actual_output.should == expected_output
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ # These templates have a different extension to keep them separate from the other acceptance tests.
66
+ describe 'processing multiple-interdependent files with Walrus' do
67
+
68
+ template_paths = Dir[File.join(File.dirname(__FILE__), '**/*.complex')].collect { |template| Pathname.new(template).realpath }
69
+ output_dir = Pathname.new(Dir.mkdtemp('/tmp/walrus.acceptance.XXXXXX'))
70
+ parser = Parser.new
71
+
72
+ search_additions = "#{ENV['RUBYLIB']}:#{Walrus::SpecHelper::LIBDIR}:#{Walrus::SpecHelper::EXTDIR}"
73
+
74
+ template_paths.each do |path|
75
+ it 'should be able to compile all the templates' do
76
+ `env RUBYLIB='#{search_additions}' #{Walrus::SpecHelper::TOOL} compile --input-extension complex --output-dir '#{output_dir}' '#{path}'`
77
+ end
78
+ end
79
+
80
+ template_paths.each do |path|
81
+ it 'should be able to fill all the templates' do
82
+ `env RUBYLIB='#{search_additions}' #{Walrus::SpecHelper::TOOL} fill --input-extension complex --output-dir '#{output_dir}' '#{path}'`
83
+ dir, base = path.split
84
+ dir = dir.to_s.sub(/\A\//, '') if dir.absolute? # and always will be absolute
85
+ base = base.basename(base.extname).to_s + '.html'
86
+ actual_output = IO.read(output_dir + dir + base)
87
+ expected_output = IO.read(path.to_s.sub(/\.complex\z/i, ".expected"))
88
+ actual_output.should == expected_output
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ end # module Walrus
97
+
@@ -0,0 +1 @@
1
+ This will be output by default.
@@ -0,0 +1,3 @@
1
+ #block body
2
+ This will be output by default.
3
+ #end
@@ -0,0 +1,5 @@
1
+ [This is the header]
2
+
3
+ Main text...
4
+
5
+ [This is the footer]
@@ -0,0 +1,11 @@
1
+ #block body
2
+ #block header
3
+ [This is the header]
4
+ #end
5
+
6
+ Main text...
7
+
8
+ #block footer
9
+ [This is the footer]
10
+ #end
11
+ #end
@@ -0,0 +1,3 @@
1
+ This file contains comments
2
+ which should not appear in the template output...
3
+ In the final output, this should be the third line...