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