module NodeattrUtils::NodeParser

Constants

CHAR
EXTERNAL_COMMA
GENERAL_REGEX
NAME
RANGE
RANGE_REGEX
SECTION
SUFFIX

Public Class Methods

expand(nodes_string) click to toggle source
# File lib/nodeattr_utils/node_parser.rb, line 38
def self.expand(nodes_string)
  return [] if nodes_string.nil? || nodes_string.empty?
  error_if_invalid_node_syntax(nodes_string)
  nodes_string.split(EXTERNAL_COMMA)
              .each_with_object([]) do |section, nodes|
    if match = section.match(RANGE_REGEX)
      # match 3 is the 2nd num of the range, used later
      prefix, ranges, _, suffix = match[1,4]
      ranges.split(',').each do |range|
        nodes.push(*expand_range(prefix, range, suffix))
      end
    else
      nodes.push(section)
    end
  end
end

Private Class Methods

error_if_invalid_node_syntax(str) click to toggle source
# File lib/nodeattr_utils/node_parser.rb, line 57
    def self.error_if_invalid_node_syntax(str)
      return if GENERAL_REGEX.match?(str)
      raise NodeSyntaxError, <<~ERROR
        #{str.inspect} does not represent a range of nodes
      ERROR
    end
expand_range(prefix, range, suffix) click to toggle source
# File lib/nodeattr_utils/node_parser.rb, line 64
    def self.expand_range(prefix, range, suffix)
      return ["#{prefix}#{range}#{suffix}"] unless range.include?('-')
      min_str, _ = indices = range.split('-')
      min, max = indices.map(&:to_i)
      raise NodeSyntaxError, <<~ERROR if min > max
        '#{range}' the minimum index can not be greater than the maximum
      ERROR
      (min .. max).map do |num|
        sprintf("#{prefix}%0#{min_str.length}d#{suffix}", num)
      end
    end