Sha256: a3d5755f3ca40f9a80b9f5d072eba5c70f40c4db0bcf6ea57d5bd315f7061d4c

Contents?: true

Size: 1.83 KB

Versions: 10

Compression:

Stored size: 1.83 KB

Contents

require_relative "../../labels"
require_relative "../../signals"

class LabParser
  def self.call(str)
    lines = str.lines

    [
      extract_signals(block_bounded_by(lines, "[RAMCELL]", /\A\[/)),
      extract_labels(block_bounded_by(lines, "[Label]", /\A\[/)),
      extract_headers(block_bounded_by(lines, 0, "[SETTINGS]")),
      extract_headers(block_bounded_by(lines, "[SETTINGS]", /\A\[/)),
    ]
  end

  def self.extract_labels(lines)
    Ecu::LabelList.new lines.
      reject { |l| l.blank? }.
      reject { |l| l.start_with?(";") }.
      map { |l| Ecu::Label.from_lab(l) }
  end

  def self.extract_signals(lines)
    Ecu::SignalList.new lines.
      reject { |l| l.blank? }.
      reject { |l| l.start_with?(";") }.
      map { |l| Ecu::Signal.from_lab(l) }
  end

  def self.extract_headers(lines)
    lines.
      select { |l| l.match(/\A; /) }.
      map { |l| l[2..-1].chomp }
  end

  def self.block_bounded_by(lines, upper, lower)
    upper = case upper
            when Integer then upper
            when String
              return [] unless lines.any? { |l| l.strip.downcase == upper.downcase }
              lines.index { |l| l.strip.downcase == upper.downcase } + 1
            when Regexp
              return [] unless lines.any? { |l| l.match?(upper) }
              lines.index { |l| l.match?(upper) } + 1
            end

    segment = lines[upper..-1]

    lower = case lower
            when Integer then lower
            when String
              return lines[upper..-1] unless segment.any? { |l| l.strip.downcase == lower.downcase }
              upper + segment.index { |l| l.strip.downcase == lower.downcase }
            when Regexp
              return lines[upper..-1] unless segment.any? { |l| l.match?(lower) }
              upper + segment.index { |l| l.match?(lower) }
            end

    lines[upper...lower]
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
automotive-ecu-0.1.9 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.8 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.7 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.6 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.5 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.4 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.3 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.2 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.1 lib/ecu/interfaces/lab/lab_parser.rb
automotive-ecu-0.1.0 lib/ecu/interfaces/lab/lab_parser.rb