Module: OrigenTesters::Decompiler::Pattern::Splitter

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

Constant Summary collapse

REQUIRED_KEYS =
[:pinlist_start, :vectors_start, :vectors_end]
OPTIONAL_KEYS =
[:separator, :vectors_include_start_line, :vectors_include_end_line]

Instance Method Summary collapse

Instance Method Details

#check_match(matcher, line, index, indices) ⇒ Object



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

def check_match(matcher, line, index, indices)
  if matcher.respond_to?(:call)
    matcher.call(line: line, index: index, current_indices: indices)
  elsif matcher.is_a?(Regexp)
    line =~ matcher
  elsif matcher.is_a?(String)
    line.start_with?(matcher)
  else
    fail "Splitter does not know how to match given matcher of class #{matcher.class}"
  end
end

#raw_endmatterObject



50
51
52
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 50

def raw_endmatter
  raw_lines(section_indices[:endmatter_start], section_indices[:endmatter_end])
end

#raw_frontmatterObject



38
39
40
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 38

def raw_frontmatter
  raw_lines(section_indices[:frontmatter_start], section_indices[:frontmatter_end])
end

#raw_lines(start, stop, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 12

def raw_lines(start, stop, &block)
  retn_lines = []
  _run_line_ = lambda do |line, index, retn_lines, &block|
    if index > stop
      break
    elsif index >= start
      if block_given?
        yield
      else
        retn_lines << line
      end
    end
  end

  if direct_source?
    source.split("\n").map { |l| "#{l}\n" }.each_with_index do |line, index|
      _run_line_.call(line, index, retn_lines, &block)
    end
  else
    File.foreach(source).each_with_index do |line, index|
      _run_line_.call(line, index, retn_lines, &block)
    end
  end
  retn_lines
end

#raw_pinlistObject



42
43
44
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 42

def raw_pinlist
  raw_lines(section_indices[:pinlist_start], section_indices[:pinlist_end])
end

#raw_vectors(&block) ⇒ Object



46
47
48
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 46

def raw_vectors(&block)
  raw_lines(section_indices[:vectors_start], section_indices[:vectors_end])
end

#section_indicesObject



8
9
10
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 8

def section_indices
  @section_indices
end

#split(pinlist_start:, vectors_start:, vectors_end:, vectors_include_start_line: false, vectors_include_end_line: false, &block) ⇒ Hash

Splits the pattern into gour secionts using regexes:

1. Frontmatter
2. Pin List
3. Vectors
4. Endmatter

The idea is that each section can be delimited by a line that matches some regex (and is not considered a comment line). The pattern will be read line-by-line, looking for the regexes. We're defining the pattern section as such:

- Frontmatter starts at the beginning of the pattern and ends at the start of the pattern header.
- Vectors start from the end of the pattern header and go until the end of the vectors.
- Endmatter starts at the end of the vectors and ends at the end of the file.
  - Its possible (and fine) for endmatter to be non-existant, or even not allowed.
  - In the latter case, the endvector symbol should the EoF symbol.

rubocop:disable Metrics/ParameterLists

Returns:

  • (Hash)


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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/origen_testers/decompiler/pattern/splitter.rb', line 98

def split(pinlist_start:, vectors_start:, vectors_end:, vectors_include_start_line: false, vectors_include_end_line: false, &block)
  def check_match(matcher, line, index, indices)
    if matcher.respond_to?(:call)
      matcher.call(line: line, index: index, current_indices: indices)
    elsif matcher.is_a?(Regexp)
      line =~ matcher
    elsif matcher.is_a?(String)
      line.start_with?(matcher)
    else
      fail "Splitter does not know how to match given matcher of class #{matcher.class}"
    end
  end

  if File.zero?(source)
    Origen.log.error('Parsing Error!')
    Origen.log.error("Pattern #{source} has size zero!")
    Origen.log.error('Decompiling empty files is not supported.')

    fail(OrigenTesters::Decompiler::ParseError, "Empty or non-readable pattern file #{source}")
  end

  indices = {
    frontmatter_start: 0,
    endmatter_end:     -1
  }
  if block_given?
    fail 'Blocks are not yet supported!'
  else
    if vectors_end == -1
      indices[:vectors_end] = -1
      indices[:endmatter_start] = -1
    end
    _split_ = lambda do |line, index, indices|
      if !indices[:pinlist_start]
        if check_match(pinlist_start, line, index, indices)
          indices[:frontmatter_end] = index - 1
          indices[:pinlist_start] = index
        end
      elsif !indices[:vectors_start]
        if check_match(vectors_start, line, index, indices)
          indices[:pinlist_end] = index - 1
          vectors_include_start_line ? indices[:vectors_start] = index : indices[:vectors_start] = index + 1
        end
      elsif !indices[:vectors_end]
        if check_match(vectors_end, line, index, indices)
          vectors_include_end_line ? indices[:vectors_end] = index : indices[:vectors_end] = index - 1
          indices[:endmatter_start] = index
        end
      end
    end

    if direct_source?
      source.split("\n").each_with_index do |line, index|
        _split_.call(line, index, indices)
      end
    else
      File.foreach(source).each_with_index do |line, index|
        _split_.call(line, index, indices)
      end
    end

    indices
  end
  # rubocop:enable Metrics/ParameterLists
end

#split!Object



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

def split!
  section_indices = split(**splitter_config)

  # Check that we found each section in the pattern.
  if section_indices[:pinlist_start].nil?
    Origen.log.error('Parsing Error!')
    Origen.log.error("Could not locate the pinlist start in pattern #{source}")
    Origen.log.error("Expected a pattern line to match '#{splitter_config[:pinlist_start]}'")

    fail OrigenTesters::Decompiler::ParseError, "Parsing Error! Could not locate the pinlist start in pattern #{source}"
  elsif section_indices[:vectors_start].nil?
    Origen.log.error('Parsing Error!')
    Origen.log.error("Could not locate the vector start in pattern #{source}")
    Origen.log.error("Expected a pattern line to match '#{splitter_config[:vector_start]}'")

    fail OrigenTesters::Decompiler::ParseError, "Parsing Error! Could not locate the vector body in pattern #{source}"
  elsif section_indices[:vectors_end].nil?
    Origen.log.error('Parsing Error!')
    Origen.log.error("Could not locate the vector body end in pattern #{source}")
    Origen.log.error("Expected a pattern line to match '#{splitter_config[:vector_end]}'")

    fail OrigenTesters::Decompiler::ParseError, "Parsing Error! Could not locate the vector body end in pattern #{source}"
  end

  @section_indices = section_indices
  @section_indices
end