lua-users home
lua-l archive

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


On Sun, Jan 29, 2012 at 17:00,  <StephenSimpson@eaton.com> wrote:
> My Application
>
> I'm working with a large and very complex C++ application that has its
> own memory management.
> I'm reimplementing the Lua interpreter as a telnet daemon inside this
> application.
> Typical telnet daemons are terminal centric, ie. the daemon responds to
> data "committed" by a user hitting  <Enter>.
> I want the daemon to handle this traditional line-by-line input,
> AND big but potentially syntactically incomplete blocks from eg. a pipe
> directed to a ptty.
>
> Is it possible to drive the Lua API so that a parse can be "put on ice"
> while waiting for the arrival of new data from the comms stack.
>
> I want to call lua_load with a lua_Reader that serialises the source
> code data for the Lua parser from various chunks of memory bubbling up
> the comms stack.
> Now when I run out of data on the comms stack, I want to return NULL
> from the lua_Reader to signal the parser to continue the opportunistic
> parse,
> ie. maybe the data is complete.
>
> The trouble is that the parser throws when the data is correct but
> "incomplete".  This throw ends up unwinding the all the Lua state formed
> from the initial lua_load call.
> This is a bummer, since that means I need to (a) reparse from the
> beginning, and (b) hang on to all the memory from the comms stack!
>
> Is there some "approved" way of detecting the "incomplete source" syntax
> error AND storing the mid-parse Lua state so that I can simply resume
> once more data arrives???
>
> Is there a better way?
>
> Am I barking up the wrong tree?  (wave's his noob flag)
>
> btw. I didn't like the way lua.c dealt with detecting "incomplete data"
> since it checks for a hardcoded "<eof>" in the stack return data.
> This "<eof>" is a replication of static string data from llex.c.  I'd
> much rather look for the EOZ token on the return stack.
>
> Cheers,
>
> Steve
>


local code = ''
while true do
    code = code .. readSomeData()
    if loadstring(code) then break --parse was successful
    else print("Incomplete or invalid code")
    end
end

If you need to detect unfinished source specifically, I suppose you
could look at the error message from loadstring() for key words like
"near <eof>". (And then, you have to ask yourself if there's a risk of
someone messing things up by sending a string like [[ x "near <eof>
]]; note the missing end quote.)

-- 
Sent from my toaster.