mirror of
https://github.com/Reuh/candran.git
synced 2026-02-04 02:08:40 +00:00
feat: add named varargs from Lua 5.5
This commit is contained in:
parent
73e3f95636
commit
c5be1088c5
3 changed files with 32 additions and 26 deletions
|
|
@ -34,7 +34,7 @@ expr:
|
||||||
| `Boolean{ <boolean> }
|
| `Boolean{ <boolean> }
|
||||||
| `Number{ <string> } -- we don't use convert to number to avoid losing precision when tostring()-ing it later
|
| `Number{ <string> } -- we don't use convert to number to avoid losing precision when tostring()-ing it later
|
||||||
| `String{ <string> }
|
| `String{ <string> }
|
||||||
| `Function{ { ( `ParPair{ Id expr } | `Id{ <string> } )* `Dots? } block }
|
| `Function{ { ( `ParPair{ Id expr } | `Id{ <string> } )* `ParDots? } block }
|
||||||
| `Table{ ( `Pair{ expr expr } | expr )* }
|
| `Table{ ( `Pair{ expr expr } | expr )* }
|
||||||
| `Op{ opid expr expr? }
|
| `Op{ opid expr expr? }
|
||||||
| `Paren{ expr } -- significant to cut multiple values returns
|
| `Paren{ expr } -- significant to cut multiple values returns
|
||||||
|
|
@ -573,8 +573,8 @@ local G = { V"Lua",
|
||||||
* (sym(":") * expect(V"StrId", "NameFunc2"))^-1 / markMethod;
|
* (sym(":") * expect(V"StrId", "NameFunc2"))^-1 / markMethod;
|
||||||
FuncBody = tagC("Function", V"FuncParams" * expectBlockWithEnd("EndFunc"));
|
FuncBody = tagC("Function", V"FuncParams" * expectBlockWithEnd("EndFunc"));
|
||||||
FuncParams = expect(sym("("), "OParenPList") * V"ParList" * expect(sym(")"), "CParenPList");
|
FuncParams = expect(sym("("), "OParenPList") * V"ParList" * expect(sym(")"), "CParenPList");
|
||||||
ParList = V"NamedParList" * (sym(",") * expect(tagC("Dots", sym("...")), "ParList"))^-1 / addDots
|
ParList = V"NamedParList" * (sym(",") * expect(tagC("ParDots", sym("...") * V"Id"^-1), "ParList"))^-1 / addDots
|
||||||
+ Ct(tagC("Dots", sym("...")))
|
+ Ct(tagC("ParDots", sym("...") * V"Id"^-1))
|
||||||
+ Ct(Cc()); -- Cc({}) generates a bug since the {} would be shared across parses
|
+ Ct(Cc()); -- Cc({}) generates a bug since the {} would be shared across parses
|
||||||
|
|
||||||
ShortFuncDef = tagC("Function", V"ShortFuncParams" * maybeBlockWithEnd()) / fixShortFunc;
|
ShortFuncDef = tagC("Function", V"ShortFuncParams" * maybeBlockWithEnd()) / fixShortFunc;
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ local traverse_block, traverse_explist, traverse_varlist, traverse_parlist
|
||||||
function traverse_parlist (env, parlist)
|
function traverse_parlist (env, parlist)
|
||||||
local len = #parlist
|
local len = #parlist
|
||||||
local is_vararg = false
|
local is_vararg = false
|
||||||
if len > 0 and parlist[len].tag == "Dots" then
|
if len > 0 and parlist[len].tag == "ParDots" then
|
||||||
is_vararg = true
|
is_vararg = true
|
||||||
end
|
end
|
||||||
set_vararg(env, is_vararg)
|
set_vararg(env, is_vararg)
|
||||||
|
|
@ -348,7 +348,7 @@ function traverse_exp (env, exp)
|
||||||
return true
|
return true
|
||||||
elseif tag == "Dots" then
|
elseif tag == "Dots" then
|
||||||
return traverse_vararg(env, exp)
|
return traverse_vararg(env, exp)
|
||||||
elseif tag == "Function" then -- `Function{ { `Id{ <string> }* `Dots? } block }
|
elseif tag == "Function" then -- `Function{ { `Id{ <string> }* `ParDots? } block }
|
||||||
return traverse_function(env, exp)
|
return traverse_function(env, exp)
|
||||||
elseif tag == "Table" then -- `Table{ ( `Pair{ expr expr } | expr )* }
|
elseif tag == "Table" then -- `Table{ ( `Pair{ expr expr } | expr )* }
|
||||||
return traverse_table(env, exp)
|
return traverse_table(env, exp)
|
||||||
|
|
|
||||||
|
|
@ -587,33 +587,35 @@ return function(code, ast, options, macros={functions={}, variables={}})
|
||||||
String = (t)
|
String = (t)
|
||||||
return "%q":format(t[1])
|
return "%q":format(t[1])
|
||||||
end,
|
end,
|
||||||
-- Function{ { ( `ParPair{ Id expr } | `Id{ <string> } )* `Dots? } block }
|
-- Function{ { ( `ParPair{ Id expr } | `Id{ <string> } )* `ParDots? } block }
|
||||||
|
_functionParameter = {
|
||||||
|
ParPair = (t, decl)
|
||||||
|
local id = lua(t[1])
|
||||||
|
indentLevel += 1
|
||||||
|
table.insert(decl, "if "..id.." == nil then "..id.." = "..lua(t[2]).." end")
|
||||||
|
indentLevel -= 1
|
||||||
|
return id
|
||||||
|
end,
|
||||||
|
ParDots = (t, decl)
|
||||||
|
if #t == 1 then
|
||||||
|
return "..." .. lua(t[1])
|
||||||
|
else
|
||||||
|
return "..."
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
},
|
||||||
_functionWithoutKeyword = (t)
|
_functionWithoutKeyword = (t)
|
||||||
local r = "("
|
local r = "("
|
||||||
local decl = {}
|
local decl = {}
|
||||||
if t[1][1] then
|
local pars = {}
|
||||||
if t[1][1].tag == "ParPair" then
|
for i=1, #t[1], 1 do
|
||||||
local id = lua(t[1][1][1])
|
if tags._functionParameter[t[1][i].tag] then
|
||||||
indentLevel += 1
|
table.insert(pars, tags._functionParameter[t[1][i].tag](t[1][i], decl))
|
||||||
table.insert(decl, "if "..id.." == nil then "..id.." = "..lua(t[1][1][2]).." end")
|
|
||||||
indentLevel -= 1
|
|
||||||
r ..= id
|
|
||||||
else
|
else
|
||||||
r ..= lua(t[1][1])
|
table.insert(pars, lua(t[1][i]))
|
||||||
end
|
|
||||||
for i=2, #t[1], 1 do
|
|
||||||
if t[1][i].tag == "ParPair" then
|
|
||||||
local id = lua(t[1][i][1])
|
|
||||||
indentLevel += 1
|
|
||||||
table.insert(decl, "if "..id.." == nil then "..id.." = "..lua(t[1][i][2]).." end")
|
|
||||||
indentLevel -= 1
|
|
||||||
r ..= ", " ..id
|
|
||||||
else
|
|
||||||
r ..= ", "..lua(t[1][i])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
r ..= table.concat(pars, ", ")..")"..indent()
|
||||||
r ..= ")"..indent()
|
|
||||||
for _, d in ipairs(decl) do
|
for _, d in ipairs(decl) do
|
||||||
r ..= d..newline()
|
r ..= d..newline()
|
||||||
end
|
end
|
||||||
|
|
@ -851,6 +853,10 @@ return function(code, ast, options, macros={functions={}, variables={}})
|
||||||
AttributeNameList = (t)
|
AttributeNameList = (t)
|
||||||
return lua(t, "_lhs")
|
return lua(t, "_lhs")
|
||||||
end,
|
end,
|
||||||
|
-- NameList{ {Id+} }
|
||||||
|
NameList = (t)
|
||||||
|
return lua(t, "_lhs")
|
||||||
|
end,
|
||||||
-- AttributeId{ <string> <string>? }
|
-- AttributeId{ <string> <string>? }
|
||||||
AttributeId = (t)
|
AttributeId = (t)
|
||||||
if t[2] then
|
if t[2] then
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue