1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 17:59:30 +00:00

Fixed missing whitespace in some continue code generation

Also made Candran returns slightly more usefull error messages
This commit is contained in:
Étienne Fildadut 2018-08-02 17:11:46 +02:00
parent 4517ee110f
commit debf0bc464
3 changed files with 110 additions and 89 deletions

View file

@ -160,7 +160,14 @@ function candran.load(chunk, chunkname, env, options={})
options = util.merge({ chunkname = tostring(chunkname or chunk) }, options) options = util.merge({ chunkname = tostring(chunkname or chunk) }, options)
codeCache[options.chunkname] = candran.make(chunk, options) codeCache[options.chunkname] = candran.make(chunk, options)
local f = util.load(codeCache[options.chunkname], options.chunkname, env) local f, err = util.load(codeCache[options.chunkname], options.chunkname, env)
-- Um. Candran isn't supposed to generate invalid Lua code, so this is a major issue.
-- This is not going to raise an error because this is supposed to behave similarly to Lua's load function.
-- But the error message will likely be useless unless you know how Candran works.
if f == nil then
return f, "Candran unexpectedly generated invalid code: "..err
end
if options.rewriteErrors == false then if options.rewriteErrors == false then
return f return f
@ -185,7 +192,13 @@ end
--- Candran equivalent to the Lua 5.3's dofile funtion. --- Candran equivalent to the Lua 5.3's dofile funtion.
-- Will rewrite errors by default. -- Will rewrite errors by default.
function candran.dofile(filename, options) function candran.dofile(filename, options)
return candran.loadfile(filename, nil, options)() local f, err = candran.loadfile(filename, nil, options)
if f == nil then
error(err)
else
return f()
end
end end
--- Candran error message handler. --- Candran error message handler.

View file

@ -394,7 +394,7 @@ r = r .. ("repeat" .. indent()) -- ./compiler/lua53.can:231
end -- ./compiler/lua53.can:231 end -- ./compiler/lua53.can:231
r = r .. (lua(t[5])) -- ./compiler/lua53.can:233 r = r .. (lua(t[5])) -- ./compiler/lua53.can:233
if hasContinue then -- ./compiler/lua53.can:234 if hasContinue then -- ./compiler/lua53.can:234
r = r .. ("until true" .. unindent()) -- ./compiler/lua53.can:235 r = r .. (unindent() .. "until true") -- ./compiler/lua53.can:235
end -- ./compiler/lua53.can:235 end -- ./compiler/lua53.can:235
return r .. unindent() .. "end" -- ./compiler/lua53.can:237 return r .. unindent() .. "end" -- ./compiler/lua53.can:237
else -- ./compiler/lua53.can:237 else -- ./compiler/lua53.can:237
@ -418,7 +418,7 @@ r = r .. ("repeat" .. indent()) -- ./compiler/lua53.can:256
end -- ./compiler/lua53.can:256 end -- ./compiler/lua53.can:256
r = r .. (lua(t[3])) -- ./compiler/lua53.can:258 r = r .. (lua(t[3])) -- ./compiler/lua53.can:258
if hasContinue then -- ./compiler/lua53.can:259 if hasContinue then -- ./compiler/lua53.can:259
r = r .. ("until true" .. unindent()) -- ./compiler/lua53.can:260 r = r .. (unindent() .. "until true") -- ./compiler/lua53.can:260
end -- ./compiler/lua53.can:260 end -- ./compiler/lua53.can:260
return r .. unindent() .. "end" -- ./compiler/lua53.can:262 return r .. unindent() .. "end" -- ./compiler/lua53.can:262
end, -- ./compiler/lua53.can:262 end, -- ./compiler/lua53.can:262
@ -953,7 +953,7 @@ r = r .. ("repeat" .. indent()) -- ./compiler/lua53.can:231
end -- ./compiler/lua53.can:231 end -- ./compiler/lua53.can:231
r = r .. (lua(t[5])) -- ./compiler/lua53.can:233 r = r .. (lua(t[5])) -- ./compiler/lua53.can:233
if hasContinue then -- ./compiler/lua53.can:234 if hasContinue then -- ./compiler/lua53.can:234
r = r .. ("until true" .. unindent()) -- ./compiler/lua53.can:235 r = r .. (unindent() .. "until true") -- ./compiler/lua53.can:235
end -- ./compiler/lua53.can:235 end -- ./compiler/lua53.can:235
return r .. unindent() .. "end" -- ./compiler/lua53.can:237 return r .. unindent() .. "end" -- ./compiler/lua53.can:237
else -- ./compiler/lua53.can:237 else -- ./compiler/lua53.can:237
@ -977,7 +977,7 @@ r = r .. ("repeat" .. indent()) -- ./compiler/lua53.can:256
end -- ./compiler/lua53.can:256 end -- ./compiler/lua53.can:256
r = r .. (lua(t[3])) -- ./compiler/lua53.can:258 r = r .. (lua(t[3])) -- ./compiler/lua53.can:258
if hasContinue then -- ./compiler/lua53.can:259 if hasContinue then -- ./compiler/lua53.can:259
r = r .. ("until true" .. unindent()) -- ./compiler/lua53.can:260 r = r .. (unindent() .. "until true") -- ./compiler/lua53.can:260
end -- ./compiler/lua53.can:260 end -- ./compiler/lua53.can:260
return r .. unindent() .. "end" -- ./compiler/lua53.can:262 return r .. unindent() .. "end" -- ./compiler/lua53.can:262
end, -- ./compiler/lua53.can:262 end, -- ./compiler/lua53.can:262
@ -2855,10 +2855,10 @@ end + P("--") * (P(1) - P("\
["ShortStr"] = P("\"") * Cs((V("EscSeq") + (P(1) - S("\"\ ["ShortStr"] = P("\"") * Cs((V("EscSeq") + (P(1) - S("\"\
"))) ^ 0) * expect(P("\""), "Quote") + P("'") * Cs((V("EscSeq") + (P(1) - S("'\ "))) ^ 0) * expect(P("\""), "Quote") + P("'") * Cs((V("EscSeq") + (P(1) - S("'\
"))) ^ 0) * expect(P("'"), "Quote"), -- ./lib/lua-parser/parser.lua:601 "))) ^ 0) * expect(P("'"), "Quote"), -- ./lib/lua-parser/parser.lua:601
["EscSeq"] = P("\\") / "" * (P("a") / "" + P("b") / "" + P("f") / " " + P("n") / "\ ["EscSeq"] = P("\\") / "" * (P("a") / "\7" + P("b") / "\8" + P("f") / "\12" + P("n") / "\
" + P("r") / "\r" + P("t") / " " + P("v") / " " + P("\ " + P("r") / "\13" + P("t") / "\9" + P("v") / "\11" + P("\
") / "\ ") / "\
" + P("\r") / "\ " + P("\13") / "\
" + P("\\") / "\\" + P("\"") / "\"" + P("'") / "'" + P("z") * space ^ 0 / "" + digit * digit ^ - 2 / tonumber / string["char"] + P("x") * expect(C(xdigit * xdigit), "HexEsc") * Cc(16) / tonumber / string["char"] + P("u") * expect("{", "OBraceUEsc") * expect(C(xdigit ^ 1), "DigitUEsc") * Cc(16) * expect("}", "CBraceUEsc") / tonumber / (utf8 and utf8["char"] or string["char"]) + throw("EscSeq")), -- ./lib/lua-parser/parser.lua:631 " + P("\\") / "\\" + P("\"") / "\"" + P("'") / "'" + P("z") * space ^ 0 / "" + digit * digit ^ - 2 / tonumber / string["char"] + P("x") * expect(C(xdigit * xdigit), "HexEsc") * Cc(16) / tonumber / string["char"] + P("u") * expect("{", "OBraceUEsc") * expect(C(xdigit ^ 1), "DigitUEsc") * Cc(16) * expect("}", "CBraceUEsc") / tonumber / (utf8 and utf8["char"] or string["char"]) + throw("EscSeq")), -- ./lib/lua-parser/parser.lua:631
["LongStr"] = V("Open") * C((P(1) - V("CloseEq")) ^ 0) * expect(V("Close"), "CloseLStr") / function(s, eqs) -- ./lib/lua-parser/parser.lua:634 ["LongStr"] = V("Open") * C((P(1) - V("CloseEq")) ^ 0) * expect(V("Close"), "CloseLStr") / function(s, eqs) -- ./lib/lua-parser/parser.lua:634
return s -- ./lib/lua-parser/parser.lua:634 return s -- ./lib/lua-parser/parser.lua:634
@ -2975,7 +2975,7 @@ env["write"](f:read("*a")) -- candran.can:93
f:close() -- candran.can:94 f:close() -- candran.can:94
end -- candran.can:94 end -- candran.can:94
env["write"] = function(...) -- candran.can:98 env["write"] = function(...) -- candran.can:98
env["output"] = env["output"] .. (table["concat"]({ ... }, " ") .. "\ env["output"] = env["output"] .. (table["concat"]({ ... }, "\9") .. "\
") -- candran.can:99 ") -- candran.can:99
end -- candran.can:99 end -- candran.can:99
env["placeholder"] = function(name) -- candran.can:103 env["placeholder"] = function(name) -- candran.can:103
@ -3020,84 +3020,92 @@ candran["load"] = function(chunk, chunkname, env, options) -- candran.can:159
if options == nil then options = {} end -- candran.can:159 if options == nil then options = {} end -- candran.can:159
options = util["merge"]({ ["chunkname"] = tostring(chunkname or chunk) }, options) -- candran.can:160 options = util["merge"]({ ["chunkname"] = tostring(chunkname or chunk) }, options) -- candran.can:160
codeCache[options["chunkname"]] = candran["make"](chunk, options) -- candran.can:162 codeCache[options["chunkname"]] = candran["make"](chunk, options) -- candran.can:162
local f = util["load"](codeCache[options["chunkname"]], options["chunkname"], env) -- candran.can:163 local f, err = util["load"](codeCache[options["chunkname"]], options["chunkname"], env) -- candran.can:163
if options["rewriteErrors"] == false then -- candran.can:165 if f == nil then -- candran.can:168
return f -- candran.can:166 return f, "Candran unexpectedly generated invalid code: " .. err -- candran.can:169
else -- candran.can:166 end -- candran.can:169
return function(...) -- candran.can:168 if options["rewriteErrors"] == false then -- candran.can:172
local params = { ... } -- candran.can:169 return f -- candran.can:173
if not errorRewritingActive then -- candran.can:170 else -- candran.can:173
errorRewritingActive = true -- candran.can:171 return function(...) -- candran.can:175
local t = { xpcall(function() -- candran.can:172 local params = { ... } -- candran.can:176
return f(unpack(params)) -- candran.can:172 if not errorRewritingActive then -- candran.can:177
end, candran["messageHandler"]) } -- candran.can:172 errorRewritingActive = true -- candran.can:178
errorRewritingActive = false -- candran.can:173 local t = { xpcall(function() -- candran.can:179
if t[1] == false then -- candran.can:174 return f(unpack(params)) -- candran.can:179
error(t[2], 0) -- candran.can:175 end, candran["messageHandler"]) } -- candran.can:179
end -- candran.can:175 errorRewritingActive = false -- candran.can:180
return unpack(t, 2) -- candran.can:177 if t[1] == false then -- candran.can:181
else -- candran.can:177 error(t[2], 0) -- candran.can:182
return f(...) -- candran.can:179 end -- candran.can:182
end -- candran.can:179 return unpack(t, 2) -- candran.can:184
end -- candran.can:179 else -- candran.can:184
end -- candran.can:179 return f(...) -- candran.can:186
end -- candran.can:179 end -- candran.can:186
candran["dofile"] = function(filename, options) -- candran.can:187 end -- candran.can:186
return candran["loadfile"](filename, nil, options)() -- candran.can:188 end -- candran.can:186
end -- candran.can:188 end -- candran.can:186
candran["messageHandler"] = function(message) -- candran.can:193 candran["dofile"] = function(filename, options) -- candran.can:194
local f, err = candran["loadfile"](filename, nil, options) -- candran.can:195
if f == nil then -- candran.can:197
error(err) -- candran.can:198
else -- candran.can:198
return f() -- candran.can:200
end -- candran.can:200
end -- candran.can:200
candran["messageHandler"] = function(message) -- candran.can:206
return debug["traceback"](message, 2):gsub("(\ return debug["traceback"](message, 2):gsub("(\
?%s*)([^\ ?%s*)([^\
]-)%:(%d+)%:", function(indentation, source, line) -- candran.can:194 ]-)%:(%d+)%:", function(indentation, source, line) -- candran.can:207
line = tonumber(line) -- candran.can:195 line = tonumber(line) -- candran.can:208
local originalFile -- candran.can:197 local originalFile -- candran.can:210
local strName = source:match("%[string \"(.-)\"%]") -- candran.can:198 local strName = source:match("%[string \"(.-)\"%]") -- candran.can:211
if strName then -- candran.can:199 if strName then -- candran.can:212
if codeCache[strName] then -- candran.can:200 if codeCache[strName] then -- candran.can:213
originalFile = codeCache[strName] -- candran.can:201 originalFile = codeCache[strName] -- candran.can:214
source = strName -- candran.can:202 source = strName -- candran.can:215
end -- candran.can:202 end -- candran.can:215
else -- candran.can:202 else -- candran.can:215
local fi = io["open"](source, "r") -- candran.can:205 local fi = io["open"](source, "r") -- candran.can:218
if fi then -- candran.can:206 if fi then -- candran.can:219
originalFile = fi:read("*a") -- candran.can:207 originalFile = fi:read("*a") -- candran.can:220
end -- candran.can:207 end -- candran.can:220
fi:close() -- candran.can:209 fi:close() -- candran.can:222
end -- candran.can:209 end -- candran.can:222
if originalFile then -- candran.can:212 if originalFile then -- candran.can:225
local i = 0 -- candran.can:213 local i = 0 -- candran.can:226
for l in originalFile:gmatch("([^\ for l in originalFile:gmatch("([^\
]*)") do -- candran.can:214 ]*)") do -- candran.can:227
i = i + 1 -- candran.can:215 i = i + 1 -- candran.can:228
if i == line then -- candran.can:216 if i == line then -- candran.can:229
local extSource, lineMap = l:match("%-%- (.-)%:(%d+)$") -- candran.can:217 local extSource, lineMap = l:match("%-%- (.-)%:(%d+)$") -- candran.can:230
if lineMap then -- candran.can:218 if lineMap then -- candran.can:231
if extSource ~= source then -- candran.can:219 if extSource ~= source then -- candran.can:232
return indentation .. extSource .. ":" .. lineMap .. "(" .. extSource .. ":" .. line .. "):" -- candran.can:220 return indentation .. extSource .. ":" .. lineMap .. "(" .. extSource .. ":" .. line .. "):" -- candran.can:233
else -- candran.can:220 else -- candran.can:233
return indentation .. extSource .. ":" .. lineMap .. "(" .. line .. "):" -- candran.can:222 return indentation .. extSource .. ":" .. lineMap .. "(" .. line .. "):" -- candran.can:235
end -- candran.can:222 end -- candran.can:235
end -- candran.can:222 end -- candran.can:235
break -- candran.can:225 break -- candran.can:238
end -- candran.can:225
end -- candran.can:225
end -- candran.can:225
end) -- candran.can:225
end -- candran.can:225
candran["searcher"] = function(modpath) -- candran.can:233
local filepath = util["search"](modpath, { "can" }) -- candran.can:234
if not filepath then -- candran.can:235
return "\
no candran file in package.path" -- candran.can:236
end -- candran.can:236
return candran["loadfile"](filepath) -- candran.can:238
end -- candran.can:238 end -- candran.can:238
candran["setup"] = function() -- candran.can:242 end -- candran.can:238
if _VERSION == "Lua 5.1" then -- candran.can:243 end -- candran.can:238
table["insert"](package["loaders"], 2, candran["searcher"]) -- candran.can:244 end) -- candran.can:238
else -- candran.can:244 end -- candran.can:238
table["insert"](package["searchers"], 2, candran["searcher"]) -- candran.can:246 candran["searcher"] = function(modpath) -- candran.can:246
end -- candran.can:246 local filepath = util["search"](modpath, { "can" }) -- candran.can:247
return candran -- candran.can:248 if not filepath then -- candran.can:248
end -- candran.can:248 return "\
return candran -- candran.can:251 \9no candran file in package.path" -- candran.can:249
end -- candran.can:249
return candran["loadfile"](filepath) -- candran.can:251
end -- candran.can:251
candran["setup"] = function() -- candran.can:255
if _VERSION == "Lua 5.1" then -- candran.can:256
table["insert"](package["loaders"], 2, candran["searcher"]) -- candran.can:257
else -- candran.can:257
table["insert"](package["searchers"], 2, candran["searcher"]) -- candran.can:259
end -- candran.can:259
return candran -- candran.can:261
end -- candran.can:261
return candran -- candran.can:264

View file

@ -232,7 +232,7 @@ return function(code, ast, options)
end end
r ..= lua(t[5]) r ..= lua(t[5])
if hasContinue then if hasContinue then
r ..= "until true" .. unindent() r ..= unindent() .. "until true"
end end
return r .. unindent() .. "end" return r .. unindent() .. "end"
else else
@ -257,7 +257,7 @@ return function(code, ast, options)
end end
r ..= lua(t[3]) r ..= lua(t[3])
if hasContinue then if hasContinue then
r ..= "until true" .. unindent() r ..= unindent() .. "until true"
end end
return r .. unindent() .. "end" return r .. unindent() .. "end"
end, end,