Schemas
A schema says which keywords a DSL has and which of them go in the block of which. With a schema, the server checks a script against it and completes its keywords.
A schema is a JSON file with the name of the scripts it is for: byterails.json is the schema of every
byterails.dsl. The server looks for it in three places:
| Where | Whose schema it is |
|---|---|
.dsl/byterails.json | The project's own. |
build/dsl/byterails.json, target/dsl/byterails.json | That of a tool, written by its plugin of Gradle or Maven. |
node_modules/<package>/.dsl/byterails.json | That of an npm package the project depends on. |
A DSL of many scripts names them <name>.<kind>.dsl, and <kind>.json is the schema of all of them:
.dsl/intent.json is that of shop.intent.dsl (many scripts, one schema).
A schema usually belongs to the tool that reads the scripts, and then the tool brings it: nothing of it is in the sources of the project. Where the server looks tells how. This page is about a schema the project writes itself.
The playground has a schema and a script to try this with, in the browser.
The schema of a project
A project that writes the schema itself has a .dsl folder at its root. Its schema is for every script of that name
in that folder and the folders under it; a script in a folder with a .dsl folder of its own uses the nearest one
that has its schema.
my-project/
.dsl/
byterails.json the schema
app/
byterails.dsl checked against it
{
"root": ["byterails"],
"keywords": {
"byterails": { "block": ["kotlin", "pkg"] },
"kotlin": { "description": "Allows what compiled Kotlin uses everywhere.", "arguments": [] },
"pkg": {
"description": "A package, and what it may use.",
"arguments": ["string"],
"block": ["flat", "allow"],
"required": ["allow"],
"single": ["flat"]
},
"flat": { "arguments": [] },
"allow": { "arguments": ["string"] }
}
}
| Property | |
|---|---|
root | The keywords that may stand at the top of a script. |
keywords | Every keyword of the DSL, by name. |
keywords.<name>.block | The keywords that may stand in its block. A keyword without block takes no block. |
keywords.<name>.required | The keywords of block that have to stand in its block, at least once. |
keywords.<name>.single | The keywords of block that may stand in its block at most once. |
keywords.<name>.arguments | Of what kind each of its arguments is: string, number, boolean or any. [] when it takes none. Used when the keyword is completed; a script is not checked against it. |
keywords.<name>.description | Shown with the keyword when it is completed. |
What is checked
A script with a schema is a whitelist. Every statement, which is a call at the top of the script or in a block, has to be a keyword of the schema, in a place where the schema allows it:
byterails {
kotlin()
package("api") { } // Unknown keyword 'package'. Allowed here: kotlin, pkg
allow("org.slf4j") // 'allow' is not allowed in 'byterails'. Allowed here: kotlin, pkg
pkg("api") {
flat { } // 'flat' does not take a block
allow("org.slf4j", transitive = true)
}
pkg("web") { // 'pkg' requires 'allow' in its block
flat()
flat() // 'flat' may stand in 'pkg' only once
}
}
What a keyword takes between its parentheses is free: any number of arguments, of any kind, named or not. A keyword means the same wherever it stands. How often it may be used is free, unless the keyword of the block says otherwise:
| The keyword stands in the block | |
|---|---|
in required | at least once |
in single | at most once |
| in both | exactly once |
Only what stands directly in the block counts, not what is in a block inside it. A keyword that requires one and is written without a block misses it too.
Completion
Completion offers the keywords that fit where the cursor is: those of root at the top of the script, those of
block in a block. It writes the keyword with what belongs to it, and puts the cursor where you go on typing:
| The keyword | arguments | What is written |
|---|---|---|
| Takes a string | ["string"] | allow(""), with the cursor between the quotes |
| Takes something else | ["number"], or no arguments | replicas(), with the cursor between the parentheses |
| Takes nothing | [] | flat() |
| Has a block | no arguments, or [] | byterails {, the cursor on a line of its own, and } |
| Has arguments and a block | ["string"] | pkg("") {, a line, and }: Tab takes the cursor from the quotes to the block |
With more arguments, as in ["string", "boolean"], there is a place for each, with a comma between them, and Tab
goes from one to the next; a boolean offers true and false. What the script already has is not written again:
a name that is changed before its ( stays a name.
This needs an editor that says it has snippets, as VS Code does. In any other, completion writes the name alone.
A keyword that the block requires is shown as required, and those come first in the list.
A schema that cannot be used
A schema that is not valid JSON, that names a keyword that is not in keywords or a kind of argument that there is
not, or in which a required or single names one that is not in the block of that keyword, is a warning on the
first line
of the script, which is then checked for syntax errors only. A script without a schema is checked for syntax errors
only too, without a warning.
An example
samples/.dsl/example.json is
the schema of
samples/example.dsl.