luapretty-printlpeg

Lua lunadry error


I've been using lunadry to reformat my code for me, but I've run into errors, namely, this happens when I try it:

lua: ./lunadry.lua:322: assertion failed!
stack traceback:
    [C]: in function 'assert'
    ./lunadry.lua:322: in main chunk
    [C]: in ?

Now I've gone through a large chunk of code I had and tracked down the source of this error to this specific function...

function e.insertvalues(e,...)g(1,e,'table')local n,t
if y('#',...)==1 then
n,t=#e+1,...else
n,t=...end
if#t>0 then
for n=#e,n,-1 do
e[n+#t]=e[n]end
local i=1-n
for n=n,n+#t-1 do
e[n]=t[n+i]end
end
return e
end

(yes, it's supposed to look ugly formatted like that).

And even more specifically, taking out this bit of code makes it work again:

if y('#',...)==1 then
n,t=#e+1,...else
n,t=...end

It is the ...else and ...end bits that cause it to mess up.

I've been trying to get it to reformat that code so it looks pretty but it causes the error. For all I know that could simply have one replication of a sea of errors in the author's code, but I hope not. Here is the source of the file which does the magic: click me.

Could someone take a look at this and tell me what needs to be changed, to solve this very annoying bug? Thank you!


Solution

  • This is caused by matching ... as a keyword. For example, instances of lunadry.lua:

    K "..."

    should instead be

    C "..."

    Use this patch:

    diff --git a/lunadry.lua b/lunadry.lua
    index e056140..19d714b 100755
    --- a/lunadry.lua
    +++ b/lunadry.lua
    @@ -201,7 +201,7 @@ local lua = lpeg.locale {
               K "true" +
               V "Number" +
               V "String" +
    -          K "..." +
    +          C "..." +
               V "function" +
               V "tableconstructor" +
               V "functioncall" +
    @@ -251,8 +251,8 @@ local lua = lpeg.locale {
    
       funcbody = C "(" * V "whitespace" * (V "parlist" * V "whitespace")^-1 * C ")" * INDENT_INCREASE(V "block" * V "whitespace") * INDENT * K "end";
    
    -  parlist = V "namelist" * (V "whitespace" * C "," * SPACE * V "whitespace" * K "...")^-1 +
    -            K "...";
    +  parlist = V "namelist" * (V "whitespace" * C "," * SPACE * V "whitespace" * C "...")^-1 +
    +            C "...";
    
       tableconstructor = FLATTEN(C "{" * (INDENT_INCREASE(V "filler" * V "fieldlist" * V "filler") * INDENT + V "filler") * C "}");
    

    I will commit the fix later today.