Class: SoftLayer::ObjectMaskParser

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

Overview

A parser that can examine and validate SoftLayer Object Mask strings

The Object Mask Parser parses Object Mask Strings into ObjectMaskProperty structures.

The Object Mask parser allows the Gem to merge Object Mask Strings to avoid errors from the SoftLayer API server about duplicate properties being provided when the same property is provided in different Object Masks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectMaskParser

Returns a new instance of ObjectMaskParser



27
28
29
# File 'lib/softlayer/ObjectMaskParser.rb', line 27

def initialize()
  @stack = []
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack



25
26
27
# File 'lib/softlayer/ObjectMaskParser.rb', line 25

def stack
  @stack
end

Instance Method Details

#parse(mask_string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/softlayer/ObjectMaskParser.rb', line 31

def parse(mask_string)
  @tokenizer = ObjectMaskTokenizer.new(mask_string)

  token = @tokenizer.current_token
  if token.type == :identifier
    property = parse_property(@tokenizer)
  elsif token.type == :property_set_start
    property_set = parse_property_set(@tokenizer)
  else
    raise ObjectMaskParserError, "A valid Object mask is a 'mask' or 'filterMask' root property, or a property set containing root properties" + ObjectMaskToken.error_for_unexpected_token(token)
  end

  recognize_token(@tokenizer, :eos, "Extraneous text after object mask: ")

  if property && (property.name != "mask" && property.name != "filterMask")
    raise ObjectMaskParserError, "Object Mask must begin with a 'mask' or 'filterMask' root property"
  end

  if property_set && property_set.find { |subproperty| subproperty.name != 'mask' && subproperty.name != 'filterMask' }
    raise ObjectMaskParserError, "A root property set must contain only 'mask' or 'filterMask' root properties"
  end

  property || property_set
end

#parse_property(tokenizer) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/softlayer/ObjectMaskParser.rb', line 79

def parse_property (tokenizer)
  property_name = nil
  property_type = nil
  property_children = nil

  property_name = parse_property_name(tokenizer)

  # look for a property type
  property_type = nil
  token = tokenizer.current_token
  if(token.type.equal?(:property_type_start))
    property_type = parse_property_type(tokenizer)
  end

  token = tokenizer.current_token
  if(token.type.equal?(:property_child_separator))
    property_children = [ parse_property_child(tokenizer) ]
  elsif (token.type.equal?(:property_set_start))
    property_children = parse_property_set(tokenizer)
  end

  new_property = ObjectMaskProperty.new(property_name, property_type)
  new_property.add_children(property_children) if property_children

  return new_property
end

#parse_property_child(tokenizer) ⇒ Object



106
107
108
109
# File 'lib/softlayer/ObjectMaskParser.rb', line 106

def parse_property_child(tokenizer)
  token = recognize_token(tokenizer, :property_child_separator, "Expected a '.': ")
  parse_property(tokenizer)
end

#parse_property_name(tokenizer) ⇒ Object



111
112
113
114
# File 'lib/softlayer/ObjectMaskParser.rb', line 111

def parse_property_name(tokenizer)
  token = recognize_token(tokenizer, :identifier, "Expected a valid property type: ") { |token| token.valid_property_name? }
  return token.value
end

#parse_property_sequence(tokenizer) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/softlayer/ObjectMaskParser.rb', line 63

def parse_property_sequence(tokenizer)
  first_property = parse_property(tokenizer)

  other_children = []
  token = tokenizer.current_token
  if(token.type.equal?(:property_set_separator))
    # skip the separator
    tokenizer.next_token

    # find another property
    other_children = parse_property_sequence(tokenizer)
  end

  return other_children.unshift(first_property)
end

#parse_property_set(tokenizer) ⇒ Object



56
57
58
59
60
61
# File 'lib/softlayer/ObjectMaskParser.rb', line 56

def parse_property_set(tokenizer)
  token = recognize_token(tokenizer, :property_set_start, "Expected '[': ")
  property_sequence = parse_property_sequence(tokenizer)
  token = recognize_token(tokenizer, :property_set_end, "Expected ']': ")
  property_sequence
end

#parse_property_type(tokenizer) ⇒ Object



116
117
118
119
120
121
# File 'lib/softlayer/ObjectMaskParser.rb', line 116

def parse_property_type(tokenizer)
  token = recognize_token(tokenizer, :property_type_start, "Expected '(': ")
  property_type = parse_property_type_name(tokenizer)
  token = recognize_token(tokenizer, :property_type_end, "Expected ')': ")
  return property_type
end

#parse_property_type_name(tokenizer) ⇒ Object



123
124
125
126
# File 'lib/softlayer/ObjectMaskParser.rb', line 123

def parse_property_type_name(tokenizer)
  token = recognize_token(tokenizer, :identifier, "Expected a valid property type: ") { |token| token.valid_property_type? }
  return token.value
end

#recognize_token(tokenizer, expected_type, error_string, &predicate) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/softlayer/ObjectMaskParser.rb', line 128

def recognize_token(tokenizer, expected_type, error_string, &predicate)
  token = tokenizer.current_token
  if token.type.equal?(expected_type) && (!predicate || predicate.call(token))
    tokenizer.next_token
  else
    raise ObjectMaskParserError, error_string + ObjectMaskToken.error_for_unexpected_token(token)
    token = nil;
  end

  return token
end