Hey guys, I got tired of writing YAML files at work and then using them for side projects. YAML is really handy except that it's not a statically typed language. There are static checkers out there, but I figured let's make one in Nim. And there went my weekend. ;)
Presenting: Ants! There's more details in the README. But there's only two real macros currently: - and list.
The goal was to have a statically typed configuration language built on top of Nim. Another goal was to use as little macros as possible while getting close to YAML. I'll probably add a YAML output option as well -- write in ANTS and output clean YAML with a statically checked frontend and very few syntax changes.
Here's a sample:
import ants/configure, cimport_options
antExport ImporterConfig:
cimports:list: item ImportConfig:
name: "rcutils"
sources: "deps/rcutils/include"
globs: ["**/*.h"]
skipFiles:list:
"rcutils/stdatomic_helper/win32/stdatomic.h"
"rcutils/stdatomic_helper/gcc/stdatomic.h"
"rcutils/stdatomic_helper.h"
includes:list:
"deps/rcutils/include"
renameFiles:list:
Replace(pattern: peg"^'string.' .+", repl: "rstring$1")
sourceMods:list:
- CSrcMods:
fileMatch: peg"'rcutils/visibility_control.h'"
deletes:list:
LineDelete(match: peg"'RCUTILS_PUBLIC'")
On my machine it runs in ~.23 seconds and can be used via CLI or via a library helper.
It can also be embedded directly into your Nim app, but pulls in the compiler so it's kind slow to compile (in Nim terms, it's still quite fast comparatively).
to include path="$nim" in a config file so the correct compiler API is used
How so? The paths are set at runtime by the ants program from the Nim compiler's dump. By default I include all the nimble paths.
is this for ants
haha, yes I will try and find a meme.
Ah gotcha. My primary use at first is for configuring another Nim program.
Hmmm, yah no reason it couldn't run without a Nim install. I'd just need to a include a subset of stdlib for that.
Sometimes you're just curious how far you can push an idea. So I kept tweaking until I was able to make an ants file which is also syntactically correct YAML!
Behold:
#? replace(sub = "!", by = "") | replace(sub = "#.. ", by = "")
#.. import ants/language_v1, cimport_options
%TAG !n! tag:github.com/elcritch/ants
--- !antStart
cimports: !list:
- !ImportConfig:
name: "rcutils"
sources: "deps/rcutils/include"
globs: ["**/*.h"]
skipFiles: !list:
- "rcutils/stdatomic_helper/win32/stdatomic.h"
- "rcutils/stdatomic_helper/gcc/stdatomic.h"
- "rcutils/stdatomic_helper.h"
includes: !list:
- "deps/rcutils/include"
#.. !antEnd
I'm actually still on the fence if I'd actually want to use this. However, for things like Github Action files which are YAML based it would be nice to have a statically checked YAML. Luckily Github Actions ignore YAML tags so it appears to work.