Class: Walrus::Grammar::ParsletRepetitionDefault

Inherits:
ParsletRepetition 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 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.



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

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

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/walrus/grammar/parslet_repetition_default.rb', line 37

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



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

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