| 1 | import type { CstNode, GrammarAST, ValueType } from 'langium'; |
| 2 | import { AbstractMermaidValueConverter } from '../common/index.js'; |
| 3 | |
| 4 | // Regular expression to extract className and styleText from a classDef terminal |
| 5 | const classDefRegex = /classDef\s+([A-Z_a-z]\w+)(?:\s+([^\n\r;]*))?;?/; |
| 6 | |
| 7 | export class TreemapValueConverter extends AbstractMermaidValueConverter { |
| 8 | protected runCustomConverter( |
| 9 | rule: GrammarAST.AbstractRule, |
| 10 | input: string, |
| 11 | _cstNode: CstNode |
| 12 | ): ValueType | undefined { |
| 13 | if (rule.name === 'NUMBER2') { |
| 14 | // Convert to a number by removing any commas and converting to float |
| 15 | return parseFloat(input.replace(/,/g, '')); |
| 16 | } else if (rule.name === 'SEPARATOR') { |
| 17 | // Remove quotes |
| 18 | return input.substring(1, input.length - 1); |
| 19 | } else if (rule.name === 'STRING2') { |
| 20 | // Remove quotes |
| 21 | return input.substring(1, input.length - 1); |
| 22 | } else if (rule.name === 'INDENTATION') { |
| 23 | return input.length; |
| 24 | } else if (rule.name === 'ClassDef') { |
| 25 | // Handle both CLASS_DEF terminal and ClassDef rule |
| 26 | if (typeof input !== 'string') { |
| 27 | // If we're dealing with an already processed object, return it as is |
| 28 | return input; |
| 29 | } |
| 30 | |
| 31 | // Extract className and styleText from classDef statement |
| 32 | const match = classDefRegex.exec(input); |
| 33 | if (match) { |
| 34 | // Use any type to avoid type issues |
| 35 | return { |
| 36 | $type: 'ClassDefStatement', |
| 37 | className: match[1], |
| 38 | styleText: match[2] || undefined, |
| 39 | } as any; |
| 40 | } |
| 41 | } |
| 42 | return undefined; |
| 43 | } |
| 44 | } |
| 45 | |