Skip to content(if available)orjump to list(if available)

Writing a DSL in Lua (2015)

Writing a DSL in Lua (2015)

10 comments

·February 21, 2025

neilv

This is a nice example of one way to implement DSLs in a language that doesn't support them well.

IIUC, they're using Lua's language's function application syntax, combined with the ability to introduce a different namespace environment for function lookup, and then they're evaluating the tree of function calls at run time.

In languages that support doing this at compile time, through compile-time evaluation or compile-time syntax transformation, you can make run time faster. For example of syntax transformation, https://www.neilvandyke.org/racket/html-template/ shows an example of expanded syntax, where it's rendered some of the angle brackets and coalesced that with (escaped) HTML CDATA.

If you wanted to extend that, to support chunks of traditional HTML angle-bracket syntax inline with code (without using string literals), like in React, you could define a Racket reader to parse those chunks and transform them to parentheses syntax above.

wahern

> This is a nice example of one way to implement DSLs in a language that doesn't support them well.

It's not a coincidence that Lua supports such syntax for such use cases.

roxolotl

Lua is a much cooler and more useful language than I think it, publicly, gets credit for. I know it’s widely used in many industries but so many times I’ve read an article like this and thought that it could be even more widely useful.

I’m currently writing a just for me static site generator in Fennel and it’s sad to see just how many powerful packages are abandoned. A lot of it is because they are mostly complete and part of why I like it is lack of churn but a last update on 18 months ago is very different from 10 years.

I want to host knitting patterns with my site generator and techniques like this make it super easy to build a little pattern dsl. It’s stuff like that which makes me wish it was more widespread.

epcoa

> Lua is a much cooler and more useful language than I think it, publicly, gets credit for.

I think it’s gets about the right amount of credit. It isn’t obscure by any means, openresty is used quite a bit in odd places, so many games have used it. PyTorch (well what became it) was originally written in it (it was just torch but basically LuaTorch)

It doesn’t get more adoption because its warts and limitations really start to show once pushed into more into a “real” language. It doesn’t have the browser monopoly. It’s also quite slow, and LuaJIT doesn’t always magically fix that.

dan353hehe

I love Lua for being able to do things like this.

I was building a bunch of html pages for an htmx frontend and a golang back end, and got really exhausted from using the builtin `html/template` library in golang. I kept trying to build reusable components, and it just didn’t work as I wanted it to.

I ended up doing this exact thing as mentioned in this blog post. https://github.com/DBarney/Glua Granted this is just for me, and for prototyping. I put it on GitHub so I wouldn’t loose it.

Writing html got so much easier this way:

    local function Component(data)
     return div{"my name is", data.name}
    end

    return function(params)
      html{
        head{title="this is my page"},
        body{
          h1{"This is my small page I wrote"},
          p{
            "some content",
            "more content"
          },
          div{"page has some dynamic values",params.count},
            Component{name="daniel"}
        }
      }
    end
Edit: formatting

arccy

you should try https://pkg.go.dev/maragu.dev/gomponents and do it all in go

moonlet

While I’m surprised to see they aren’t writing about https://www.inf.puc-rio.br/~roberto/lpeg/ it looks like they’ve covered LPEG in a previous post, and their chosen method this time around is neat anyway!

dang

Related:

Writing a DSL in Lua (2015) - https://news.ycombinator.com/item?id=22223542 - Feb 2020 (41 comments)

alberth

I've always wanted to recreate a Python like syntax for Lua, this might be a good guide for that.

stdbrouw

Around the time when CoffeeScript was popular, there was also https://moonscript.org/ for Lua. Made by the very same prolific Leaf who authored this DSL tutorial.