Class: Sexpr::Matcher::Many
- Inherits:
-
Object
- Object
- Sexpr::Matcher::Many
- Includes:
- Sexpr::Matcher
- Defined in:
- lib/sexpr/matcher/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 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
#max ⇒ Object (readonly)
Returns the value of attribute max.
6 7 8 |
# File 'lib/sexpr/matcher/many.rb', line 6 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
6 7 8 |
# File 'lib/sexpr/matcher/many.rb', line 6 def min @min end |
#term ⇒ Object (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 |
#inspect ⇒ Object
31 32 33 |
# File 'lib/sexpr/matcher/many.rb', line 31 def inspect "(many #{term.inspect}, #{min}, #{max})" end |
#match?(sexp) ⇒ 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 |