lua-users home
lua-l archive

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


2010/9/3 Silas Silva <silasdb@gmail.com>:
> I've been done some research for a project of mine, regarding
> "embeddable" languages to make it extensible, and figured out that Lua
> would be the best solution for this particular situation.
>
> My question is:  Lua is a language made to be embedded (like Tcl and
> Guile -- not sure about the last one), not like Python or Perl, which
> make it perfect to be used as a scripting language in a C program.  But,
> over the time, lots of libraries were built on the top of Lua, that
> makes it a great general-purpose language.  Should I make a C + Lua
> program or only Lua?  Let me explain my problem:
>
> Over the years, I've done some GUI applications in several ways: using
> Tk, Qt, HTML forms, ncurses etc.  But never found a uniform way to
> specify a GUI and generate the resulting code for a language/toolkit
> combination.  I didn't find anything like that on the internet (please,
> tell me if I'm wrong) so I'll invent the wheel.
>
> This program will read a file (whose content will be a description of a
> GUI in a DSL (domain-specific language) that I intend to specify soon)
> and generate the code.  So, basically, some would call the program in
> that way:
>
>    program --generate qt-xml < file.dsl > output.ui
>
>    program --generate html < file.dsl > output.html
>
> The DSL should be simple (similar to Tcl/Tk code):
>
>    button foo -text "Foo" -x 10 -y 20
>    label ...
>
> So, I though about using Lex & Yacc in C to parse the DSL, put
> everything in well defined data structures and generate the output.  The
> generation rules would be in Lua scripts, something like (for HTML):
>
>    -- Oversimplified example
>    function button()
>        print("<input type=submit>")
>    end
>
> So, my questions are:
>
> 1. Is there any replacement for lex/flex & yacc/bison for Lua?  How do
>   you design DSLs in Lua?  I thought about using Lex & Yacc + C it is a
>   long standing wish of mine to learn parsers, BNF with Lex & Yacc...
>
> 2. I was thinking that it would be interesting to make the DSL
>   Turing-complete, to make it easily programmable.  Use Lua itself as
>   the DSL would solve this problem, but what about if I need different
>   syntax not present in Lua, like in "button foo ..."?

The Lua syntax is very flexible. The above example can be written :

button { 'foo', text = "Foo", x = 10, y = 20 }

With a custom metatable on _G (__index = function(_, k) return k end),
you could even have :

button { foo, text = Foo, x = 10, y = 20 }

And for something a little more advanced, you can use token filters,
which stays simple enough to design, reuses the Lua parser as much as
possible, and still directly gives you Lua objects.