Module: OrigenTesters::Decompiler::Pattern::EnumerableExt

Included in:
OrigenTesters::Decompiler::Pattern
Defined in:
lib/origen_testers/decompiler/pattern/enumerable_ext.rb

Overview

We can't use Ruby's enumerable mix-in directly, as our each method isn't a true implementation, as the mix-in expects. Instead, we'll provide our own enumerable methods as needed that use our own each method.

Instance Method Summary collapse

Instance Method Details

#collect(&block) ⇒ Object Also known as: collect_vectors, map, map_vectors



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 84

def collect(&block)
  vectors = []
  each_vector do |v|
    if block_given?
      vectors << yield(v)
    else
      vectors << v
    end
  end
  vectors
end

#collect_with_index(&block) ⇒ Object Also known as: collect_vectors_with_index



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 99

def collect_with_index(&block)
  vectors = []
  each_vector_with_index do |v, i|
    if block_given?
      vectors << yield(v, i)
    else
      vectors << v
    end
  end
  vectors
end

#count(&block) ⇒ Object Also known as: size



133
134
135
136
137
138
139
140
141
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 133

def count(&block)
  cnt = 0
  if block_given?
    each_vector { |v| cnt += 1 if yield(v) }
  else
    each_vector { |v| cnt += 1 }
  end
  cnt
end

#each(**options, &block) ⇒ Object Also known as: each_vector

Iterate through the vectors one by one. We'll begin reading the file from the vector start until the regex/block vector_delimiter is met. This delimiter can include multiple lines. Once the delimiter finishes, those lines will be sent to the treetop parser for conversion to an AST. Lastly, the next vector will start where the previous one left off.



16
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
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 16

def each(**options, &block)
  unless decompiled?
    Origen.app!.fail(message: 'Pattern has not yet been decompiled! Cannot iterate through vectors or query pattern aspects!')
  end
  vectors_started = false
  delimiter_klass = VectorDelimiterBase
  v = delimiter_klass.new(self)
  vector_index = 0

  if direct_source?
    kickoff = 'source.split("\n")'
  else
    kickoff = 'File.foreach(source)'
  end

  eval(kickoff).each_with_index do |line, index|
    # Get to the point in the file where the vectors begin
    if index > section_indices[:vectors_end] && section_indices[:vectors_end] != -1
      break
    elsif index < section_indices[:vectors_start]
      next
    end

    v.shift(line)
    if v.delimited?
      # index starts at 0, but most file editors start the line numbers at 1.
      yield(_parse_vector_(v.current_vector!, vector_index: vector_index, line: index + 1))
      vector_index += 1

      if v.include_last_line?
        # The last line is included in the current vector.
        v = delimiter_klass.new(self)
      else
        # The last line shouldn't be included in this vector.
        # Shift it into a new one.
        v = delimiter_klass.new(self)
        v.shift(line)

        # Check if this new vector is delimited before grabbing
        # the next line.
        if v.delimited?
          yield(_parse_vector_(v.current_vector!, vector_index: vector_index))
          v = delimiter_klass.new(self)
          vector_index += 1
        end
      end
    end
  end
end

#each_vector_with_index(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 67

def each_vector_with_index(&block)
  i = 0
  each_vector do |v|
    yield(v, i)
    i += 1
  end
end

#find(&block) ⇒ Object Also known as: detect



122
123
124
125
126
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 122

def find(&block)
  if block_given?
    each_vector { |v| return v if yield(v) }
  end
end

#find_all(&block) ⇒ Object Also known as: select, filter



112
113
114
115
116
117
118
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 112

def find_all(&block)
  vectors = []
  if block_given?
    each_vector { |v| vectors << v if yield(v) }
  end
  vectors
end

#find_index(&block) ⇒ Object



129
130
131
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 129

def find_index(&block)
  find(&block).vector_index
end

#first(n = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 152

def first(n = nil)
  if n
    if n <= 0
      return nil
    end

    vectors = []
    each_vector_with_index do |v, i|
      vectors << v
      if n == (i - 1)
        return vectors
      end
    end
    vectors
  else
    each_vector { |v| return v }
  end
end

#reject(&block) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 144

def reject(&block)
  vectors = []
  if block_given?
    each_vector { |v| vectors << v unless yield(v) }
  end
  vectors
end

#vector_at(index, &block) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/origen_testers/decompiler/pattern/enumerable_ext.rb', line 75

def vector_at(index, &block)
  each_vector_with_index do |v, i|
    if i == index
      return v
    end
  end
  nil
end