Created
December 7, 2012 06:20
-
-
Save sam-github/4231179 to your computer and use it in GitHub Desktop.
Another DSL in lua, for Vasiliy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print"TABLE" | |
function directory(path) | |
print("directory", path) | |
return function(attrs) | |
for key,value in pairs(attrs) do | |
print("", key, value) | |
end | |
end | |
end | |
directory "/etc/" { | |
owner = "root"; | |
group = "wheel"; | |
mode = "0755"; | |
action = "create"; | |
} | |
print"WITHOUT =" | |
function directory(path) | |
print("directory", path) | |
return function(attrs) | |
for _,attr in pairs(attrs) do | |
key, value = next(attr) | |
print("", key, value) | |
end | |
end | |
end | |
function owner(value) return { owner = value } end | |
function group(value) return { group = value } end | |
function mode(value) return { mode = value } end | |
function action(value) return { action = value } end | |
directory "/etc/" { | |
owner "root"; | |
group "wheel"; | |
mode "0755"; | |
action "create"; | |
} | |
print "LESS TYPING" | |
function ConfigBlock(type, ...) | |
_G[type] = function (name) | |
print(type, name) | |
return function(attrs) | |
for _,attr in pairs(attrs) do | |
key, value = next(attr) | |
print("", key, value) | |
end | |
end | |
end | |
for _,key in ipairs{...} do | |
_G[key] = function (value) | |
return { [key] = value } | |
end | |
end | |
end | |
ConfigBlock("directory", "owner", "group", "mode", "action") | |
directory "/etc/" { | |
owner "root"; | |
group "wheel"; | |
mode "0755"; | |
action "create"; | |
} | |
-- wipe globals away | |
for _,v in ipairs{"directory", "owner", "group", "mode", "action"} do | |
_G[v] = nil | |
end | |
print "NO GLOBALS, LOAD DATA" | |
env = {} | |
function ConfigBlock(type, ...) | |
env[type] = function (name) | |
print(type, name) | |
return function(attrs) | |
for _,attr in pairs(attrs) do | |
key, value = next(attr) | |
print("", key, value) | |
end | |
end | |
end | |
for _,key in ipairs{...} do | |
env[key] = function (value) | |
return { [key] = value } | |
end | |
end | |
end | |
function ConfigLoad(loader, config) | |
local cfg = assert(loader(config)) | |
assert(setfenv(cfg, env)) | |
cfg() | |
end | |
ConfigBlock("directory", "owner", "group", "mode", "action") | |
-- ConfigLoad(loadfile, "filename") would load cfg from file, for | |
-- sake of example, this loads from a string | |
ConfigLoad(loadstring, [[ | |
directory "/etc/" { | |
owner "root"; | |
group "wheel"; | |
mode "0755"; | |
action "create"; | |
} | |
]]) | |
print "OOPS!" | |
env = {} | |
function ConfigBlock(type, ...) | |
env[type] = function (name) | |
print(type, name) | |
return function(attrs) | |
for _,attr in pairs(attrs) do | |
key, value = next(attr) | |
print("", key, value) | |
end | |
end | |
end | |
for _,key in ipairs{...} do | |
env[key] = function (value) | |
return { [key] = value } | |
end | |
end | |
end | |
function ConfigLoad(loader, config) | |
local cfg = assert(loader(config)) | |
assert(setfenv(cfg, env)) | |
cfg() | |
end | |
ConfigBlock("directory", "owner", "group", "mode", "action") | |
ConfigBlock("phone", "model", "colour") | |
-- ConfigLoad(loadfile, "filename") would load cfg from file, for | |
-- sake of example, this loads from a string | |
ConfigLoad(loadstring, [[ | |
directory "/etc/" { | |
owner "root"; | |
group "wheel"; | |
mode "0755"; | |
action "create"; | |
colour "black"; | |
} | |
phone "class" { | |
model "1.2"; | |
colour "black"; | |
mode "0755"; | |
} | |
]]) | |
print "AAH, BETTER" | |
env = setmetatable({}, { | |
__index = function (t, key) | |
return function (value) | |
return { [key] = value } | |
end | |
end | |
}) | |
function ConfigBlock(type, ...) | |
local allowed = {} | |
for _,v in ipairs{...} do | |
allowed[v] = true | |
end | |
env[type] = function (name) | |
print(type, name) | |
return function(attrs) | |
for _,attr in pairs(attrs) do | |
key, value = next(attr) | |
assert(allowed[key], type.." does not support attribute "..key) | |
print("", key, value) | |
end | |
end | |
end | |
end | |
function ConfigLoad(loader, config) | |
local cfg = assert(loader(config)) | |
assert(setfenv(cfg, env)) | |
local ok, emsg = xpcall(cfg, debug.traceback) | |
if not ok then | |
print("config syntax error!\n"..emsg) | |
end | |
end | |
ConfigBlock("directory", "owner", "group", "mode", "action") | |
ConfigBlock("phone", "model", "colour") | |
-- ConfigLoad(loadfile, "filename") would load cfg from file, for | |
-- sake of example, this loads from a string | |
ConfigLoad(loadstring, [[ | |
directory "/etc/" { | |
owner "root"; | |
group "wheel"; | |
mode "0755"; | |
action "create"; | |
} | |
phone "class" { | |
mode "0755"; | |
} | |
]]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment