mermaid-js/mermaid · error · MermaidParseError

Parsing failed: ${lexerErrors} ${parserErrors}

Error message

Parsing failed: ${lexerErrors} ${parserErrors}

What it means

Same Langium-based parse guard as the ABNF parser but for the EBNF railroad grammar. When lexerErrors or parserErrors are non-empty, a MermaidParseError is thrown carrying the full ParseResult, so callers can inspect precise token-level diagnostics. No rules are committed to the railroad db on failure.

Source

Thrown at packages/mermaid/src/diagrams/railroad/parser/ebnfParser.ts:155

const populateDb = (ast: RailroadEbnf): 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('[EBNF Parser] Starting Langium parse');

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

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

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

export default parser;

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Use the EBNF parser only with `::=` / `|` / `*` `+` `?` / `(...)` style input.
  2. Address the first lexer/parser error reported with its line/column.
  3. Switch the diagram flavor if the source grammar is actually ABNF or PEG.
  4. Catch MermaidParseError and log e.result for structured diagnostics.

Example fix

// before — ABNF '/' alternation fed to EBNF parser
rule ::= 'a' / 'b'

// after — EBNF uses '|'
rule ::= 'a' | 'b'
Defensive patterns

Strategy: try-catch

Validate before calling

// Heuristic only — EBNF should contain '::=' and use '|'
if (input.includes('::=') === false && input.includes('|') === false && input.trim()) {
  // not necessarily invalid; defer to parser
}

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 EBNF ParserDefinition with input that breaks EBNF grammar rules (bad `::=` usage, unbalanced grouping, invalid repetition `*`/`+`/`?`, stray terminals).

Common situations: Mixing EBNF meta-syntax with ABNF or PEG; trailing garbage after a rule; non-ASCII identifiers; pasting ISO-EBNF when the dialect expects the mermaid EBNF subset.

Related errors


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