mirror of
https://github.com/sasjs/core.git
synced 2026-04-16 13:23:12 +00:00
fix lineending CRLF to LF
This commit is contained in:
@@ -1,52 +1,52 @@
|
|||||||
/**
|
/**
|
||||||
@file ml_gsubfile.sas
|
@file ml_gsubfile.sas
|
||||||
@brief Compiles the gsubfile.lua lua file
|
@brief Compiles the gsubfile.lua lua file
|
||||||
@details Writes gsubfile.lua to the work directory
|
@details Writes gsubfile.lua to the work directory
|
||||||
and then includes it.
|
and then includes it.
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
%ml_gsubfile()
|
%ml_gsubfile()
|
||||||
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
%macro ml_gsubfile();
|
%macro ml_gsubfile();
|
||||||
data _null_;
|
data _null_;
|
||||||
file "%sysfunc(pathname(work))/ml_gsubfile.lua";
|
file "%sysfunc(pathname(work))/ml_gsubfile.lua";
|
||||||
put 'local fpath, outpath, file, fcontent ';
|
put 'local fpath, outpath, file, fcontent ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put '-- configure in / out paths ';
|
put '-- configure in / out paths ';
|
||||||
put 'fpath = sas.symget("file") ';
|
put 'fpath = sas.symget("file") ';
|
||||||
put 'outpath = sas.symget("outfile") ';
|
put 'outpath = sas.symget("outfile") ';
|
||||||
put 'if ( outpath == 0 ) ';
|
put 'if ( outpath == 0 ) ';
|
||||||
put 'then ';
|
put 'then ';
|
||||||
put ' outpath=fpath ';
|
put ' outpath=fpath ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put '-- open file and perform the substitution ';
|
put '-- open file and perform the substitution ';
|
||||||
put 'file = io.open(fpath,"r") ';
|
put 'file = io.open(fpath,"r") ';
|
||||||
put 'fcontent = file:read("*all") ';
|
put 'fcontent = file:read("*all") ';
|
||||||
put 'file:close() ';
|
put 'file:close() ';
|
||||||
put 'fcontent = string.gsub( ';
|
put 'fcontent = string.gsub( ';
|
||||||
put ' fcontent, ';
|
put ' fcontent, ';
|
||||||
put ' sas.symget(sas.symget("patternvar")), ';
|
put ' sas.symget(sas.symget("patternvar")), ';
|
||||||
put ' sas.symget(sas.symget("replacevar")) ';
|
put ' sas.symget(sas.symget("replacevar")) ';
|
||||||
put ') ';
|
put ') ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put '-- write the file back out ';
|
put '-- write the file back out ';
|
||||||
put 'file = io.open(outpath, "w+") ';
|
put 'file = io.open(outpath, "w+") ';
|
||||||
put 'io.output(file) ';
|
put 'io.output(file) ';
|
||||||
put 'io.write(fcontent) ';
|
put 'io.write(fcontent) ';
|
||||||
put 'io.close(file) ';
|
put 'io.close(file) ';
|
||||||
run;
|
run;
|
||||||
|
|
||||||
/* ensure big enough lrecl to avoid lua compilation issues */
|
/* ensure big enough lrecl to avoid lua compilation issues */
|
||||||
%local optval;
|
%local optval;
|
||||||
%let optval=%sysfunc(getoption(lrecl));
|
%let optval=%sysfunc(getoption(lrecl));
|
||||||
options lrecl=1024;
|
options lrecl=1024;
|
||||||
|
|
||||||
/* execute the lua code by using a .lua extension */
|
/* execute the lua code by using a .lua extension */
|
||||||
%inc "%sysfunc(pathname(work))/ml_gsubfile.lua" /source2;
|
%inc "%sysfunc(pathname(work))/ml_gsubfile.lua" /source2;
|
||||||
|
|
||||||
options lrecl=&optval;
|
options lrecl=&optval;
|
||||||
|
|
||||||
%mend ml_gsubfile;
|
%mend ml_gsubfile;
|
||||||
|
|||||||
804
lua/ml_json.sas
804
lua/ml_json.sas
@@ -1,402 +1,402 @@
|
|||||||
/**
|
/**
|
||||||
@file ml_json.sas
|
@file ml_json.sas
|
||||||
@brief Compiles the json.lua lua file
|
@brief Compiles the json.lua lua file
|
||||||
@details Writes json.lua to the work directory
|
@details Writes json.lua to the work directory
|
||||||
and then includes it.
|
and then includes it.
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
%ml_json()
|
%ml_json()
|
||||||
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
%macro ml_json();
|
%macro ml_json();
|
||||||
data _null_;
|
data _null_;
|
||||||
file "%sysfunc(pathname(work))/ml_json.lua";
|
file "%sysfunc(pathname(work))/ml_json.lua";
|
||||||
put '-- NOTE - THE COPYRIGHT BELOW IS IN RELATION TO THE JSON.LUA FILE ONLY ';
|
put '-- NOTE - THE COPYRIGHT BELOW IS IN RELATION TO THE JSON.LUA FILE ONLY ';
|
||||||
put '-- THIS FILE STARTS ON THE NEXT LINE AND WILL FINISH WITH "JSON.LUA ENDS HERE" ';
|
put '-- THIS FILE STARTS ON THE NEXT LINE AND WILL FINISH WITH "JSON.LUA ENDS HERE" ';
|
||||||
put '-- ';
|
put '-- ';
|
||||||
put '-- json.lua ';
|
put '-- json.lua ';
|
||||||
put '-- ';
|
put '-- ';
|
||||||
put '-- Copyright (c) 2019 rxi ';
|
put '-- Copyright (c) 2019 rxi ';
|
||||||
put '-- ';
|
put '-- ';
|
||||||
put '-- Permission is hereby granted, free of charge, to any person obtaining a copy of ';
|
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 '-- this software and associated documentation files (the "Software"), to deal in ';
|
||||||
put '-- the Software without restriction, including without limitation the rights to ';
|
put '-- the Software without restriction, including without limitation the rights to ';
|
||||||
put '-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ';
|
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 '-- of the Software, and to permit persons to whom the Software is furnished to do ';
|
||||||
put '-- so, subject to the following conditions: ';
|
put '-- so, subject to the following conditions: ';
|
||||||
put '-- ';
|
put '-- ';
|
||||||
put '-- The above copyright notice and this permission notice shall be included in all ';
|
put '-- The above copyright notice and this permission notice shall be included in all ';
|
||||||
put '-- copies or substantial portions of the Software. ';
|
put '-- copies or substantial portions of the Software. ';
|
||||||
put '-- ';
|
put '-- ';
|
||||||
put '-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ';
|
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 '-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ';
|
||||||
put '-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ';
|
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 '-- 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 '-- 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 '-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ';
|
||||||
put '-- SOFTWARE. ';
|
put '-- SOFTWARE. ';
|
||||||
put '-- ';
|
put '-- ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'json = { _version = "0.1.2" } ';
|
put 'json = { _version = "0.1.2" } ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put '------------------------------------------------------------------------------- ';
|
put '------------------------------------------------------------------------------- ';
|
||||||
put '-- Encode ';
|
put '-- Encode ';
|
||||||
put '------------------------------------------------------------------------------- ';
|
put '------------------------------------------------------------------------------- ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local encode ';
|
put 'local encode ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local escape_char_map = { ';
|
put 'local escape_char_map = { ';
|
||||||
put ' [ "\\" ] = "\\\\", ';
|
put ' [ "\\" ] = "\\\\", ';
|
||||||
put ' [ "\"" ] = "\\\"", ';
|
put ' [ "\"" ] = "\\\"", ';
|
||||||
put ' [ "\b" ] = "\\b", ';
|
put ' [ "\b" ] = "\\b", ';
|
||||||
put ' [ "\f" ] = "\\f", ';
|
put ' [ "\f" ] = "\\f", ';
|
||||||
put ' [ "\n" ] = "\\n", ';
|
put ' [ "\n" ] = "\\n", ';
|
||||||
put ' [ "\r" ] = "\\r", ';
|
put ' [ "\r" ] = "\\r", ';
|
||||||
put ' [ "\t" ] = "\\t", ';
|
put ' [ "\t" ] = "\\t", ';
|
||||||
put '} ';
|
put '} ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local escape_char_map_inv = { [ "\\/" ] = "/" } ';
|
put 'local escape_char_map_inv = { [ "\\/" ] = "/" } ';
|
||||||
put 'for k, v in pairs(escape_char_map) do ';
|
put 'for k, v in pairs(escape_char_map) do ';
|
||||||
put ' escape_char_map_inv[v] = k ';
|
put ' escape_char_map_inv[v] = k ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function escape_char(c) ';
|
put 'local function escape_char(c) ';
|
||||||
put ' return escape_char_map[c] or string.format("\\u%04x", c:byte()) ';
|
put ' return escape_char_map[c] or string.format("\\u%04x", c:byte()) ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function encode_nil(val) ';
|
put 'local function encode_nil(val) ';
|
||||||
put ' return "null" ';
|
put ' return "null" ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function encode_table(val, stack) ';
|
put 'local function encode_table(val, stack) ';
|
||||||
put ' local res = {} ';
|
put ' local res = {} ';
|
||||||
put ' stack = stack or {} ';
|
put ' stack = stack or {} ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put ' -- Circular reference? ';
|
put ' -- Circular reference? ';
|
||||||
put ' if stack[val] then error("circular reference") end ';
|
put ' if stack[val] then error("circular reference") end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put ' stack[val] = true ';
|
put ' stack[val] = true ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put ' if rawget(val, 1) ~= nil or next(val) == nil then ';
|
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 ' -- Treat as array -- check keys are valid and it is not sparse ';
|
||||||
put ' local n = 0 ';
|
put ' local n = 0 ';
|
||||||
put ' for k in pairs(val) do ';
|
put ' for k in pairs(val) do ';
|
||||||
put ' if type(k) ~= "number" then ';
|
put ' if type(k) ~= "number" then ';
|
||||||
put ' error("invalid table: mixed or invalid key types") ';
|
put ' error("invalid table: mixed or invalid key types") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' n = n + 1 ';
|
put ' n = n + 1 ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' if n ~= #val then ';
|
put ' if n ~= #val then ';
|
||||||
put ' error("invalid table: sparse array") ';
|
put ' error("invalid table: sparse array") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' -- Encode ';
|
put ' -- Encode ';
|
||||||
put ' for i, v in ipairs(val) do ';
|
put ' for i, v in ipairs(val) do ';
|
||||||
put ' table.insert(res, encode(v, stack)) ';
|
put ' table.insert(res, encode(v, stack)) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' stack[val] = nil ';
|
put ' stack[val] = nil ';
|
||||||
put ' return "[" .. table.concat(res, ",") .. "]" ';
|
put ' return "[" .. table.concat(res, ",") .. "]" ';
|
||||||
put ' else ';
|
put ' else ';
|
||||||
put ' -- Treat as an object ';
|
put ' -- Treat as an object ';
|
||||||
put ' for k, v in pairs(val) do ';
|
put ' for k, v in pairs(val) do ';
|
||||||
put ' if type(k) ~= "string" then ';
|
put ' if type(k) ~= "string" then ';
|
||||||
put ' error("invalid table: mixed or invalid key types") ';
|
put ' error("invalid table: mixed or invalid key types") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) ';
|
put ' table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' stack[val] = nil ';
|
put ' stack[val] = nil ';
|
||||||
put ' return "{" .. table.concat(res, ",") .. "}" ';
|
put ' return "{" .. table.concat(res, ",") .. "}" ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function encode_string(val) ';
|
put 'local function encode_string(val) ';
|
||||||
put ' return ''"'' .. val:gsub(''[%z\1-\31\\"]'', escape_char) .. ''"'' ';
|
put ' return ''"'' .. val:gsub(''[%z\1-\31\\"]'', escape_char) .. ''"'' ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function encode_number(val) ';
|
put 'local function encode_number(val) ';
|
||||||
put ' -- Check for NaN, -inf and inf ';
|
put ' -- Check for NaN, -inf and inf ';
|
||||||
put ' if val ~= val or val <= -math.huge or val >= math.huge then ';
|
put ' if val ~= val or val <= -math.huge or val >= math.huge then ';
|
||||||
put ' error("unexpected number value ''" .. tostring(val) .. "''") ';
|
put ' error("unexpected number value ''" .. tostring(val) .. "''") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return string.format("%.14g", val) ';
|
put ' return string.format("%.14g", val) ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local type_func_map = { ';
|
put 'local type_func_map = { ';
|
||||||
put ' [ "nil" ] = encode_nil, ';
|
put ' [ "nil" ] = encode_nil, ';
|
||||||
put ' [ "table" ] = encode_table, ';
|
put ' [ "table" ] = encode_table, ';
|
||||||
put ' [ "string" ] = encode_string, ';
|
put ' [ "string" ] = encode_string, ';
|
||||||
put ' [ "number" ] = encode_number, ';
|
put ' [ "number" ] = encode_number, ';
|
||||||
put ' [ "boolean" ] = tostring, ';
|
put ' [ "boolean" ] = tostring, ';
|
||||||
put '} ';
|
put '} ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'encode = function(val, stack) ';
|
put 'encode = function(val, stack) ';
|
||||||
put ' local t = type(val) ';
|
put ' local t = type(val) ';
|
||||||
put ' local f = type_func_map[t] ';
|
put ' local f = type_func_map[t] ';
|
||||||
put ' if f then ';
|
put ' if f then ';
|
||||||
put ' return f(val, stack) ';
|
put ' return f(val, stack) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' error("unexpected type ''" .. t .. "''") ';
|
put ' error("unexpected type ''" .. t .. "''") ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'function json.encode(val) ';
|
put 'function json.encode(val) ';
|
||||||
put ' return ( encode(val) ) ';
|
put ' return ( encode(val) ) ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put '------------------------------------------------------------------------------- ';
|
put '------------------------------------------------------------------------------- ';
|
||||||
put '-- Decode ';
|
put '-- Decode ';
|
||||||
put '------------------------------------------------------------------------------- ';
|
put '------------------------------------------------------------------------------- ';
|
||||||
put 'local parse ';
|
put 'local parse ';
|
||||||
put 'local function create_set(...) ';
|
put 'local function create_set(...) ';
|
||||||
put ' local res = {} ';
|
put ' local res = {} ';
|
||||||
put ' for i = 1, select("#", ...) do ';
|
put ' for i = 1, select("#", ...) do ';
|
||||||
put ' res[ select(i, ...) ] = true ';
|
put ' res[ select(i, ...) ] = true ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return res ';
|
put ' return res ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local space_chars = create_set(" ", "\t", "\r", "\n") ';
|
put 'local space_chars = create_set(" ", "\t", "\r", "\n") ';
|
||||||
put 'local delim_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 escape_chars = create_set("\\", "/", ''"'', "b", "f", "n", "r", "t", "u") ';
|
||||||
put 'local literals = create_set("true", "false", "null") ';
|
put 'local literals = create_set("true", "false", "null") ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local literal_map = { ';
|
put 'local literal_map = { ';
|
||||||
put ' [ "true" ] = true, ';
|
put ' [ "true" ] = true, ';
|
||||||
put ' [ "false" ] = false, ';
|
put ' [ "false" ] = false, ';
|
||||||
put ' [ "null" ] = nil, ';
|
put ' [ "null" ] = nil, ';
|
||||||
put '} ';
|
put '} ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function next_char(str, idx, set, negate) ';
|
put 'local function next_char(str, idx, set, negate) ';
|
||||||
put ' for i = idx, #str do ';
|
put ' for i = idx, #str do ';
|
||||||
put ' if set[str:sub(i, i)] ~= negate then ';
|
put ' if set[str:sub(i, i)] ~= negate then ';
|
||||||
put ' return i ';
|
put ' return i ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return #str + 1 ';
|
put ' return #str + 1 ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function decode_error(str, idx, msg) ';
|
put 'local function decode_error(str, idx, msg) ';
|
||||||
put ' local line_count = 1 ';
|
put ' local line_count = 1 ';
|
||||||
put ' local col_count = 1 ';
|
put ' local col_count = 1 ';
|
||||||
put ' for i = 1, idx - 1 do ';
|
put ' for i = 1, idx - 1 do ';
|
||||||
put ' col_count = col_count + 1 ';
|
put ' col_count = col_count + 1 ';
|
||||||
put ' if str:sub(i, i) == "\n" then ';
|
put ' if str:sub(i, i) == "\n" then ';
|
||||||
put ' line_count = line_count + 1 ';
|
put ' line_count = line_count + 1 ';
|
||||||
put ' col_count = 1 ';
|
put ' col_count = 1 ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' error( string.format("%s at line %d col %d", msg, line_count, col_count) ) ';
|
put ' error( string.format("%s at line %d col %d", msg, line_count, col_count) ) ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function codepoint_to_utf8(n) ';
|
put 'local function codepoint_to_utf8(n) ';
|
||||||
put ' -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa ';
|
put ' -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa ';
|
||||||
put ' local f = math.floor ';
|
put ' local f = math.floor ';
|
||||||
put ' if n <= 0x7f then ';
|
put ' if n <= 0x7f then ';
|
||||||
put ' return string.char(n) ';
|
put ' return string.char(n) ';
|
||||||
put ' elseif n <= 0x7ff then ';
|
put ' elseif n <= 0x7ff then ';
|
||||||
put ' return string.char(f(n / 64) + 192, n % 64 + 128) ';
|
put ' return string.char(f(n / 64) + 192, n % 64 + 128) ';
|
||||||
put ' elseif n <= 0xffff then ';
|
put ' elseif n <= 0xffff then ';
|
||||||
put ' return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) ';
|
put ' return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) ';
|
||||||
put ' elseif n <= 0x10ffff then ';
|
put ' elseif n <= 0x10ffff then ';
|
||||||
put ' return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, ';
|
put ' return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, ';
|
||||||
put ' f(n % 4096 / 64) + 128, n % 64 + 128) ';
|
put ' f(n % 4096 / 64) + 128, n % 64 + 128) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' error( string.format("invalid unicode codepoint ''%x''", n) ) ';
|
put ' error( string.format("invalid unicode codepoint ''%x''", n) ) ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function parse_unicode_escape(s) ';
|
put 'local function parse_unicode_escape(s) ';
|
||||||
put ' local n1 = tonumber( s:sub(3, 6), 16 ) ';
|
put ' local n1 = tonumber( s:sub(3, 6), 16 ) ';
|
||||||
put ' local n2 = tonumber( s:sub(9, 12), 16 ) ';
|
put ' local n2 = tonumber( s:sub(9, 12), 16 ) ';
|
||||||
put ' -- Surrogate pair? ';
|
put ' -- Surrogate pair? ';
|
||||||
put ' if n2 then ';
|
put ' if n2 then ';
|
||||||
put ' return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) ';
|
put ' return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) ';
|
||||||
put ' else ';
|
put ' else ';
|
||||||
put ' return codepoint_to_utf8(n1) ';
|
put ' return codepoint_to_utf8(n1) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function parse_string(str, i) ';
|
put 'local function parse_string(str, i) ';
|
||||||
put ' local has_unicode_escape = false ';
|
put ' local has_unicode_escape = false ';
|
||||||
put ' local has_surrogate_escape = false ';
|
put ' local has_surrogate_escape = false ';
|
||||||
put ' local has_escape = false ';
|
put ' local has_escape = false ';
|
||||||
put ' local last ';
|
put ' local last ';
|
||||||
put ' for j = i + 1, #str do ';
|
put ' for j = i + 1, #str do ';
|
||||||
put ' local x = str:byte(j) ';
|
put ' local x = str:byte(j) ';
|
||||||
put ' if x < 32 then ';
|
put ' if x < 32 then ';
|
||||||
put ' decode_error(str, j, "control character in string") ';
|
put ' decode_error(str, j, "control character in string") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' if last == 92 then -- "\\" (escape char) ';
|
put ' if last == 92 then -- "\\" (escape char) ';
|
||||||
put ' if x == 117 then -- "u" (unicode escape sequence) ';
|
put ' if x == 117 then -- "u" (unicode escape sequence) ';
|
||||||
put ' local hex = str:sub(j + 1, j + 5) ';
|
put ' local hex = str:sub(j + 1, j + 5) ';
|
||||||
put ' if not hex:find("%x%x%x%x") then ';
|
put ' if not hex:find("%x%x%x%x") then ';
|
||||||
put ' decode_error(str, j, "invalid unicode escape in string") ';
|
put ' decode_error(str, j, "invalid unicode escape in string") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' if hex:find("^[dD][89aAbB]") then ';
|
put ' if hex:find("^[dD][89aAbB]") then ';
|
||||||
put ' has_surrogate_escape = true ';
|
put ' has_surrogate_escape = true ';
|
||||||
put ' else ';
|
put ' else ';
|
||||||
put ' has_unicode_escape = true ';
|
put ' has_unicode_escape = true ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' else ';
|
put ' else ';
|
||||||
put ' local c = string.char(x) ';
|
put ' local c = string.char(x) ';
|
||||||
put ' if not escape_chars[c] then ';
|
put ' if not escape_chars[c] then ';
|
||||||
put ' decode_error(str, j, "invalid escape char ''" .. c .. "'' in string") ';
|
put ' decode_error(str, j, "invalid escape char ''" .. c .. "'' in string") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' has_escape = true ';
|
put ' has_escape = true ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' last = nil ';
|
put ' last = nil ';
|
||||||
put ' elseif x == 34 then -- ''"'' (end of string) ';
|
put ' elseif x == 34 then -- ''"'' (end of string) ';
|
||||||
put ' local s = str:sub(i + 1, j - 1) ';
|
put ' local s = str:sub(i + 1, j - 1) ';
|
||||||
put ' if has_surrogate_escape then ';
|
put ' if has_surrogate_escape then ';
|
||||||
put ' s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) ';
|
put ' s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' if has_unicode_escape then ';
|
put ' if has_unicode_escape then ';
|
||||||
put ' s = s:gsub("\\u....", parse_unicode_escape) ';
|
put ' s = s:gsub("\\u....", parse_unicode_escape) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' if has_escape then ';
|
put ' if has_escape then ';
|
||||||
put ' s = s:gsub("\\.", escape_char_map_inv) ';
|
put ' s = s:gsub("\\.", escape_char_map_inv) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return s, j + 1 ';
|
put ' return s, j + 1 ';
|
||||||
put ' else ';
|
put ' else ';
|
||||||
put ' last = x ';
|
put ' last = x ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' decode_error(str, i, "expected closing quote for string") ';
|
put ' decode_error(str, i, "expected closing quote for string") ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function parse_number(str, i) ';
|
put 'local function parse_number(str, i) ';
|
||||||
put ' local x = next_char(str, i, delim_chars) ';
|
put ' local x = next_char(str, i, delim_chars) ';
|
||||||
put ' local s = str:sub(i, x - 1) ';
|
put ' local s = str:sub(i, x - 1) ';
|
||||||
put ' local n = tonumber(s) ';
|
put ' local n = tonumber(s) ';
|
||||||
put ' if not n then ';
|
put ' if not n then ';
|
||||||
put ' decode_error(str, i, "invalid number ''" .. s .. "''") ';
|
put ' decode_error(str, i, "invalid number ''" .. s .. "''") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return n, x ';
|
put ' return n, x ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function parse_literal(str, i) ';
|
put 'local function parse_literal(str, i) ';
|
||||||
put ' local x = next_char(str, i, delim_chars) ';
|
put ' local x = next_char(str, i, delim_chars) ';
|
||||||
put ' local word = str:sub(i, x - 1) ';
|
put ' local word = str:sub(i, x - 1) ';
|
||||||
put ' if not literals[word] then ';
|
put ' if not literals[word] then ';
|
||||||
put ' decode_error(str, i, "invalid literal ''" .. word .. "''") ';
|
put ' decode_error(str, i, "invalid literal ''" .. word .. "''") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return literal_map[word], x ';
|
put ' return literal_map[word], x ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function parse_array(str, i) ';
|
put 'local function parse_array(str, i) ';
|
||||||
put ' local res = {} ';
|
put ' local res = {} ';
|
||||||
put ' local n = 1 ';
|
put ' local n = 1 ';
|
||||||
put ' i = i + 1 ';
|
put ' i = i + 1 ';
|
||||||
put ' while 1 do ';
|
put ' while 1 do ';
|
||||||
put ' local x ';
|
put ' local x ';
|
||||||
put ' i = next_char(str, i, space_chars, true) ';
|
put ' i = next_char(str, i, space_chars, true) ';
|
||||||
put ' -- Empty / end of array? ';
|
put ' -- Empty / end of array? ';
|
||||||
put ' if str:sub(i, i) == "]" then ';
|
put ' if str:sub(i, i) == "]" then ';
|
||||||
put ' i = i + 1 ';
|
put ' i = i + 1 ';
|
||||||
put ' break ';
|
put ' break ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' -- Read token ';
|
put ' -- Read token ';
|
||||||
put ' x, i = parse(str, i) ';
|
put ' x, i = parse(str, i) ';
|
||||||
put ' res[n] = x ';
|
put ' res[n] = x ';
|
||||||
put ' n = n + 1 ';
|
put ' n = n + 1 ';
|
||||||
put ' -- Next token ';
|
put ' -- Next token ';
|
||||||
put ' i = next_char(str, i, space_chars, true) ';
|
put ' i = next_char(str, i, space_chars, true) ';
|
||||||
put ' local chr = str:sub(i, i) ';
|
put ' local chr = str:sub(i, i) ';
|
||||||
put ' i = i + 1 ';
|
put ' i = i + 1 ';
|
||||||
put ' if chr == "]" then break end ';
|
put ' if chr == "]" then break end ';
|
||||||
put ' if chr ~= "," then decode_error(str, i, "expected '']'' or '',''") end ';
|
put ' if chr ~= "," then decode_error(str, i, "expected '']'' or '',''") end ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return res, i ';
|
put ' return res, i ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local function parse_object(str, i) ';
|
put 'local function parse_object(str, i) ';
|
||||||
put ' local res = {} ';
|
put ' local res = {} ';
|
||||||
put ' i = i + 1 ';
|
put ' i = i + 1 ';
|
||||||
put ' while 1 do ';
|
put ' while 1 do ';
|
||||||
put ' local key, val ';
|
put ' local key, val ';
|
||||||
put ' i = next_char(str, i, space_chars, true) ';
|
put ' i = next_char(str, i, space_chars, true) ';
|
||||||
put ' -- Empty / end of object? ';
|
put ' -- Empty / end of object? ';
|
||||||
put ' if str:sub(i, i) == "}" then ';
|
put ' if str:sub(i, i) == "}" then ';
|
||||||
put ' i = i + 1 ';
|
put ' i = i + 1 ';
|
||||||
put ' break ';
|
put ' break ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' -- Read key ';
|
put ' -- Read key ';
|
||||||
put ' if str:sub(i, i) ~= ''"'' then ';
|
put ' if str:sub(i, i) ~= ''"'' then ';
|
||||||
put ' decode_error(str, i, "expected string for key") ';
|
put ' decode_error(str, i, "expected string for key") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' key, i = parse(str, i) ';
|
put ' key, i = parse(str, i) ';
|
||||||
put ' -- Read '':'' delimiter ';
|
put ' -- Read '':'' delimiter ';
|
||||||
put ' i = next_char(str, i, space_chars, true) ';
|
put ' i = next_char(str, i, space_chars, true) ';
|
||||||
put ' if str:sub(i, i) ~= ":" then ';
|
put ' if str:sub(i, i) ~= ":" then ';
|
||||||
put ' decode_error(str, i, "expected '':'' after key") ';
|
put ' decode_error(str, i, "expected '':'' after key") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' i = next_char(str, i + 1, space_chars, true) ';
|
put ' i = next_char(str, i + 1, space_chars, true) ';
|
||||||
put ' -- Read value ';
|
put ' -- Read value ';
|
||||||
put ' val, i = parse(str, i) ';
|
put ' val, i = parse(str, i) ';
|
||||||
put ' -- Set ';
|
put ' -- Set ';
|
||||||
put ' res[key] = val ';
|
put ' res[key] = val ';
|
||||||
put ' -- Next token ';
|
put ' -- Next token ';
|
||||||
put ' i = next_char(str, i, space_chars, true) ';
|
put ' i = next_char(str, i, space_chars, true) ';
|
||||||
put ' local chr = str:sub(i, i) ';
|
put ' local chr = str:sub(i, i) ';
|
||||||
put ' i = i + 1 ';
|
put ' i = i + 1 ';
|
||||||
put ' if chr == "}" then break end ';
|
put ' if chr == "}" then break end ';
|
||||||
put ' if chr ~= "," then decode_error(str, i, "expected ''}'' or '',''") end ';
|
put ' if chr ~= "," then decode_error(str, i, "expected ''}'' or '',''") end ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return res, i ';
|
put ' return res, i ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'local char_func_map = { ';
|
put 'local char_func_map = { ';
|
||||||
put ' [ ''"'' ] = parse_string, ';
|
put ' [ ''"'' ] = parse_string, ';
|
||||||
put ' [ "0" ] = parse_number, ';
|
put ' [ "0" ] = parse_number, ';
|
||||||
put ' [ "1" ] = parse_number, ';
|
put ' [ "1" ] = parse_number, ';
|
||||||
put ' [ "2" ] = parse_number, ';
|
put ' [ "2" ] = parse_number, ';
|
||||||
put ' [ "3" ] = parse_number, ';
|
put ' [ "3" ] = parse_number, ';
|
||||||
put ' [ "4" ] = parse_number, ';
|
put ' [ "4" ] = parse_number, ';
|
||||||
put ' [ "5" ] = parse_number, ';
|
put ' [ "5" ] = parse_number, ';
|
||||||
put ' [ "6" ] = parse_number, ';
|
put ' [ "6" ] = parse_number, ';
|
||||||
put ' [ "7" ] = parse_number, ';
|
put ' [ "7" ] = parse_number, ';
|
||||||
put ' [ "8" ] = parse_number, ';
|
put ' [ "8" ] = parse_number, ';
|
||||||
put ' [ "9" ] = parse_number, ';
|
put ' [ "9" ] = parse_number, ';
|
||||||
put ' [ "-" ] = parse_number, ';
|
put ' [ "-" ] = parse_number, ';
|
||||||
put ' [ "t" ] = parse_literal, ';
|
put ' [ "t" ] = parse_literal, ';
|
||||||
put ' [ "f" ] = parse_literal, ';
|
put ' [ "f" ] = parse_literal, ';
|
||||||
put ' [ "n" ] = parse_literal, ';
|
put ' [ "n" ] = parse_literal, ';
|
||||||
put ' [ "[" ] = parse_array, ';
|
put ' [ "[" ] = parse_array, ';
|
||||||
put ' [ "{" ] = parse_object, ';
|
put ' [ "{" ] = parse_object, ';
|
||||||
put '} ';
|
put '} ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'parse = function(str, idx) ';
|
put 'parse = function(str, idx) ';
|
||||||
put ' local chr = str:sub(idx, idx) ';
|
put ' local chr = str:sub(idx, idx) ';
|
||||||
put ' local f = char_func_map[chr] ';
|
put ' local f = char_func_map[chr] ';
|
||||||
put ' if f then ';
|
put ' if f then ';
|
||||||
put ' return f(str, idx) ';
|
put ' return f(str, idx) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' decode_error(str, idx, "unexpected character ''" .. chr .. "''") ';
|
put ' decode_error(str, idx, "unexpected character ''" .. chr .. "''") ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'function json.decode(str) ';
|
put 'function json.decode(str) ';
|
||||||
put ' if type(str) ~= "string" then ';
|
put ' if type(str) ~= "string" then ';
|
||||||
put ' error("expected argument of type string, got " .. type(str)) ';
|
put ' error("expected argument of type string, got " .. type(str)) ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' local res, idx = parse(str, next_char(str, 1, space_chars, true)) ';
|
put ' local res, idx = parse(str, next_char(str, 1, space_chars, true)) ';
|
||||||
put ' idx = next_char(str, idx, space_chars, true) ';
|
put ' idx = next_char(str, idx, space_chars, true) ';
|
||||||
put ' if idx <= #str then ';
|
put ' if idx <= #str then ';
|
||||||
put ' decode_error(str, idx, "trailing garbage") ';
|
put ' decode_error(str, idx, "trailing garbage") ';
|
||||||
put ' end ';
|
put ' end ';
|
||||||
put ' return res ';
|
put ' return res ';
|
||||||
put 'end ';
|
put 'end ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put 'return json ';
|
put 'return json ';
|
||||||
put ' ';
|
put ' ';
|
||||||
put '-- JSON.LUA ENDS HERE ';
|
put '-- JSON.LUA ENDS HERE ';
|
||||||
run;
|
run;
|
||||||
|
|
||||||
/* ensure big enough lrecl to avoid lua compilation issues */
|
/* ensure big enough lrecl to avoid lua compilation issues */
|
||||||
%local optval;
|
%local optval;
|
||||||
%let optval=%sysfunc(getoption(lrecl));
|
%let optval=%sysfunc(getoption(lrecl));
|
||||||
options lrecl=1024;
|
options lrecl=1024;
|
||||||
|
|
||||||
/* execute the lua code by using a .lua extension */
|
/* execute the lua code by using a .lua extension */
|
||||||
%inc "%sysfunc(pathname(work))/ml_json.lua" /source2;
|
%inc "%sysfunc(pathname(work))/ml_json.lua" /source2;
|
||||||
|
|
||||||
options lrecl=&optval;
|
options lrecl=&optval;
|
||||||
|
|
||||||
%mend ml_json;
|
%mend ml_json;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user