lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello All,

I am implementing a Lua shell using load function.
It is working fine for single line entries. I would like to add multiline support WITHOUT adding a explicit multi-line line terminator (like ‘\’ in C and makefiles)

Question: Is there a parser that the reader can use to figure out when the chunk is completed?

For example, what could be done inside the following “chunk_reader” for detecting it?
Or should the load caller keep calling it based on “<eof>” presence in the error message?
Thanks in advance,
Varanda
 

idx = 1

-- complete chunk:
test_chunck1 = {
  "print(\"result of 1 == 1:\")\n",
  "if 1 == 1 then \n",
  "print(\"  true\")\n",
  "else print(\"  false\")\n",
  "end",
  "print(\"All Done\")\n",
  "end\n"
}

-- incomplete chunk
test_chunck2 = {
  "print(\"result of 1 == 1:\")\n",
  "if 1 == 1 then \n"
}


selected_chunk = test_chunck1


function chunck_reader()
  if idx > #selected_chunk then
    return nil
  end
  local ret = selected_chunk[idx]
  idx = idx + 1
  return ret
end
  
function test_load()
  local f,msg = load(chunck_reader)
  if f ~= nil then
    f()
  else
    print("error: " .. msg)
  end
end

print("load a complete chunk:")
selected_chunk = test_chunck1
idx = 1
test_load()

print("load an incomplete chunk:")
selected_chunk = test_chunck2
idx = 1
test_load()