{"record":{"id":"e742dec66b589097","repo":"mermaid-js/mermaid","slug":"parsing-failed-lexererrors-parsererrors-e742de","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/pegParser.ts","lineNumber":146,"sourceCode":"\nconst populateDb = (ast: RailroadPeg): 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('[PEG Parser] Starting Langium parse');\n\n    const result = langiumParser.parse<RailroadPeg>(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('[PEG Parser] Parsed rules:', ast.rules.length);\n\n    populateDb(ast);\n    log.debug('[PEG Parser] Parse complete');\n  },\n  parser: {\n    yy: db,\n  },\n};\n\nexport default parser;\n","sourceCodeStart":128,"sourceCodeEnd":161,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/railroad/parser/pegParser.ts#L128-L161","documentation":"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.","triggerScenarios":"parser.parse(input) on the PEG ParserDefinition with malformed PEG — bad `/` choice, unbalanced `(` `)`, invalid `*` `+` `?` repetition, missing rule references, or lexically illegal tokens.","commonSituations":"PEG-specific constructs (e.g. `&`/`!` predicates, `:` labels) unsupported by the mermaid PEG subset; mismatched dialect; partial paste truncating a rule.","solutions":["Confirm the input is PEG (uses `/` for ordered choice).","Fix the first error at the reported line/column.","Use the ABNF or EBNF parser flavor if the source is not PEG.","Inspect e.result on MermaidParseError for machine-readable diagnostics."],"exampleFix":"// before — EBNF '|' fed to PEG parser\nrule <- 'a' | 'b'\n\n// after — PEG uses '/'\nrule <- 'a' / 'b'","handlingStrategy":"try-catch","validationCode":"// Heuristic: PEG typically uses '<-' or '/' for ordered choice\nif (input.includes('<-') === false && input.includes('/') === false && input.trim()) {\n  // possibly wrong flavor; verify before parsing\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":["Ensure the input is PEG (ordered choice '/').","Switch dialect if the source is ABNF/EBNF.","Expose e.result diagnostics for editing feedback."],"tags":["railroad","peg","parser","syntax-error","langium"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}