2018-06-24 22:33:00 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
SaferLua [safer_lua]
|
|
|
|
====================
|
|
|
|
|
|
|
|
Copyright (C) 2018 Joachim Stolberg
|
|
|
|
|
|
|
|
LGPLv2.1+
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
scanner.lua:
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2018-06-24 15:01:33 +02:00
|
|
|
local function trim(s)
|
|
|
|
return (s:gsub("^%s*(.-)%s*$", "%1"))
|
|
|
|
end
|
|
|
|
|
|
|
|
function safer_lua:word(ch, pttrn)
|
|
|
|
local word = ""
|
|
|
|
while ch:match(pttrn) do
|
|
|
|
word = word .. ch
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
ch = self.line:sub(self.pos, self.pos)
|
|
|
|
end
|
|
|
|
return word
|
|
|
|
end
|
|
|
|
|
|
|
|
function safer_lua:string(pttrn)
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
local ch = self.line:sub(self.pos, self.pos)
|
|
|
|
while not ch:match(pttrn) and self.pos < #self.line do
|
2018-07-05 21:46:13 +02:00
|
|
|
if ch == "\\" then
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
end
|
2018-06-24 15:01:33 +02:00
|
|
|
self.pos = self.pos + 1
|
|
|
|
ch = self.line:sub(self.pos, self.pos)
|
|
|
|
end
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
-- result is not needed
|
|
|
|
end
|
|
|
|
|
2019-01-20 17:39:34 +01:00
|
|
|
local function lines(str)
|
|
|
|
local t = {}
|
|
|
|
local function helper(line)
|
|
|
|
table.insert(t, line)
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
helper((str:gsub("(.-)\r?\n", helper)))
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2018-06-24 15:01:33 +02:00
|
|
|
function safer_lua:scanner(text)
|
|
|
|
local lToken = {}
|
2019-01-20 17:39:34 +01:00
|
|
|
for idx, line in ipairs(lines(text)) do
|
2018-06-24 15:01:33 +02:00
|
|
|
self.line = line
|
|
|
|
self.pos = 1
|
|
|
|
self.line = trim(self.line)
|
2020-07-21 17:12:54 +02:00
|
|
|
self.line = string.split(self.line, "--", true, 1)[1]
|
2019-01-20 17:39:34 +01:00
|
|
|
table.insert(lToken, idx) -- line number
|
2018-06-24 15:01:33 +02:00
|
|
|
if self.line then
|
|
|
|
-- devide line in tokens
|
|
|
|
while true do
|
|
|
|
if self.pos > #self.line then break end
|
|
|
|
local ch = self.line:sub(self.pos, self.pos)
|
|
|
|
if ch:match("[%u%l_]") then -- identifier?
|
|
|
|
table.insert(lToken, self:word(ch, "[%w_]"))
|
|
|
|
elseif ch:match("[%d]") then -- number?
|
|
|
|
table.insert(lToken, self:word(ch, "[%d%xx]"))
|
|
|
|
elseif ch:match("'") then -- string?
|
|
|
|
self:string("'")
|
|
|
|
elseif ch:match('"') then -- string?
|
|
|
|
self:string('"')
|
|
|
|
elseif ch:match("[%s]") then -- Space?
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
elseif ch:match("[:{}]") then -- critical tokens?
|
|
|
|
table.insert(lToken,ch)
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
else
|
|
|
|
self.pos = self.pos + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return lToken
|
|
|
|
end
|
|
|
|
|
|
|
|
local InvalidKeywords = {
|
|
|
|
["while"] = true,
|
|
|
|
["repeat"] = true,
|
|
|
|
["until"] = true,
|
|
|
|
["for"] = true,
|
2019-01-20 17:39:34 +01:00
|
|
|
["range"] = true,
|
2019-01-13 21:46:53 +01:00
|
|
|
--["function"] = true,
|
2018-06-24 15:01:33 +02:00
|
|
|
["_G"] = true,
|
2018-06-30 13:48:48 +02:00
|
|
|
["__load"] = true,
|
|
|
|
["__dump"] = true,
|
2018-06-24 15:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local InvalidChars = {
|
|
|
|
[":"] = true,
|
|
|
|
["{"] = true,
|
2018-07-05 21:46:13 +02:00
|
|
|
["["] = true,
|
|
|
|
["]"] = true,
|
2018-06-24 15:01:33 +02:00
|
|
|
["}"] = true,
|
|
|
|
}
|
|
|
|
|
2018-07-01 12:34:55 +02:00
|
|
|
function safer_lua:check(pos, text, label, err_clbk)
|
2018-06-24 15:01:33 +02:00
|
|
|
local lToken = self:scanner(text)
|
|
|
|
local lineno = 0
|
|
|
|
local errno = 0
|
2018-06-30 16:04:55 +02:00
|
|
|
for idx,token in ipairs(lToken) do
|
2018-06-24 15:01:33 +02:00
|
|
|
if type(token) == "number" then
|
|
|
|
lineno = token
|
|
|
|
elseif InvalidKeywords[token] then
|
2018-07-01 12:34:55 +02:00
|
|
|
if token == "for" then
|
|
|
|
-- invalid for statement?
|
2019-01-20 17:39:34 +01:00
|
|
|
if lToken[idx + 3] == "in" and lToken[idx + 5] == "next" then
|
|
|
|
--
|
|
|
|
elseif lToken[idx + 2] == "in" and lToken[idx + 3] == "range" then
|
|
|
|
--
|
|
|
|
else
|
|
|
|
err_clbk(pos, label..":"..lineno..": Invalid use of 'for'")
|
|
|
|
errno = errno + 1
|
|
|
|
end
|
|
|
|
elseif token == "range" then
|
|
|
|
if lToken[idx - 1] ~= "in" then
|
|
|
|
err_clbk(pos, label..":"..lineno..": Invalid use of 'range'")
|
|
|
|
errno = errno + 1
|
2018-07-01 12:34:55 +02:00
|
|
|
end
|
|
|
|
else
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, label..":"..lineno..": Invalid keyword '"..token.."'")
|
2018-06-30 16:04:55 +02:00
|
|
|
errno = errno + 1
|
|
|
|
end
|
2018-06-24 15:01:33 +02:00
|
|
|
elseif InvalidChars[token] then
|
2019-01-19 20:22:26 +01:00
|
|
|
err_clbk(pos, label..":"..lineno..": Invalid character '"..token.."'")
|
2018-06-24 15:01:33 +02:00
|
|
|
errno = errno + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return errno
|
|
|
|
end
|