npm
@flock/dsl-lang, with TypeScript types and no dependencies. It runs in Node.js and, with a bundler, in a browser.
Install
npm install @flock/dsl-lang
Read a script
services.dsl
service("orders") {
replicas(3)
routes {
post("/orders", auth = true)
}
}
import { readFileSync } from 'node:fs';
import { allCalls, argument, call, calls, parseOrThrow } from '@flock/dsl-lang';
const file = parseOrThrow(readFileSync('services.dsl', 'utf8'));
for (const service of calls(file, 'service')) {
const name = argument(service, 0).value; // "orders"
const replicas = argument(call(service, 'replicas'), 0).value; // 3
for (const route of allCalls(service, 'post')) {
const path = argument(route, 0).value; // "/orders"
const auth = argument(route, 'auth')?.value ?? false; // true
}
}
require('@flock/dsl-lang') works too.
Parsing
| Function | |
|---|---|
parse(text) | Never throws. A script with mistakes gives problems, and everything that could still be read; valid tells whether it has errors. |
parseOrThrow(text) | Throws a DslSyntaxError for a script with errors. Its problems are those of the file, and its message has each on a line: 2:11: Missing ')' to end the arguments of the call. |
A file is plain objects, so it can go through JSON.stringify:
| Property | |
|---|---|
statements | The values at the top of the script. Most are calls. |
comments | The comments, each with its text and whether it is a block comment. |
problems | Each with a message, a severity and a range. |
valid | Whether there are no errors. |
text | The text that was parsed. |
Finding things
| Function | |
|---|---|
calls(parent, name?) | The calls directly in a file, a block or the block of a call; with a name, only those of that function. |
call(parent, name) | The first of them, or undefined. |
allCalls(parent, name?) | Every call, however deep, in the order of the script. |
argument(call, nameOrIndex) | The value of an argument, by its index or by its name (auth = true); undefined when the call does not have it. |
Values
A statement or an argument is a value, and its kind tells which. Every value also has its range and its source,
how it is written in the script.
kind | Written as | Properties |
|---|---|---|
'call' | post("/orders") { } | name, arguments (each with a name or null, and a value), block or null |
'string' | "text", """text""" | value, raw |
'number' | 3, 1_000, 0xFF, 1.5 | value, null when it is not a number |
'boolean' | true, false | value |
'null' | null | value, always null |
'name' | STRICT | name |
'qualified' | Level.STRICT | receiver, selector, path ("Level.STRICT") |
'block' | { } | statements |
In TypeScript, checking the kind narrows the type:
const value = argument(route, 'auth');
if (value?.kind === 'boolean') {
const auth: boolean = value.value;
}
A range has a start and an end, each with an offset that counts from 0, and a line and a column that count
from 1.