Sha256: 98a581d3a4c29c938d1b73f9d54c8b8e984f669c376ba081e92e63e761646f64

Contents?: true

Size: 1.71 KB

Versions: 9

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module MuxTf
  class ResourceTokenizer
    def self.split(resource)
      tokenize(resource).map(&:last)
    end

    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mux_tf-0.15.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.14.2 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.14.1 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.14.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.13.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.12.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.11.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.10.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.9.0 lib/mux_tf/resource_tokenizer.rb