dslutils

A macro collection for creating DSL in nim

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-22 05:28

Installation

nimble install dslutils
choosenim install dslutils
git clone https://github.com/codehz/dslutils

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
dslutils - - - - - - -

Source

Repository https://github.com/codehz/dslutils
Homepage https://github.com/codehz/dslutils
Registry Source nimble_official

README

A macro collection for creating DSL in nim

Current modules: astpat, identsubs, metadata

ASTPAT: Do pattern match on nim ast

WARNING: require experimental option: caseStmtMacros, you can enable it by pragma {.experimental: "caseStmtMacros".}

usage:

case line:
of (`key` = `value*`):
  # key match only "simple" node like ident, sym, string, int
  # value* match all node
  echo repr key, " = ", repr value
of (a.`key` = func(`value*`)):
  echo "a.", repr key, " = func(", repr value, ")"

IDENTSUBS: Replace left most identifier in expression

usage:

type MyObj = object
  vint: int
  varr: array[2, int]

proc fun(self: MyObj) = echo repr self

var a = MyObj(vint: 1)
var b = MyObj(vint: 2, varr: [5, 6])

macro withObj(body: untyped{nkStmtList}) =
  result = newStmtList()
  for item in body.items:
    result.add: item.identsubs: {
      "vint": newDotExpr(bindSym "a", ident "vint"),
      "varr": newDotExpr(bindSym "a", ident "varr"),
      "primary": bindSym "a",
      "secondary": bindSym "b",
    }

withObj:
  vint = 4
  varr[0] = 1
  primary.fun()
  fun(secondary)

result:

a.vint = 4
a.varr[0] = 1
a.fun()
fun(b)

METADATA: Attach metadata to any value

Usage:

let value = 5 ~~ { meta: 1 }

macro dumpMetadata(target: typed) =
  let meta = target.metadata
  for kv in meta:
    echo treerepr kv

dumpMetadata value

got:

ExprColonExpr
  Ident "meta"
  IntLit 1