|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +import type { CstNode, GrammarAST, ValueType } from 'langium'; |
| 3 | +import { DefaultValueConverter } from 'langium'; |
| 4 | + |
| 5 | +import { accessibilityDescrRegex, accessibilityTitleRegex, titleRegex } from './matcher.js'; |
| 6 | + |
| 7 | +const rulesRegexes: Record<string, RegExp> = { |
| 8 | + ACC_DESCR: accessibilityDescrRegex, |
| 9 | + ACC_TITLE: accessibilityTitleRegex, |
| 10 | + TITLE: titleRegex, |
| 11 | +}; |
| 12 | + |
| 13 | +export abstract class MermaidValueConverter extends DefaultValueConverter { |
| 14 | + /** |
| 15 | + * A method contains convert logic to be used by class. |
| 16 | + * |
| 17 | + * @param rule - Parsed rule. |
| 18 | + * @param input - Matched string. |
| 19 | + * @param cstNode - Node in the Concrete Syntax Tree (CST). |
| 20 | + * @returns converted the value if it's available or `undefined` if it's not. |
| 21 | + */ |
| 22 | + protected abstract runCustomConverter( |
| 23 | + rule: GrammarAST.AbstractRule, |
| 24 | + input: string, |
| 25 | + cstNode: CstNode |
| 26 | + ): ValueType | undefined; |
| 27 | + |
| 28 | + protected override runConverter( |
| 29 | + rule: GrammarAST.AbstractRule, |
| 30 | + input: string, |
| 31 | + cstNode: CstNode |
| 32 | + ): ValueType { |
| 33 | + let value: ValueType | undefined = this.runCommonConverter(rule, input, cstNode); |
| 34 | + |
| 35 | + if (value === undefined) { |
| 36 | + value = this.runCustomConverter(rule, input, cstNode); |
| 37 | + } |
| 38 | + if (value === undefined) { |
| 39 | + return super.runConverter(rule, input, cstNode); |
| 40 | + } |
| 41 | + |
| 42 | + return value; |
| 43 | + } |
| 44 | + |
| 45 | + private runCommonConverter( |
| 46 | + rule: GrammarAST.AbstractRule, |
| 47 | + input: string, |
| 48 | + _cstNode: CstNode |
| 49 | + ): ValueType | undefined { |
| 50 | + const regex: RegExp | undefined = rulesRegexes[rule.name]; |
| 51 | + if (regex === undefined) { |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + const match = regex.exec(input); |
| 55 | + if (match === null) { |
| 56 | + return undefined; |
| 57 | + } |
| 58 | + // single line title, accTitle, accDescr |
| 59 | + if (match[1] !== undefined) { |
| 60 | + return match[1].trim().replace(/[\t ]{2,}/gm, ' '); |
| 61 | + } |
| 62 | + // multi line accDescr |
| 63 | + if (match[2] !== undefined) { |
| 64 | + return match[2] |
| 65 | + .replace(/^\s*/gm, '') |
| 66 | + .replace(/\s+$/gm, '') |
| 67 | + .replace(/[\t ]{2,}/gm, ' ') |
| 68 | + .replace(/[\n\r]{2,}/gm, '\n'); |
| 69 | + } |
| 70 | + return undefined; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export class CommonValueConverter extends MermaidValueConverter { |
| 75 | + protected runCustomConverter( |
| 76 | + _rule: GrammarAST.AbstractRule, |
| 77 | + _input: string, |
| 78 | + _cstNode: CstNode |
| 79 | + ): ValueType | undefined { |
| 80 | + return undefined; |
| 81 | + } |
| 82 | +} |
0 commit comments