Sha256: b14e0e35bd38c809ccb5f5dc50000e9c11544e6ab411a3b102c2e69950d9afa5

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

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

  def self.tokenize(resource)
    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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
mux_tf-0.8.4 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.8.3 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.8.2 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.8.1 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.8.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.7.2 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.7.1 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.7.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.6.1 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.6.0 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.5.3 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.5.2 lib/mux_tf/resource_tokenizer.rb
mux_tf-0.5.1 lib/mux_tf/resource_tokenizer.rb