mermaid-js/mermaid · error · MermaidParseError

Parsing failed: ${lexerErrors} ${parserErrors}

Error message

Parsing failed: ${lexerErrors} ${parserErrors}

What it means

Generic railroad grammar parse guard (the default `railroad` flavor). Runs Langium, and if lexerErrors or parserErrors are present, throws a MermaidParseError bundling the full ParseResult so the caller can read token-level diagnostics. Aborts before any rule is added to the db.

Source

Thrown at packages/mermaid/src/diagrams/railroad/parser/railroadParser.ts:99

const populateDb = (ast: Railroad): void => {
  populateCommonDb(ast, db);

  if (ast.title) {
    db.setTitle(ast.title);
  }

  ast.rules.map((rule) => db.addRule(transformRule(rule)));
};

export const parser: ParserDefinition = {
  parse: (input: string): void => {
    db.clear();
    log.debug('[Railroad Parser] Starting Langium parse');

    const result = langiumParser.parse<Railroad>(input);
    if (result.lexerErrors.length > 0 || result.parserErrors.length > 0) {
      throw new MermaidParseError(result);
    }

    const ast = result.value;
    log.debug('[Railroad Parser] Parsed rules:', ast.rules.length);

    populateDb(ast);
    log.debug('[Railroad Parser] Parse complete');
  },
  parser: {
    yy: db,
  },
};

export default parser;

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Address the first reported lexer/parser error using its line/column.
  2. If the input is ABNF/EBNF/PEG, switch to the matching dialect diagram.
  3. Strip non-ASCII and normalise quotes before parsing.
  4. Catch MermaidParseError and surface e.result.lexerErrors/parserErrors.

Example fix

// before — unbalanced grouping
rule ::= 'a' ( 'b' | 'c'

// after
rule ::= 'a' ( 'b' | 'c' )
Defensive patterns

Strategy: try-catch

Validate before calling

// Strip non-ASCII and balance-check parentheses as a cheap pre-check
const cleaned = input.replace(/[^\x00-\x7F]/g, '');
const open = (cleaned.match(/\(/g) || []).length;
const close = (cleaned.match(/\)/g) || []).length;
if (open !== close) throw new Error('Unbalanced parentheses');

Type guard

import { MermaidParseError } from '@mermaid-js/parser';
const isMermaidParseError = (e): e is MermaidParseError => e instanceof MermaidParseError;

Try / catch

try {
  parser.parse(input);
} catch (e) {
  if (e instanceof MermaidParseError) {
    reportErrors([...e.result.lexerErrors, ...e.result.parserErrors]);
  } else { throw e; }
}

Prevention

When it happens

Trigger: parser.parse(input) on the generic railroad ParserDefinition with input violating the default railroad grammar — undeclared rule reference, unbalanced grouping, invalid operator, stray character.

Common situations: Default railroad syntax mixed with one of the dialect-specific flavors; copy-paste from a source that uses different operators; truncation; invisible/non-ASCII characters from rich text paste.

Related errors


AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12). Data as JSON: /api/errors/d177151b4ac7ac5e. Report an issue: GitHub.