walrus 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/bin/walrus +19 -12
  2. data/lib/walrus.rb +27 -70
  3. data/lib/walrus/additions/string.rb +21 -35
  4. data/lib/walrus/compile_error.rb +19 -16
  5. data/lib/walrus/compiler.rb +66 -55
  6. data/lib/walrus/contrib/spec/walruscloth_spec.rb +21 -17
  7. data/lib/walrus/contrib/walruscloth.rb +19 -11
  8. data/lib/walrus/document.rb +41 -33
  9. data/lib/walrus/grammar.rb +474 -162
  10. data/lib/walrus/grammar/assignment_expression.rb +33 -0
  11. data/lib/walrus/grammar/block_directive.rb +37 -0
  12. data/lib/walrus/grammar/comment.rb +33 -0
  13. data/lib/walrus/grammar/def_directive.rb +80 -0
  14. data/lib/walrus/grammar/echo_directive.rb +56 -0
  15. data/lib/walrus/grammar/escape_sequence.rb +33 -0
  16. data/lib/walrus/grammar/import_directive.rb +54 -0
  17. data/lib/walrus/grammar/include_directive.rb +36 -0
  18. data/lib/walrus/grammar/instance_variable.rb +33 -0
  19. data/lib/walrus/grammar/literal.rb +33 -0
  20. data/lib/walrus/grammar/message_expression.rb +34 -0
  21. data/lib/walrus/grammar/multiline_comment.rb +70 -0
  22. data/lib/walrus/grammar/placeholder.rb +47 -0
  23. data/lib/walrus/grammar/raw_directive.rb +50 -0
  24. data/lib/walrus/grammar/raw_text.rb +56 -0
  25. data/lib/walrus/grammar/ruby_directive.rb +41 -0
  26. data/lib/walrus/grammar/ruby_expression.rb +42 -0
  27. data/lib/walrus/grammar/set_directive.rb +34 -0
  28. data/lib/walrus/grammar/silent_directive.rb +51 -0
  29. data/lib/walrus/grammar/slurp_directive.rb +36 -0
  30. data/lib/walrus/grammar/super_directive.rb +34 -0
  31. data/lib/walrus/parser.rb +26 -408
  32. data/lib/walrus/runner.rb +37 -20
  33. data/lib/walrus/template.rb +34 -25
  34. data/lib/walrus/version.rb +24 -1
  35. metadata +57 -71
  36. data/ext/extconf.rb +0 -16
  37. data/ext/jindex.c +0 -92
  38. data/lib/walrus/diff.rb +0 -95
  39. data/lib/walrus/grammar/additions/proc.rb +0 -26
  40. data/lib/walrus/grammar/additions/regexp.rb +0 -27
  41. data/lib/walrus/grammar/additions/string.rb +0 -58
  42. data/lib/walrus/grammar/additions/symbol.rb +0 -49
  43. data/lib/walrus/grammar/and_predicate.rb +0 -46
  44. data/lib/walrus/grammar/array_result.rb +0 -25
  45. data/lib/walrus/grammar/continuation_wrapper_exception.rb +0 -34
  46. data/lib/walrus/grammar/left_recursion_exception.rb +0 -33
  47. data/lib/walrus/grammar/location_tracking.rb +0 -115
  48. data/lib/walrus/grammar/match_data_wrapper.rb +0 -71
  49. data/lib/walrus/grammar/memoizing.rb +0 -47
  50. data/lib/walrus/grammar/memoizing_cache.rb +0 -103
  51. data/lib/walrus/grammar/node.rb +0 -66
  52. data/lib/walrus/grammar/not_predicate.rb +0 -46
  53. data/lib/walrus/grammar/parse_error.rb +0 -45
  54. data/lib/walrus/grammar/parser_state.rb +0 -187
  55. data/lib/walrus/grammar/parslet.rb +0 -34
  56. data/lib/walrus/grammar/parslet_choice.rb +0 -128
  57. data/lib/walrus/grammar/parslet_combination.rb +0 -32
  58. data/lib/walrus/grammar/parslet_combining.rb +0 -160
  59. data/lib/walrus/grammar/parslet_merge.rb +0 -94
  60. data/lib/walrus/grammar/parslet_omission.rb +0 -63
  61. data/lib/walrus/grammar/parslet_repetition.rb +0 -106
  62. data/lib/walrus/grammar/parslet_repetition_default.rb +0 -64
  63. data/lib/walrus/grammar/parslet_sequence.rb +0 -214
  64. data/lib/walrus/grammar/predicate.rb +0 -63
  65. data/lib/walrus/grammar/proc_parslet.rb +0 -58
  66. data/lib/walrus/grammar/regexp_parslet.rb +0 -79
  67. data/lib/walrus/grammar/skipped_substring_exception.rb +0 -42
  68. data/lib/walrus/grammar/string_enumerator.rb +0 -53
  69. data/lib/walrus/grammar/string_parslet.rb +0 -81
  70. data/lib/walrus/grammar/string_result.rb +0 -30
  71. data/lib/walrus/grammar/symbol_parslet.rb +0 -69
  72. data/lib/walrus/no_parameter_marker.rb +0 -25
  73. data/lib/walrus/walrus_grammar/assignment_expression.rb +0 -30
  74. data/lib/walrus/walrus_grammar/block_directive.rb +0 -34
  75. data/lib/walrus/walrus_grammar/comment.rb +0 -30
  76. data/lib/walrus/walrus_grammar/def_directive.rb +0 -72
  77. data/lib/walrus/walrus_grammar/echo_directive.rb +0 -50
  78. data/lib/walrus/walrus_grammar/escape_sequence.rb +0 -30
  79. data/lib/walrus/walrus_grammar/import_directive.rb +0 -50
  80. data/lib/walrus/walrus_grammar/include_directive.rb +0 -33
  81. data/lib/walrus/walrus_grammar/instance_variable.rb +0 -30
  82. data/lib/walrus/walrus_grammar/literal.rb +0 -30
  83. data/lib/walrus/walrus_grammar/message_expression.rb +0 -31
  84. data/lib/walrus/walrus_grammar/multiline_comment.rb +0 -60
  85. data/lib/walrus/walrus_grammar/placeholder.rb +0 -46
  86. data/lib/walrus/walrus_grammar/raw_directive.rb +0 -48
  87. data/lib/walrus/walrus_grammar/raw_text.rb +0 -51
  88. data/lib/walrus/walrus_grammar/ruby_directive.rb +0 -35
  89. data/lib/walrus/walrus_grammar/ruby_expression.rb +0 -37
  90. data/lib/walrus/walrus_grammar/set_directive.rb +0 -30
  91. data/lib/walrus/walrus_grammar/silent_directive.rb +0 -50
  92. data/lib/walrus/walrus_grammar/slurp_directive.rb +0 -31
  93. data/lib/walrus/walrus_grammar/super_directive.rb +0 -33
@@ -1,26 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- class Proc
18
-
19
- include Walrus::Grammar::ParsletCombining
20
-
21
- # Returns a ProcParslet based on the receiver
22
- def to_parseable
23
- Walrus::Grammar::ProcParslet.new(self)
24
- end
25
-
26
- end # class Proc
@@ -1,27 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- class Regexp
18
-
19
- require 'walrus/grammar/parslet_combining'
20
- include Walrus::Grammar::ParsletCombining
21
-
22
- # Returns a RegexpParslet based on the receiver
23
- def to_parseable
24
- Walrus::Grammar::RegexpParslet.new(self)
25
- end
26
-
27
- end # class Regexp
@@ -1,58 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- require 'jindex' # multibyte-friendly implementations of String#index and String#rindex
18
-
19
- # Additions to String class for Unicode support.
20
- class String
21
-
22
- # Returns an array of Unicode characters.
23
- def chars
24
- scan(/./m)
25
- end
26
-
27
- alias old_range []
28
-
29
- # multi-byte friendly [] implementation
30
- def [](range, other = Walrus::NoParameterMarker.instance)
31
- if other == Walrus::NoParameterMarker.instance
32
- if range.kind_of? Range
33
- chars[range].join
34
- else
35
- old_range range
36
- end
37
- else
38
- old_range range, other
39
- end
40
- end
41
-
42
- # Returns a character-level enumerator for the receiver.
43
- def enumerator
44
- Walrus::Grammar::StringEnumerator.new(self)
45
- end
46
-
47
- end # class String
48
-
49
- class String
50
-
51
- include Walrus::Grammar::ParsletCombining # Rationale: it's ok to add "&" and "|" methods to string because they don't exist yet (they're not overrides).
52
-
53
- # Returns a StringParslet based on the receiver
54
- def to_parseable
55
- Walrus::Grammar::StringParslet.new(self)
56
- end
57
-
58
- end # class String
@@ -1,49 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- class Symbol
18
-
19
- include Walrus::Grammar::ParsletCombining
20
-
21
- # Returns a SymbolParslet based on the receiver.
22
- # Symbols can be used in Grammars when specifying rules and productions to refer to other rules and productions that have not been defined yet.
23
- # They can also be used to allow self-references within rules and productions (recursion); for example:
24
- # rule :thing & :thing.optional & :other_thing
25
- # Basically these SymbolParslets allow deferred evaluation of a rule or production (deferred until parsing takes place) rather than being evaluated at the time a rule or production is defined.
26
- def to_parseable
27
- Walrus::Grammar::SymbolParslet.new(self)
28
- end
29
-
30
- # Dynamically creates a subclass named after the receiver, with parent class superclass, taking params.
31
- def build(superclass, *params)
32
-
33
- # first use the continuation trick to find out what grammar (namespace) receiver is being messaged in
34
- # Ruby 1.9/2.0 will not support continuations, so may need to come up with an alternative
35
- continuation = nil
36
- value = callcc { |c| continuation = c }
37
- if value == continuation # first time that we're here
38
- raise Walrus::Grammar::ContinuationWrapperException.new(continuation) # give higher up a chance to help us
39
- else # value is the Grammar instance passed to us from higher up using call on the continuation
40
- grammar = value
41
- end
42
-
43
- # actually create the subclass
44
- grammar.const_get(superclass.to_s.to_class_name.to_s).subclass(self.to_s.to_class_name.to_s, grammar, *params)
45
- self
46
-
47
- end
48
-
49
- end # class Symbol
@@ -1,46 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- module Walrus
18
- class Grammar
19
-
20
- class AndPredicate < Predicate
21
-
22
- def parse(string, options = {})
23
- raise ArgumentError if string.nil?
24
- catch :ZeroWidthParseSuccess do
25
- begin
26
- parsed = @parseable.memoizing_parse(string, options)
27
- rescue ParseError
28
- raise ParseError.new('predicate not satisfied (expected "%s") while parsing "%s"' % [@parseable.to_s, string],
29
- :line_end => options[:line_start], :column_end => options[:column_start])
30
- end
31
- end
32
-
33
- # getting this far means that parsing succeeded (just what we wanted)
34
- throw :AndPredicateSuccess # pass succeeded
35
- end
36
-
37
- private
38
-
39
- def hash_offset
40
- 12
41
- end
42
-
43
- end
44
-
45
- end # class Grammar
46
- end # module Walrus
@@ -1,25 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- module Walrus
18
- class Grammar
19
- class ArrayResult < Array
20
-
21
- include LocationTracking
22
-
23
- end # class ArrayResult
24
- end # class Grammar
25
- end # module Walrus
@@ -1,34 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- module Walrus
18
- class Grammar
19
-
20
- class ContinuationWrapperException < Exception
21
-
22
- attr_reader :continuation
23
-
24
- def initialize(continuation)
25
- raise ArgumentError if continuation.nil?
26
- super self.class.to_s
27
- @continuation = continuation
28
- end
29
-
30
- end # class ContinuationWrapperException
31
-
32
- end # class Grammar
33
- end # module Walrus
34
-
@@ -1,33 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- module Walrus
18
- class Grammar
19
-
20
- class LeftRecursionException < Exception
21
-
22
- attr_accessor :continuation
23
-
24
- def initialize(continuation = nil)
25
- super self.class.to_s
26
- @continuation = continuation
27
- end
28
-
29
- end # class LeftRecursionException
30
-
31
- end # class Grammar
32
- end # module Walrus
33
-
@@ -1,115 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- module Walrus
18
- class Grammar
19
-
20
- # Methods for embedding location information in objects returned (or exceptions raised) from parse methods.
21
- module LocationTracking
22
-
23
- attr_reader :source_text
24
-
25
- # For occasions where a single item must serve as a carrier for array-like information
26
- # (that is, its own start, end and source_text, as well as the "outer" equivalents).
27
- # This can happen where a single node appears in a list context surrounded only by skipped content.
28
- attr_accessor :outer_start, :outer_end, :outer_source_text
29
-
30
- def source_text=(string)
31
- @source_text = string.to_s.clone
32
- end
33
-
34
- # Sets @column_start to col.
35
- # Sets @column_start to 0 if passed nil (for ease of use, users of classes that mix-in this module don't have to worry about special casing nil values).
36
- def column_start=(column_start)
37
- @column_start = column_start.to_i
38
- end
39
-
40
- # Returns 0 if @column_start is nil (for ease of use, users of classes that mix-in this module don't have to worry about special casing nil values).
41
- def column_start
42
- @column_start || 0
43
- end
44
-
45
- # Sets @line_start to line.
46
- # Sets @line_start to 0 if passed nil (for ease of use, users of classes that mix-in this module don't have to worry about special casing nil values).
47
- def line_start=(line_start)
48
- @line_start = line_start.to_i
49
- end
50
-
51
- # Returns 0 if @line_start is nil (for ease of use, users of classes that mix-in this module don't have to worry about special casing nil values).
52
- def line_start
53
- @line_start || 0
54
- end
55
-
56
- # Convenience method for getting both line_start and column_start at once.
57
- def start
58
- [self.line_start, self.column_start]
59
- end
60
-
61
- # Convenience method for setting both line_start and column_start at once.
62
- def start=(array)
63
- raise ArgumentError if array.nil?
64
- raise ArgumentError if array.length != 2
65
- self.line_start = array[0]
66
- self.column_start = array[1]
67
- end
68
-
69
- def line_end=(line_end)
70
- @line_end = line_end.to_i
71
- end
72
-
73
- def line_end
74
- @line_end || 0
75
- end
76
-
77
- def column_end=(column_end)
78
- @column_end = column_end.to_i
79
- end
80
-
81
- def column_end
82
- @column_end || 0
83
- end
84
-
85
- # Convenience method for getting both line_end and column_end at once.
86
- def end
87
- [self.line_end, self.column_end]
88
- end
89
-
90
- # Convenience method for setting both line_end and column_end at once.
91
- def end=(array)
92
- raise ArgumentError if array.nil?
93
- raise ArgumentError if array.length != 2
94
- self.line_end = array[0]
95
- self.column_end = array[1]
96
- end
97
-
98
- # Given another object that responds to column_end and line_end, returns true if the receiver is rightmost or equal.
99
- # If the other object is farther to the right returns false.
100
- def rightmost?(other)
101
- if self.line_end > other.line_end
102
- true
103
- elsif other.line_end > self.line_end
104
- false
105
- elsif self.column_end >= other.column_end
106
- true
107
- else
108
- false
109
- end
110
- end
111
-
112
- end # module LocationTracking
113
-
114
- end # class Grammar
115
- end # module Walrus
@@ -1,71 +0,0 @@
1
- # Copyright 2007 Wincent Colaiuta
2
- # This program is free software: you can redistribute it and/or modify
3
- # it under the terms of the GNU General Public License as published by
4
- # the Free Software Foundation, either version 3 of the License, or
5
- # (at your option) any later version.
6
- #
7
- # This program is distributed in the hope that it will be useful,
8
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
- # GNU General Public License for more details.
11
- #
12
- # You should have received a copy of the GNU General Public License
13
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
-
15
- require 'walrus'
16
-
17
- module Walrus
18
- class Grammar
19
-
20
- # Simple wrapper for MatchData objects that implements length, to_s and to_str methods.
21
- # By implementing to_str, MatchDataWrappers can be directly compared with Strings using the == method.
22
- # The original MatchData instance can be obtained using the match_data accessor.
23
- # Upon creation a clone of the passed in MatchData object is stored; this means that the $~ global variable can be conveniently wrapped without having to worry that subsequent operations will alter the contents of the variable.
24
- class MatchDataWrapper
25
-
26
- include Walrus::Grammar::LocationTracking
27
-
28
- attr_reader :match_data
29
-
30
- # Raises if data is nil.
31
- def initialize(data)
32
- raise ArgumentError if data.nil?
33
- self.match_data = data
34
- end
35
-
36
- # The definition of this method, in conjunction with the == method, allows automatic comparisons with String objects using the == method.
37
- # This is because in a parser matches essentially are Strings (just like Exceptions and Pathnames); it's just that this class encapsulates a little more information (the match data) for those who want it.
38
- def to_str
39
- self.to_s
40
- end
41
-
42
- # Although this method explicitly allows for MatchDataWrapper to MatchDataWrapper comparisons, not that all such comparisons will return false except for those between instances which were initialized with exactly the same match data instance; this is because the MatchData class itself always returns false when compared with other MatchData instances.
43
- def ==(other)
44
- if other.kind_of? MatchDataWrapper
45
- self.match_data == other.match_data
46
- elsif other.respond_to? :to_str
47
- self.to_str == other.to_str
48
- else
49
- false
50
- end
51
- end
52
-
53
- def to_s
54
- @match_data[0]
55
- end
56
-
57
- def jlength
58
- self.to_s.jlength
59
- end
60
-
61
- private
62
-
63
- def match_data=(data)
64
- @match_data = (data.clone rescue data)
65
- end
66
-
67
- end # class MatchDataWrapper
68
-
69
- end # class Grammar
70
- end # module Walrus
71
-