Class: SexpGrammar::Many

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/sexp_grammar/many.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Element

#===

Constructor Details

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

Returns a new instance of Many.



7
8
9
10
# File 'lib/sexp_grammar/many.rb', line 7

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.



5
6
7
# File 'lib/sexp_grammar/many.rb', line 5

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



5
6
7
# File 'lib/sexp_grammar/many.rb', line 5

def min
  @min
end

#termObject (readonly)

Returns the value of attribute term.



5
6
7
# File 'lib/sexp_grammar/many.rb', line 5

def term
  @term
end

Instance Method Details

#eat(sexp) ⇒ Object



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

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



30
31
32
# File 'lib/sexp_grammar/many.rb', line 30

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

#match?(sexp) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
# File 'lib/sexp_grammar/many.rb', line 12

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