summaryrefslogtreecommitdiff
path: root/config.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2025-02-22 00:20:52 +0000
committerubq323 <ubq323@ubq323.website>2025-02-22 00:20:52 +0000
commit989b6349b0c7e5a0d7ec06d2ca24d629c618fe12 (patch)
treec424e6eac3ee626db4b4993def6eda4b9292eb66 /config.lua
parentbf776624fb59d147b82d2a6a13c36292844a47b7 (diff)
many thingsHEADtrunk
Diffstat (limited to 'config.lua')
-rw-r--r--config.lua45
1 files changed, 45 insertions, 0 deletions
diff --git a/config.lua b/config.lua
new file mode 100644
index 0000000..a8838c8
--- /dev/null
+++ b/config.lua
@@ -0,0 +1,45 @@
+local function partial(f,x) return function(...) return f(x,...) end end
+
+local function kv_syntax(block,line)
+ local k,v = line:match"^([a-z0-9_-]+)%s*=%s*(.*)$"
+ assert(k,"syntax error in kv line: "..line)
+ k = k:gsub("-","_")
+ block[k]=v
+end
+
+local function tuple_syntax(pattern) return function(block,line)
+ local matches = {line:match(pattern)}
+ assert(matches[1], "syntax error in tuple line: "..line)
+ table.insert(block, matches)
+end end
+
+local schema = {
+ pylon = kv_syntax,
+ bus = tuple_syntax"^(%S+)%s+(%S+)$",
+}
+
+local function parse_file(file)
+ local blocks = {}
+ for k in pairs(schema) do blocks[k] = {} end
+ local line_handler = function() error("line outside of block") end
+
+ for line in file:lines() do
+ line = line:gsub("#.*$",""):gsub("^%s*",""):gsub("%s*$","")
+ if line == '' then goto next end
+
+ local block_type, block_name = line:match"%[%s*(%S+)%s+(%S+)%s*%]"
+ if block_type then
+ local blocks_of_this_type = assert(blocks[block_type],"no such block type "..block_type)
+ local block = {}
+ blocks_of_this_type[block_name] = block
+ line_handler = partial(schema[block_type], block)
+ else
+ line_handler(line)
+ end
+ ::next::
+ end
+
+ return blocks
+end
+
+return {parse=parse_file}