mirror of
https://github.com/sasjs/core.git
synced 2025-12-11 06:24:35 +00:00
feat(*): recreate library as scoped package
This commit is contained in:
455
lua/json2sas.lua
Normal file
455
lua/json2sas.lua
Normal file
@@ -0,0 +1,455 @@
|
||||
--
|
||||
-- json2sas.lua (modified from json.lua)
|
||||
--
|
||||
-- Copyright (c) 2019 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 json2sas = { _version = "0.1.2" }
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Encode
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
local encode
|
||||
|
||||
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
|
||||
|
||||
local function escape_char(c)
|
||||
return escape_char_map[c] or string.format("\\u%04x", c:byte())
|
||||
end
|
||||
|
||||
local function encode_nil(val)
|
||||
return "null"
|
||||
end
|
||||
|
||||
local function encode_table(val, stack)
|
||||
local res = {}
|
||||
stack = stack or {}
|
||||
|
||||
-- Circular reference?
|
||||
if stack[val] then error("circular reference") end
|
||||
|
||||
stack[val] = true
|
||||
|
||||
if rawget(val, 1) ~= nil or next(val) == nil then
|
||||
-- Treat as array -- check keys are valid and it is not sparse
|
||||
local n = 0
|
||||
for k in pairs(val) do
|
||||
if type(k) ~= "number" then
|
||||
error("invalid table: mixed or invalid key types")
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
if n ~= #val then
|
||||
error("invalid table: sparse array")
|
||||
end
|
||||
-- Encode
|
||||
for i, v in ipairs(val) do
|
||||
table.insert(res, encode(v, stack))
|
||||
end
|
||||
stack[val] = nil
|
||||
return "[" .. table.concat(res, ",") .. "]"
|
||||
else
|
||||
-- Treat as an object
|
||||
for k, v in pairs(val) do
|
||||
if type(k) ~= "string" then
|
||||
error("invalid table: mixed or invalid key types")
|
||||
end
|
||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
||||
end
|
||||
stack[val] = nil
|
||||
return "{" .. table.concat(res, ",") .. "}"
|
||||
end
|
||||
end
|
||||
|
||||
local function encode_string(val)
|
||||
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
|
||||
end
|
||||
|
||||
local function encode_number(val)
|
||||
-- Check for NaN, -inf and inf
|
||||
if val ~= val or val <= -math.huge or val >= math.huge then
|
||||
error("unexpected number value '" .. tostring(val) .. "'")
|
||||
end
|
||||
return string.format("%.14g", val)
|
||||
end
|
||||
|
||||
local type_func_map = {
|
||||
[ "nil" ] = encode_nil,
|
||||
[ "table" ] = encode_table,
|
||||
[ "string" ] = encode_string,
|
||||
[ "number" ] = encode_number,
|
||||
[ "boolean" ] = tostring,
|
||||
}
|
||||
|
||||
encode = function(val, stack)
|
||||
local t = type(val)
|
||||
local f = type_func_map[t]
|
||||
if f then
|
||||
return f(val, stack)
|
||||
end
|
||||
error("unexpected type '" .. t .. "'")
|
||||
end
|
||||
|
||||
function json2sas.encode(val)
|
||||
return ( encode(val) )
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Decode
|
||||
-------------------------------------------------------------------------------
|
||||
local parse
|
||||
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 next_char(str, idx, set, negate)
|
||||
for i = idx, #str do
|
||||
if set[str:sub(i, i)] ~= negate then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return #str + 1
|
||||
end
|
||||
|
||||
local function decode_error(str, idx, msg)
|
||||
local line_count = 1
|
||||
local col_count = 1
|
||||
for i = 1, idx - 1 do
|
||||
col_count = col_count + 1
|
||||
if str:sub(i, i) == "\n" then
|
||||
line_count = line_count + 1
|
||||
col_count = 1
|
||||
end
|
||||
end
|
||||
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
|
||||
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(3, 6), 16 )
|
||||
local n2 = tonumber( s:sub(9, 12), 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(str, i)
|
||||
local has_unicode_escape = false
|
||||
local has_surrogate_escape = false
|
||||
local has_escape = false
|
||||
local last
|
||||
for j = i + 1, #str do
|
||||
local x = str:byte(j)
|
||||
if x < 32 then
|
||||
decode_error(str, j, "control character in string")
|
||||
end
|
||||
if last == 92 then -- "\\" (escape char)
|
||||
if x == 117 then -- "u" (unicode escape sequence)
|
||||
local hex = str:sub(j + 1, j + 5)
|
||||
if not hex:find("%x%x%x%x") then
|
||||
decode_error(str, j, "invalid unicode escape in string")
|
||||
end
|
||||
if hex:find("^[dD][89aAbB]") then
|
||||
has_surrogate_escape = true
|
||||
else
|
||||
has_unicode_escape = true
|
||||
end
|
||||
else
|
||||
local c = string.char(x)
|
||||
if not escape_chars[c] then
|
||||
decode_error(str, j, "invalid escape char '" .. c .. "' in string")
|
||||
end
|
||||
has_escape = true
|
||||
end
|
||||
last = nil
|
||||
elseif x == 34 then -- '"' (end of string)
|
||||
local s = str:sub(i + 1, j - 1)
|
||||
if has_surrogate_escape then
|
||||
s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape)
|
||||
end
|
||||
if has_unicode_escape then
|
||||
s = s:gsub("\\u....", parse_unicode_escape)
|
||||
end
|
||||
if has_escape then
|
||||
s = s:gsub("\\.", escape_char_map_inv)
|
||||
end
|
||||
return s, j + 1
|
||||
else
|
||||
last = x
|
||||
end
|
||||
end
|
||||
decode_error(str, i, "expected closing quote for string")
|
||||
end
|
||||
|
||||
local function parse_number(str, i)
|
||||
local x = next_char(str, i, delim_chars)
|
||||
local s = str:sub(i, x - 1)
|
||||
local n = tonumber(s)
|
||||
if not n then
|
||||
decode_error(str, i, "invalid number '" .. s .. "'")
|
||||
end
|
||||
return n, x
|
||||
end
|
||||
|
||||
local function parse_literal(str, i)
|
||||
local x = next_char(str, i, delim_chars)
|
||||
local word = str:sub(i, x - 1)
|
||||
if not literals[word] then
|
||||
decode_error(str, i, "invalid literal '" .. word .. "'")
|
||||
end
|
||||
return literal_map[word], x
|
||||
end
|
||||
|
||||
local function parse_array(str, i)
|
||||
local res = {}
|
||||
local n = 1
|
||||
i = i + 1
|
||||
while 1 do
|
||||
local x
|
||||
i = next_char(str, i, space_chars, true)
|
||||
-- Empty / end of array?
|
||||
if str:sub(i, i) == "]" then
|
||||
i = i + 1
|
||||
break
|
||||
end
|
||||
-- Read token
|
||||
x, i = parse(str, i)
|
||||
res[n] = x
|
||||
n = n + 1
|
||||
-- Next token
|
||||
i = next_char(str, i, space_chars, true)
|
||||
local chr = str:sub(i, i)
|
||||
i = i + 1
|
||||
if chr == "]" then break end
|
||||
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
|
||||
end
|
||||
return res, i
|
||||
end
|
||||
|
||||
local function parse_object(str, i)
|
||||
local res = {}
|
||||
i = i + 1
|
||||
while 1 do
|
||||
local key, val
|
||||
i = next_char(str, i, space_chars, true)
|
||||
-- Empty / end of object?
|
||||
if str:sub(i, i) == "}" then
|
||||
i = i + 1
|
||||
break
|
||||
end
|
||||
-- Read key
|
||||
if str:sub(i, i) ~= '"' then
|
||||
decode_error(str, i, "expected string for key")
|
||||
end
|
||||
key, i = parse(str, i)
|
||||
-- Read ':' delimiter
|
||||
i = next_char(str, i, space_chars, true)
|
||||
if str:sub(i, i) ~= ":" then
|
||||
decode_error(str, i, "expected ':' after key")
|
||||
end
|
||||
i = next_char(str, i + 1, space_chars, true)
|
||||
-- Read value
|
||||
val, i = parse(str, i)
|
||||
-- Set
|
||||
res[key] = val
|
||||
-- Next token
|
||||
i = next_char(str, i, space_chars, true)
|
||||
local chr = str:sub(i, i)
|
||||
i = i + 1
|
||||
if chr == "}" then break end
|
||||
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
|
||||
end
|
||||
return res, i
|
||||
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(str, idx)
|
||||
local chr = str:sub(idx, idx)
|
||||
local f = char_func_map[chr]
|
||||
if f then
|
||||
return f(str, idx)
|
||||
end
|
||||
decode_error(str, idx, "unexpected character '" .. chr .. "'")
|
||||
end
|
||||
|
||||
function json2sas.decode(str)
|
||||
if type(str) ~= "string" then
|
||||
error("expected argument of type string, got " .. type(str))
|
||||
end
|
||||
local res, idx = parse(str, next_char(str, 1, space_chars, true))
|
||||
idx = next_char(str, idx, space_chars, true)
|
||||
if idx <= #str then
|
||||
decode_error(str, idx, "trailing garbage")
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
-- convert macro variable array into one variable and decode
|
||||
function json2sas.go(macvar)
|
||||
local x=1
|
||||
local cnt=0
|
||||
local mac=sas.symget(macvar..'0')
|
||||
local newstr=''
|
||||
if mac and mac ~= '' then
|
||||
cnt=mac
|
||||
for x=1,cnt,1 do
|
||||
mac=sas.symget(macvar..x)
|
||||
if mac and mac ~= '' then
|
||||
newstr=newstr..mac
|
||||
else
|
||||
return print(macvar..x..' NOT FOUND!!')
|
||||
end
|
||||
end
|
||||
else
|
||||
return print(macvar..'0 NOT FOUND!!')
|
||||
end
|
||||
-- print('mac:'..mac..'cnt:'..cnt..'newstr'..newstr)
|
||||
local oneVar=json2sas.decode(newstr)
|
||||
local jsdata=oneVar["data"]
|
||||
local meta={}
|
||||
local attrs={}
|
||||
for tablename, data in pairs(jsdata) do -- each table
|
||||
print("Processing table: "..tablename)
|
||||
attrs[tablename]={}
|
||||
for k, v in ipairs(data) do -- each row
|
||||
if(k==1) then -- column names
|
||||
for a, b in pairs(v) do
|
||||
attrs[tablename][a]={}
|
||||
attrs[tablename][a]["name"]=b
|
||||
end
|
||||
elseif(k==2) then -- get types
|
||||
for a, b in pairs(v) do
|
||||
if type(b)=='number' then
|
||||
attrs[tablename][a]["type"]="N"
|
||||
attrs[tablename][a]["length"]=8
|
||||
else
|
||||
attrs[tablename][a]["type"]="C"
|
||||
attrs[tablename][a]["length"]=string.len(b)
|
||||
end
|
||||
end
|
||||
else --update lengths
|
||||
for a, b in pairs(v) do
|
||||
if (type(b)=='string' and string.len(b)>attrs[tablename][a]["length"])
|
||||
then
|
||||
attrs[tablename][a]["length"]=string.len(b)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
print(json2sas.encode(attrs[tablename])) -- show results
|
||||
|
||||
-- Now create the SAS table
|
||||
sas.new_table("work."..tablename,attrs[tablename])
|
||||
local dsid=sas.open("work."..tablename, "u")
|
||||
for k, v in ipairs(data) do
|
||||
if k>1 then
|
||||
sas.append(dsid)
|
||||
for a, b in pairs(v) do
|
||||
sas.put_value(dsid, attrs[tablename][a]["name"], b)
|
||||
end
|
||||
sas.update(dsid)
|
||||
end
|
||||
end
|
||||
sas.close(dsid)
|
||||
end
|
||||
return json2sas.decode(newstr)
|
||||
end
|
||||
|
||||
|
||||
function quote(str)
|
||||
return sas.quote(str)
|
||||
end
|
||||
function sasvar(str)
|
||||
print("processing: "..str)
|
||||
print(sas.symexist(str))
|
||||
if sas.symexist(str)==1 then
|
||||
return quote(str)..':'..quote(sas.symget(str))..','
|
||||
end
|
||||
return ''
|
||||
end
|
||||
|
||||
return json2sas
|
||||
470
lua/ml_json2sas.sas
Normal file
470
lua/ml_json2sas.sas
Normal file
@@ -0,0 +1,470 @@
|
||||
/**
|
||||
@file ml_json2sas.sas
|
||||
@brief Creates the json2sas.lua file
|
||||
@details Writes json2sas.lua to the work directory
|
||||
Usage:
|
||||
|
||||
%ml_json2sas()
|
||||
|
||||
**/
|
||||
|
||||
%macro ml_json2sas();
|
||||
data _null_;
|
||||
file "%sysfunc(pathname(work))/json2sas.lua";
|
||||
put '-- ';
|
||||
put '-- json2sas.lua (modified from json.lua) ';
|
||||
put '-- ';
|
||||
put '-- Copyright (c) 2019 rxi ';
|
||||
put '-- ';
|
||||
put '-- Permission is hereby granted, free of charge, to any person obtaining a copy of ';
|
||||
put '-- this software and associated documentation files (the "Software"), to deal in ';
|
||||
put '-- the Software without restriction, including without limitation the rights to ';
|
||||
put '-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ';
|
||||
put '-- of the Software, and to permit persons to whom the Software is furnished to do ';
|
||||
put '-- so, subject to the following conditions: ';
|
||||
put '-- ';
|
||||
put '-- The above copyright notice and this permission notice shall be included in all ';
|
||||
put '-- copies or substantial portions of the Software. ';
|
||||
put '-- ';
|
||||
put '-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ';
|
||||
put '-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ';
|
||||
put '-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ';
|
||||
put '-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ';
|
||||
put '-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ';
|
||||
put '-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ';
|
||||
put '-- SOFTWARE. ';
|
||||
put '-- ';
|
||||
put ' ';
|
||||
put 'local json2sas = { _version = "0.1.2" } ';
|
||||
put ' ';
|
||||
put '------------------------------------------------------------------------------- ';
|
||||
put '-- Encode ';
|
||||
put '------------------------------------------------------------------------------- ';
|
||||
put ' ';
|
||||
put 'local encode ';
|
||||
put ' ';
|
||||
put 'local escape_char_map = { ';
|
||||
put ' [ "\\" ] = "\\\\", ';
|
||||
put ' [ "\"" ] = "\\\"", ';
|
||||
put ' [ "\b" ] = "\\b", ';
|
||||
put ' [ "\f" ] = "\\f", ';
|
||||
put ' [ "\n" ] = "\\n", ';
|
||||
put ' [ "\r" ] = "\\r", ';
|
||||
put ' [ "\t" ] = "\\t", ';
|
||||
put '} ';
|
||||
put ' ';
|
||||
put 'local escape_char_map_inv = { [ "\\/" ] = "/" } ';
|
||||
put 'for k, v in pairs(escape_char_map) do ';
|
||||
put ' escape_char_map_inv[v] = k ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function escape_char(c) ';
|
||||
put ' return escape_char_map[c] or string.format("\\u%04x", c:byte()) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function encode_nil(val) ';
|
||||
put ' return "null" ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function encode_table(val, stack) ';
|
||||
put ' local res = {} ';
|
||||
put ' stack = stack or {} ';
|
||||
put ' ';
|
||||
put ' -- Circular reference? ';
|
||||
put ' if stack[val] then error("circular reference") end ';
|
||||
put ' ';
|
||||
put ' stack[val] = true ';
|
||||
put ' ';
|
||||
put ' if rawget(val, 1) ~= nil or next(val) == nil then ';
|
||||
put ' -- Treat as array -- check keys are valid and it is not sparse ';
|
||||
put ' local n = 0 ';
|
||||
put ' for k in pairs(val) do ';
|
||||
put ' if type(k) ~= "number" then ';
|
||||
put ' error("invalid table: mixed or invalid key types") ';
|
||||
put ' end ';
|
||||
put ' n = n + 1 ';
|
||||
put ' end ';
|
||||
put ' if n ~= #val then ';
|
||||
put ' error("invalid table: sparse array") ';
|
||||
put ' end ';
|
||||
put ' -- Encode ';
|
||||
put ' for i, v in ipairs(val) do ';
|
||||
put ' table.insert(res, encode(v, stack)) ';
|
||||
put ' end ';
|
||||
put ' stack[val] = nil ';
|
||||
put ' return "[" .. table.concat(res, ",") .. "]" ';
|
||||
put ' else ';
|
||||
put ' -- Treat as an object ';
|
||||
put ' for k, v in pairs(val) do ';
|
||||
put ' if type(k) ~= "string" then ';
|
||||
put ' error("invalid table: mixed or invalid key types") ';
|
||||
put ' end ';
|
||||
put ' table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) ';
|
||||
put ' end ';
|
||||
put ' stack[val] = nil ';
|
||||
put ' return "{" .. table.concat(res, ",") .. "}" ';
|
||||
put ' end ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function encode_string(val) ';
|
||||
put ' return ''"'' .. val:gsub(''[%z\1-\31\\"]'', escape_char) .. ''"'' ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function encode_number(val) ';
|
||||
put ' -- Check for NaN, -inf and inf ';
|
||||
put ' if val ~= val or val <= -math.huge or val >= math.huge then ';
|
||||
put ' error("unexpected number value ''" .. tostring(val) .. "''") ';
|
||||
put ' end ';
|
||||
put ' return string.format("%.14g", val) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local type_func_map = { ';
|
||||
put ' [ "nil" ] = encode_nil, ';
|
||||
put ' [ "table" ] = encode_table, ';
|
||||
put ' [ "string" ] = encode_string, ';
|
||||
put ' [ "number" ] = encode_number, ';
|
||||
put ' [ "boolean" ] = tostring, ';
|
||||
put '} ';
|
||||
put ' ';
|
||||
put 'encode = function(val, stack) ';
|
||||
put ' local t = type(val) ';
|
||||
put ' local f = type_func_map[t] ';
|
||||
put ' if f then ';
|
||||
put ' return f(val, stack) ';
|
||||
put ' end ';
|
||||
put ' error("unexpected type ''" .. t .. "''") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'function json2sas.encode(val) ';
|
||||
put ' return ( encode(val) ) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put '------------------------------------------------------------------------------- ';
|
||||
put '-- Decode ';
|
||||
put '------------------------------------------------------------------------------- ';
|
||||
put 'local parse ';
|
||||
put 'local function create_set(...) ';
|
||||
put ' local res = {} ';
|
||||
put ' for i = 1, select("#", ...) do ';
|
||||
put ' res[ select(i, ...) ] = true ';
|
||||
put ' end ';
|
||||
put ' return res ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local space_chars = create_set(" ", "\t", "\r", "\n") ';
|
||||
put 'local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") ';
|
||||
put 'local escape_chars = create_set("\\", "/", ''"'', "b", "f", "n", "r", "t", "u") ';
|
||||
put 'local literals = create_set("true", "false", "null") ';
|
||||
put ' ';
|
||||
put 'local literal_map = { ';
|
||||
put ' [ "true" ] = true, ';
|
||||
put ' [ "false" ] = false, ';
|
||||
put ' [ "null" ] = nil, ';
|
||||
put '} ';
|
||||
put ' ';
|
||||
put 'local function next_char(str, idx, set, negate) ';
|
||||
put ' for i = idx, #str do ';
|
||||
put ' if set[str:sub(i, i)] ~= negate then ';
|
||||
put ' return i ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' return #str + 1 ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function decode_error(str, idx, msg) ';
|
||||
put ' local line_count = 1 ';
|
||||
put ' local col_count = 1 ';
|
||||
put ' for i = 1, idx - 1 do ';
|
||||
put ' col_count = col_count + 1 ';
|
||||
put ' if str:sub(i, i) == "\n" then ';
|
||||
put ' line_count = line_count + 1 ';
|
||||
put ' col_count = 1 ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' error( string.format("%s at line %d col %d", msg, line_count, col_count) ) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function codepoint_to_utf8(n) ';
|
||||
put ' -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa ';
|
||||
put ' local f = math.floor ';
|
||||
put ' if n <= 0x7f then ';
|
||||
put ' return string.char(n) ';
|
||||
put ' elseif n <= 0x7ff then ';
|
||||
put ' return string.char(f(n / 64) + 192, n % 64 + 128) ';
|
||||
put ' elseif n <= 0xffff then ';
|
||||
put ' return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) ';
|
||||
put ' elseif n <= 0x10ffff then ';
|
||||
put ' return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, ';
|
||||
put ' f(n % 4096 / 64) + 128, n % 64 + 128) ';
|
||||
put ' end ';
|
||||
put ' error( string.format("invalid unicode codepoint ''%x''", n) ) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function parse_unicode_escape(s) ';
|
||||
put ' local n1 = tonumber( s:sub(3, 6), 16 ) ';
|
||||
put ' local n2 = tonumber( s:sub(9, 12), 16 ) ';
|
||||
put ' -- Surrogate pair? ';
|
||||
put ' if n2 then ';
|
||||
put ' return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) ';
|
||||
put ' else ';
|
||||
put ' return codepoint_to_utf8(n1) ';
|
||||
put ' end ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function parse_string(str, i) ';
|
||||
put ' local has_unicode_escape = false ';
|
||||
put ' local has_surrogate_escape = false ';
|
||||
put ' local has_escape = false ';
|
||||
put ' local last ';
|
||||
put ' for j = i + 1, #str do ';
|
||||
put ' local x = str:byte(j) ';
|
||||
put ' if x < 32 then ';
|
||||
put ' decode_error(str, j, "control character in string") ';
|
||||
put ' end ';
|
||||
put ' if last == 92 then -- "\\" (escape char) ';
|
||||
put ' if x == 117 then -- "u" (unicode escape sequence) ';
|
||||
put ' local hex = str:sub(j + 1, j + 5) ';
|
||||
put ' if not hex:find("%x%x%x%x") then ';
|
||||
put ' decode_error(str, j, "invalid unicode escape in string") ';
|
||||
put ' end ';
|
||||
put ' if hex:find("^[dD][89aAbB]") then ';
|
||||
put ' has_surrogate_escape = true ';
|
||||
put ' else ';
|
||||
put ' has_unicode_escape = true ';
|
||||
put ' end ';
|
||||
put ' else ';
|
||||
put ' local c = string.char(x) ';
|
||||
put ' if not escape_chars[c] then ';
|
||||
put ' decode_error(str, j, "invalid escape char ''" .. c .. "'' in string") ';
|
||||
put ' end ';
|
||||
put ' has_escape = true ';
|
||||
put ' end ';
|
||||
put ' last = nil ';
|
||||
put ' elseif x == 34 then -- ''"'' (end of string) ';
|
||||
put ' local s = str:sub(i + 1, j - 1) ';
|
||||
put ' if has_surrogate_escape then ';
|
||||
put ' s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) ';
|
||||
put ' end ';
|
||||
put ' if has_unicode_escape then ';
|
||||
put ' s = s:gsub("\\u....", parse_unicode_escape) ';
|
||||
put ' end ';
|
||||
put ' if has_escape then ';
|
||||
put ' s = s:gsub("\\.", escape_char_map_inv) ';
|
||||
put ' end ';
|
||||
put ' return s, j + 1 ';
|
||||
put ' else ';
|
||||
put ' last = x ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' decode_error(str, i, "expected closing quote for string") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function parse_number(str, i) ';
|
||||
put ' local x = next_char(str, i, delim_chars) ';
|
||||
put ' local s = str:sub(i, x - 1) ';
|
||||
put ' local n = tonumber(s) ';
|
||||
put ' if not n then ';
|
||||
put ' decode_error(str, i, "invalid number ''" .. s .. "''") ';
|
||||
put ' end ';
|
||||
put ' return n, x ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function parse_literal(str, i) ';
|
||||
put ' local x = next_char(str, i, delim_chars) ';
|
||||
put ' local word = str:sub(i, x - 1) ';
|
||||
put ' if not literals[word] then ';
|
||||
put ' decode_error(str, i, "invalid literal ''" .. word .. "''") ';
|
||||
put ' end ';
|
||||
put ' return literal_map[word], x ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function parse_array(str, i) ';
|
||||
put ' local res = {} ';
|
||||
put ' local n = 1 ';
|
||||
put ' i = i + 1 ';
|
||||
put ' while 1 do ';
|
||||
put ' local x ';
|
||||
put ' i = next_char(str, i, space_chars, true) ';
|
||||
put ' -- Empty / end of array? ';
|
||||
put ' if str:sub(i, i) == "]" then ';
|
||||
put ' i = i + 1 ';
|
||||
put ' break ';
|
||||
put ' end ';
|
||||
put ' -- Read token ';
|
||||
put ' x, i = parse(str, i) ';
|
||||
put ' res[n] = x ';
|
||||
put ' n = n + 1 ';
|
||||
put ' -- Next token ';
|
||||
put ' i = next_char(str, i, space_chars, true) ';
|
||||
put ' local chr = str:sub(i, i) ';
|
||||
put ' i = i + 1 ';
|
||||
put ' if chr == "]" then break end ';
|
||||
put ' if chr ~= "," then decode_error(str, i, "expected '']'' or '',''") end ';
|
||||
put ' end ';
|
||||
put ' return res, i ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local function parse_object(str, i) ';
|
||||
put ' local res = {} ';
|
||||
put ' i = i + 1 ';
|
||||
put ' while 1 do ';
|
||||
put ' local key, val ';
|
||||
put ' i = next_char(str, i, space_chars, true) ';
|
||||
put ' -- Empty / end of object? ';
|
||||
put ' if str:sub(i, i) == "}" then ';
|
||||
put ' i = i + 1 ';
|
||||
put ' break ';
|
||||
put ' end ';
|
||||
put ' -- Read key ';
|
||||
put ' if str:sub(i, i) ~= ''"'' then ';
|
||||
put ' decode_error(str, i, "expected string for key") ';
|
||||
put ' end ';
|
||||
put ' key, i = parse(str, i) ';
|
||||
put ' -- Read '':'' delimiter ';
|
||||
put ' i = next_char(str, i, space_chars, true) ';
|
||||
put ' if str:sub(i, i) ~= ":" then ';
|
||||
put ' decode_error(str, i, "expected '':'' after key") ';
|
||||
put ' end ';
|
||||
put ' i = next_char(str, i + 1, space_chars, true) ';
|
||||
put ' -- Read value ';
|
||||
put ' val, i = parse(str, i) ';
|
||||
put ' -- Set ';
|
||||
put ' res[key] = val ';
|
||||
put ' -- Next token ';
|
||||
put ' i = next_char(str, i, space_chars, true) ';
|
||||
put ' local chr = str:sub(i, i) ';
|
||||
put ' i = i + 1 ';
|
||||
put ' if chr == "}" then break end ';
|
||||
put ' if chr ~= "," then decode_error(str, i, "expected ''}'' or '',''") end ';
|
||||
put ' end ';
|
||||
put ' return res, i ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'local char_func_map = { ';
|
||||
put ' [ ''"'' ] = parse_string, ';
|
||||
put ' [ "0" ] = parse_number, ';
|
||||
put ' [ "1" ] = parse_number, ';
|
||||
put ' [ "2" ] = parse_number, ';
|
||||
put ' [ "3" ] = parse_number, ';
|
||||
put ' [ "4" ] = parse_number, ';
|
||||
put ' [ "5" ] = parse_number, ';
|
||||
put ' [ "6" ] = parse_number, ';
|
||||
put ' [ "7" ] = parse_number, ';
|
||||
put ' [ "8" ] = parse_number, ';
|
||||
put ' [ "9" ] = parse_number, ';
|
||||
put ' [ "-" ] = parse_number, ';
|
||||
put ' [ "t" ] = parse_literal, ';
|
||||
put ' [ "f" ] = parse_literal, ';
|
||||
put ' [ "n" ] = parse_literal, ';
|
||||
put ' [ "[" ] = parse_array, ';
|
||||
put ' [ "{" ] = parse_object, ';
|
||||
put '} ';
|
||||
put ' ';
|
||||
put 'parse = function(str, idx) ';
|
||||
put ' local chr = str:sub(idx, idx) ';
|
||||
put ' local f = char_func_map[chr] ';
|
||||
put ' if f then ';
|
||||
put ' return f(str, idx) ';
|
||||
put ' end ';
|
||||
put ' decode_error(str, idx, "unexpected character ''" .. chr .. "''") ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'function json2sas.decode(str) ';
|
||||
put ' if type(str) ~= "string" then ';
|
||||
put ' error("expected argument of type string, got " .. type(str)) ';
|
||||
put ' end ';
|
||||
put ' local res, idx = parse(str, next_char(str, 1, space_chars, true)) ';
|
||||
put ' idx = next_char(str, idx, space_chars, true) ';
|
||||
put ' if idx <= #str then ';
|
||||
put ' decode_error(str, idx, "trailing garbage") ';
|
||||
put ' end ';
|
||||
put ' return res ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put '-- convert macro variable array into one variable and decode ';
|
||||
put 'function json2sas.go(macvar) ';
|
||||
put ' local x=1 ';
|
||||
put ' local cnt=0 ';
|
||||
put ' local mac=sas.symget(macvar..''0'') ';
|
||||
put ' local newstr='''' ';
|
||||
put ' if mac and mac ~= '''' then ';
|
||||
put ' cnt=mac ';
|
||||
put ' for x=1,cnt,1 do ';
|
||||
put ' mac=sas.symget(macvar..x) ';
|
||||
put ' if mac and mac ~= '''' then ';
|
||||
put ' newstr=newstr..mac ';
|
||||
put ' else ';
|
||||
put ' return print(macvar..x..'' NOT FOUND!!'') ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' else ';
|
||||
put ' return print(macvar..''0 NOT FOUND!!'') ';
|
||||
put ' end ';
|
||||
put ' -- print(''mac:''..mac..''cnt:''..cnt..''newstr''..newstr) ';
|
||||
put ' local oneVar=json2sas.decode(newstr) ';
|
||||
put ' local jsdata=oneVar["data"] ';
|
||||
put ' local meta={} ';
|
||||
put ' local attrs={} ';
|
||||
put ' for tablename, data in pairs(jsdata) do -- each table ';
|
||||
put ' print("Processing table: "..tablename) ';
|
||||
put ' attrs[tablename]={} ';
|
||||
put ' for k, v in ipairs(data) do -- each row ';
|
||||
put ' if(k==1) then -- column names ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' attrs[tablename][a]={} ';
|
||||
put ' attrs[tablename][a]["name"]=b ';
|
||||
put ' end ';
|
||||
put ' elseif(k==2) then -- get types ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' if type(b)==''number'' then ';
|
||||
put ' attrs[tablename][a]["type"]="N" ';
|
||||
put ' attrs[tablename][a]["length"]=8 ';
|
||||
put ' else ';
|
||||
put ' attrs[tablename][a]["type"]="C" ';
|
||||
put ' attrs[tablename][a]["length"]=string.len(b) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' else --update lengths ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' if (type(b)==''string'' and string.len(b)>attrs[tablename][a]["length"]) ';
|
||||
put ' then ';
|
||||
put ' attrs[tablename][a]["length"]=string.len(b) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' print(json2sas.encode(attrs[tablename])) -- show results ';
|
||||
put ' ';
|
||||
put ' -- Now create the SAS table ';
|
||||
put ' sas.new_table("work."..tablename,attrs[tablename]) ';
|
||||
put ' local dsid=sas.open("work."..tablename, "u") ';
|
||||
put ' for k, v in ipairs(data) do ';
|
||||
put ' if k>1 then ';
|
||||
put ' sas.append(dsid) ';
|
||||
put ' for a, b in pairs(v) do ';
|
||||
put ' sas.put_value(dsid, attrs[tablename][a]["name"], b) ';
|
||||
put ' end ';
|
||||
put ' sas.update(dsid) ';
|
||||
put ' end ';
|
||||
put ' end ';
|
||||
put ' sas.close(dsid) ';
|
||||
put ' end ';
|
||||
put ' return json2sas.decode(newstr) ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put ' ';
|
||||
put 'function quote(str) ';
|
||||
put ' return sas.quote(str) ';
|
||||
put 'end ';
|
||||
put 'function sasvar(str) ';
|
||||
put ' print("processing: "..str) ';
|
||||
put ' print(sas.symexist(str)) ';
|
||||
put ' if sas.symexist(str)==1 then ';
|
||||
put ' return quote(str)..'':''..quote(sas.symget(str))..'','' ';
|
||||
put ' end ';
|
||||
put ' return '''' ';
|
||||
put 'end ';
|
||||
put ' ';
|
||||
put 'return json2sas ';
|
||||
run;
|
||||
%mend;
|
||||
Reference in New Issue
Block a user