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