mirror of
https://github.com/appgurueu/modlib.git
synced 2024-12-22 13:22:27 +01:00
Luon & lua log file: Fix handling of keyword keys
This commit is contained in:
parent
a243d2ae48
commit
fa328b16db
3
luon.lua
3
luon.lua
@ -2,6 +2,7 @@ local assert, next, pairs, pcall, error, type, table_insert, table_concat, strin
|
||||
= assert, next, pairs, pcall, error, type, table.insert, table.concat, string.format, string.match, setmetatable, select, setfenv, math.huge, loadfile, loadstring
|
||||
|
||||
local count_objects = modlib.table.count_objects
|
||||
local is_identifier = modlib.text.is_identifier
|
||||
|
||||
-- Build a table with the succeeding character from A-Z
|
||||
local succ = {}
|
||||
@ -60,7 +61,7 @@ function write(self, value, write)
|
||||
end
|
||||
end
|
||||
local function is_short_key(key)
|
||||
return not references[key] and type(key) == "string" and string_match(key, "^[%a_][%a%d_]*$")
|
||||
return not references[key] and type(key) == "string" and is_identifier(key)
|
||||
end
|
||||
local function dump(value)
|
||||
-- Primitive types
|
||||
|
@ -124,7 +124,7 @@ local function _dump(self, value, is_key)
|
||||
end
|
||||
if _type == "string" then
|
||||
local reference_strings = self.reference_strings
|
||||
if is_key and ((not reference_strings) or value:len() <= key:len()) and value:match"^[%a_][%a%d_]*$" then
|
||||
if is_key and ((not reference_strings) or value:len() <= key:len()) and modlib.text.is_identifier(value) then
|
||||
-- Short key
|
||||
return value, true
|
||||
end
|
||||
|
5
test.lua
5
test.lua
@ -274,6 +274,7 @@ local function serializer_test(is_json, preserve)
|
||||
c[a] = a; c[b] = b; c[c] = c;
|
||||
a.a = {"a", a = a}
|
||||
assert_preserves(a)
|
||||
assert_preserves{["for"] = "keyword", ["in"] = "keyword"}
|
||||
end
|
||||
|
||||
-- JSON
|
||||
@ -352,7 +353,7 @@ local function test_logfile(reference_strings)
|
||||
assert(logfile.root.root_preserved)
|
||||
logfile.root = {a_longer_string = "test"}
|
||||
logfile:rewrite()
|
||||
logfile:set_root({a = 1}, {b = 2, c = 3, d = _G.math.huge, e = -_G.math.huge})
|
||||
logfile:set_root({a = 1}, {b = 2, c = 3, d = _G.math.huge, e = -_G.math.huge, ["in"] = "keyword"})
|
||||
local circular = {}
|
||||
circular[circular] = circular
|
||||
logfile:set_root(circular, circular)
|
||||
@ -360,7 +361,7 @@ local function test_logfile(reference_strings)
|
||||
logfile:init()
|
||||
assert(table.equals_references(logfile.root, {
|
||||
a_longer_string = "test",
|
||||
[{a = 1}] = {b = 2, c = 3, d = _G.math.huge, e = -_G.math.huge},
|
||||
[{a = 1}] = {b = 2, c = 3, d = _G.math.huge, e = -_G.math.huge, ["in"] = "keyword"},
|
||||
[circular] = circular,
|
||||
}))
|
||||
if not reference_strings then
|
||||
|
10
text.lua
10
text.lua
@ -176,5 +176,15 @@ function handle_ifdefs(code, vars)
|
||||
return table.concat(finalcode, "")
|
||||
end
|
||||
|
||||
local keywords = modlib.table.set{"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"}
|
||||
|
||||
function is_keyword(text)
|
||||
return keywords[text]
|
||||
end
|
||||
|
||||
function is_identifier(text)
|
||||
return (not keywords[text]) and text:match"^[%a_][%a%d_]*$"
|
||||
end
|
||||
|
||||
-- Export environment
|
||||
return _ENV
|
Loading…
Reference in New Issue
Block a user