lua-users home
lua-l archive

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


On Fri, Mar 17, 2006 at 12:05:02PM +0800, mos wrote:
> Hi!
>     From 5.02 to 5.1, it can not surpport the string like [[abc[[def]]ghl]], I indeed have the requirement because I use Lua as the RPC's parser.
>     I like [[ ]], it have begin and end so can use it freely.
>     for example: I send a cmd to server like 
>     "DoCmd([[ShutDown([[the server will shut down!]])]])"; 
>     and more 
>     SendCmdRemote("topserver", [[DoCmd([[ShutDown([[the server will shut down!]])]]]]) 
>     and I feel very sad when use [=[ ]=] etc. How can I know how many '=' I should append?
> 

One more than the previous one?

  [==[DoCmd([=[ShutDown([[the server will shut down!]])]=]]==]

The reason for the change was to simplify the language. For instance,
now it is easy to write a Lua pattern to match such comments. Also, it
should be easier to write syntax highlight and other tools for Lua. I
also think it is easier to read your example with the new syntax (but
this is my personal taste, of course).

(I know; the new format is not a regular expression, too. It cannot
be. But if you assume the tool should work only up to 10 equals, then
you can define it as a regular expression.)

The next function builds the "correct" brackets for a given string:

function quote (s)
  -- find maximum length of sequences of equal signs
  local n = -1
  for w in string.gmatch(s, "]=*") do
    n = math.max(n, #w - 1)
  end

  -- produce a string with 'n' plus one equal signs
  local eq = string.rep("=", n + 1)

  -- build quoted string
  return string.format(" [%s[\n%s]%s] ", eq, s, eq)
end

(Example taken from the 2nd edition of the Lua book :)
 
-- Roberto