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.



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/neg/parser.rb', line 200

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, opts) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/neg/parser.rb', line 212

def do_parse(i, opts)

  rs = []

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

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

  [ success, nil, rs ]
end

#to_s(parent = nil) ⇒ Object



229
230
231
232
# File 'lib/neg/parser.rb', line 229

def to_s(parent=nil)

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