Class: SoftLayer::ObjectMaskTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer/ObjectMaskTokenizer.rb

Overview

This class is an implementation detail of the ObjectMaskParser

It takes an Object Mask String and breaks it down into ObjectMaskToken instances.

Constant Summary collapse

ObjectMask_Token_Specs =
[
  [/\[/, :property_set_start],
  [/\,/, :property_set_separator],
  [/\]/, :property_set_end],
  [/\(/, :property_type_start],
  [/\)/, :property_type_end],
  [/\./, :property_child_separator],
  [/[a-z][a-z0-9_]*/i, :identifier]
]

Instance Method Summary collapse

Constructor Details

#initialize(mask_string) ⇒ ObjectMaskTokenizer

Returns a new instance of ObjectMaskTokenizer.



44
45
46
47
48
# File 'lib/softlayer/ObjectMaskTokenizer.rb', line 44

def initialize(mask_string)
  @mask_string = mask_string.clone
  @scanner = StringScanner.new(@mask_string)
  @current_token = nil
end

Instance Method Details

#current_tokenObject



54
55
56
57
# File 'lib/softlayer/ObjectMaskTokenizer.rb', line 54

def current_token
  @current_token = next_token if !@current_token
  @current_token
end

#more_tokens?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/softlayer/ObjectMaskTokenizer.rb', line 50

def more_tokens?
  return @current_token == nil || !@current_token.end_of_string?
end

#next_tokenObject



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/softlayer/ObjectMaskTokenizer.rb', line 59

def next_token
  # if we're at the end of the string, we keep returning the
  # EOS token
  if more_tokens? then

    if !@scanner.eos?
      # skip whitespace
      @scanner.skip(/\s+/)

      # search through the token specs to find which (if any) matches
      token_spec = ObjectMask_Token_Specs.find() do |token_spec|
        @scanner.check(token_spec[0])
      end

      # if a good token spec was found, set the current token to the one found
      if token_spec
        @current_token = ObjectMaskToken.new(token_spec.last, @scanner.scan(token_spec[0]))
      else
        @current_token = ObjectMaskToken.new(:invalid_token, @scanner.rest)
        @scanner.terminate
      end
    else
      @current_token = ObjectMaskToken.new(:eos)
    end
  end

  @current_token
end