lua-users home
lua-l archive

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


In message <CABx9NuRSYUo3pX9O_vN3ou5YEwjBPS6mrWTFHP7zFtmWOuLqRQ@mail.gmail.com>
          Russell Haley <russ.haley@gmail.com> wrote:

> Late night thought. I want to implement a way to tell Lua to wrap a
> segment of text and run it as the arguments of os.execute(). Could you
> give me a hint on how I would do that with ltokenp?

Please excuse the fact that this response stands at a slightly oblique angle
to the thread. On the RISC OS platform the StrongED editor lets one drag
scripts to an icon of a text window to transform the text - the script sees
the text as the contents of the file whose pathname is in arg[1], and
stdout becomes the replacement text.

The following script (in RiscLua) separates out the code between <lua> .. </lua>
tags and assigns everything else to the variable "text" then runs the code.

 #! lua
 -- untangle
 -- separate the text and the code embedded in it
 -- and run the code between the tags <lua> and </lua>

 local input, read in io
 input (arg[1]);local s = read "*all";input ( )
 local P, C, Ct in require "lpeg"
 local concat in table
 local start, stop = P "<lua>" , P "</lua>"
 local script, stk = start*C((1-stop)^0)*stop, { }
 local push = \ (x) stk[1+#stk] = x end
 local nonscript = P (1)/push
 local codepat = Ct ((script+nonscript)^0)
 local code = codepat:match (s)
 text = concat (stk)
 if code then
   assert (load (concat (code)), "Bad code") ( )
 end -- if

-- 
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/