Class: Haxe::Format::JsonParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/haxe/format/json_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ JsonParser

Returns a new instance of JsonParser.



8
9
10
11
# File 'lib/lib/haxe/format/json_parser.rb', line 8

def initialize(str)
  @str = str
  @pos = 0
end

Instance Attribute Details

#posObject

Returns the value of attribute pos.



16
17
18
# File 'lib/lib/haxe/format/json_parser.rb', line 16

def pos
  @pos
end

#strObject

protected - in ruby this doesn’t play well with static/inline methods



15
16
17
# File 'lib/lib/haxe/format/json_parser.rb', line 15

def str
  @str
end

Instance Method Details

#invalid_charObject



252
253
254
255
# File 'lib/lib/haxe/format/json_parser.rb', line 252

def invalid_char 
  @pos-=1
  raise hx_raise("Invalid char " + _hx_str((@str[@pos] || 0).ord) + " at position " + _hx_str(@pos))
end

#invalid_number(start) ⇒ Object



257
258
259
# File 'lib/lib/haxe/format/json_parser.rb', line 257

def invalid_number(start)
  raise hx_raise("Invalid number at position " + _hx_str(start) + ": " + _hx_str(@str[start,@pos - start]))
end

#parse_recObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/lib/haxe/format/json_parser.rb', line 18

def parse_rec 
  while(true) 
    c = nil
    begin
      index = @pos
      @pos+=1
      c = (@str[index] || 0).ord
    end
    case(c)
    when 32,13,10,9
    when 123
      obj = { }
      field = nil
      comma = nil
      while(true) 
        c1 = nil
        begin
          index1 = @pos
          @pos+=1
          c1 = (@str[index1] || 0).ord
        end
        case(c1)
        when 32,13,10,9
        when 125
          self.invalid_char if field != nil || comma == false
          return obj
        when 58
          self.invalid_char if field == nil
          begin
            value = self.parse_rec
            obj[field] = value
          end
          field = nil
          comma = true
        when 44
          if comma 
            comma = false
          else 
            self.invalid_char
          end
        when 34
          self.invalid_char if comma
          field = self.parse_string
        else
          self.invalid_char
        end
      end
    when 91
      arr = []
      comma1 = nil
      while(true) 
        c2 = nil
        begin
          index2 = @pos
          @pos+=1
          c2 = (@str[index2] || 0).ord
        end
        case(c2)
        when 32,13,10,9
        when 93
          self.invalid_char if comma1 == false
          return arr
        when 44
          if comma1 
            comma1 = false
          else 
            self.invalid_char
          end
        else
          self.invalid_char if comma1
          @pos-=1
          arr.push(self.parse_rec)
          comma1 = true
        end
      end
    when 116
      save = @pos
      if lambda{|_this_| index3 = @pos
      @pos+=1
      _r = (@str[index3] || 0).ord}.call(self) != 114 || lambda{|_this_| index4 = @pos
      @pos+=1
      _r2 = (@str[index4] || 0).ord}.call(self) != 117 || lambda{|_this_| index5 = @pos
      @pos+=1
      _r3 = (@str[index5] || 0).ord}.call(self) != 101 
        @pos = save
        self.invalid_char
      end
      return true
    when 102
      save1 = @pos
      if lambda{|_this_| index6 = @pos
      @pos+=1
      _r4 = (@str[index6] || 0).ord}.call(self) != 97 || lambda{|_this_| index7 = @pos
      @pos+=1
      _r5 = (@str[index7] || 0).ord}.call(self) != 108 || lambda{|_this_| index8 = @pos
      @pos+=1
      _r6 = (@str[index8] || 0).ord}.call(self) != 115 || lambda{|_this_| index9 = @pos
      @pos+=1
      _r7 = (@str[index9] || 0).ord}.call(self) != 101 
        @pos = save1
        self.invalid_char
      end
      return false
    when 110
      save2 = @pos
      if lambda{|_this_| index10 = @pos
      @pos+=1
      _r8 = (@str[index10] || 0).ord}.call(self) != 117 || lambda{|_this_| index11 = @pos
      @pos+=1
      _r9 = (@str[index11] || 0).ord}.call(self) != 108 || lambda{|_this_| index12 = @pos
      @pos+=1
      _r10 = (@str[index12] || 0).ord}.call(self) != 108 
        @pos = save2
        self.invalid_char
      end
      return nil
    when 34
      return self.parse_string
    when 48,49,50,51,52,53,54,55,56,57,45
      c3 = c
      start = @pos - 1
      minus = c3 == 45
      digit = !minus
      zero = c3 == 48
      point = false
      e = false
      pm = false
      _end = false
      while(true) 
        begin
          index13 = @pos
          @pos+=1
          c3 = (@str[index13] || 0).ord
        end
        case(c3)
        when 48
          self.invalid_number(start) if zero && !point
          if minus 
            minus = false
            zero = true
          end
          digit = true
        when 49,50,51,52,53,54,55,56,57
          self.invalid_number(start) if zero && !point
          minus = false if minus
          digit = true
          zero = false
        when 46
          self.invalid_number(start) if minus || point
          digit = false
          point = true
        when 101,69
          self.invalid_number(start) if minus || zero || e
          digit = false
          e = true
        when 43,45
          self.invalid_number(start) if !e || pm
          digit = false
          pm = true
        else
          self.invalid_number(start) if !digit
          @pos-=1
          _end = true
        end
        break if _end
      end
      f = nil
      begin
        x = @str[start,@pos - start]
        f = x.to_f
      end
      i = f.to_i
      if i == f 
        return i
      else 
        return f
      end
    else
      self.invalid_char
    end
  end
end

#parse_stringObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/lib/haxe/format/json_parser.rb', line 201

def parse_string 
  start = @pos
  buf_b = ""
  while(true) 
    c = nil
    begin
      index = @pos
      @pos+=1
      c = (@str[index] || 0).ord
    end
    break if c == 34
    if c == 92 
      buf_b += @str[start,@pos - start - 1]
      begin
        index1 = @pos
        @pos+=1
        c = (@str[index1] || 0).ord
      end
      case(c)
      when 114
        buf_b += [13].pack("U")
      when 110
        buf_b += [10].pack("U")
      when 116
        buf_b += [9].pack("U")
      when 98
        buf_b += [8].pack("U")
      when 102
        buf_b += [12].pack("U")
      when 47,92,34
        buf_b += [c].pack("U")
      when 117
        uc = nil
        begin
          x = "0x" + _hx_str(@str[@pos,4])
          uc = x.to_i
        end
        @pos += 4
        buf_b += [uc].pack("U")
      else
        raise hx_raise("Invalid escape sequence \\" + _hx_str([c].pack("U")) + " at position " + _hx_str((@pos - 1)))
      end
      start = @pos
    elsif c == 0 
      raise hx_raise("Unclosed string")
    end
  end
  buf_b += @str[start,@pos - start - 1]
  buf_b
end