lua-users home
lua-l archive

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


Thanks V.
As incomplete is checking for LUA_ERRSYNTAX I guess in Lua I can do similar thing by checking near <eof>” error.

So my shell looks like:

LUA_PROMPT = "Lua> "
LUA_PROMPT_MULTI = "Lua>> "

function luashell( ctx )
  print_sl ("Lua EOS Shell version 0.01\nCopyrights 2021 Varanda Labs\n\n" .. LUA_PROMPT)
  
  -- subscribe for events
  res,msg = eos.subscribe_event_by_name(ctx, "EV_SYS_TEXT_FROM_CONSOLE")
  if res == false then
    print(msg)
  end
  local f,msg, ok
  local err
  local chunk = ""
  local more = false
  while(1) do
    local ev, arg = eos.wait_event(ctx)
    if more == true then
      chunk = chunk .. arg
      f, msg = load(chunk)
    else
      f, msg = load(arg)
    end
    if f == nil then
      if string.find(msg, "<eof>") ~= nil then
        if more == false then -- if first time:
          chunk = chunk .. arg
          more = true
        end
        print_sl(LUA_PROMPT_MULTI)
      else
        print(“load error: " .. msg)
        chunk = ""
        more = false
      end
    else
      --local ok, e = xpcall( f, lua_error_handler )
      local ok, msg = pcall(f)

      if ok == false then
        print(msg)
      end
      chunk = ""
      more = false
      print_sl(LUA_PROMPT)
    end
  end
end


On Mar 12, 2021, at 8:18 AM, v <v19930312@gmail.com> wrote:

On Fri, 2021-03-12 at 07:59 -0500, Varanda wrote:
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


Vanilla Lua REPL checks if it can compile chunk as entered and expects
continuation if it can't. If such continuation is an empty string,
input buffer gets reset and error is printed.
--
v <v19930312@gmail.com>