summaryrefslogtreecommitdiff
path: root/src/lua/clean.lua
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2010-01-08 20:28:34 +0100
committerBardur Arantsson <bardur@scientician.net>2010-01-08 23:46:06 +0100
commit6aa48afdd57d03314fdf4be6c9da911c32277c84 (patch)
tree2dc401f9aae2dc6736d2fc3811c8f8099d3eabe6 /src/lua/clean.lua
Import tome-2.3.5.
Diffstat (limited to 'src/lua/clean.lua')
-rw-r--r--src/lua/clean.lua74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/lua/clean.lua b/src/lua/clean.lua
new file mode 100644
index 00000000..ba08d534
--- /dev/null
+++ b/src/lua/clean.lua
@@ -0,0 +1,74 @@
+-- mark up comments and strings
+STR1 = "\001"
+STR2 = "\002"
+STR3 = "\003"
+STR4 = "\004"
+REM = "\005"
+ANY = "([\001-\005])"
+ESC1 = "\006"
+ESC2 = "\007"
+
+MASK = { -- the substitution order is important
+ {ESC1, "\\'"},
+ {ESC2, '\\"'},
+ {STR1, "'"},
+ {STR2, '"'},
+ {STR3, "%[%["},
+ {STR4, "%]%]"},
+ {REM , "%-%-"},
+}
+
+function mask (s)
+ for i = 1,getn(MASK) do
+ s = gsub(s,MASK[i][2],MASK[i][1])
+ end
+ return s
+end
+
+function unmask (s)
+ for i = 1,getn(MASK) do
+ s = gsub(s,MASK[i][1],MASK[i][2])
+ end
+ return s
+end
+
+function clean (s)
+ -- check for compilation error
+ local code = "return function () " .. s .. " end"
+ if not dostring(code) then
+ return nil
+ end
+
+ local S = "" -- saved string
+
+ s = mask(s)
+
+ -- remove blanks and comments
+ while 1 do
+ local b,e,d = strfind(s,ANY)
+ if b then
+ S = S..strsub(s,1,b-1)
+ s = strsub(s,b+1)
+ if d==STR1 or d==STR2 then
+ e = strfind(s,d)
+ S = S ..d..strsub(s,1,e)
+ s = strsub(s,e+1)
+ elseif d==STR3 then
+ e = strfind(s,STR4)
+ S = S..d..strsub(s,1,e)
+ s = strsub(s,e+1)
+ elseif d==REM then
+ s = gsub(s,"[^\n]*(\n?)","%1",1)
+ end
+ else
+ S = S..s
+ break
+ end
+ end
+ -- eliminate unecessary spaces
+ S = gsub(S,"[ \t]+"," ")
+ S = gsub(S,"[ \t]*\n[ \t]*","\n")
+ S = unmask(S)
+ return S
+end
+