Module: NodeattrUtils::NodeParser

Defined in:
lib/nodeattr_utils/node_parser.rb

Constant Summary collapse

EXTERNAL_COMMA =
/,(?![^\[]*\])/
CHAR =
/[^\s\=\,\[]/
NAME =
/#{CHAR}+/
SUFFIX =
/#{CHAR}*/
RANGE =

Exclude invalid: [] [,] [1-] etc…

/\[(\d+([,-]\d+)*)\]/
SECTION =
/#{NAME}(#{RANGE}#{SUFFIX})?/
GENERAL_REGEX =
/\A#{SECTION}(,#{SECTION})*\Z/
RANGE_REGEX =
/\A(#{NAME})#{RANGE}(#{SUFFIX})\Z/

Class Method Summary collapse

Class Method Details

.error_if_invalid_node_syntax(str) ⇒ Object

Raises:



57
58
59
60
61
62
# 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, "    \#{str.inspect} does not represent a range of nodes\n  ERROR\nend\n"

.expand(nodes_string) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

.expand_range(prefix, range, suffix) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
# 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, "    '\#{range}' the minimum index can not be greater than the maximum\n  ERROR\n  (min .. max).map do |num|\n    sprintf(\"\#{prefix}%0\#{min_str.length}d\#{suffix}\", num)\n  end\nend\n" if min > max