Class: Walrat::ParsletRepetitionDefault

Inherits:
ParsletRepetition show all
Defined in:
lib/walrat/parslet_repetition_default.rb

Overview

ParsletRepetitionDefault is a subclass that modifies the behaviour of its parent, ParsletRepetition, in a very small way. Namely, if the outcome of parsing is a ZeroWidthParse success then it is caught and the default value (defined at initialization time) is returned instead.

Instance Attribute Summary

Attributes inherited from ParsletRepetition

#hash

Instance Method Summary collapse

Methods inherited from ParsletCombination

#to_parseable

Methods included from Memoizing

#check_left_recursion, #memoizing_parse

Methods included from ParsletCombining

#&, #>>, #and?, #and_predicate, #choice, #memoizing_parse, #merge, #not!, #not_predicate, #omission, #one_or_more, #optional, #repeat, #repeat_with_default, #repetition, #repetition_with_default, #sequence, #skip, #zero_or_more, #zero_or_one, #|

Constructor Details

#initialize(parseable, min, max = nil, default = nil) ⇒ ParsletRepetitionDefault

Possible re-factoring to consider for the future: roll the functionality of this class in to ParsletRepetition itself. Benefit of keeping it separate is that the ParsletRepetition itself is kept simple.



35
36
37
38
# File 'lib/walrat/parslet_repetition_default.rb', line 35

def initialize parseable, min, max = nil, default = nil
  super parseable, min, max
  self.default  = default
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/walrat/parslet_repetition_default.rb', line 47

def eql?(other)
  other.instance_of? ParsletRepetitionDefault and
    @min == other.min and
    @max == other.max and
    @parseable.eql? other.parseable and
    @default == other.default
end

#parse(string, options = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/walrat/parslet_repetition_default.rb', line 40

def parse string, options = {}
  catch :ZeroWidthParseSuccess do
    return super string, options
  end
  @default.clone rescue @default
end