# The diagram language

> Flow, sequence and class diagrams from text: lines, names, operators, styles and layout

The language of `diagram` code blocks, exactly. For a gentler introduction with examples, see [Diagrams](../using-kribble/diagrams.md).

Version 1.

A diagram block ([document.md](../specification/the-document-format.md)) is a drawing written as text: what connects to what, one fact per line. A layout engine turns the text into a scene ([drawing.md](../specification/drawings.md)); the text is the source of truth and holds no coordinates.

This page is the language. The user guide (`docs/Using Kribble/Diagrams.md`) explains it with examples; the two say the same thing. The reference implementations are `@kribble/diagram` (TypeScript: parser, layout, editing) and `backend/internal/diagram` (Go: parser and layout), line for line the same and checked against the same fixtures.

## Kinds

A diagram has a kind, the drawing node's `diagram` attribute:

| Kind | Markdown fence | For |
| --- | --- | --- |
| `flow` | ` ```diagram ` | flowcharts, architecture, activity and use case diagrams |
| `sequence` | ` ```diagram sequence ` | sequence diagrams |
| `class` | ` ```diagram class ` | class diagrams |

## Lines

The text is read line by line. Lines are separated by `\n` (a `\r` before it is ignored). Columns count UTF-16 code units from 0, lines from 1.

- **Comments.** A `#` that is at the start of the line or after a space or tab, and is followed by a space, a tab or the end of the line, starts a comment that runs to the end of the line, unless it's inside double quotes. A `#` followed by anything else isn't a comment (it's a colour, or text).
- **Blank lines** (nothing but spaces, tabs and a comment) are ignored.
- **Indentation** is the line's leading spaces and tabs (a tab counts as one column). A line that opens a block (a group, a lane, a class, a package, a fragment) holds the lines after it that are indented more than it, up to the first that isn't. Two spaces per level is the convention; any increase works.
- Spaces and tabs around names, operators and labels don't count.

## Names

A **name** is the text of a box, a participant or a class: any run of characters up to an operator, a comma, a colon, a `[` or the end of the statement, with runs of spaces and tabs inside it collapsed to one space. Names are case-sensitive (`api` and `API` are two boxes).

A name in double quotes can contain anything but a double quote: `"Sales, EMEA"`. The quotes aren't part of the name.

## Settings

A line `direction: <value>` or `look: <value>` is a setting, wherever it is (usually at the top):

| Setting | Values | Default |
| --- | --- | --- |
| `direction` | `down`, `right`, `up`, `left`: which way arrows flow | `down` |
| `look` | `hand-drawn`, `clean` | `hand-drawn` |

An unknown value is a problem on its line, and the setting keeps its default.

## Statements (flow and class)

| Statement |  |
| --- | --- |
| `A` | A box (a class, in class diagrams) on its own. |
| `A [style]` | A box with a style (see Styles). |
| `A -> B` | A line from A to B, drawn as the operator says (see Operators). |
| `A -> B -> C` | A chain: one line per step. |
| `A -> B, C` / `A, B -> C` | Fan-out and fan-in: a line to (from) each. |
| `A -> B: label` | A label on the last step's lines (every line of a fan-out). |
| `A "1" *-- "1..*" B` | End labels: quoted text next to an operator labels that end of the line. Either may be left out. |
| `Name:` / `Name [style]:` + indented lines | A group: the indented lines' boxes belong to it. Groups nest. |
| `Name: A, B` | A short group with those boxes in it. |
| `lane Name:` + indented lines, `lane Name: A, B` | A swimlane (flow only; can't be combined with groups). All lanes of a diagram form one pool, side by side; the flow goes down. |
| `package Name:` + indented lines | A package around classes (class only). |

A box belongs to the group (or lane) where it starts a line or stands on a line of its own; a box that only ever ends lines belongs to the first group that mentions it. A box in no lane joins the lane of the first box it connects to.

**Class diagrams.** A class name on a line of its own followed by indented lines is a class, and the indented lines are its members, one per line, kept as written: those with a `(` go in the operations compartment, the others in the attributes compartment. After the name, `(abstract)` sets it in italics, `(interface)` adds «interface», `(enum)` adds «enumeration». A class written again later adds to its members.

**Activity diagrams** (flow) have special names: `start` and `end` (lowercase) are the start and end nodes; a name ending in `?` is a decision diamond; `fork` and `join`, optionally numbered (`fork 2`), are bars. Labels on lines out of a decision are its guards (`[yes]`).

## Operators

Operators are matched longest first. Word operators are whole words between names.

| Operator | Draws (start head, end head, line) |
| --- | --- |
| `->` | none, `arrow`, solid (sequence: `triangle`) |
| `->>` | none, `arrow`, solid |
| `-->`, `-->>` | none, `arrow`, dashed |
| `--` | none, none, solid |
| `..` | none, none, dashed |
| `<->` | `arrow`, `arrow`, solid |
| `<-` | `arrow`, none, solid |
| `<--` | `arrow`, none, dashed |
| `=>` | none, `arrow`, solid, thick (width 4) |
| `<=>` | `arrow`, `arrow`, solid, thick |
| `--\|>`, `extends` | none, `triangle_outline`, solid; the parent (B) is placed above |
| `<\|--` | `triangle_outline`, none, solid |
| `..\|>`, `implements` | none, `triangle_outline`, dashed; the parent above |
| `<\|..` | `triangle_outline`, none, dashed |
| `*--` / `--*` | `diamond` at the `*` end, solid |
| `o--` / `--o` | `diamond_outline` at the `o` end, solid |
| `..>` / `<..` | `arrow` at the `>`/`<` end, dashed |
| `include`, `includes` | none, `arrow`, dashed, labelled «include» (doesn't order the layout) |
| `extend` | none, `arrow`, dashed, labelled «extend» (doesn't order the layout) |
| `create` | none, `arrow`, dashed (sequence: creates the participant) |

Head names are the drawing's arrowheads ([drawing.md](../specification/drawings.md)).

## Styles

Square brackets after a name hold its style, words separated by commas or spaces; a colour may also follow the name as `#name` or `#rrggbb`.

- **Shapes**: `box` (default), `square`, `circle`, `ellipse` (= circle), `diamond`, `cylinder`, `queue`, `cloud`, `document`, `person`, `actor` (= person), `note`, `component`, `package`, `text`, `usecase`, `start`, `end`, `flow end`, `action`, `decision`, `fork`, `join`, `object`, `boundary` (groups: a use case system boundary), `group`.
- **Colours**: `red`, `orange`, `yellow`, `green`, `teal`, `blue`, `purple`, `pink`, `gray`, or a hex colour. They colour the border and the text and give a light fill; the palette is in `parse.ts` (`COLORS`).
- **Lines**: `dashed`, `dotted`, `thick`.

An unknown word is a problem on its line, naming the nearest known one when there's one close enough (edit distance at most max(2, half its length)); the box is drawn as a plain box.

## Sequence diagrams

Each statement is an event, top to bottom. Participants appear left to right in the order they're first mentioned.

| Statement |  |
| --- | --- |
| `participant A`, `actor A` | Declares a participant (an actor is drawn as a stick figure), fixing the order. |
| `A -> B: text` | A call. |
| `A ->> B: text` | An asynchronous message. |
| `A --> B: text` | A return. |
| `A -> A: text` | A message to itself. |
| `A create B: text` | Creates B: its head appears at this message. |
| `note over A: text`, `note over A, B: text`, `note left of A: text`, `note right of A: text` | A note. |
| `alt cond` … `else cond` … | Alternatives. Also `opt cond`, `loop cond`, `par` … `and`, `break cond`, `critical`. The messages inside are indented; an `end` line is accepted and not needed. |

A call answered by a later return from the same participant gets an activation bar from the call to the return; calls inside it nest.

## Problems

Parsing never fails. A line that can't be read, an unknown setting value, shape or colour, or a statement that doesn't fit the kind (a lane in a class diagram) is a **problem**: its line, a column range, and a message. The rest of the diagram is still read and drawn, and editors show the problem on its line. Messages are free text; the fixtures fix them.

## Layout

The layout engine turns the model into a scene: boxes become shapes, lines become bound arrows, groups and lanes become containers, sequence diagrams become lifelines, activations, fragments and messages. Every generated element has a `key` naming what in the text it comes from:

| Key | For |
| --- | --- |
| `n:<name>` | a box (`p:<name>` a participant, `g:<name>` a group) |
| `e:<from>→<to>#<k>` | the k-th line from `<from>` to `<to>` |
| `m:<k>` | the k-th message |
| `f:<id>`, `note:<k>`, `a:<name>#<k>` | fragments, notes, activations |

Layout is stable: the engine takes what it knew from the last time (`{ src, pos, ids }`: the previous text, the positions of boxes by key, and element ids by key) and keeps boxes the user moved where they are, reusing element ids, while new boxes are placed around them. A name that disappears while a new one appears in its place is a rename: the new name takes over the old one's position. Swimlane and sequence diagrams always follow the text.

Elements that aren't generated (drawn by hand next to the diagram) have no key and are kept as they are.

## Markdown

A diagram is a fenced code block whose info string is `diagram`, optionally followed by the kind (`diagram sequence`, `diagram class`), holding the text. Positions aren't written: a diagram read from markdown is laid out afresh (the server keeps the previous positions when markdown replaces a note whose diagram is still there).

## Conformance

fixtures/diagrams has, for each case, `<name>.<kind>.txt` (the text), optionally `<name>.<kind>.prev.json` (what was known from before), and `<name>.<kind>.json`: the expected elements, positions and problems (line and message), in canonical JSON (object keys sorted, indented by one space: `canonical` in `@kribble/diagram`'s `elements.ts`).

- A **parser** conforms when it reports the same problems (lines and messages) for every fixture.
- A **layout engine** conforms when it produces the expected output byte for byte. Both reference implementations do. Other renderers can draw the stored scene instead of laying the text out themselves.
