1
0
Fork 0
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:
Étienne Fildadut 2025-12-26 15:12:33 +01:00
parent 73e3f95636
commit c5be1088c5
3 changed files with 32 additions and 26 deletions

View file

@ -34,7 +34,7 @@ expr:
| `Boolean{ <boolean> }
| `Number{ <string> } -- we don't use convert to number to avoid losing precision when tostring()-ing it later
| `String{ <string> }
| `Function{ { ( `ParPair{ Id expr } | `Id{ <string> } )* `Dots? } block }
| `Function{ { ( `ParPair{ Id expr } | `Id{ <string> } )* `ParDots? } block }
| `Table{ ( `Pair{ expr expr } | expr )* }
| `Op{ opid expr expr? }
| `Paren{ expr } -- significant to cut multiple values returns
@ -573,8 +573,8 @@ local G = { V"Lua",
* (sym(":") * expect(V"StrId", "NameFunc2"))^-1 / markMethod;
FuncBody = tagC("Function", V"FuncParams" * expectBlockWithEnd("EndFunc"));
FuncParams = expect(sym("("), "OParenPList") * V"ParList" * expect(sym(")"), "CParenPList");
ParList = V"NamedParList" * (sym(",") * expect(tagC("Dots", sym("...")), "ParList"))^-1 / addDots
+ Ct(tagC("Dots", sym("...")))
ParList = V"NamedParList" * (sym(",") * expect(tagC("ParDots", sym("...") * V"Id"^-1), "ParList"))^-1 / addDots
+ Ct(tagC("ParDots", sym("...") * V"Id"^-1))
+ Ct(Cc()); -- Cc({}) generates a bug since the {} would be shared across parses
ShortFuncDef = tagC("Function", V"ShortFuncParams" * maybeBlockWithEnd()) / fixShortFunc;

View file

@ -71,7 +71,7 @@ local traverse_block, traverse_explist, traverse_varlist, traverse_parlist
function traverse_parlist (env, parlist)
local len = #parlist
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
end
set_vararg(env, is_vararg)
@ -348,7 +348,7 @@ function traverse_exp (env, exp)
return true
elseif tag == "Dots" then
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)
elseif tag == "Table" then -- `Table{ ( `Pair{ expr expr } | expr )* }
return traverse_table(env, exp)

View file

@ -587,33 +587,35 @@ return function(code, ast, options, macros={functions={}, variables={}})
String = (t)
return "%q":format(t[1])
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)
local r = "("
local decl = {}
if t[1][1] then
if t[1][1].tag == "ParPair" then
local id = lua(t[1][1][1])
indentLevel += 1
table.insert(decl, "if "..id.." == nil then "..id.." = "..lua(t[1][1][2]).." end")
indentLevel -= 1
r ..= id
local pars = {}
for i=1, #t[1], 1 do
if tags._functionParameter[t[1][i].tag] then
table.insert(pars, tags._functionParameter[t[1][i].tag](t[1][i], decl))
else
r ..= lua(t[1][1])
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])
table.insert(pars, lua(t[1][i]))
end
end
end
r ..= ")"..indent()
r ..= table.concat(pars, ", ")..")"..indent()
for _, d in ipairs(decl) do
r ..= d..newline()
end
@ -851,6 +853,10 @@ return function(code, ast, options, macros={functions={}, variables={}})
AttributeNameList = (t)
return lua(t, "_lhs")
end,
-- NameList{ {Id+} }
NameList = (t)
return lua(t, "_lhs")
end,
-- AttributeId{ <string> <string>? }
AttributeId = (t)
if t[2] then