lua-users home
lua-l archive

Custom-environment DSL vs. require()

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


Hi, list!
I have some code, written in a Lua-based DSL, a lot of constructs like this:
foo:bar "title"
{
 baz = "quo";
 alpha:beta "gamma"
 {
 "delta";
 };
}
This code is executed inside a custom global environment with
pre-defined global objects foo and alpha (one can say these objects
implement Builder pattern — things are a bit more complicated in my
case, but that doesn't matter for this question).
After the code is run, I inspect foo and alpha variables, and get the
built data from them.
Now I want to move this code to the Lua module — to make it reusable
via require().
Obviously, I can't just require() the file with DSL — the environment
would be wrong.
This means that I have to put my DSL into a function:
local dsl_chunk = function()
 return foo:bar "title"
 {
 baz = "quo";
 alpha:beta "gamma"
 {
 "delta";
 };
 }
end
return
{
 dsl_chunk = dsl_chunk;
}
This works, but is less pretty. Also it affects the code that loads
the DSL: I have to handle require-friendly DSL files differently.
Perhaps there is some trick that I miss? I don't mind to add some
boilreplate code to the "library" portions of my DSL code. But I'd
like to keep that boilerplate to a bare minimum.
Alexander.

AltStyle によって変換されたページ (->オリジナル) /