Class: SexpGrammar::Many
- Inherits:
-
Object
- Object
- SexpGrammar::Many
- Includes:
- Element
- Defined in:
- lib/sexp_grammar/many.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
Instance Method Summary collapse
- #eat(sexp) ⇒ Object
-
#initialize(term, min, max = nil) ⇒ Many
constructor
A new instance of Many.
- #inspect ⇒ Object
- #match?(sexp) ⇒ Boolean
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
#max ⇒ Object (readonly)
Returns the value of attribute max.
5 6 7 |
# File 'lib/sexp_grammar/many.rb', line 5 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
5 6 7 |
# File 'lib/sexp_grammar/many.rb', line 5 def min @min end |
#term ⇒ Object (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 |
#inspect ⇒ Object
30 31 32 |
# File 'lib/sexp_grammar/many.rb', line 30 def inspect "(many #{term.inspect}, #{min}, #{max})" end |
#match?(sexp) ⇒ 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 |