From 1cef968731d8400c4975c5ad0bd3b7013560e24b Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 17:06:23 -0700 Subject: [PATCH] Key Instance type checking --- .../utils/parse/key_instance.lua | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/worldeditadditions/utils/parse/key_instance.lua b/worldeditadditions/utils/parse/key_instance.lua index fc78a86..1cec51d 100644 --- a/worldeditadditions/utils/parse/key_instance.lua +++ b/worldeditadditions/utils/parse/key_instance.lua @@ -1,16 +1,42 @@ ---- A container for transmitting axis, sign pairs within parsing functions. +--- A container for transmitting (axis table, sign) or (dir, sign) pairs +-- and other data within parsing functions. -- @class local key_instance = {} key_instance.__index = key_instance key_instance.__name = "Key Instance" +-- Allowed values for "type" field +local types = { + err = true, rev = true, + axis = true, dir = true, + replace = { + error = "err", + axes = "axis", + }, +} + +-- Simple function for putting stuff in quotes +local function enquote(take) + if type(take) == "string" then + return '"'..take..'"' + else return tostring(take) end +end + --- Creates a new Key Instance. --- @param: type: String: Key type (axis, direction, mirroring or error). +-- This is a table with a "type" string, an entry string or table +-- and an optional signed integer (or code number in the case of errors) +-- @param: type: String: Key type (axis, dir(ection), rev (mirroring) or error). -- @param: entry: String: The main content of the key. -- @param: sign: Int: The signed multiplier of the key (if any). -- @return: Key Instance: The new Key Instance. function key_instance.new(type,entry,sign) - if type == "error" then type = "err" end + if types.replace[type] then + type = types.replace[type] + elseif not types[type] then + return key_instance.new("err", + "Key Instance internal error: Invalid type "..enquote(type)..".", + 500) + end local tbl = {type = type, entry = entry} if sign and sign ~= 0 then if type == "err" then tbl.code = sign @@ -46,11 +72,6 @@ function key_instance.is_error(a) end function key_instance.__tostring(a) - local function enquote(take) - if type(take) == "string" then - return '"'..take..'"' - else return tostring(take) end - end local ret = "{type = "..enquote(a.type)..", entry = " if type(a.entry) == "table" and #a.entry <= 3 then ret = ret.."{"