Class: PatternMatching

Inherits:
Object show all
Defined in:
opal/opal/corelib/pattern_matching.rb

Overview

A "userland" implementation of pattern matching for Opal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, pattern) ⇒ PatternMatching

Returns a new instance of PatternMatching.



10
11
12
13
# File 'opal/opal/corelib/pattern_matching.rb', line 10

def initialize(from, pattern)
  @from, @pattern = from, pattern
  @returns = []
end

Instance Attribute Details

#returnsObject (readonly)

Returns the value of attribute returns.



15
16
17
# File 'opal/opal/corelib/pattern_matching.rb', line 15

def returns
  @returns
end

Class Method Details

.call(from, pattern) ⇒ Object



4
5
6
7
8
# File 'opal/opal/corelib/pattern_matching.rb', line 4

def self.call(from, pattern)
  pm = new(from, pattern)
  pm.match || (return nil)
  pm.returns
end

Instance Method Details

#match(from = @from, pattern = @pattern) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'opal/opal/corelib/pattern_matching.rb', line 17

def match(from = @from, pattern = @pattern)
  if pattern == :var
    @returns << from
    true
  else # Pattern is otherwise an Array
    type, *args = *pattern

    case type
    when :save # from =>
      @returns << from
      match(from, args[0])
    when :lit # 3, 4, :a, (1..), ... (but also ^a)
      args[0] === from
    when :any # a | b
      args.any? { |arg| match(from, arg) }
    when :all # Array(1) which works as Array & [1] (& doesn't exist though...)
      args.all? { |arg| match(from, arg) }
    when :array # [...]
      fixed_size, array_size, array_match = *args
      return false unless from.respond_to? :deconstruct
      a = from.deconstruct
      return false if fixed_size && a.length != array_size
      return false if a.length < array_size

      skip_elems = 0
      skip_rests = 0

      array_match.each_with_index.all? do |elem, i|
        type, *args = elem
        case type
        when :rest
          skip_elems = a.size - array_size
          skip_rests = 1
          match(a[i...i + skip_elems], args[0]) if args[0] # :save?
          true
        else
          match(a[i + skip_elems - skip_rests], elem)
        end
      end
    when :find # [*, a, b, *]
      find_match, = *args
      first, *find_match, last = *find_match
      pattern_length = find_match.length

      return false unless from.respond_to? :deconstruct
      a = from.deconstruct
      a_length = a.length
      return false if a_length < pattern_length

      # We will save the backup of returns, to be restored
      # on each iteration to try again.
      returns_backup = @returns.dup

      # Extract the capture info from first and last.
      # Both are of a form [:rest], or [:rest, :var].
      # So our new variables will be either :var, or nil.
      first, last = first[1], last[1]

      # Let's try to match each possibility...
      # [A, B, c, d], [a, B, C, d], [a, b, C, D]
      iterations = a_length - pattern_length + 1

      iterations.times.any? do |skip|
        first_part = a[0, skip]
        content = a[skip, pattern_length]
        last_part = a[skip + pattern_length..-1]

        match(first_part, first) if first
        success = content.each_with_index.all? do |e, i|
          match(e, find_match[i])
        end
        match(last_part, last) if last

        # Match failed. Let's not return anything.
        @returns = returns_backup.dup unless success

        success
      end
    when :hash # {...}
      any_size, hash_match = *args

      hash_match = hash_match.to_h

      return false unless from.respond_to? :deconstruct_keys

      if any_size && any_size != true # a => {a:, **other}
        a = from.deconstruct_keys(nil) #          ^^^^^^^
      else
        a = from.deconstruct_keys(hash_match.keys)
      end

      hash_match.all? do |k, v|
        return false unless a.key? k
        match(a[k], v)
      end || (return false)

      if any_size && any_size != true
        match(a.except(*hash_match.keys), args[0])
      elsif !any_size
        return false unless a.except(*hash_match.keys).empty?
      end

      true
    end
  end
end