Class: Sexpr::Matcher::Many

Inherits:
Object
  • Object
show all
Includes:
Sexpr::Matcher
Defined in:
lib/sexpr/matcher/many.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sexpr::Matcher

#===

Constructor Details

#initialize(term, min, max = nil) ⇒ Many

Returns a new instance of Many.



8
9
10
11
# File 'lib/sexpr/matcher/many.rb', line 8

def initialize(term, min, max = nil)
  @term       = term
  @min, @max  = minmax(min, max)
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



6
7
8
# File 'lib/sexpr/matcher/many.rb', line 6

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



6
7
8
# File 'lib/sexpr/matcher/many.rb', line 6

def min
  @min
end

#termObject (readonly)

Returns the value of attribute term.



6
7
8
# File 'lib/sexpr/matcher/many.rb', line 6

def term
  @term
end

Instance Method Details

#eat(sexp) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sexpr/matcher/many.rb', line 19

def eat(sexp)
  i, last = 0, sexp
  while sexp && (@max.nil? || i < @max)
    if res = @term.eat(sexp)
      last = res
      i += 1
    end
    sexp = res
  end
  i >= @min ? last : nil
end

#inspectObject



31
32
33
# File 'lib/sexpr/matcher/many.rb', line 31

def inspect
  "(many #{term.inspect}, #{min}, #{max})"
end

#match?(sexp) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/sexpr/matcher/many.rb', line 13

def match?(sexp)
  return nil unless sexp.is_a?(Array)
  eat = eat(sexp)
  eat && eat.empty?
end