lua-users home
lua-l archive

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


Peter Odding wrote

>> For example:
>>
> >'foo "hello world" bar'
>>
>> How to turn the above into:
>>
>> '"foo" "hello world" "bar"'
>>
>> Thoughts?
>
> You can do this in plain Lua but it's not nice, I would prefer LPeg:

If you demand full Lua quoting inside your source, certainly, it's not nice.  If you use only
the double-quote as in your example, I prefer Lua.

~~~~
local append=table.insert

function tokenize(s)
-- Turn 'foo "hello world" bar' into {'foo','hello world','bar'}
    local t={}
    while #s>0 do
        start, quoted, stop = s:match('(.*)(%b"")(.*)')
        if not stop then break end
        for item in start:gmatch('%S+') do append(t,'"'..item..'"') end
        append(t,quoted)
        s = stop
        end
    for item in s:gmatch('%S+') do append(t,'"'..item..'"') end
    return t
    end

print (table.concat(tokenize 'foo "hello world" bar', ' ')) -- "foo" "hello world" "bar"
~~~~

Dirk