1 # Copyright 2007-2010 Wincent Colaiuta. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are met:
5 # 1. Redistributions of source code must retain the above copyright notice,
6 # this list of conditions and the following disclaimer.
7 # 2. Redistributions in binary form must reproduce the above copyright notice,
8 # this list of conditions and the following disclaimer in the documentation
9 # and/or other materials provided with the distribution.
11 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
15 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 # POSSIBILITY OF SUCH DAMAGE.
26 class ParsletRepetition < ParsletCombination
29 # Raises an ArgumentError if parseable or min is nil.
30 def initialize parseable, min, max = nil
31 raise ArgumentError, 'nil parseable' if parseable.nil?
32 raise ArgumentError, 'nil min' if min.nil?
33 @parseable = parseable
38 def parse string, options = {}
39 raise ArgumentError, 'nil string' if string.nil?
40 state = ParserState.new string, options
41 catch :ZeroWidthParseSuccess do # a zero-width match is grounds for immediate abort
42 while @max.nil? or state.length < @max # try forever if max is nil; otherwise keep trying while match count < max
44 parsed = @parseable.memoizing_parse state.remainder, state.options
46 rescue SkippedSubstringException => e
48 rescue ParseError => e # failed, will try to skip; save original error in case skipping fails
49 if options.has_key?(:skipping_override)
50 skipping_parslet = options[:skipping_override]
51 elsif options.has_key?(:skipping)
52 skipping_parslet = options[:skipping]
54 skipping_parslet = nil
56 break if skipping_parslet.nil?
58 # guard against self references (possible infinite recursion) here?
59 parsed = skipping_parslet.memoizing_parse state.remainder, state.options
61 redo # skipping succeeded, try to redo
63 break # skipping didn't help either, give up
69 # now assess whether our tries met the requirements
70 if state.length == 0 and @min == 0 # success (special case)
71 throw :ZeroWidthParseSuccess
72 elsif state.length < @min # matches < min (failure)
73 raise ParseError.new('required %d matches but obtained %d while parsing "%s"' % [@min, state.length, string],
74 :line_end => state.options[:line_end],
75 :column_end => state.options[:column_end])
76 else # success (general case)
77 state.results # returns multiple matches as an array, single matches as a single object
82 other.instance_of? ParsletRepetition and
85 @parseable.eql? other.parseable
90 # For determining equality.
91 attr_reader :parseable, :min, :max
100 # fixed offset to minimize risk of collisions
101 @hash = @min.hash + @max.hash + @parseable.hash + hash_offset
105 @min = (min.clone rescue min)
110 @max = (max.clone rescue max)
113 end # class ParsletRepetition