# Syntax highlighting

How to colour MUL source, for an editor plugin, a documentation site or any
other highlighter. The lexical rules come from [syntax.md](syntax.md); this
page sorts them into token classes. The documentation site implements exactly
this table in
`site/theme/mul/js/highlight.js`, and the
test suite checks that its word lists cover every keyword of the language.

Code fences use the language name `mul` for source and `mul-ir` for the output
of `mul ir`.

A `mul` block that holds a whole module (it starts with `module`) is shown in
the pretty layout and gets a Compact/Pretty switch; the site lays it out with
`site/theme/mul/js/layout.js`, which
produces the same text as `mul fmt --pretty` (RFC-011). Copy always copies the
compact layout. Fragments are highlighted as written.

## Token classes of `mul` source

Rules are tried in this order at each position; the first match wins.

| Class | CSS class | Matches |
|---|---|---|
| comment | `hl-com` | `//` to the end of the line; there are no block comments |
| string | `hl-str` | `"` … `"` on one line, with the escapes `\n`, `\t`, `\\`, `\"` |
| keyword | `hl-kw` | `module`, `pub`, `fn`, `return`, `let`, `var`, `if`, `else`, `while` |
| effect | `hl-eff` | `pure`, `io` (keywords that mark a function's effect) |
| type | `hl-type` | `i32`, `bool`, `string` (type names; lexically identifiers) |
| literal | `hl-num` | `true`, `false` (keywords) and decimal integers: ASCII digits only, no sign, no `_`, no hex |
| function | `hl-fn` | `print`, and any identifier followed by `(` |
| operator | `hl-op` | `->`, `==`, `!=`, `<=`, `>=`, `&&`, `\|\|`, `+ - * / % = < > !` |
| plain | — | other identifiers, `( ) { } . , :` and whitespace |

An identifier is an ASCII letter or `_` followed by letters, digits or `_`; a
keyword is matched only as a whole word, so `ioCount` is a plain identifier.

## Token classes of `mul-ir`

| Class | CSS class | Matches |
|---|---|---|
| keyword | `hl-kw` | `module`, `fn`, and an instruction name before a space, such as `const.i32`, `add.i32`, `print`, `return`, `br` |
| effect | `hl-eff` | `pure`, `io` |
| type | `hl-type` | `i32`, `bool`, `string` |
| function | `hl-fn` | `@name` |
| value | `hl-var` | `%0`, `%1`, … |
| block | `hl-label` | `b0`, `b1`, … |
| string, number | `hl-str`, `hl-num` | as in source |

## Colours on the site

The site defines one colour per class for the light and the dark scheme, each
with a contrast ratio of at least 4.5:1 against the code background (WCAG 2.2,
level AA). The values are listed in the
site style guide (`docs/site-style.md`).
