Module: DTRCore::Common

Included in:
Function, InstructionValidator, Parser, State, TypeValidator, UserDefinedType
Defined in:
lib/dtr_core/common.rb

Overview

Common methods used by the DTRCore module.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_input_content(some_string) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/dtr_core/common.rb', line 24

def self.get_input_content(some_string)
  start_index = some_string.index('input:')
  return nil if start_index.nil?

  start_parse_parent_index = some_string.index('(', start_index)

  paren_contents(some_string[start_parse_parent_index..])
end

.nested_array_to_string(array) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/dtr_core/common.rb', line 88

def self.nested_array_to_string(array)
  # Base case: if the element is not an array, return the element as a string
  return array.to_s unless array.is_a?(Array)

  # Recursively process each element in the array and join them with commas
  elements = array.map { |element| nested_array_to_string(element) }.join(', ')

  # Wrap the joined elements with parentheses
  "(#{elements})"
end

.paren_contents(some_string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
81
82
83
84
85
86
# File 'lib/dtr_core/common.rb', line 33

def self.paren_contents(some_string)
  cur_word = ''
  index = 0
  cur_words = []
  how_many_deep_soft_brackets = 0
  how_many_deep_hard_brackets = 0
  how_many_deep_square_brackets = 0
  in_string = false

  while index < some_string.length
    char = some_string[index]

    if char == ')' && !in_string
      how_many_deep_soft_brackets -= 1

      if how_many_deep_soft_brackets.zero?
        cur_words.push(cur_word.strip) unless cur_word.empty?
        return cur_words
      else
        cur_word += char
      end
    elsif char == '[' && !in_string
      how_many_deep_square_brackets += 1
      cur_word += char
    elsif char == ']' && !in_string
      how_many_deep_square_brackets -= 1
      cur_word += char
    elsif char == '{' && !in_string
      how_many_deep_hard_brackets += 1
      cur_word += char
    elsif char == '}' && !in_string
      how_many_deep_hard_brackets -= 1
      cur_word += char
    elsif ['\'', '"'].include?(char)
      in_string = in_string ? false : true
      cur_word += char
    elsif char == '(' && !in_string
      how_many_deep_soft_brackets += 1
      cur_word += char if how_many_deep_soft_brackets > 1
    elsif char == ',' && how_many_deep_soft_brackets == 1 &&
          how_many_deep_hard_brackets.zero? &&
          how_many_deep_square_brackets.zero? &&
          !in_string
      cur_words.push(cur_word.strip) unless cur_word.empty?
      cur_word = ''
    else
      cur_word += char
    end

    index += 1
  end

  raise 'No closing parenthesis found.'
end

.parse_instruction_input(instruction) ⇒ Object



99
100
101
102
103
# File 'lib/dtr_core/common.rb', line 99

def self.parse_instruction_input(instruction)
  result = get_input_content(instruction)

  result.nil? || result.empty? ? nil : result
end

Instance Method Details

#capture_section(pattern) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dtr_core/common.rb', line 10

def capture_section(pattern)
  captures = content.match(pattern)&.captures

  if content.scan(pattern).length > 1
    raise 'Multiple captures found for a section.'
  elsif captures
    captures&.first
  end
end

#clean_name(definition) ⇒ Object



20
21
22
# File 'lib/dtr_core/common.rb', line 20

def clean_name(definition)
  definition.gsub(/[\*\n\[]/, '').strip
end

#split_strip_select(some_list) ⇒ Object



6
7
8
# File 'lib/dtr_core/common.rb', line 6

def split_strip_select(some_list)
  some_list&.split("\n")&.map(&:strip)&.select { |x| x.length.positive? }
end