🎓 A1Learner
🔍
0 of 49 lessons completed

Lua Config Language DSLs

A configuration language DSL is a small language written as tables that describe settings. Sections group related settings, and an interpreter turns them into a summary.

A section groups server settings

The server section holds a host and a port. The printed line joins them into one address.

local config = { server = { host = "example.com", port = 8080 } }
print(config.server.host .. ":" .. config.server.port)

A list names the enabled features

The features list holds three names in order. The printed line joins them in that order.

local config = { features = { "search", "chat", "maps" } }
print("features: " .. table.concat(config.features, ", "))

An interpreter turns settings into lines

The interpreter reads the host, the port, and the cache flag. Each line reports one setting.

local config = {
  server = { host = "shop.example.com", port = 443 },
  cache = { enabled = true },
}
local lines = {}
lines[1] = "host: " .. config.server.host
lines[2] = "port: " .. config.server.port
if config.cache.enabled then
  lines[3] = "cache: on"
else
  lines[3] = "cache: off"
end
print(table.concat(lines, "\n"))

Example

local config = {
  server = { host = "shop.example.com", port = 443 },
  features = { "search", "checkout" },
  limits = { users = 100, items = 50 },
}
local lines = {}
lines[1] = "server: " .. config.server.host .. ":" .. config.server.port
lines[2] = "features: " .. table.concat(config.features, ", ")
lines[3] = "limits: " .. config.limits.users .. " users, " .. config.limits.items .. " items"
print(table.concat(lines, "\n"))

Key points

  • Configuration: settings that describe how a program should behave, without changing its code.
  • Section: a nested table that groups settings for one part of a program.
  • Setting: one key and value pair inside a section.
  • Feature list: a list of named features that are turned on.
  • Summary: lines the interpreter prints from the settings, in a fixed order.

In this example, the config table holds a server section, a features list, and a limits section. The interpreter builds one line from each part in a fixed order. The printed lines show the address, the features, and the limits together.

Note

a configuration description is data, so the same interpreter can summarize many different settings.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A configuration DSL describes a server with host shop.example.com and port 443. What should the interpreter print for the server line?