Fixes formatting.

This commit is contained in:
gamemanj
2015-11-14 11:59:19 +00:00
parent 853dcc50d5
commit 83d4264d8a

View File

@ -334,7 +334,7 @@ local function create_environment(pos, mem, event)
env.pcall=function(...) env.pcall=function(...)
local pcr={pcall(...)} local pcr={pcall(...)}
if not pcr[1] then if not pcr[1] then
if pcr[2]~=timeout_error then if pcr[2] ~= timeout_error then
error(pcr[2]) error(pcr[2])
end end
end end
@ -352,7 +352,7 @@ end
--A VERY minimalistic lexer, does what it needs to for this job and no more. --A VERY minimalistic lexer, does what it needs to for this job and no more.
local function lexLua(code) local function lexLua(code)
local lexElements={} local lexElements = {}
--Find keywords, whitespace, strings, and then everything else is "cleanup" --Find keywords, whitespace, strings, and then everything else is "cleanup"
--Keywords and numbers. --Keywords and numbers.
@ -360,7 +360,7 @@ local function lexLua(code)
return str:match("^[%w_]+") return str:match("^[%w_]+")
end end
function lexElements.whitespace(str) function lexElements.whitespace(str)
return str:match("^%s+") return str:match("^[\r \t]+")
end end
--Unimplemented stuff goes here. --Unimplemented stuff goes here.
function lexElements.cleanup(str) function lexElements.cleanup(str)
@ -368,42 +368,47 @@ local function lexLua(code)
end end
function lexElements.string(str) function lexElements.string(str)
--Now parse a string. --Now parse a string.
local quoteType=str:sub(1,1) local quoteType = str:sub(1,1)
if quoteType~="\"" and quoteType~="\'" then return nil end if quoteType ~= "\"" and quoteType ~= "\'" then return nil end
local inEscape=true local inEscape = true
local rstr="" local rstr = ""
for i=1,str:len() do for i=1, str:len() do
local c=str:sub(i,i) local c = str:sub(i,i)
rstr=rstr..c rstr = rstr..c
if inEscape then if inEscape then
inEscape=false inEscape = false
else else
if c==quoteType then return rstr end if c == quoteType then return rstr end
if c=="\\" then inEscape=true end if c == "\\" then inEscape = true end
end end
end end
return nil --unfinished string return nil --unfinished string
end end
function lexElements.blockcomment(str) function lexElements.blockcomment(str)
local a=str:match("^%-%-%[%[") local a = str:match("^%-%-%[%[")
if not a then return nil end if not a then return nil end
local s=str:find("%-%-%]%]") local s = str:find("%-%-%]%]")
if not s then return nil end if not s then return nil end
return str:sub(1,s+3) return str:sub(1,s+3)
end end
function lexElements.linecomment(str) function lexElements.linecomment(str)
return str:match("^%-%-[^\r\n]+") return str:match("^%-%-[^\r\n]+")
end end
local lexElementsOrder={"keyword","whitespace","string","blockcomment","linecomment","cleanup"} -- Note: EOL is not reliable for linecounting purposes, but keeps the code better formatted, if anything
local lexResults={} function lexElements.eol()
while code:len()>0 do return str:match("^[\n]+")
end
local lexElementsOrder = {"keyword", "eol", "whitespace",
"string", "blockcomment", "linecomment", "cleanup"}
local lexResults = {}
while code:len() > 0 do
--Because break doesn't exist. --Because break doesn't exist.
local function parseElem() local function parseElem()
for _,v in ipairs(lexElementsOrder) do for _,v in ipairs(lexElementsOrder) do
local t,e=lexElements[v](code) local t, e=lexElements[v](code)
if t then if t then
code=code:sub(t:len()+1) code=code:sub(t:len() + 1)
table.insert(lexResults,{v,t}) table.insert(lexResults,{v, t})
return nil return nil
end end
if e then if e then
@ -413,7 +418,7 @@ local function lexLua(code)
return "no match" return "no match"
end end
local err=parseElem() local err=parseElem()
if err then return nil,"Lexer Error:"..err..": "..code:sub(1,32) end if err then return nil, "Lexer Error:"..err..": "..code:sub(1, 32) end
end end
return lexResults return lexResults
end end
@ -432,30 +437,31 @@ local function code_prohibited(code)
-- This only exists in Lua 5.2 -- This only exists in Lua 5.2
--(dummy) goto --(dummy) goto
local lexed,err=lexLua(code) local lexed, err=lexLua(code)
local rcode="" local rcode = ""
if err then if err then
return nil,"Couldn't lex code:"..err return nil, "Couldn't lex code:"..err
end end
for _,v in ipairs(lexed) do for _, v in ipairs(lexed) do
--remove useless stuff since we're going over it anyway. --remove/reduce useless stuff since we're going over it anyway.
if v[1]=="whitespace" then v[2]="\r\n" end if v[1] == "whitespace" then v[2] = " " end
if v[1]=="blockcomment" then v[2]="" end if v[1] == "eol" then v[2] = "\r\n" end -- Windows users may like the \r
if v[1]=="linecomment" then v[2]="" end if v[1] == "blockcomment" then v[2] = "" end
if v[1] == "linecomment" then v[2] = "" end
--Code injection so we can safely use every feature in Lua. The only purpose of the lexer is to filter stuff that would break this, really. --Code injection so we can safely use every feature in Lua. The only purpose of the lexer is to filter stuff that would break this, really.
--(the old method would stop you from using keywords in strings.) --(the old method would stop you from using keywords in strings.)
--You can also replace this with coroutine.yield, but that's best left to a computercraft-style mod. --You can also replace this with coroutine.yield, but that's best left to a computercraft-style mod.
local dummy="(function() end)()" local dummy = "(function() end)()"
if v[2]=="do" then if v[2] == "do" then
v[2]="do "..dummy v[2] = "do "..dummy
end end
if v[2]=="repeat" then if v[2] == "repeat" then
v[2]="repeat "..dummy v[2] = "repeat "..dummy
end end
if v[2]=="goto" then if v[2] == "goto" then
v[2]=dummy.." goto" v[2] = dummy.." goto"
end end
rcode=rcode..v[2] rcode = rcode..v[2]
end end
return rcode return rcode
end end
@ -507,7 +513,7 @@ local function run(pos, event)
local mem = load_memory(meta) local mem = load_memory(meta)
local code = meta:get_string("code") local code = meta:get_string("code")
local err local err
code,err = code_prohibited(code) code, err = code_prohibited(code)
if not code then return err end if not code then return err end
-- Create environment -- Create environment