refactor: Simplify TokenBuilder and ValueConverter

This commit is contained in:
Sidharth Vinod
2023-09-14 17:32:44 +05:30
parent 52b33f6f47
commit 271b779995
10 changed files with 118 additions and 93 deletions

View File

@@ -0,0 +1,28 @@
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
import type { TokenType } from '../chevrotainWrapper.js';
import { DefaultTokenBuilder } from 'langium';
export abstract class MermaidTokenBuilder extends DefaultTokenBuilder {
private keywords: Set<string>;
public constructor(keywords: string[]) {
super();
this.keywords = new Set<string>(keywords);
}
protected override buildKeywordTokens(
rules: Stream<GrammarAST.AbstractRule>,
terminalTokens: TokenType[],
options?: TokenBuilderOptions
): TokenType[] {
const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options);
// to restrict users, they mustn't have any non-whitespace characters after the keyword.
tokenTypes.forEach((tokenType: TokenType): void => {
if (this.keywords.has(tokenType.name) && tokenType.PATTERN !== undefined) {
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)');
}
});
return tokenTypes;
}
}