mirror of
https://github.com/Reuh/candran.git
synced 2025-10-27 17:59:30 +00:00
candran.default
This commit is contained in:
parent
2d297db687
commit
3dcdfdc144
3 changed files with 23 additions and 17 deletions
|
|
@ -410,6 +410,8 @@ chunkname = "nil" -- The chunkname used when running code using the helper funct
|
||||||
rewriteErrors = true -- True to enable error rewriting when loading code using the helper functions. Will wrap the whole code in a xpcall().
|
rewriteErrors = true -- True to enable error rewriting when loading code using the helper functions. Will wrap the whole code in a xpcall().
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can change these values in the table `candran.default`.
|
||||||
|
|
||||||
There are also a few function-specific options available, see the associated functions documentation for more information.
|
There are also a few function-specific options available, see the associated functions documentation for more information.
|
||||||
|
|
||||||
### Compiling the library
|
### Compiling the library
|
||||||
|
|
|
||||||
13
candran.can
13
candran.can
|
|
@ -14,7 +14,7 @@ local candran = {
|
||||||
}
|
}
|
||||||
|
|
||||||
--- Default options.
|
--- Default options.
|
||||||
local default = {
|
candran.default = {
|
||||||
target = "lua53",
|
target = "lua53",
|
||||||
indentation = "",
|
indentation = "",
|
||||||
newline = "\n",
|
newline = "\n",
|
||||||
|
|
@ -29,7 +29,7 @@ local default = {
|
||||||
-- @tparam options table arguments for the preprocessor. They will be inserted into the preprocessor environement.
|
-- @tparam options table arguments for the preprocessor. They will be inserted into the preprocessor environement.
|
||||||
-- @treturn output string output code
|
-- @treturn output string output code
|
||||||
function candran.preprocess(input, options={})
|
function candran.preprocess(input, options={})
|
||||||
options = util.merge(default, options)
|
options = util.merge(candran.default, options)
|
||||||
|
|
||||||
-- generate preprocessor code
|
-- generate preprocessor code
|
||||||
local preprocessor = ""
|
local preprocessor = ""
|
||||||
|
|
@ -122,7 +122,7 @@ end
|
||||||
-- @tparam options table options for the compiler
|
-- @tparam options table options for the compiler
|
||||||
-- @treturn output string output code
|
-- @treturn output string output code
|
||||||
function candran.compile(input, options={})
|
function candran.compile(input, options={})
|
||||||
options = util.merge(default, options)
|
options = util.merge(candran.default, options)
|
||||||
|
|
||||||
local ast, errmsg = parser.parse(input, options.chunkname)
|
local ast, errmsg = parser.parse(input, options.chunkname)
|
||||||
|
|
||||||
|
|
@ -165,17 +165,18 @@ function candran.load(chunk, chunkname, env, options={})
|
||||||
if options.rewriteErrors == false then
|
if options.rewriteErrors == false then
|
||||||
return f
|
return f
|
||||||
else
|
else
|
||||||
return function()
|
return function(...)
|
||||||
|
local params = {...}
|
||||||
if not errorRewritingActive then
|
if not errorRewritingActive then
|
||||||
errorRewritingActive = true
|
errorRewritingActive = true
|
||||||
local t = { xpcall(f, candran.messageHandler) }
|
local t = { xpcall(() return f(unpack(params)) end, candran.messageHandler) }
|
||||||
errorRewritingActive = false
|
errorRewritingActive = false
|
||||||
if t[1] == false then
|
if t[1] == false then
|
||||||
error(t[2], 0)
|
error(t[2], 0)
|
||||||
end
|
end
|
||||||
return unpack(t, 2)
|
return unpack(t, 2)
|
||||||
else
|
else
|
||||||
return f()
|
return f(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
25
candran.lua
25
candran.lua
|
|
@ -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"),
|
"))) ^ 0) * expect(P("'"), "Quote"),
|
||||||
["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")),
|
" + 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")),
|
||||||
["LongStr"] = V("Open") * C((P(1) - V("CloseEq")) ^ 0) * expect(V("Close"), "CloseLStr") / function(s, eqs)
|
["LongStr"] = V("Open") * C((P(1) - V("CloseEq")) ^ 0) * expect(V("Close"), "CloseLStr") / function(s, eqs)
|
||||||
return s
|
return s
|
||||||
|
|
@ -2907,7 +2907,7 @@ end
|
||||||
local parser = _() or parser
|
local parser = _() or parser
|
||||||
package["loaded"]["lib.lua-parser.parser"] = parser or true
|
package["loaded"]["lib.lua-parser.parser"] = parser or true
|
||||||
local candran = { ["VERSION"] = "0.6.0" }
|
local candran = { ["VERSION"] = "0.6.0" }
|
||||||
local default = {
|
candran["default"] = {
|
||||||
["target"] = "lua53",
|
["target"] = "lua53",
|
||||||
["indentation"] = "",
|
["indentation"] = "",
|
||||||
["newline"] = "\
|
["newline"] = "\
|
||||||
|
|
@ -2919,7 +2919,7 @@ local default = {
|
||||||
}
|
}
|
||||||
candran["preprocess"] = function(input, options)
|
candran["preprocess"] = function(input, options)
|
||||||
if options == nil then options = {} end
|
if options == nil then options = {} end
|
||||||
options = util["merge"](default, options)
|
options = util["merge"](candran["default"], options)
|
||||||
local preprocessor = ""
|
local preprocessor = ""
|
||||||
local i = 0
|
local i = 0
|
||||||
for line in (input .. "\
|
for line in (input .. "\
|
||||||
|
|
@ -2975,7 +2975,7 @@ env["write"](f:read("*a"))
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
env["write"] = function(...)
|
env["write"] = function(...)
|
||||||
env["output"] = env["output"] .. (table["concat"]({ ... }, " ") .. "\
|
env["output"] = env["output"] .. (table["concat"]({ ... }, "\9") .. "\
|
||||||
")
|
")
|
||||||
end
|
end
|
||||||
env["placeholder"] = function(name)
|
env["placeholder"] = function(name)
|
||||||
|
|
@ -2995,7 +2995,7 @@ return output
|
||||||
end
|
end
|
||||||
candran["compile"] = function(input, options)
|
candran["compile"] = function(input, options)
|
||||||
if options == nil then options = {} end
|
if options == nil then options = {} end
|
||||||
options = util["merge"](default, options)
|
options = util["merge"](candran["default"], options)
|
||||||
local ast, errmsg = parser["parse"](input, options["chunkname"])
|
local ast, errmsg = parser["parse"](input, options["chunkname"])
|
||||||
if not ast then
|
if not ast then
|
||||||
error("Compiler: error while parsing file: " .. errmsg)
|
error("Compiler: error while parsing file: " .. errmsg)
|
||||||
|
|
@ -3024,17 +3024,20 @@ local f = util["load"](codeCache[options["chunkname"]], options["chunkname"], en
|
||||||
if options["rewriteErrors"] == false then
|
if options["rewriteErrors"] == false then
|
||||||
return f
|
return f
|
||||||
else
|
else
|
||||||
return function()
|
return function(...)
|
||||||
|
local params = { ... }
|
||||||
if not errorRewritingActive then
|
if not errorRewritingActive then
|
||||||
errorRewritingActive = true
|
errorRewritingActive = true
|
||||||
local t = { xpcall(f, candran["messageHandler"]) }
|
local t = { xpcall(function()
|
||||||
|
return f(unpack(params))
|
||||||
|
end, candran["messageHandler"]) }
|
||||||
errorRewritingActive = false
|
errorRewritingActive = false
|
||||||
if t[1] == false then
|
if t[1] == false then
|
||||||
error(t[2], 0)
|
error(t[2], 0)
|
||||||
end
|
end
|
||||||
return unpack(t, 2)
|
return unpack(t, 2)
|
||||||
else
|
else
|
||||||
return f()
|
return f(...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -3085,7 +3088,7 @@ candran["searcher"] = function(modpath)
|
||||||
local filepath = util["search"](modpath, { "can" })
|
local filepath = util["search"](modpath, { "can" })
|
||||||
if not filepath then
|
if not filepath then
|
||||||
return "\
|
return "\
|
||||||
no candran file in package.path"
|
\9no candran file in package.path"
|
||||||
end
|
end
|
||||||
return candran["loadfile"](filepath)
|
return candran["loadfile"](filepath)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue