{"record":{"id":"742f2ce8e3a09294","repo":"mermaid-js/mermaid","slug":"parsing-failed-lexererrors-parsererrors-742f2c","errorCode":null,"errorMessage":"Parsing failed: ${lexerErrors} ${parserErrors}","messagePattern":"Parsing failed: (.+?) (.+?)","errorType":"validation","errorClass":"MermaidParseError","httpStatus":null,"severity":"error","filePath":"packages/mermaid/src/diagrams/railroad/parser/ebnfParser.ts","lineNumber":155,"sourceCode":"\nconst populateDb = (ast: RailroadEbnf): void => {\n  populateCommonDb(ast, db);\n\n  if (ast.title) {\n    db.setTitle(ast.title);\n  }\n\n  ast.rules.map((rule) => db.addRule(transformRule(rule)));\n};\n\nexport const parser: ParserDefinition = {\n  parse: (input: string): void => {\n    db.clear();\n    log.debug('[EBNF Parser] Starting Langium parse');\n\n    const result = langiumParser.parse<RailroadEbnf>(input);\n    if (result.lexerErrors.length > 0 || result.parserErrors.length > 0) {\n      throw new MermaidParseError(result);\n    }\n\n    const ast = result.value;\n    log.debug('[EBNF Parser] Parsed rules:', ast.rules.length);\n\n    populateDb(ast);\n    log.debug('[EBNF Parser] Parse complete');\n  },\n  parser: {\n    yy: db,\n  },\n};\n\nexport default parser;\n","sourceCodeStart":137,"sourceCodeEnd":170,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/railroad/parser/ebnfParser.ts#L137-L170","documentation":"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.","triggerScenarios":"parser.parse(input) on the EBNF ParserDefinition with input that breaks EBNF grammar rules (bad `::=` usage, unbalanced grouping, invalid repetition `*`/`+`/`?`, stray terminals).","commonSituations":"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.","solutions":["Use the EBNF parser only with `::=` / `|` / `*` `+` `?` / `(...)` style input.","Address the first lexer/parser error reported with its line/column.","Switch the diagram flavor if the source grammar is actually ABNF or PEG.","Catch MermaidParseError and log e.result for structured diagnostics."],"exampleFix":"// before — ABNF '/' alternation fed to EBNF parser\nrule ::= 'a' / 'b'\n\n// after — EBNF uses '|'\nrule ::= 'a' | 'b'","handlingStrategy":"try-catch","validationCode":"// Heuristic only — EBNF should contain '::=' and use '|'\nif (input.includes('::=') === false && input.includes('|') === false && input.trim()) {\n  // not necessarily invalid; defer to parser\n}","typeGuard":"import { MermaidParseError } from '@mermaid-js/parser';\nconst isMermaidParseError = (e): e is MermaidParseError => e instanceof MermaidParseError;","tryCatchPattern":"try {\n  parser.parse(input);\n} catch (e) {\n  if (e instanceof MermaidParseError) {\n    reportErrors([...e.result.lexerErrors, ...e.result.parserErrors]);\n  } else { throw e; }\n}","preventionTips":["Confirm input uses EBNF '::=' / '|' operators, not ABNF '/' or PEG '/'.","Read the first reported error; cascades usually resolve after it.","Show e.result diagnostics to end users."],"tags":["railroad","ebnf","parser","syntax-error","langium"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}