Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
ml_json.sas
Go to the documentation of this file.
1/**
2 @file ml_json.sas
3 @brief Compiles the json.lua lua file
4 @details Writes json.lua to the work directory
5 and then includes it.
6 Usage:
7
8 %ml_json()
9
10 @sasjslint {"maxLineLength": 92}
11**/
12
13%macro ml_json();
14/* a narrow LRECL truncates the wide lines of the library as they
15 are written, so widen it for the write as well as the include */
16%local optval;
17%let optval=%sysfunc(getoption(lrecl));
18options lrecl=1024;
19
20data _null_;
21 file "%sysfunc(pathname(work))/ml_json.lua";
22 put '-- NOTE - THE COPYRIGHT BELOW IS IN RELATION TO THE JSON.LUA FILE ONLY ';
23 put '-- THIS FILE STARTS ON THE NEXT LINE AND WILL FINISH WITH "JSON.LUA ENDS HERE" ';
24 put '-- ';
25 put '-- json.lua ';
26 put '-- ';
27 put '-- Copyright (c) 2019 rxi ';
28 put '-- ';
29 put '-- Permission is hereby granted, free of charge, to any person obtaining a copy of ';
30 put '-- this software and associated documentation files (the "Software"), to deal in ';
31 put '-- the Software without restriction, including without limitation the rights to ';
32 put '-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ';
33 put '-- of the Software, and to permit persons to whom the Software is furnished to do ';
34 put '-- so, subject to the following conditions: ';
35 put '-- ';
36 put '-- The above copyright notice and this permission notice shall be included in all ';
37 put '-- copies or substantial portions of the Software. ';
38 put '-- ';
39 put '-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ';
40 put '-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ';
41 put '-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ';
42 put '-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ';
43 put '-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ';
44 put '-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ';
45 put '-- SOFTWARE. ';
46 put '-- ';
47 put ' ';
48 put 'json = { _version = "0.1.2" } ';
49 put ' ';
50 put '------------------------------------------------------------------------------- ';
51 put '-- Encode ';
52 put '------------------------------------------------------------------------------- ';
53 put ' ';
54 put 'local encode ';
55 put ' ';
56 put 'local escape_char_map = { ';
57 put ' [ "\\" ] = "\\\\", ';
58 put ' [ "\"" ] = "\\\"", ';
59 put ' [ "\b" ] = "\\b", ';
60 put ' [ "\f" ] = "\\f", ';
61 put ' [ "\n" ] = "\\n", ';
62 put ' [ "\r" ] = "\\r", ';
63 put ' [ "\t" ] = "\\t", ';
64 put '} ';
65 put ' ';
66 put 'local escape_char_map_inv = { [ "\\/" ] = "/" } ';
67 put 'for k, v in pairs(escape_char_map) do ';
68 put ' escape_char_map_inv[v] = k ';
69 put 'end ';
70 put ' ';
71 put 'local function escape_char(c) ';
72 put ' return escape_char_map[c] or string.format("\\u%04x", c:byte()) ';
73 put 'end ';
74 put ' ';
75 put 'local function encode_nil(val) ';
76 put ' return "null" ';
77 put 'end ';
78 put ' ';
79 put 'local function encode_table(val, stack) ';
80 put ' local res = {} ';
81 put ' stack = stack or {} ';
82 put ' ';
83 put ' -- Circular reference? ';
84 put ' if stack[val] then error("circular reference") end ';
85 put ' ';
86 put ' stack[val] = true ';
87 put ' ';
88 put ' if rawget(val, 1) ~= nil or next(val) == nil then ';
89 put ' -- Treat as array -- check keys are valid and it is not sparse ';
90 put ' local n = 0 ';
91 put ' for k in pairs(val) do ';
92 put ' if type(k) ~= "number" then ';
93 put ' error("invalid table: mixed or invalid key types") ';
94 put ' end ';
95 put ' n = n + 1 ';
96 put ' end ';
97 put ' if n ~= #val then ';
98 put ' error("invalid table: sparse array") ';
99 put ' end ';
100 put ' -- Encode ';
101 put ' for i, v in ipairs(val) do ';
102 put ' table.insert(res, encode(v, stack)) ';
103 put ' end ';
104 put ' stack[val] = nil ';
105 put ' return "[" .. table.concat(res, ",") .. "]" ';
106 put ' else ';
107 put ' -- Treat as an object ';
108 put ' for k, v in pairs(val) do ';
109 put ' if type(k) ~= "string" then ';
110 put ' error("invalid table: mixed or invalid key types") ';
111 put ' end ';
112 put ' table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) ';
113 put ' end ';
114 put ' stack[val] = nil ';
115 put ' return "{" .. table.concat(res, ",") .. "}" ';
116 put ' end ';
117 put 'end ';
118 put ' ';
119 put 'local function encode_string(val) ';
120 put ' return ''"'' .. val:gsub(''[%z\1-\31\\"]'', escape_char) .. ''"'' ';
121 put 'end ';
122 put ' ';
123 put 'local function encode_number(val) ';
124 put ' -- Check for NaN, -inf and inf ';
125 put ' if val ~= val or val <= -math.huge or val >= math.huge then ';
126 put ' error("unexpected number value ''" .. tostring(val) .. "''") ';
127 put ' end ';
128 put ' return string.format("%.14g", val) ';
129 put 'end ';
130 put ' ';
131 put 'local type_func_map = { ';
132 put ' [ "nil" ] = encode_nil, ';
133 put ' [ "table" ] = encode_table, ';
134 put ' [ "string" ] = encode_string, ';
135 put ' [ "number" ] = encode_number, ';
136 put ' [ "boolean" ] = tostring, ';
137 put '} ';
138 put ' ';
139 put 'encode = function(val, stack) ';
140 put ' local t = type(val) ';
141 put ' local f = type_func_map[t] ';
142 put ' if f then ';
143 put ' return f(val, stack) ';
144 put ' end ';
145 put ' error("unexpected type ''" .. t .. "''") ';
146 put 'end ';
147 put ' ';
148 put 'function json.encode(val) ';
149 put ' return ( encode(val) ) ';
150 put 'end ';
151 put ' ';
152 put '------------------------------------------------------------------------------- ';
153 put '-- Decode ';
154 put '------------------------------------------------------------------------------- ';
155 put 'local parse ';
156 put 'local function create_set(...) ';
157 put ' local res = {} ';
158 put ' for i = 1, select("#", ...) do ';
159 put ' res[ select(i, ...) ] = true ';
160 put ' end ';
161 put ' return res ';
162 put 'end ';
163 put ' ';
164 put 'local space_chars = create_set(" ", "\t", "\r", "\n") ';
165 put 'local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") ';
166 put 'local escape_chars = create_set("\\", "/", ''"'', "b", "f", "n", "r", "t", "u") ';
167 put 'local literals = create_set("true", "false", "null") ';
168 put ' ';
169 put 'local literal_map = { ';
170 put ' [ "true" ] = true, ';
171 put ' [ "false" ] = false, ';
172 put ' [ "null" ] = nil, ';
173 put '} ';
174 put ' ';
175 put 'local function next_char(str, idx, set, negate) ';
176 put ' for i = idx, #str do ';
177 put ' if set[str:sub(i, i)] ~= negate then ';
178 put ' return i ';
179 put ' end ';
180 put ' end ';
181 put ' return #str + 1 ';
182 put 'end ';
183 put ' ';
184 put 'local function decode_error(str, idx, msg) ';
185 put ' local line_count = 1 ';
186 put ' local col_count = 1 ';
187 put ' for i = 1, idx - 1 do ';
188 put ' col_count = col_count + 1 ';
189 put ' if str:sub(i, i) == "\n" then ';
190 put ' line_count = line_count + 1 ';
191 put ' col_count = 1 ';
192 put ' end ';
193 put ' end ';
194 put ' error( string.format("%s at line %d col %d", msg, line_count, col_count) ) ';
195 put 'end ';
196 put ' ';
197 put 'local function codepoint_to_utf8(n) ';
198 put ' -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa ';
199 put ' local f = math.floor ';
200 put ' if n <= 0x7f then ';
201 put ' return string.char(n) ';
202 put ' elseif n <= 0x7ff then ';
203 put ' return string.char(f(n / 64) + 192, n % 64 + 128) ';
204 put ' elseif n <= 0xffff then ';
205 put ' return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) ';
206 put ' elseif n <= 0x10ffff then ';
207 put ' return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, ';
208 put ' f(n % 4096 / 64) + 128, n % 64 + 128) ';
209 put ' end ';
210 put ' error( string.format("invalid unicode codepoint ''%x''", n) ) ';
211 put 'end ';
212 put ' ';
213 put 'local function parse_unicode_escape(s) ';
214 put ' local n1 = tonumber( s:sub(3, 6), 16 ) ';
215 put ' local n2 = tonumber( s:sub(9, 12), 16 ) ';
216 put ' -- Surrogate pair? ';
217 put ' if n2 then ';
218 put ' return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) ';
219 put ' else ';
220 put ' return codepoint_to_utf8(n1) ';
221 put ' end ';
222 put 'end ';
223 put ' ';
224 put 'local function parse_string(str, i) ';
225 put ' local has_unicode_escape = false ';
226 put ' local has_surrogate_escape = false ';
227 put ' local has_escape = false ';
228 put ' local last ';
229 put ' for j = i + 1, #str do ';
230 put ' local x = str:byte(j) ';
231 put ' if x < 32 then ';
232 put ' decode_error(str, j, "control character in string") ';
233 put ' end ';
234 put ' if last == 92 then -- "\\" (escape char) ';
235 put ' if x == 117 then -- "u" (unicode escape sequence) ';
236 put ' local hex = str:sub(j + 1, j + 5) ';
237 put ' if not hex:find("%x%x%x%x") then ';
238 put ' decode_error(str, j, "invalid unicode escape in string") ';
239 put ' end ';
240 put ' if hex:find("^[dD][89aAbB]") then ';
241 put ' has_surrogate_escape = true ';
242 put ' else ';
243 put ' has_unicode_escape = true ';
244 put ' end ';
245 put ' else ';
246 put ' local c = string.char(x) ';
247 put ' if not escape_chars[c] then ';
248 put ' decode_error(str, j, "invalid escape char ''" .. c .. "'' in string") ';
249 put ' end ';
250 put ' has_escape = true ';
251 put ' end ';
252 put ' last = nil ';
253 put ' elseif x == 34 then -- ''"'' (end of string) ';
254 put ' local s = str:sub(i + 1, j - 1) ';
255 put ' if has_surrogate_escape then ';
256 put ' s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) ';
257 put ' end ';
258 put ' if has_unicode_escape then ';
259 put ' s = s:gsub("\\u....", parse_unicode_escape) ';
260 put ' end ';
261 put ' if has_escape then ';
262 put ' s = s:gsub("\\.", escape_char_map_inv) ';
263 put ' end ';
264 put ' return s, j + 1 ';
265 put ' else ';
266 put ' last = x ';
267 put ' end ';
268 put ' end ';
269 put ' decode_error(str, i, "expected closing quote for string") ';
270 put 'end ';
271 put ' ';
272 put 'local function parse_number(str, i) ';
273 put ' local x = next_char(str, i, delim_chars) ';
274 put ' local s = str:sub(i, x - 1) ';
275 put ' local n = tonumber(s) ';
276 put ' if not n then ';
277 put ' decode_error(str, i, "invalid number ''" .. s .. "''") ';
278 put ' end ';
279 put ' return n, x ';
280 put 'end ';
281 put ' ';
282 put 'local function parse_literal(str, i) ';
283 put ' local x = next_char(str, i, delim_chars) ';
284 put ' local word = str:sub(i, x - 1) ';
285 put ' if not literals[word] then ';
286 put ' decode_error(str, i, "invalid literal ''" .. word .. "''") ';
287 put ' end ';
288 put ' return literal_map[word], x ';
289 put 'end ';
290 put ' ';
291 put 'local function parse_array(str, i) ';
292 put ' local res = {} ';
293 put ' local n = 1 ';
294 put ' i = i + 1 ';
295 put ' while 1 do ';
296 put ' local x ';
297 put ' i = next_char(str, i, space_chars, true) ';
298 put ' -- Empty / end of array? ';
299 put ' if str:sub(i, i) == "]" then ';
300 put ' i = i + 1 ';
301 put ' break ';
302 put ' end ';
303 put ' -- Read token ';
304 put ' x, i = parse(str, i) ';
305 put ' res[n] = x ';
306 put ' n = n + 1 ';
307 put ' -- Next token ';
308 put ' i = next_char(str, i, space_chars, true) ';
309 put ' local chr = str:sub(i, i) ';
310 put ' i = i + 1 ';
311 put ' if chr == "]" then break end ';
312 put ' if chr ~= "," then decode_error(str, i, "expected '']'' or '',''") end ';
313 put ' end ';
314 put ' return res, i ';
315 put 'end ';
316 put ' ';
317 put 'local function parse_object(str, i) ';
318 put ' local res = {} ';
319 put ' i = i + 1 ';
320 put ' while 1 do ';
321 put ' local key, val ';
322 put ' i = next_char(str, i, space_chars, true) ';
323 put ' -- Empty / end of object? ';
324 put ' if str:sub(i, i) == "}" then ';
325 put ' i = i + 1 ';
326 put ' break ';
327 put ' end ';
328 put ' -- Read key ';
329 put ' if str:sub(i, i) ~= ''"'' then ';
330 put ' decode_error(str, i, "expected string for key") ';
331 put ' end ';
332 put ' key, i = parse(str, i) ';
333 put ' -- Read '':'' delimiter ';
334 put ' i = next_char(str, i, space_chars, true) ';
335 put ' if str:sub(i, i) ~= ":" then ';
336 put ' decode_error(str, i, "expected '':'' after key") ';
337 put ' end ';
338 put ' i = next_char(str, i + 1, space_chars, true) ';
339 put ' -- Read value ';
340 put ' val, i = parse(str, i) ';
341 put ' -- Set ';
342 put ' res[key] = val ';
343 put ' -- Next token ';
344 put ' i = next_char(str, i, space_chars, true) ';
345 put ' local chr = str:sub(i, i) ';
346 put ' i = i + 1 ';
347 put ' if chr == "}" then break end ';
348 put ' if chr ~= "," then decode_error(str, i, "expected ''}'' or '',''") end ';
349 put ' end ';
350 put ' return res, i ';
351 put 'end ';
352 put ' ';
353 put 'local char_func_map = { ';
354 put ' [ ''"'' ] = parse_string, ';
355 put ' [ "0" ] = parse_number, ';
356 put ' [ "1" ] = parse_number, ';
357 put ' [ "2" ] = parse_number, ';
358 put ' [ "3" ] = parse_number, ';
359 put ' [ "4" ] = parse_number, ';
360 put ' [ "5" ] = parse_number, ';
361 put ' [ "6" ] = parse_number, ';
362 put ' [ "7" ] = parse_number, ';
363 put ' [ "8" ] = parse_number, ';
364 put ' [ "9" ] = parse_number, ';
365 put ' [ "-" ] = parse_number, ';
366 put ' [ "t" ] = parse_literal, ';
367 put ' [ "f" ] = parse_literal, ';
368 put ' [ "n" ] = parse_literal, ';
369 put ' [ "[" ] = parse_array, ';
370 put ' [ "{" ] = parse_object, ';
371 put '} ';
372 put ' ';
373 put 'parse = function(str, idx) ';
374 put ' local chr = str:sub(idx, idx) ';
375 put ' local f = char_func_map[chr] ';
376 put ' if f then ';
377 put ' return f(str, idx) ';
378 put ' end ';
379 put ' decode_error(str, idx, "unexpected character ''" .. chr .. "''") ';
380 put 'end ';
381 put ' ';
382 put 'function json.decode(str) ';
383 put ' if type(str) ~= "string" then ';
384 put ' error("expected argument of type string, got " .. type(str)) ';
385 put ' end ';
386 put ' local res, idx = parse(str, next_char(str, 1, space_chars, true)) ';
387 put ' idx = next_char(str, idx, space_chars, true) ';
388 put ' if idx <= #str then ';
389 put ' decode_error(str, idx, "trailing garbage") ';
390 put ' end ';
391 put ' return res ';
392 put 'end ';
393 put ' ';
394 put 'return json ';
395 put ' ';
396 put '-- JSON.LUA ENDS HERE ';
397run;
398
399/* execute the lua code by using a .lua extension */
400%inc "%sysfunc(pathname(work))/ml_json.lua" /source2;
401
402options lrecl=&optval;
403
404%mend ml_json;