Class: MuxTf::ResourceTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/mux_tf/resource_tokenizer.rb

Class Method Summary collapse

Class Method Details

.split(resource) ⇒ Object



5
6
7
# File 'lib/mux_tf/resource_tokenizer.rb', line 5

def self.split(resource)
  tokenize(resource).map(&:last)
end

.tokenize(resource) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/mux_tf/resource_tokenizer.rb', line 9

def self.tokenize(resource) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  result = []
  n = 0
  pn = 0
  state = :rt
  until n >= resource.length
    case state
    when :rt
      # looking for .
      if resource[n] == "."
        # reached the dot ..
        result << [:rt, resource[pn...n]]
        pn = n + 1
        state = :rn
      end
    when :rn
      # looking for [ or .
      if resource[n] == "."
        # reached the dot ..
        result << [:rn, resource[pn...n]]
        pn = n + 1
        state = :rt
      end
      if resource[n] == "["
        # reached the open bracket
        result << [:rn, resource[pn...n]]
        pn = n
        state = :ri
      end
      if n == resource.length - 1
        # last character .. close the current group
        # the last thing should only ever be an index or a name
        result << [:rn, resource[pn..n]]
        pn = n
        state = :done
      end
    when :ri
      # looking for ]
      if resource[n] == "]"
        # reached the close bracket
        result << [:ri, resource[pn..n]]
        pn = n + 1
        state = :rt
        if resource[n + 1] == "."
          pn = n + 2
          n += 1
        end
      end
    else
      warn "unhandled state: #{state.inspect}"
    end
    # p resource[n]
    n += 1
  end
  result
end