line parsing in lua

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

line parsing in lua

Max Ischenko
Hi.
I'm trying to implement line parsing in Lua.
Line syntax is similar to shell one, where double quotes protect words in
them to be splitted.

<word> [<word> <"word with spaces"> ...]

I want to parse the line into Lua table where fields are line words.
For example:

cmd first-arg "second arg" 3

should be converted into table t
t.1 = 'cmd' t.2 = 'first-arg', t.3='second arg', t.4='3'

I found two variants but both are sucks:
gsub(args, '"?([^"]+)"?', function (w) tinsert(%t, w) end)
gsub(args, '(%w+)', function (w) tinsert(%t, w) end)

Has anyone better one?


PS: Why Lua uses it's own pattern syntax instead of popular and standard
POSIX REs?

-- 
BYTE editors are people who separate the wheat from the chaff, and then
carefully print the chaff.

Reply | Threaded
Open this post in threaded view
|

Re: line parsing in lua

Tomas-14
> I'm trying to implement line parsing in Lua.
> Line syntax is similar to shell one, where double quotes protect words in
> them to be splitted.
> 
> <word> [<word> <"word with spaces"> ...]
> 
> I want to parse the line into Lua table where fields are line words.
> For example:
> 
> cmd first-arg "second arg" 3
> 
> should be converted into table t
> t.1 = 'cmd' t.2 = 'first-arg', t.3='second arg', t.4='3'
> 
	You can substitute spaces between a pair of `"' by another character
then split the string using spaces as separators and reconstructing the
original spaces:

function parse (s)
    local t = {}
    s = gsub(s,'"([^"]*)"',function(str) return gsub(str,' ','\1') end)
    s = gsub(s,'(%S+)', function (str)
        local s=gsub(str,'\1',' ')
        tinsert(%t,s)
    end)
    return t
end

	Tomas


Reply | Threaded
Open this post in threaded view
|

Re: line parsing in lua

Luiz Henrique de Figueiredo
In reply to this post by Max Ischenko
>> I want to parse the line into Lua table where fields are line words.

>	You can substitute spaces between a pair of `"' by another character
>then split the string using spaces as separators and reconstructing the
>original spaces:

This is a nice solution.
To avoid the remote case when the line contains "\1", you can use "\n" instead.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: line parsing in lua

Max Ischenko
In reply to this post by Tomas-14
	Hi there, Tomas!
	On Tuesday, 12 of December you wrote:

> > I'm trying to implement line parsing in Lua.
> > Line syntax is similar to shell one, where double quotes protect words in
> > them to be splitted.
> > 
> > <word> [<word> <"word with spaces"> ...]
> > 
> > I want to parse the line into Lua table where fields are line words.
> > For example:
> > 
> > cmd first-arg "second arg" 3
> > 
> > should be converted into table t
> > t.1 = 'cmd' t.2 = 'first-arg', t.3='second arg', t.4='3'
> > 
> 	You can substitute spaces between a pair of `"' by another character
> then split the string using spaces as separators and reconstructing the
> original spaces:
> 
> function parse (s)
>     local t = {}
>     s = gsub(s,'"([^"]*)"',function(str) return gsub(str,' ','\1') end)
>     s = gsub(s,'(%S+)', function (str)
>         local s=gsub(str,'\1',' ')
>         tinsert(%t,s)
>     end)
>     return t
> end
> 

Tnx, that was exactly what I wanted.

BTW, \1 is octal \001?


-- 
Best regards, Maxim F. Ischenko.

Reply | Threaded
Open this post in threaded view
|

Re: line parsing in lua

Tomas-14
> BTW, \1 is octal \001?
	Yes.  The best choice is a character that will not be present in
the string (like '\n', as Luiz noted).
	Another thing you must need is a escape for `"' inside a quoted
string.  For example:

cmd first-arg "second arg" 3 "a\"b" de "fg"

	Must be splitted in:

1       cmd
2       first-arg
3       second arg
4       3
5       a"b
6       de
7       fg

	To achieve that, just add the following line as the first substitution:

s = gsub(s,'\\"','\2')

	And change the correction of the `\1', to correct these too:

local s=gsub(gsub(str,'\1',' '), '\2', '"')

	Got it?

		Tomas


Reply | Threaded
Open this post in threaded view
|

Re: line parsing in lua

Max Ischenko
	Hi there, Tomas!
	On Wednesday, 13 of December you wrote:

> > BTW, \1 is octal \001?
> 	Yes.  The best choice is a character that will not be present in
> the string (like '\n', as Luiz noted).
> 	Another thing you must need is a escape for `"' inside a quoted
> string.  For example:

[ example skipped ]
> 
> 	Got it?

Yup. You've anticipated my new posting ;-))

-- 
If a program can't rewrite its own code, what good is it?

Reply | Threaded
Open this post in threaded view
|

Re: line parsing in lua

Luiz Henrique de Figueiredo
In reply to this post by Max Ischenko
> BTW, \1 is octal \001?

No, it's *decimal* \001.
--lhf