Quantcast

Fixed sort to avoid using unquoted keywords

Kevin Lyles [02-09-13 - 20:43]
Fixed sort to avoid using unquoted keywords
Filename
sort.lua
diff --git a/sort.lua b/sort.lua
index a5ee036..1f9633b 100644
--- a/sort.lua
+++ b/sort.lua
@@ -80,11 +80,35 @@ local function quotedString(str)
 	return string.gsub(string.format("%q", str), "\n", "n")
 end

+local keywords = {
+	["and"] = true,
+	["break"] = true,
+	["do"] = true,
+	["else"] = true,
+	["elseif"] = true,
+	["end"] = true,
+	["false"] = true,
+	["for"] = true,
+	["function"] = 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 varName(name)
 	if type(name) == "number" then
 		return string.format("[%d]", name)
 	elseif type(name) == "string" then
-		if name:find("^%a[%w_]+$") then
+		if name:find("^%a[%w_]+$") and not keywords[name] then
 			return name
 		else
 			return string.format("[%s]", quotedString(name))