Class: PatternMatch::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/pattern-match/core.rb

Direct Known Subclasses

PatternElement, PatternQuantifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*subpatterns) ⇒ Pattern

Returns a new instance of Pattern.



31
32
33
34
35
36
37
# File 'lib/pattern-match/core.rb', line 31

def initialize(*subpatterns)
  @parent = nil
  @next = nil
  @prev = nil
  @subpatterns = subpatterns.map {|i| i.kind_of?(Pattern) ? i : PatternValue.new(i) }
  set_subpatterns_relation
end

Instance Attribute Details

#nextObject

Returns the value of attribute next.



29
30
31
# File 'lib/pattern-match/core.rb', line 29

def next
  @next
end

#parentObject

Returns the value of attribute parent.



29
30
31
# File 'lib/pattern-match/core.rb', line 29

def parent
  @parent
end

#prevObject

Returns the value of attribute prev.



29
30
31
# File 'lib/pattern-match/core.rb', line 29

def prev
  @prev
end

Instance Method Details

#!@Object



59
60
61
# File 'lib/pattern-match/core.rb', line 59

def !@
  PatternNot.new(self)
end

#&(pattern) ⇒ Object



51
52
53
# File 'lib/pattern-match/core.rb', line 51

def &(pattern)
  PatternAnd.new(self, pattern)
end

#ancestorsObject



43
44
45
# File 'lib/pattern-match/core.rb', line 43

def ancestors
  root? ? [self] : parent.ancestors.unshift(self)
end

#append(pattern) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pattern-match/core.rb', line 105

def append(pattern)
  if @next
    @next.append(pattern)
  else
    if @subpatterns.empty?
      if root?
        new_root = PatternAnd.new(self)
        self.parent = new_root
      end
      pattern.parent = @parent
      @next = pattern
    else
      @subpatterns[-1].append(pattern)
    end
  end
end

#inspectObject



122
123
124
# File 'lib/pattern-match/core.rb', line 122

def inspect
  "#<#{self.class.name}: subpatterns=#{@subpatterns.inspect}>"
end

#match(vals) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pattern-match/core.rb', line 87

def match(vals)
  if @next and @next.quantifier?
    q = @next
    repeating_match(vals, q.longest?) do |vs, rest|
      if vs.length < q.min_k
        next false
      end
      vs.all? {|v| yield(v) } and q.match(rest)
    end
  else
    if vals.empty?
      return false
    end
    val, *rest = vals
    yield(val) and (@next ? @next.match(rest) : rest.empty?)
  end
end

#quantified?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/pattern-match/core.rb', line 71

def quantified?
  (@next and @next.quantifier?) or (root? ? false : @parent.quantified?)
end

#quantifier?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/pattern-match/core.rb', line 67

def quantifier?
  raise NotImplementedError
end

#quasibindingObject



47
48
49
# File 'lib/pattern-match/core.rb', line 47

def quasibinding
  vars.each_with_object({}) {|v, h| h[v.name] = v.val }
end

#rootObject



75
76
77
# File 'lib/pattern-match/core.rb', line 75

def root
  root? ? self : @parent.root
end

#root?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/pattern-match/core.rb', line 79

def root?
  @parent == nil
end

#to_aObject



63
64
65
# File 'lib/pattern-match/core.rb', line 63

def to_a
  [self, PatternQuantifier.new(0, true)]
end

#validateObject



83
84
85
# File 'lib/pattern-match/core.rb', line 83

def validate
  @subpatterns.each(&:validate)
end

#varsObject



39
40
41
# File 'lib/pattern-match/core.rb', line 39

def vars
  @subpatterns.map(&:vars).flatten
end

#|(pattern) ⇒ Object



55
56
57
# File 'lib/pattern-match/core.rb', line 55

def |(pattern)
  PatternOr.new(self, pattern)
end