lua-users home
lua-l archive

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


> What is the best way to create custom DSL in Lua? Google says, that it
> can be possible via base lua or using lpeg.
> 
> I need something like this
> direcotry "/etc/" {
>   owner "root"
>   group "wheel"
>   mode "0755"
>   action "create"
> }
> 
> Can somebody provie minimal lua code for this sample?

I'm late in this thread but here is a simple parser in pure Lua.
There is no error handling, sorry.


DATA=[[
direcotry "/etc/" {
  owner "root"
  group "wheel"
  mode "0755"
  action "create"
}

direcotry "/home/lua/" {
  group "wheel"
  action "remove"
  mode "0755"
  owner "lua"
}

]]

local config={}
for verb,name,body in DATA:gmatch('(%w+)%s+"(.-)"%s+(%b{})') do
	if config[verb]==nil then config[verb]={} end
	local t={}
	for k,v in body:gmatch('(%w+)%s+"(.-)"') do
		t[k]=v
	end
	config[verb][name]=t
end

-- see what has been built
for K,V in pairs(config) do
	print(K,V)
	for k,v in pairs(V) do
		print(k,v)
		for k,v in pairs(v) do
			print(k,v)
		end
	end
end