Class: Walrus::Grammar::ParsletRepetitionDefault

Inherits:
ParsletRepetition
  • Object
show all
Defined in:
lib/walrus/grammar/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 Method Summary collapse

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.



19
20
21
22
# File 'lib/walrus/grammar/parslet_repetition_default.rb', line 19

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

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/walrus/grammar/parslet_repetition_default.rb', line 31

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



24
25
26
27
28
29
# File 'lib/walrus/grammar/parslet_repetition_default.rb', line 24

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