Skip to content

Parser

A hand-written recursive-descent parser per diagram type, with no parser-generator runtime. It accepts Mermaid syntax as mermaid 12.0.0 documents it and recovers from the errors people and LLMs commonly make.

Type Header Milestone
Flowchart flowchart, graph + TB/TD/BT/LR/RL M1
Sequence sequenceDiagram M4 (sequence.md)
State stateDiagram, stateDiagram-v2 M4 (state.md)
Class classDiagram M4
ER erDiagram M4
Gantt gantt M3 (gantt.md)
Quadrant quadrantChart M3 (quadrant.md)
Timeline, pie, XY, packet, kanban timeline, pie, xychart-beta/xychart, packet-beta/packet, kanban M3

Any other header returns UnsupportedDiagram { header }, never a partial render.

Compatibility means rendering the same graph that mermaid 12.0.0 renders: the same nodes, edges, labels, clusters and directions. Pixel positions are not part of it. The benchmark reports the pass rate per diagram type against the mermaid repository’s own demo and test diagrams (benchmark.md).

For Gantt, compatibility compares the resolved schedule and visible task content; dates, dependencies, statuses and deterministic calendar differences follow gantt.md. For quadrant charts, it compares the title, quadrant and axis labels with their placement, and every point’s coordinates and explicit style (quadrant.md).

mermaid 12’s small symbol shapes (@{ shape: … } sm-circ, f-circ, fr-circ, cross-circ, fork, hourglass, bolt and their aliases) are drawn at a fixed size without their label, as mermaid draws them; the label stays in the model for the text alternative. The other expanded shapes (the document, stacked, cylinder, triangle, brace and process variants, text and datastore, with their aliases) are drawn with their own outline; text draws no outline and datastore only the lines above and below the label. bang, cloud, folder, bucket, console, browser and person become rectangles with W015, as does any other name.

Subgraph ids follow mermaid’s resolution, which happens after the whole source is read:

  • A node id that names a subgraph stands for the subgraph unless it is given a bracket shape, an @{…} shape or label, or a label by R004. So id@{…} with other keys (view, algorithm) configures the subgraph and never creates a node.
  • Named inside another subgraph, it nests that subgraph there, even when the subgraph is declared later; a link that would close a nesting cycle is not made. Parents always precede their children in the model.
  • As an edge endpoint it connects to the subgraph, which the model represents by the subgraph’s first member node. An edge between a subgraph and one of its own members is dropped.
  • YAML front matter (--- … ---) accepts title, config.flowchart.curve, config.layout, config.merlion.autoTone, config.merlion.rings, accTitle, accDescr.
  • Gantt also accepts top-level displayMode and config.gantt keys for display mode and axes (gantt.md).
  • config.quadrantChart takes mermaid’s quadrant chart keys, and the quadrant colours of themeVariables are read as fixed colours (quadrant.md).
  • The front matter parser accepts a YAML subset: block mappings, plain and quoted scalars, and flow sequences. Anchors, aliases, tags, multi-document streams and duplicate keys are rejected with an Error (E011 FrontMatterUnsupported), which rules out alias-expansion attacks.
  • %%{init: …}%% accepts the same keys as JSON. The JSON parser rejects nesting deeper than 64 and strings longer than 4,096 bytes (E012 DirectiveTooLarge).
  • Every accepted key takes an enumerated or numeric value (curve is one of Mermaid’s curve names, layout is dagre, elk or merlion, all laid out by Merlion’s engine); a value outside its set is ignored with W016.
  • merlion holds Merlion’s own keys. merlion.autoTone is true or false (a JSON boolean, or the YAML scalar true / false); false turns the automatic tones off for the diagram (svg-output.md), and true leaves the render option in charge. merlion.rings takes the same values; false lays every flowchart cycle out in layers instead of drawing it as a ring (layout.md). Any other merlion.* key, a non-boolean autoTone or rings, or a merlion value that is not a mapping gives W016.
  • theme, themeVariables and look are accepted and ignored with I011, because themes are CSS (svg-output.md). The quadrant colour keys of themeVariables are the exception: a quadrant chart reads them, and the I011 for a themeVariables holding nothing else waits for the header, so only another diagram type reports it.
  • Unknown keys and values outside a key’s set produce W016 and are otherwise ignored.
  • Neither front matter nor %%{init}%% names a stylesheet; only the caller passes one (integrations.md).
  • accTitle: and accDescr: statements override the generated <title> and <desc>.

In the default mode, the parser applies each repair below, records it as a Repair diagnostic, and continues. With strict: true, any repair becomes an Error and rendering fails.

Code Input Repair
R001 Unquoted label containing (), [], {} or : inside a node shape Quote the label
R002 subgraph without a matching end Close it at end of input
R003 Typographic quotes (“ ” ‘ ’) used as delimiters Replace with ASCII quotes
R004 A reserved word as a node id (end, graph, subgraph) Rename to end_ etc., appending further _ until the id is unused, and keep the original text as the label
R005 An edge to a node id that is never declared Declare the node with its id as the label (Mermaid behaviour; recorded because it often hides a typo)
R006 Markdown code fence left inside the source Strip it
R007 Tabs mixed with spaces in indentation-sensitive types (mindmap, kanban) Treat each tab as 4 spaces
R008 An edge id (e1@-->) already given to an earlier edge Drop the id from the later edge; the id keeps naming the first edge

A syntax error that no rule repairs stops parsing and returns an Error diagnostic with its location and the tokens expected at that point.

Style statements (classDef, style, linkStyle) and click statements are parsed into typed values and validated as specified in svg-output.md; the parser never passes their text through. Edge ids (a e1@--> b) are kept in the model: class e1 <name> gives the edge a role (svg-output.md), and e1@{…} still configures it. An edge id names exactly one edge: on a fan-out (a & b e1@--> c & d) it names the edge from the last source to the first target, as mermaid does, and a later link reusing it loses it with R008. An element keeps at most 32 classes from class and :::; further classes are dropped with one W020 per element. TODO(owner): decide what class does with an id that names both a node and an edge. Subgraphs nest at most 64 deep (E010 NestingTooDeep); the parser tracks depth explicitly, so deep input fails with a diagnostic instead of exhausting the stack.

A label reaches the model normalised, in this order:

  1. A Markdown string ("`…`") loses its backticks.
  2. Every <br> variant — <br>, <br/>, <br />, any case, spaces before the / or > — and every newline inside a quoted label becomes a \n, the model’s hard line break.
  3. Each line is trimmed, then its Mermaid entity codes (#quot;, #35;, #x2665;, the named codes) are decoded. The named codes cover the ASCII punctuation a label cannot carry as itself, each spelled as HTML5 spells it — #colon;, #semi;, #lpar;, #rpar;, #lbrace;, #rbrace;, #lbrack;, #rbrack;, #num;, #percnt;, #commat;, #dollar;, #excl;, #quest;, #sol;, #bsol;, #verbar;, #lowbar;, #ast;, #plus;, #equals;, #period;, #comma;, #grave; — beside the typographic names (#hearts;, #mdash;, #nbsp;, …). A name the table does not hold stays literal.

The order of 2 and 3 is the contract, not an implementation detail. #lt;br#gt; is how a source writes a literal <br>: decoding it before the split would produce a <br> indistinguishable from the source’s own, and the text stage would break the line and swallow the text. Splitting first keeps it. A code can never decode to a \n — decode_entity refuses control characters — so the separator is unforgeable and a <br> left in the model is always text.

The text stage splits a label at \n and at nothing else (text-measurement.md); <br> is source syntax, never model text.

A flowchart edge or state transition label that names only the shapes it joins carries no information: the arrow already shows both ends. The parser records W026 for each such label and draws it unchanged, since mermaid draws it too.

A shape’s names are its id and its label; a choice, fork or join state has only its id, and [*] has none. Labels and names are compared lower case, with each run of whitespace, - and _ read as one space, and with →, ->, --> and => in a label read as to. A label repeats its endpoints when it equals, for some name t of the target and some name s of the source:

Form Example
t idle --> on_hold : on-hold
to t idle --> on_hold : to on-hold
w to t, w one word idle --> on_hold : pause to on-hold
s to t oh --> idle : on hold to idle, with state "on hold" as oh

A label naming a trigger or a condition (queue drains), or the target with more words (on-hold after three failures), is quiet. W026 has no fix: the right replacement is the event the label leaves out, or no label.

Diagnostic {
severity: Error | Warning | Repair | Info,
code: &'static str, // "E0xx", "W0xx", "R0xx", "I0xx"
span: { line, column, byte_start, byte_end }, // 1-based line; 1-based column in Unicode scalar values; UTF-8 byte offsets
message: String,
fix: Option<{ span, replacement }>, // present for every Repair
}

A message is one printable line: every source excerpt it quotes (a token, an id, a style value, the E003 header) is cut to 64 characters followed by …, and C0/C1 controls, tab, newline, bidi controls and non-characters are written as \u{…} escapes, so untrusted source never drives a terminal or a CI log. The whole message is capped at 512 characters. The CLI escapes the file name it prints before each message the same way.

The fix field lets an editor or an LLM loop apply the repair to the source text. merlion check --fix writes every fix back to the file (integrations.md). merlion lsp converts columns to the position encoding the client negotiates (integrations.md).

Code Severity Meaning
E001 InternalError Error The WASM instance trapped and was replaced (integrations.md)
E002 SyntaxError Error Syntax error no repair covers; the message names the expected tokens
E003 UnsupportedDiagram Error The header names a diagram type Merlion does not render, or there is no header
E004 TooLarge Error Input over its size limit, a structural limit exceeded, or mandatory-phase fuel exhausted
E010 NestingTooDeep Error Subgraphs nested beyond 64
E011 FrontMatterUnsupported Error YAML outside the accepted subset, or nested beyond 64
E012 DirectiveTooLarge Error %%{init}%% JSON nested beyond 64 or with a string over 4,096 bytes
E013 StylesheetTooLarge Error Stylesheet over 64 KiB, 512 rules, 32 declarations per rule, 8 selectors per token rule, 16 theme names, 256 role selectors or block depth 2, or compiled output over 64 KiB (svg-output.md)
E020–E023 Error Chart specs: invalid JSON, spec shape, value, more than eight series (chart.md). Layer-map and architecture-map specs use E020 and E021 with the same meanings (layer-map.md, architecture-map.md)
E024 QuadrantValueOutOfRange Error A quadrant chart point coordinate outside 0–1 (quadrant.md)
E025 DuplicateSpecId Error A layer-map or architecture-map id that an earlier element already uses (layer-map.md, architecture-map.md)
E026 ArchitecturePlacement Error An architecture-map cell outside the grid or overlapping another, a span wider than its zone, zones nested deeper than 4 or in a cycle, a separator on column 0, or a flow joining an element to itself or to its own zone or column (architecture-map.md)
E027 ArchitectureUnknownId Error An id that an architecture map’s in, at.column, flow, view or view option names and no element of the right kind has (architecture-map.md)
W010 StyleRejected Warning Style property or value outside the accepted set, including a quadrant chart point style outside radius, color, stroke-color and stroke-width (quadrant.md)
W011 ClassNameRejected Warning classDef name outside [A-Za-z_][A-Za-z0-9_-]{0,63}
W012 LabelTruncated Warning Label longer than 4,096 bytes
W013 LinkRejected Warning click … href URL outside the accepted schemes
W014 BidiControlStripped Warning Bidirectional formatting characters removed from a label
W015 ShapeUnsupported Warning @{ shape: … } names a shape Merlion draws as a rectangle (Compatibility)
W016 ConfigRejected Warning Front-matter or %%{init}%% key unknown, or its value outside the key’s set
W017 StylesheetRuleRejected Warning A rule declaring a token under a selector or at-rule outside the subset; or a role left out of the embedded style by the 16 KiB cap
W018 StylesheetDeclarationRejected Warning A property outside the token list, a font token, or a value outside the token’s grammar
W019 StylesheetReferenceInvalid Warning var() naming an undefined token, forming a cycle, or nested deeper than 8
W020 ClassesTruncated Warning An element given more than 32 classes; the rest are dropped
W021–W023 Warning Sequence diagrams (sequence.md)
W024–W025 Warning State diagrams (state.md)
W026 LabelRepeatsEndpoint Warning A flowchart edge or state transition label names only its endpoints (Endpoint labels)
W027 IconUnknown Warning An architecture-map icon outside the built-in set; the element draws no icon (architecture-map.md)
W028 FlowUnrouted Warning An architecture-map flow with no open route through the gutters, drawn as a direct path (architecture-map.md)
I010 UnmeasuredGlyph Info Code point outside the font table (text-measurement.md)
I011 ThemeConfigIgnored Info theme, themeVariables or look in front matter or %%{init}%%
I020 LayoutHintDiscarded Info Fewer than 50% of nodes survive (layout.md)
I021 LayoutHintPartial Info Some nodes treated as new
I022 LayoutHintInvalid Info Hint malformed, of unknown version, or too large
I030 FixedColour Info Source sets a colour that ignores the theme: a classDef colour is fixed unless a stylesheet or page sets its --merlion-c-{name}-* token; a style or linkStyle colour, a quadrant chart point style colour and a quadrant colour of themeVariables are fixed
I031 ClickCallbackIgnored Info click callback or call dropped
I032 StylesheetRulesIgnored Info Count of stylesheet rules declaring no --merlion-* token
I033 ToneMasked Info A source style colour, or a classDef colour whose token the stylesheet leaves unset, overrides a stylesheet tone on the same element
I034 DiagnosticsTruncated Info Count of diagnostics past limits.diagnostics, which are counted rather than kept; always the last diagnostic
I037 NeutralCalendarOrigin Info Gantt dates omit calendar components and use the Unix epoch origin instead of the current date
I038 TodayMarkerOmitted Info Gantt’s live today marker is omitted for deterministic output
I039 AxisIntervalCoarsened Info An overly dense Gantt axis uses an aligned multiple of the requested interval; task dates stay unchanged
I040 ExclusionBandsOmitted Info Gantt weekday bands are too dense to draw individually at this time scale
I041 ChartRowsSkipped Info A chart mark skips rows whose value is missing or null (chart.md)
R001–R008 Repair See Error tolerance
R009–R013 Repair Sequence diagrams (sequence.md)
R014–R019 Repair State diagrams (state.md)
R020 PaddedGanttDate Repair An unpadded ISO year, month or day becomes a valid zero-padded date; the numeric year stays unchanged (gantt.md)
R021 QuadrantValueNormalised Repair A quadrant chart coordinate in 0–1 written in a form mermaid’s lexer rejects (1.0, .5, +0.5, 00.5) becomes the form it reads (1, 0.5) (quadrant.md)

Under strict: true, every Warning and Repair becomes an Error.

The list holds at most limits.diagnostics (4,000). Past that a diagnostic is counted rather than kept and the last slot holds I034 with the count, so a source that repairs every second byte costs bounded memory. A dropped Error still fails the render: the count never turns a rejected source into a rendered one.

A failed render always carries at least one Error diagnostic. When no stage recorded one, the core adds E002, E003 or E004 for its RenderError, so the CLI, the WASM module and every other caller report the same codes.