lua-users home
lua-l archive

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


On Oct 5, 2011, at 10:25 PM, Peter Odding wrote:

> You can do this in plain Lua but it's not nice, I would prefer LPeg:

Thank you very much for the example. And yes, LPeg is on my "to learn" list :)

Here is my feeble attempt in plain Lua:

local aLine = 'foo "hello world" bar'

print( 1, aLine )

aLine = aLine:gsub( '(%b"")', function( aValue ) return aValue:gsub( ' ', '\\032' ) end )
aLine = aLine:gsub( '([^ ]+)', function( aValue ) if not aValue:find( '^"' ) then return ( '"%s"' ):format( aValue ) else return aValue:gsub( '\\032', ' ' ) end end )

print( 2, aLine )

for aToken in aLine:gmatch( '(%b"")' ) do
  print( 3, aToken )
end

> 1	foo "hello world" bar
> 2	"foo" "hello world" "bar"
> 3	"foo"
> 3	"hello world"
> 3	"bar"

In other words, first, encode all the quoted space - assuming the space character is the token separator. Second, quote all the sequences between spaces, if not already quoted.

Rather clunky, but it seems to be doing the bare minimum.

> One notable advantage of LPeg is that it's dead easy to extend this example with support for escape sequences and stuff like that :-)

Yes, LPeg looks very powerful. Need to invest some time in it :))