Class: Neg::Parser::RepetitionParser

Inherits:
SubParser
  • Object
show all
Defined in:
lib/neg/parser.rb

Instance Method Summary collapse

Methods inherited from SubParser

#*, #+, #[], #parse, #|

Constructor Details

#initialize(child, range) ⇒ RepetitionParser

Returns a new instance of RepetitionParser.



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/neg/parser.rb', line 174

def initialize(child, range)

  @range = range
  @child = child

  @min, @max = case range
    when -1 then [ 0, 1 ]
    when Array then range
    else [ range, nil ]
  end
end

Instance Method Details

#do_parse(i) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/neg/parser.rb', line 186

def do_parse(i)

  rs = []

  loop do
    r = @child.parse(i)
    break if ! r[1] && rs.size >= @min && (@max.nil? || rs.size <= @max)
    rs << r
    break if ! r[1]
    break if rs.size == @max
  end

  success = (rs.empty? || rs.last[1]) && (rs.size >= @min)

  [ success, rs ]
end

#to_s(parent = nil) ⇒ Object



203
204
205
206
# File 'lib/neg/parser.rb', line 203

def to_s(parent=nil)

  "#{@child.to_s(self)} * #{@range.inspect}"
end