about summary refs log tree commit diff stats
path: root/src/jsonf.lua
blob: aec0e0a9126b317ed56b3fc0e20ed0b15341a309 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
200
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
--
-- variant of https://github.com/rxi/json.lua decoding from channels of
-- characters rather than strings
--
-- Copyright (c) 2020 rxi
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--

local jsonf = { _version = "0.1.2" }

local escape_char_map = {
  [ "\\" ] = "\\",
  [ "\"" ] = "\"",
  [ "\b" ] = "b",
  [ "\f" ] = "f",
  [ "\n" ] = "n",
  [ "\r" ] = "r",
  [ "\t" ] = "t",
}

local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
  escape_char_map_inv[v] = k
end


-------------------------------------------------------------------------------
-- Decode
-------------------------------------------------------------------------------

local function create_set(...)
  local res = {}
  for i = 1, select("#", ...) do
    res[ select(i, ...) ] = true
  end
  return res
end

local space_chars   = create_set(" ", "\t", "\r", "\n")
local delim_chars   = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars  = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals      = create_set("true", "false", "null")

local literal_map = {
  [ "true"  ] = true,
  [ "false" ] = false,
  [ "null"  ] = nil,
}


local function skip_spaces(infile)
  while true do
    local c = infile.read(1)
    if c == nil then break end
    if space_chars[c] == nil then return c end
  end
  return nil
end


local function next_chars(infile, set, firstc)
  local res = {firstc}
  local nextc
  while true do
    nextc = infile.read(1)
    if nextc == nil then break end
    if set[nextc] then break end
    table.insert(res, nextc)
  end
  return table.concat(res), nextc
end


local function codepoint_to_utf8(n)
  -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
  local f = math.floor
  if n <= 0x7f then
    return string.char(n)
  elseif n <= 0x7ff then
    return string.char(f(n / 64) + 192, n % 64 + 128)
  elseif n <= 0xffff then
    return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
  elseif n <= 0x10ffff then
    return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
                       f(n % 4096 / 64) + 128, n % 64 + 128)
  end
  error( string.format("invalid unicode codepoint '%x'", n) )
end


local function parse_unicode_escape(s)
  local n1 = tonumber( s:sub(1, 4),  16 )
  local n2 = tonumber( s:sub(7, 10), 16 )
   -- Surrogate pair?
  if n2 then
    return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
  else
    return codepoint_to_utf8(n1)
  end
end


local function parse_string(infile, firstc)
  local res = {}

  while true do
    local chr = infile.read(1)
    if chr == nil then break end
    local x = chr:byte()

    if x < 32 then
      error("control character in string")
    elseif chr == '\\' then
      local c = infile.read(1)
      if c == nil then break end
      if c == "u" then
        local hex = ''
        c = infile.read(1)
        if c == nil then break end
        hex = hex..c
        c = infile.read(1)
        if c == nil then break end
        hex = hex..c
        c = infile.read(1)
        if c == nil then break end
        hex = hex..c
        c = infile.read(1)
        if c == nil then break end
        hex = hex..c
        if not hex:match('^%x%x%x%x') then
          error('invalid unicode escape in string')
        end
        table.insert(res, parse_unicode_escape(hex))
      else
        if not escape_chars[c] then
          error("invalid escape char '" .. c .. "' in string")
        end
        table.insert(res, escape_char_map_inv[c])
      end
    elseif chr == '"' then
      return table.concat(res), infile.read(1)
    else
      table.insert(res, chr)
    end
  end

  error("expected closing quote for string")
end


local function parse_number(infile, firstc)
--?   print('parse_number')
  local res = {firstc}
  local nextc
  while true do
    nextc = infile.read(1)
    if nextc == nil then break end
    if delim_chars[nextc] then break end
    table.insert(res, nextc)
  end
  local s = table.concat(res)
--?   print('parse_number: '..s)
  local n = tonumber(s)
  if not n then
    error("invalid number '" .. s .. "'")
  end
  return n, nextc
end


local function parse_literal(infile, firstc)
--?   print('parse_literal')
  local word, nextc = next_chars(infile, delim_chars, firstc)
  if not literals[word] then
    error("invalid literal '" .. word .. "'")
  end
--?   print('parse_literal: '..word)
  return literal_map[word], nextc
end


local function parse_array(infile, firstc)
  local res = {}
  local x, nextc
  while true do
    nextc = skip_spaces(infile)
    if nextc == nil then
      error("expected ']' or ','")
    end
    if nextc == ']' then break end  -- empty array
    -- Read token
    x, nextc = parse(infile, nextc)
--?     print('array elem: '..str(x))
    table.insert(res, x)
    -- Next token
    if space_chars[nextc] then
      nextc = skip_spaces(infile)
    end
    if nextc == ']' then break end
    if nextc ~= ',' then
      error("expected ']' or ','")
    end
  end
  return res, skip_spaces(infile)
end


local function parse_object(infile, firstc)
  local res = {}
  local nextc
  while true do
    local key, val
    nextc = skip_spaces(infile)
    if nextc == nil then
      error("expected '}' or ','")
    end
    if nextc == '}' then break end  -- empty object
    -- Read key
    if nextc ~= '"' then
      error("expected string for key")
    end
    key, nextc = parse(infile, nextc)
--?     print('object key: '..key)
    -- Read ':' delimiter
    if space_chars[nextc] then
      nextc = skip_spaces(infile)
    end
    if nextc ~= ':' then
      error("expected ':' after key")
    end
    -- Read value
    nextc = skip_spaces(infile)
    val, nextc = parse(infile, nextc)
--?     print('object val: '..str(val))
    -- Set
    res[key] = val
    -- Next token
    if space_chars[nextc] then
      nextc = skip_spaces(infile)
    end
    if nextc == '}' then break end
    if nextc ~= ',' then
      error("expected '}' or ','")
    end
  end
  return res, skip_spaces(infile)
end


local char_func_map = {
  [ '"' ] = parse_string,
  [ "0" ] = parse_number,
  [ "1" ] = parse_number,
  [ "2" ] = parse_number,
  [ "3" ] = parse_number,
  [ "4" ] = parse_number,
  [ "5" ] = parse_number,
  [ "6" ] = parse_number,
  [ "7" ] = parse_number,
  [ "8" ] = parse_number,
  [ "9" ] = parse_number,
  [ "-" ] = parse_number,
  [ "t" ] = parse_literal,
  [ "f" ] = parse_literal,
  [ "n" ] = parse_literal,
  [ "[" ] = parse_array,
  [ "{" ] = parse_object,
}


parse = function(infile, chr)
  local f = char_func_map[chr]
  if f then
    return f(infile, chr)
  end
  error("unexpected character '" .. chr .. "'")
end


function jsonf.decode(infile)
  local firstc = skip_spaces(infile)
  local res, nextc = parse(infile, firstc)
  if nextc then
    error("trailing garbage")
  end
  return res
end


-- test cases:
--   "abc"
--   234
--   true
--   false
--   nil
--   ["abc", 234, true, false, nil]
--   ["abc", 234, true, false, nil
--   ["abc",
--   {"abc": 234, "def": true}

return jsonf