How a load reader can pre-parse chunks?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How a load reader can pre-parse chunks?

Varanda
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()

v
Reply | Threaded
Open this post in threaded view
|

Re: How a load reader can pre-parse chunks?

v
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 <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: How a load reader can pre-parse chunks?

Varanda
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 <[hidden email]> 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 <[hidden email]>