Class: FixtureFox::Value

Inherits:
Token
  • Object
show all
Defined in:
lib/fixture_fox/token.rb

Instance Attribute Summary collapse

Attributes inherited from Token

#file, #initial_indent, #lineno, #litt, #pos, #value

Instance Method Summary collapse

Methods inherited from Token

#==, #error

Constructor Details

#initialize(file, lineno, initial_indent, pos, litt) ⇒ Value

Returns a new instance of Value.



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fixture_fox/token.rb', line 59

def initialize(file, lineno, initial_indent, pos, litt)
  super

  i = 1
  case litt
    when /^(nil)/, /^(null)/
      i += $1.size
      @klass = NilClass
      @value = nil

    when /^(true)/, /^(false)/
      i += $1.size
      @klass = TrueClass
      @value = ($1 == "true")

    when /^([-+]?\d+\.\d+)/
      i += $1.size
      @klass = Float
      @value = $1.to_f

    when /^([-+]?\d+)/
      i += $1.size
      @klass = Integer
      @value = $1.to_i

    when /^"/
      @value = ""
      @klass = String
      loop do
        i < litt.size or self.error("Unterminated string")
        if litt[i] == "\\"
          if litt[i+1] == "\\"
            @value += "\\"
            i += 2
          elsif litt[i+1] == '"'
            @value += '"'
            i += 2
          elsif litt[i+1] == 'n'
            @value += "\n"
            i += 2
          elsif litt[i+1] == 't'
            @value += "\t"
            i += 2
          else
            @value += "\\"
            i += 1
          end
        elsif litt[i] == '"'
          i += 1
          break
        else
          @value += litt[i]
          i += 1
        end
      end

    when /^'/
      @value = ""
      @klass = String
      loop do
        i < litt.size or self.error("Unterminated string")
        if litt[i] == "'"
          if litt[i+1] == "'"
            @value += "'"
            i += 2
          else
            i += 1
            break
          end
        else
          @value += litt[i]
          i += 1
        end
      end

    # Inline arrays are split on ',' and on blanks. '\' is the escape
    # character
    #
    # FIXME: Quotes doesn't override separator. Eg. this throws an
    # exception: ['hello world']
    when /^(\[\s*)(.*)\]$/
      i += $1.size
      elements = $2
      matches = elements.split(/(\s*(?<!(?<!\\{2})\\)[, ]\s*)/)

      @value = []
      while element = matches.shift
        unescaped_element = element.gsub(/\\/, "")
        @value << Value.new(file, lineno, initial_indent, i, unescaped_element).value
        i += element.size + (matches.shift&.size || 0)
      end
      @klass = Array

    when /^\{/
      i += litt.size
      @value = HashParser.parse(litt)
      @klass = Hash

    else
      i += litt.size
      @klass = String
      @value = litt.dup
  end

  (litt[i..-1] || "") =~ /^\s*(?:#.*)?$/ or self.error("Illegal characters after literal")
end

Instance Attribute Details

#klassObject (readonly)

NilClass, TrueClass, Integer, Float, String, Array



57
58
59
# File 'lib/fixture_fox/token.rb', line 57

def klass
  @klass
end

Instance Method Details

#to_sObject



166
167
168
169
170
171
172
173
174
# File 'lib/fixture_fox/token.rb', line 166

def to_s()
  if klass == NilClass
    "null"
#     elsif klass == String
#       "'#{value.gsub("'", "''")}'"
  else
    value.to_s
  end
end