Class: GhostWheel::Expression::Repetition

Inherits:
GhostWheel::Expression show all
Defined in:
lib/ghost_wheel/expression/repetition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GhostWheel::Expression

#parse

Constructor Details

#initialize(expression, minimum_count = 0, maximum_count = 1.0 / 0.0) ⇒ Repetition

Returns a new instance of Repetition.



6
7
8
9
10
# File 'lib/ghost_wheel/expression/repetition.rb', line 6

def initialize(expression, minimum_count = 0, maximum_count = 1.0 / 0.0)
  @expression    = expression
  @minimum_count = minimum_count
  @maximum_count = maximum_count
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



12
13
14
# File 'lib/ghost_wheel/expression/repetition.rb', line 12

def expression
  @expression
end

#maximum_countObject (readonly)

Returns the value of attribute maximum_count.



12
13
14
# File 'lib/ghost_wheel/expression/repetition.rb', line 12

def maximum_count
  @maximum_count
end

#minimum_countObject (readonly)

Returns the value of attribute minimum_count.



12
13
14
# File 'lib/ghost_wheel/expression/repetition.rb', line 12

def minimum_count
  @minimum_count
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
41
# File 'lib/ghost_wheel/expression/repetition.rb', line 36

def ==(other)
  super                                 and
  @expression    == other.expression    and
  @minimum_count == other.minimum_count and
  @maximum_count == other.maximum_count
end

#uncached_parse(scanner, cache) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ghost_wheel/expression/repetition.rb', line 14

def uncached_parse(scanner, cache)
  scanner.transaction do
    parsed = ParseResult.new(Array.new)
    while parsed.value.size < @maximum_count
      case result = @expression.parse(scanner, cache)
      when FailedParseResult then break
      when ParseResult       then parsed.value << result.value
      end
    end
  
    if parsed.value.size >= @minimum_count
      if parsed.value.empty?
        EmptyParseResult.instance
      else
        parsed
      end
    else
      scanner.abort(FailedParseResult.instance)
    end
  end
end