← API reference

//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: tokenizer maps state names to rules; root (or the first state) is where every document starts. A state named a.b that is not defined falls back to a.
  • 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 array name), @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 whose nexts 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: defaultToken for text no rule matches, ignoreCase, tokenPostfix added to every token name, comments as in Monaco’s language configuration ({ "lineComment": "#", "blockComment": ["<!--", "-->"] }, for toggling comments), and any other string, which @name in a regex stands for, or string array, for the @name guards.

Not supported, and rejected byparse: lookbehind ((?<=, (?<!), which Kotlin/wasm’s regex engine makes very slow.

Types

NameSummary
Companionobject Companion

Properties

NameSummary
tokenNamesval tokenNames: Set<String>
Every token name the grammar can produce, with its postfix: for giving each a colour.