mermaid-js/mermaid · error · MermaidParseError

Parsing failed: ${lexerErrors} ${parserErrors}

Error message

Parsing failed: ${lexerErrors} ${parserErrors}

What it means

PEG railroad grammar parse guard. Identical mechanism to the ABNF/EBNF variants: the Langium ParseResult is inspected and, on any lexer or parser error, a MermaidParseError is thrown with all diagnostics concatenated. Ensures only fully-parsed PEG grammars populate the railroad db.

Source

Thrown at packages/mermaid/src/diagrams/railroad/parser/pegParser.ts:146

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

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

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

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

export default parser;

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Confirm the input is PEG (uses `/` for ordered choice).
  2. Fix the first error at the reported line/column.
  3. Use the ABNF or EBNF parser flavor if the source is not PEG.
  4. Inspect e.result on MermaidParseError for machine-readable diagnostics.

Example fix

// before — EBNF '|' fed to PEG parser
rule <- 'a' | 'b'

// after — PEG uses '/'
rule <- 'a' / 'b'
Defensive patterns

Strategy: try-catch

Validate before calling

// Heuristic: PEG typically uses '<-' or '/' for ordered choice
if (input.includes('<-') === false && input.includes('/') === false && input.trim()) {
  // possibly wrong flavor; verify before parsing
}

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 PEG ParserDefinition with malformed PEG — bad `/` choice, unbalanced `(` `)`, invalid `*` `+` `?` repetition, missing rule references, or lexically illegal tokens.

Common situations: PEG-specific constructs (e.g. `&`/`!` predicates, `:` labels) unsupported by the mermaid PEG subset; mismatched dialect; partial paste truncating a rule.

Related errors


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