[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Custom-environment DSL vs. require()
- From: Alexander Gladysh <agladysh@...>
- Date: Tue, 14 Dec 2010 05:06:46 +0300
On Mon, Dec 13, 2010 at 14:06, Jerome Vuarand <jerome.vuarand@gmail.com> wrote:
> 2010/12/12 Alexander Gladysh <agladysh@gmail.com>:
>> I have some code, written in a Lua-based DSL, a lot of constructs like this:
...
>> 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).
>> Now I want to move this code to the Lua module — to make it reusable
>> via require().
> If you don't mind changing the extension for your DSL files (or have
> any other way to distinguish them from regular Lua modules), a very
> elegant solution (imho) would be to add a searcher function for your
> files. A searcher is a function called by require, put in the
> (misnamed) package.loaders table [1], that will look for for files
> matching some path (the standard package.path or preferably some
> custom package.dslpath), and try to load them using loadfile and
> setfenv.
Interesting. I'm trying to figure out if putting something into
package.loaders is a "dialect-inducing" change... Well, if the
extension is unique, I guess it is not.
> With that solution you can simply call require "foo" in your main
> script, and that will load your files which contains only your bare
> DSL code, the necessary boilerplate (the setfenv call and post-call
> data extraction) being added by the searcher on the fly.
Nice, thanks. I should try it.
> I've
> described extensively the search protocol in the Lua Programming Gems
> book (last gem), and wrote some examples on the wiki [2][3][4], but I
> can re-explain it here if you want.
I have the book, thank you. (Haven't read that particular gem yet though.)
Is there some nice module around to help the loader creation? Or
should I just copy the code and hack it?
Alexander.