//languages/com.aardarch.aardink.languages/DeclarativeGrammar
DeclarativeGrammar
class DeclarativeGrammar
A syntax grammar written as data, for DeclarativeTokenizer: a subset of Monaco’s Monarch, so that a grammar can come from JavaScript (the web’s registerLanguage) or a file, with no code.
{
"defaultToken": "",
"keywords": ["if", "else", "while"],
"digits": "[0-9]+",
"tokenizer": {
"root": [
["[a-z_]\\w*", { "cases": { "@keywords": "keyword", "@default": "identifier" } }],
["@digits", "number"],
["\"", "string", "@string"],
["(\\w+)(=)", ["attribute.name", "delimiter"]],
{ "include": "@whitespace" }
],
"string": [
["[^\"]+", "string"],
["\"", "string", "@pop"]
],
"whitespace": [["\\s+", ""], ["#.*", "comment"]]
}
}
- States:
tokenizermaps state names to rules;root(or the first state) is where every document starts. A state nameda.bthat is not defined falls back toa. - Rules:
[regex, action]or[regex, action, next], or{ "regex", "action", "next" }, or{ "include": "@state" }for another state’s rules. The first rule whose regex matches at the current position wins. Regexes see one line at a time. - Actions: a token name;
{ "token", "next" };{ "cases": { guard: action } }, where a guard is@name(the match is in the arrayname),@default,@eos(the match ends the line), or a regex the whole match must match; or an array with an action per capture group, any of those three, whose cases test the group’s own text and whosenexts apply in order. The token@rematch(not in a group) consumes nothing and matches again in the next state. - Next: a state name to enter (pushed on the state stack),
@pop,@push(the current state again) or@popall. - Top level:
defaultTokenfor text no rule matches,ignoreCase,tokenPostfixadded to every token name,commentsas in Monaco’s language configuration ({ "lineComment": "#", "blockComment": ["<!--", "-->"] }, for toggling comments), and any other string, which@namein a regex stands for, or string array, for the@nameguards.
Not supported, and rejected byparse: lookbehind ((?<=, (?<!), which Kotlin/wasm’s regex engine makes very slow.
Types
| Name | Summary |
|---|---|
| Companion | object Companion |
Properties
| Name | Summary |
|---|---|
| tokenNames | val tokenNames: Set<String> Every token name the grammar can produce, with its postfix: for giving each a colour. |