ldoc icon indicating copy to clipboard operation
ldoc copied to clipboard

Ldoc and Lua disagrees concerning tables

Open FredrikKarlssonSpeech opened this issue 7 years ago • 1 comments
trafficstars

Hi,

I got this error message from Ldoc related to a table that I defined.

DEFAULT_TIME_OF_DAY: declared table cannot have array entries

Now, Lua itself seems is fine with the table:

function debugTable(node)
  -- handing two printing functions
  local printFunc = print
  if (fibaro or {}).debug then
    function printFunc(...);
      return fibaro:debug(...);
    end;
  end;
  -- to make output beautiful
  local function tab(amt)
    local str = "";
    for i=1,amt do
      str = str .. "\t";
    end;
    return str;
  end;

  local cache, stack = {},{};
  local depth = 1;
  local output_str = "{\n";

  while true do
    if not (cache[node]) then
      cache[node] = {};
    end;

    local size = 0;
    for k,v in pairs(node) do
      size = size + 1;
    end;

    local cur_index = 1;
    for k,v in pairs(node) do
      if not (cache[node][k]) then
        cache[node][k] = {};
      end;

      -- caches results since we will be recursing child nodes
      if (cache[node][k][v] == nil) then
        cache[node][k][v] = true;

        if (string.find(output_str,"}",output_str:len())) then
          output_str = output_str .. ",\n";
        elseif not (string.find(output_str,"\n",output_str:len())) then
          output_str = output_str .. "\n";
        end;

        local key;
        if (type(k) == "userdata") then
          key = "[userdata]";
        elseif (type(k) == "string") then
          key = "['"..tostring(k).."']";
        else
          key = "["..tostring(k).."]";
        end;

        if (type(v) == "table") then
          output_str = output_str .. tab(depth) .. key .. " = {\n";
          table.insert(stack,node);
          table.insert(stack,v);
          break;
        elseif (type(v) == "userdata") then
          output_str = output_str .. tab(depth) .. key .. " = userdata";
        elseif (type(v) == "string") then
          output_str = output_str .. tab(depth) .. key .. " = '"..v.."'";
        else
          output_str = output_str .. tab(depth) .. key .. " = "..tostring(v);
        end;

        if (cur_index == size) then
          output_str = output_str .. "\n" .. tab(depth-1) .. "}";
        else
          output_str = output_str .. ",";
        end;
      else
        -- close the table
        if (cur_index == size) then
          output_str = output_str .. "\n" .. tab(depth-1) .. "}";
        end;
      end;
      cur_index = cur_index + 1;
    end;

    if (#stack > 0) then
      node = stack[#stack];
      stack[#stack] = nil;
      depth = cache[node] == nil and depth + 1 or depth - 1;
    else
      break;
    end;
  end;
  printFunc(output_str);
end;

DEFAULT_TIME_OF_DAY = {
	{["days"]={2,3,4,5,6},["time"]="06:30",["tod"]="Morning"},
	{["days"]={1,7},["time"]="06:30",["tod"]="Morning"},
	{["days"]={2,3,4,5,6},["time"]="08:00",["tod"]="Day"},
	{["days"]={1,7},["time"]="10:00",["tod"]="Day"},
	{["days"]={1,2,3,4,5,6,7},["time"]="18:00",["tod"]="Evening"},
	{["days"]={2,3,4,5,6},["time"]="23:30",["tod"]="Night"},
	{["days"]={1,7},["time"]="23:59",["tod"]="Night"}
};

> debugTable(DEFAULT_TIME_OF_DAY)
{
	[1] = {
		['days'] = {
			[1] = 2,
			[2] = 3,
			[3] = 4,
			[4] = 5,
			[5] = 6
		},
		['time'] = '06:30',
		['tod'] = 'Morning'
	},
	[2] = {
		['days'] = {
			[1] = 1,
			[2] = 7
		},
		['time'] = '06:30',
		['tod'] = 'Morning'
	},
	[3] = {
		['days'] = {
			[1] = 2,
			[2] = 3,
			[3] = 4,
			[4] = 5,
			[5] = 6
		},
		['time'] = '08:00',
		['tod'] = 'Day'
	},
	[4] = {
		['days'] = {
			[1] = 1,
			[2] = 7
		},
		['time'] = '10:00',
		['tod'] = 'Day'
	},
	[5] = {
		['days'] = {
			[1] = 1,
			[2] = 2,
			[3] = 3,
			[4] = 4,
			[5] = 5,
			[6] = 6,
			[7] = 7
		},
		['time'] = '18:00',
		['tod'] = 'Evening'
	},
	[6] = {
		['days'] = {
			[1] = 2,
			[2] = 3,
			[3] = 4,
			[4] = 5,
			[5] = 6
		},
		['time'] = '23:30',
		['tod'] = 'Night'
	},
	[7] = {
		['days'] = {
			[1] = 1,
			[2] = 7
		},
		['time'] = '23:59',
		['tod'] = 'Night'
	}
}

I assumed that Lua and Ldoc were in agreement on what the correct syntax for Lua would be, but maybe I assumed wrong.

FredrikKarlssonSpeech avatar Nov 21 '17 13:11 FredrikKarlssonSpeech

Quote from the doc:

Only single-level tables are currently supported, and the fields must be valid identifiers.

So, for documentation purposes, only non-keyword string keys work, apparently.

mwchase avatar Nov 18 '18 21:11 mwchase

I don't get this error when running ldoc on the sample input. I'm going to assume that means this was addressed at some point, but it could also mean the original post wasn't actually a full working MWE. If the later turns out to be the case anyone is welcome to comment with a case we can look into.

alerque avatar May 04 '23 21:05 alerque

Could also have been a duplicate of #101 if that is what this was about. I'm a little confused.

alerque avatar May 04 '23 21:05 alerque