[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: DSL in lua
- From: Philipp Janda <siffiejoe@...>
- Date: Fri, 07 Dec 2012 09:10:02 +0100
Am 06.12.2012 06:52, schrieb Vasiliy Tolstov:
Hello.
Hi!
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? How can i do
this using lua lpeg?
Here is a possible solution using LPeg:
#!/usr/bin/lua
local grammar = require( "grammar" )
local g = grammar.define( function(_ENV)
local _ = WS^0
local str_E = (P'"'*C( (ANY-P'"')^0 )*P'"') +
E"string literal expected"
local eof = -ANY + E"directory expected"
startrule( "dsl", _ * (V"directory")^0 * eof )
rule( "directory", W"directory" * _ * (str_E) * _ *
(P"{"+E"{ expected") * _ *
(V"owner" + V"group" + V"mode" + V"action")^0 *
(P"}"+E"owner|group|mode|action|} expected") * _ )
rule( "owner", W"owner" * _ * (str_E) * _ )
rule( "group", W"group" * _ * (str_E) * _ )
rule( "mode", W"mode" * _ * (str_E) * _ )
rule( "action", W"action" * _ * (str_E) * _ )
end )
local function test( s )
local ok, ast = pcall( grammar.parsestring, g, s )
if ok then
grammar.dumpast( ast )
else
print( ast )
end
print( ("#"):rep( 80 ) )
end
test[[
directory "/etc/" {
owner "root"
group "wheel"
mode "0755"
action "create"
}
]]
test[[
directory "/etc/" {
owner "root"
gruop "wheel"
mode "0755"
action "create"
}
]]
But it requires my grammar-module[1] (which is still work-in-progress).
If you want to use it anyway, consider it MIT-licensed.
[1]: http://lua.siffiejoe.dtdns.net/grammar/grammar.lua
Output is:
{
id = dsl, pos = 1
1 = {
id = directory, pos = 1
1 = /etc/
2 = {
id = owner, pos = 23
1 = root
}
3 = {
id = group, pos = 38
1 = wheel
}
4 = {
id = mode, pos = 54
1 = 0755
}
5 = {
id = action, pos = 68
1 = create
}
}
}
################################################################################
["directory "/etc/" {\n..."]:3: parse error, owner|group|mode|action|}
expected
gruop "wheel"
^
################################################################################
HTH,
Philipp