{"record":{"id":"08ec5e63ebe0c334","repo":"mermaid-js/mermaid","slug":"parsing-failed-lexererrors-parsererrors","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/abnfParser.ts","lineNumber":137,"sourceCode":"\nconst populateDb = (ast: RailroadAbnf): 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('[ABNF Parser] Starting Langium parse');\n\n    const result = langiumParser.parse<RailroadAbnf>(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('[ABNF Parser] Parsed rules:', ast.rules.length);\n\n    populateDb(ast);\n    log.debug('[ABNF Parser] Parse complete');\n  },\n  parser: {\n    yy: db,\n  },\n};\n\nexport default parser;\n","sourceCodeStart":119,"sourceCodeEnd":152,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/railroad/parser/abnfParser.ts#L119-L152","documentation":"The ABNF railroad parser runs the input through a Langium parser; if either the lexer or the parser reports any errors it wraps the ParseResult in a MermaidParseError whose message lists every lexer and parser error with line/column. The diagram is rejected before its rules are added to the db, so no partial railroad is rendered.","triggerScenarios":"Calling parser.parse(input) (the ABNF ParserDefinition) with text that violates the railroad ABNF grammar — unterminated rule, illegal character, unknown production, or malformed syntax. Equivalent guards exist for the EBNF, PEG and generic railroad grammars.","commonSituations":"Picking the wrong grammar flavor for the input (e.g. EBNF `::=` syntax fed to the ABNF parser), pasting grammar from another tool with different meta-syntax, stray unicode/whitespace, or a partial copy that cut a rule in half.","solutions":["Read the embedded line/column in the message and fix the first reported error (later errors often cascade).","Confirm the diagram type matches the grammar: use the ABNF flavor only for ABNF-style input.","Validate the grammar in an external ABNF checker, then re-render.","If integrating programmatically, catch MermaidParseError and surface e.result.lexerErrors / e.result.parserErrors to the user."],"exampleFix":"// before — '=' is not ABNF rule syntax\nrule = \"a\" | \"b\"\n\n// after — ABNF uses '=' as definition operator with CRLF\nrule = %x61 / %x62","handlingStrategy":"try-catch","validationCode":"// No safe pre-check for grammar validity without parsing; lint heuristics only\nif (!/::=|=/.test(input) && !input.trim()) {\n  throw new Error('Empty railroad ABNF input');\n}","typeGuard":"import { MermaidParseError } from '@mermaid-js/parser';\nconst isMermaidParseError = (e): e is MermaidParseError =>\n  e instanceof MermaidParseError || (e instanceof Error && /^Parsing failed:/.test(e.message));","tryCatchPattern":"try {\n  parser.parse(input);\n} catch (e) {\n  if (e instanceof MermaidParseError) {\n    const lex = e.result.lexerErrors.map(er => `${er.line}:${er.column} ${er.message}`);\n    const par = e.result.parserErrors.map(er => `${er.token.startLine}:${er.token.startColumn} ${er.message}`);\n    showDiagnostics([...lex, ...par]);\n  } else { throw e; }\n}","preventionTips":["Validate ABNF in a dedicated linter before feeding mermaid.","Surface line/column diagnostics from e.result to users.","Pick the parser flavor matching your grammar dialect."],"tags":["railroad","abnf","parser","syntax-error","langium"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}