Lua DSL for configuration

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua DSL for configuration

Joshua Jensen
There is a configuration file I deal with that has a syntax like so:

VARIABLE "MyVar" = "Hello"

I thought that, perhaps, Lua could read it directly.  Having a function
called VARIABLE that works with Lua's simplified single parameter string
syntax will solve the VARIABLE "MyVar" portion:

function VARIABLE(name)
     --
end

At this point, I am uncertain what I would have VARIABLE() return that
would allow me to pick up the right side (= "Hello") of the
configuration line.

Any thoughts?  Is it possible?

Thanks.

Josh
Reply | Threaded
Open this post in threaded view
|

Re: Lua DSL for configuration

Rena
I can't see how that would work. You're assigning "Hello" to either a
string or a function call; neither are valid.

On Tue, Apr 27, 2010 at 20:55, Joshua Jensen <[hidden email]> wrote:

> There is a configuration file I deal with that has a syntax like so:
>
> VARIABLE "MyVar" = "Hello"
>
> I thought that, perhaps, Lua could read it directly.  Having a function
> called VARIABLE that works with Lua's simplified single parameter string
> syntax will solve the VARIABLE "MyVar" portion:
>
> function VARIABLE(name)
>    --
> end
>
> At this point, I am uncertain what I would have VARIABLE() return that would
> allow me to pick up the right side (= "Hello") of the configuration line.
>
> Any thoughts?  Is it possible?
>
> Thanks.
>
> Josh
>



--
Sent from my toaster.
Reply | Threaded
Open this post in threaded view
|

Re: Lua DSL for configuration

Mark Feldman
I don't think that's the problem, MyVar is simply an identifier that can
be used to look up whatever. The problem is it's failing the parsing
stage, so it won't work regardless of what VARIABLE returns. I don't
think you can do it without stripping the = token:

   function VARIABLE(handler)
       return function(value)
           _G[handler] = value
       end
   end

VARIABLE "MyVar" "Hello"
print(MyVar)        --> Hello

Mark Feldman

HyperHacker wrote:

> I can't see how that would work. You're assigning "Hello" to either a
> string or a function call; neither are valid.
>
> On Tue, Apr 27, 2010 at 20:55, Joshua Jensen <[hidden email]> wrote:
>  
>> There is a configuration file I deal with that has a syntax like so:
>>
>> VARIABLE "MyVar" = "Hello"
>>
>> I thought that, perhaps, Lua could read it directly.  Having a function
>> called VARIABLE that works with Lua's simplified single parameter string
>> syntax will solve the VARIABLE "MyVar" portion:
>>
>> function VARIABLE(name)
>>    --
>> end
>>
>> At this point, I am uncertain what I would have VARIABLE() return that would
>> allow me to pick up the right side (= "Hello") of the configuration line.
>>
>> Any thoughts?  Is it possible?
>>
>> Thanks.
>>
>> Josh
>>
>>    
>
>
>
>  


This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect.

Reply | Threaded
Open this post in threaded view
|

Re: Lua DSL for configuration

Luiz Henrique de Figueiredo
In reply to this post by Joshua Jensen
> There is a configuration file I deal with that has a syntax like so:
>
> VARIABLE "MyVar" = "Hello"
>
> I thought that, perhaps, Lua could read it directly.

Lua cannot read this syntax directly. If you want to keep it, try my lcl:
        http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lcl

With lcl, you can even use other variations, such as
        VARIABLE MyVar "Hello World"
Reply | Threaded
Open this post in threaded view
|

Re: Lua DSL for configuration

Vaughan McAlley
Depending on how regular the declarations are, pattern matching seems
easier than messing with syntax:

local file = [[
VARIABLE "MyVar" = "Hello"
VARIABLE "OtherVar"="Goodbye"
]]

for name, value in string.gmatch(file,
"VARIABLE%s*\"([%w_]+)\"%s*=%s*\"([^\"]*)\"") do
    _G[name] = value
end

print(MyVar, OtherVar)

--> Hello Goodbye

Vaughan
Reply | Threaded
Open this post in threaded view
|

Re: Lua DSL for configuration

Joshua Jensen
In reply to this post by Luiz Henrique de Figueiredo
----- Original Message -----
From: Luiz Henrique de Figueiredo
Date: 4/28/2010 5:35 AM
>> There is a configuration file I deal with that has a syntax like so:
>>
>> VARIABLE "MyVar" = "Hello"
>>
>> I thought that, perhaps, Lua could read it directly.
>>      
> Lua cannot read this syntax directly. If you want to keep it, try my lcl:
> http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lcl
>    
Thanks everyone.  I thought that Lua could not handle the assignment
portion, but I thought I would ask.  I already have an LPEG reader for
the file format.  It just would have been neat to have read it directly.

-Josh