Class: RLSL::BaseTranslator::CallParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rlsl/base_translator/call_parser.rb

Class Method Summary collapse

Class Method Details

.extract_group(segment, opening_index) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rlsl/base_translator/call_parser.rb', line 6

def self.extract_group(segment, opening_index)
  depth = 0
  index = opening_index
  quote = nil

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

    if quote
      quote = nil if char == quote && segment[index - 1] != "\\"
    else
      case char
      when '"', "'"
        quote = char
      when "(", "[", "{"
        depth += 1
      when ")", "]", "}"
        depth -= 1
        return [segment[(opening_index + 1)...index], index] if depth.zero?
      end
    end

    index += 1
  end

  raise ArgumentError, "Unbalanced delimiter in translation source"
end

.split_arguments(arguments_source) ⇒ Object



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
# File 'lib/rlsl/base_translator/call_parser.rb', line 34

def self.split_arguments(arguments_source)
  arguments = []
  depth = 0
  quote = nil
  start_index = 0

  arguments_source.each_char.with_index do |char, index|
    if quote
      quote = nil if char == quote && arguments_source[index - 1] != "\\"
      next
    end

    case char
    when '"', "'"
      quote = char
    when "(", "[", "{"
      depth += 1
    when ")", "]", "}"
      depth -= 1
    when ","
      next unless depth.zero?

      arguments << arguments_source[start_index...index]
      start_index = index + 1
    end
  end

  tail = arguments_source[start_index..]
  return [] if arguments.empty? && tail.to_s.strip.empty?

  arguments << tail
end