Skip to main content

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
statementsThe values at the top of the script. Most are calls.
commentsThe comments, each with its text and whether it is a block comment.
problemsEach with a message, a severity and a range.
validWhether there are no errors.
textThe 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.

kindWritten asProperties
'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.5value, null when it is not a number
'boolean'true, falsevalue
'null'nullvalue, always null
'name'STRICTname
'qualified'Level.STRICTreceiver, 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.