fix edge case when lua keywords are used as keys
This commit is contained in:
parent
9c723301ec
commit
9b440f279e
27
alg1.lua
27
alg1.lua
@ -44,11 +44,36 @@ local function stringToTable(str)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local lua_keywords = {
|
||||||
|
["and"] = true,
|
||||||
|
["break"] = true,
|
||||||
|
["do"] = true,
|
||||||
|
["else"] = true,
|
||||||
|
["elseif"] = true,
|
||||||
|
["end"] = true,
|
||||||
|
["false"] = true,
|
||||||
|
["for"] = true,
|
||||||
|
["function"] = true,
|
||||||
|
["goto"] = true,
|
||||||
|
["if"] = true,
|
||||||
|
["in"] = true,
|
||||||
|
["local"] = true,
|
||||||
|
["nil"] = true,
|
||||||
|
["not"] = true,
|
||||||
|
["or"] = true,
|
||||||
|
["repeat"] = true,
|
||||||
|
["return"] = true,
|
||||||
|
["then"] = true,
|
||||||
|
["true"] = true,
|
||||||
|
["until"] = true,
|
||||||
|
["while"] = true,
|
||||||
|
}
|
||||||
|
|
||||||
local function parseKey(s, i)
|
local function parseKey(s, i)
|
||||||
s = s:match("^%s*(.-)%s*$", i) -- Trim leading/trailing spaces
|
s = s:match("^%s*(.-)%s*$", i) -- Trim leading/trailing spaces
|
||||||
if s:find("^[%a_]") then
|
if s:find("^[%a_]") then
|
||||||
local key = s:match("^[%a%d_]*")
|
local key = s:match("^[%a%d_]*")
|
||||||
if key ~= "true" and key ~= "false" and key ~= "nil" then
|
if not lua_keywords[key] then
|
||||||
return key, #key
|
return key, #key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
34
alg2.lua
34
alg2.lua
@ -41,15 +41,41 @@ local function codedStringToTable(str)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local lua_keywords = {
|
||||||
|
["and"] = true,
|
||||||
|
["break"] = true,
|
||||||
|
["do"] = true,
|
||||||
|
["else"] = true,
|
||||||
|
["elseif"] = true,
|
||||||
|
["end"] = true,
|
||||||
|
["false"] = true,
|
||||||
|
["for"] = true,
|
||||||
|
["function"] = true,
|
||||||
|
["goto"] = true,
|
||||||
|
["if"] = true,
|
||||||
|
["in"] = true,
|
||||||
|
["local"] = true,
|
||||||
|
["nil"] = true,
|
||||||
|
["not"] = true,
|
||||||
|
["or"] = true,
|
||||||
|
["repeat"] = true,
|
||||||
|
["return"] = true,
|
||||||
|
["then"] = true,
|
||||||
|
["true"] = true,
|
||||||
|
["until"] = true,
|
||||||
|
["while"] = true,
|
||||||
|
}
|
||||||
|
|
||||||
local function parseKey(s)
|
local function parseKey(s)
|
||||||
s = s:match("^%s*(.-)%s*$") -- trim whitespace
|
s = s:match("^%s*(.-)%s*$") -- trim whitespace
|
||||||
if s:match("^%[(.*)%]$") then
|
if s:match("^%[(.*)%]$") then
|
||||||
return parseValue(s:match("^%[(.*)%]$"))
|
return parseValue(s:match("^%[(.*)%]$"))
|
||||||
elseif s:find("^[%a_][%a%d_]*$") then -- make sure the string is a valid lua identifier
|
|
||||||
return s
|
|
||||||
else
|
|
||||||
error("Unrecognized key: " .. s)
|
|
||||||
end
|
end
|
||||||
|
s = s:match("^[%a_][%a%d_]*$") -- make sure the string is a valid lua identifier
|
||||||
|
if s and not lua_keywords[s] then
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
error("Unrecognized key: " .. s)
|
||||||
end
|
end
|
||||||
|
|
||||||
local tbl = {}
|
local tbl = {}
|
||||||
|
27
algtour.lua
27
algtour.lua
@ -38,6 +38,31 @@ local function parse_value(str)
|
|||||||
return false, string.format("unable to parse '%s'", str)
|
return false, string.format("unable to parse '%s'", str)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local lua_keywords = {
|
||||||
|
["and"] = true,
|
||||||
|
["break"] = true,
|
||||||
|
["do"] = true,
|
||||||
|
["else"] = true,
|
||||||
|
["elseif"] = true,
|
||||||
|
["end"] = true,
|
||||||
|
["false"] = true,
|
||||||
|
["for"] = true,
|
||||||
|
["function"] = true,
|
||||||
|
["goto"] = true,
|
||||||
|
["if"] = true,
|
||||||
|
["in"] = true,
|
||||||
|
["local"] = true,
|
||||||
|
["nil"] = true,
|
||||||
|
["not"] = true,
|
||||||
|
["or"] = true,
|
||||||
|
["repeat"] = true,
|
||||||
|
["return"] = true,
|
||||||
|
["then"] = true,
|
||||||
|
["true"] = true,
|
||||||
|
["until"] = true,
|
||||||
|
["while"] = true,
|
||||||
|
}
|
||||||
|
|
||||||
local function parse_key(str)
|
local function parse_key(str)
|
||||||
if str:find("^%[.*%]$") then
|
if str:find("^%[.*%]$") then
|
||||||
-- if in brackets, we can threat it like a value
|
-- if in brackets, we can threat it like a value
|
||||||
@ -51,7 +76,7 @@ local function parse_key(str)
|
|||||||
return true, key
|
return true, key
|
||||||
end
|
end
|
||||||
-- make sure the key is a valid identifier
|
-- make sure the key is a valid identifier
|
||||||
if str:find("^[%a_][%a%d_]*$") then
|
if str:find("^[%a_][%a%d_]*$") and not lua_keywords[str] then
|
||||||
return true, str
|
return true, str
|
||||||
end
|
end
|
||||||
return false, string.format("'%s' is not a valid key", str)
|
return false, string.format("'%s' is not a valid key", str)
|
||||||
|
3
init.lua
3
init.lua
@ -26,6 +26,7 @@ local tests = {
|
|||||||
"{a = 1",
|
"{a = 1",
|
||||||
"{'unclosed string\\'}",
|
"{'unclosed string\\'}",
|
||||||
"{invalid key = 3}",
|
"{invalid key = 3}",
|
||||||
|
"{false = 2}", -- invalid key
|
||||||
"{1 = 2}", -- invalid key
|
"{1 = 2}", -- invalid key
|
||||||
"{,}",
|
"{,}",
|
||||||
"{[nil] = 1}"
|
"{[nil] = 1}"
|
||||||
@ -44,7 +45,7 @@ local function test(alg)
|
|||||||
end
|
end
|
||||||
local t2 = minetest.get_us_time()
|
local t2 = minetest.get_us_time()
|
||||||
minetest.log("action",alg .. " = " .. (t2-t1))
|
minetest.log("action",alg .. " = " .. (t2-t1))
|
||||||
print(dump(outcome))
|
-- print(dump(outcome))
|
||||||
end
|
end
|
||||||
|
|
||||||
test("s2t1")
|
test("s2t1")
|
||||||
|
Loading…
Reference in New Issue
Block a user