Class: Walrus::Grammar::ParsletRepetitionDefault
- Inherits:
-
ParsletRepetition
- Object
- ParsletRepetition
- Walrus::Grammar::ParsletRepetitionDefault
- 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
- #eql?(other) ⇒ Boolean
-
#initialize(parseable, min, max = nil, default = nil) ⇒ ParsletRepetitionDefault
constructor
Possible re-factoring to consider for the future: roll the functionality of this class in to ParsletRepetition itself.
- #parse(string, options = {}) ⇒ Object
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
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, = {}) catch :ZeroWidthParseSuccess do return super(string, ) end @default.clone rescue @default end |