Java
community.flock.dsl:dsl-lang on Maven Central, for Java 17 and later. It needs the Kotlin standard library and
nothing else.
Install
With Maven:
<dependency>
<groupId>community.flock.dsl</groupId>
<artifactId>dsl-lang</artifactId>
<version>0.0.10</version>
</dependency>
With Gradle:
implementation("community.flock.dsl:dsl-lang:0.0.10")
Read a script
service("orders") {
replicas(3)
routes {
post("/orders", auth = true)
}
}
import community.flock.dsl.language.Dsl;
import community.flock.dsl.language.DslCall;
import community.flock.dsl.language.DslFile;
import community.flock.dsl.language.DslValue;
DslFile file = Dsl.parse(Path.of("services.dsl")).requireValid();
for (DslCall service : file.calls("service")) {
String name = service.argument(0).asString(); // "orders"
int replicas = service.call("replicas").argument(0).asInt(); // 3
for (DslCall route : service.allCalls("post")) {
String path = route.argument(0).asString(); // "/orders"
DslValue auth = route.argument("auth"); // null when the call does not have it
boolean secured = auth != null && auth.asBoolean(); // true
}
}
Parsing
Dsl.parse takes a Path or the text. It never fails on what is in a script: a script with mistakes gives
getProblems(), and everything that could still be read.
| Method | |
|---|---|
isValid() | Whether the script has no errors. |
requireValid() | Gives the file, or throws a DslSyntaxException with the problems. |
getProblems() | The problems of the script. |
getComments() | The comments of the file. |
Finding things
| Method | |
|---|---|
calls() | The calls directly in a file, a block or the block of a call. |
calls(name) | Those of one function. |
call(name) | The first of them. |
allCalls() | Every call, however deep. |
argument(index), argument(name) | The value of an argument, or null when the call does not have it. |
Values
A DslValue is one of:
| Class | Written as |
|---|---|
DslCall | post("/orders") { } |
DslString | "text", """text""" |
DslNumber | 3, 1_000, 0xFF, 1.5 |
DslBoolean | true, false |
DslNull | null |
DslName | STRICT |
DslQualified | Level.STRICT |
DslBlock | { } |
asString(), asInt(), asLong(), asDouble(), asBoolean() and asCall() give null for a value that is
something else.
Every value has its getRange(), with lines and columns that count from 1, and its getSource(), how it is
written in the script.
Kotlin
Checking against a schema
A schema says which keywords a DSL has and which of them go in the block of which. The language server checks
a script against it in an editor; Dsl.parse does the same when it gets one, so an application refuses what the
editor marks, in the same words.
import community.flock.dsl.language.schema.Keyword;
import community.flock.dsl.language.schema.Schema;
Schema schema = new Schema(List.of("service"), List.of(
new Keyword("service", List.of("replicas", "routes"), "A service."), // name, block, description
new Keyword("replicas", null, null, List.of("number")), // and arguments, for completion
new Keyword("routes", List.of("post"), null, null, List.of("post")), // and what the block requires
new Keyword("post")));
DslFile file = Dsl.parse(Path.of("services.dsl"), schema);
for (DslProblem problem : file.getProblems()) {
System.err.println(problem); // 2:5: Unknown keyword 'replica'. Allowed here: replicas, routes
}
Dsl.parse(text, schema),Dsl.parse(path, schema)andDsl.parse(path, charset, schema)add to the problems every statement that is not a keyword of the schema, that stands where the schema does not allow it or that misses a keyword the schema requires in its block. A sixth argument ofKeywordnames the keywords its block takes at most once.isValid()andrequireValid()go by all of them.getCode()of a problem tells what kind it is:DslProblem.SYNTAX,UNKNOWN_KEYWORD,MISPLACED_KEYWORD,UNEXPECTED_BLOCK,UNEXPECTED_STATEMENT,MISSING_KEYWORDorREPEATED_KEYWORD.isSyntax()is true for the first, which is the only one without a schema.- What a keyword takes between its parentheses is free, with a schema too: that is for the application to check.
- The library does not read the JSON file of a schema: it has no dependencies, so it has no JSON parser. An
application makes the
Schemain code, and can write the JSON for the editor from the same keywords.
From Kotlin it is the same library; parseDsl(text) and parseDsl(text, schema) are there next to Dsl.parse.