| 1 | import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium'; |
| 2 | import type { TokenType } from 'chevrotain'; |
| 3 | |
| 4 | import { DefaultTokenBuilder } from 'langium'; |
| 5 | |
| 6 | export abstract class AbstractMermaidTokenBuilder extends DefaultTokenBuilder { |
| 7 | private keywords: Set<string>; |
| 8 | |
| 9 | public constructor(keywords: string[]) { |
| 10 | super(); |
| 11 | this.keywords = new Set<string>(keywords); |
| 12 | } |
| 13 | |
| 14 | protected override buildKeywordTokens( |
| 15 | rules: Stream<GrammarAST.AbstractRule>, |
| 16 | terminalTokens: TokenType[], |
| 17 | options?: TokenBuilderOptions |
| 18 | ): TokenType[] { |
| 19 | const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options); |
| 20 | // to restrict users, they mustn't have any non-whitespace characters after the keyword. |
| 21 | tokenTypes.forEach((tokenType: TokenType): void => { |
| 22 | if (this.keywords.has(tokenType.name) && tokenType.PATTERN !== undefined) { |
| 23 | // eslint-disable-next-line @typescript-eslint/no-base-to-string |
| 24 | tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?:(?=%%)|(?!\\S))'); |
| 25 | } |
| 26 | }); |
| 27 | return tokenTypes; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export class CommonTokenBuilder extends AbstractMermaidTokenBuilder {} |
| 32 | |