mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
refactor: Simplify TokenBuilder and ValueConverter
This commit is contained in:
parent
52b33f6f47
commit
271b779995
@ -1,14 +1,19 @@
|
|||||||
|
interface Common {
|
||||||
|
accDescr?: string;
|
||||||
|
accTitle?: string;
|
||||||
|
title?: string;
|
||||||
|
}
|
||||||
|
|
||||||
fragment TitleAndAccessibilities:
|
fragment TitleAndAccessibilities:
|
||||||
((accDescr=ACC_DESCR | accTitle=ACC_TITLE | title=TITLE) NEWLINE+)+
|
((accDescr=ACC_DESCR | accTitle=ACC_TITLE | title=TITLE) NEWLINE+)+
|
||||||
;
|
;
|
||||||
|
|
||||||
terminal NEWLINE: /\r?\n/;
|
terminal NEWLINE: /\r?\n/;
|
||||||
terminal ACC_DESCR: /accDescr(?:[\t ]*:[\t ]*[^\n\r]*?(?=%%)|\s*{[^}]*})|accDescr(?:[\t ]*:[\t ]*[^\n\r]*|\s*{[^}]*})/;
|
terminal ACC_DESCR: /accDescr([\t ]*:[^\n\r]*(?=%%)|\s*{[^}]*})|accDescr([\t ]*:[^\n\r]*|\s*{[^}]*})/;
|
||||||
terminal ACC_TITLE: /accTitle[\t ]*:[\t ]*[^\n\r]*?(?=%%)|accTitle[\t ]*:[\t ]*[^\n\r]*/;
|
terminal ACC_TITLE: /accTitle[\t ]*:[^\n\r]*(?=%%)|accTitle[\t ]*:[^\n\r]*/;
|
||||||
terminal TITLE: /title(?:[\t ]+[^\n\r]*?|)(?=%%)|title(?:[\t ]+[^\n\r]*|)/;
|
terminal TITLE: /title([\t ][^\n\r]*|)(?=%%)|title([\t ][^\n\r]*|)/;
|
||||||
|
|
||||||
hidden terminal WHITESPACE: /[\t ]+/;
|
hidden terminal WHITESPACE: /[\t ]+/;
|
||||||
// TODO: add YAML_COMMENT hidden rule without interfere actual grammar
|
|
||||||
hidden terminal YAML: /---[\t ]*\r?\n[\S\s]*?---[\t ]*(?!.)/;
|
hidden terminal YAML: /---[\t ]*\r?\n[\S\s]*?---[\t ]*(?!.)/;
|
||||||
hidden terminal DIRECTIVE: /[\t ]*%%{[\S\s]*?}%%\s*/;
|
hidden terminal DIRECTIVE: /[\t ]*%%{[\S\s]*?}%%\s*/;
|
||||||
hidden terminal SINGLE_LINE_COMMENT: /[\t ]*%%[^\n\r]*/;
|
hidden terminal SINGLE_LINE_COMMENT: /[\t ]*%%[^\n\r]*/;
|
||||||
|
@ -1,74 +0,0 @@
|
|||||||
import type { CstNode, GrammarAST, ValueType } from 'langium';
|
|
||||||
import { DefaultValueConverter } from 'langium';
|
|
||||||
|
|
||||||
import { accessibilityDescrRegex, accessibilityTitleRegex, titleRegex } from './commonMatcher.js';
|
|
||||||
|
|
||||||
export class CommonValueConverter extends DefaultValueConverter {
|
|
||||||
protected override runConverter(
|
|
||||||
rule: GrammarAST.AbstractRule,
|
|
||||||
input: string,
|
|
||||||
cstNode: CstNode
|
|
||||||
): ValueType {
|
|
||||||
const value: ValueType | undefined = CommonValueConverter.customRunConverter(
|
|
||||||
rule,
|
|
||||||
input,
|
|
||||||
cstNode
|
|
||||||
);
|
|
||||||
if (value === undefined) {
|
|
||||||
return super.runConverter(rule, input, cstNode);
|
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A method contains convert logic to be used by class itself or `MermaidValueConverter`.
|
|
||||||
*
|
|
||||||
* @param rule - Parsed rule.
|
|
||||||
* @param input - Matched string.
|
|
||||||
* @param _cstNode - Node in the Concrete Syntax Tree (CST).
|
|
||||||
* @returns converted the value if it's common rule or `undefined` if it's not.
|
|
||||||
*/
|
|
||||||
public static customRunConverter(
|
|
||||||
rule: GrammarAST.AbstractRule,
|
|
||||||
input: string,
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
_cstNode: CstNode
|
|
||||||
): ValueType | undefined {
|
|
||||||
let regex: RegExp | undefined;
|
|
||||||
switch (rule.name) {
|
|
||||||
case 'ACC_DESCR': {
|
|
||||||
regex = new RegExp(accessibilityDescrRegex.source);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'ACC_TITLE': {
|
|
||||||
regex = new RegExp(accessibilityTitleRegex.source);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'TITLE': {
|
|
||||||
regex = new RegExp(titleRegex.source);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (regex === undefined) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const match = regex.exec(input);
|
|
||||||
if (match === null) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
// single line title, accTitle, accDescr
|
|
||||||
if (match[1] !== undefined) {
|
|
||||||
return match[1].trim().replaceAll(/[\t ]{2,}/gm, ' ');
|
|
||||||
}
|
|
||||||
// multi line accDescr
|
|
||||||
if (match[2] !== undefined) {
|
|
||||||
return match[2]
|
|
||||||
.replaceAll(/^\s*/gm, '')
|
|
||||||
.replaceAll(/\s+$/gm, '')
|
|
||||||
.replaceAll(/[\t ]{2,}/gm, ' ')
|
|
||||||
.replaceAll(/[\n\r]{2,}/gm, '\n');
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,2 +1,3 @@
|
|||||||
export * from './commonLexer.js';
|
export * from './lexer.js';
|
||||||
export * from './commonValueConverters.js';
|
export * from './tokenBuilder.js';
|
||||||
|
export { MermaidValueConverter } from './valueConverter.js';
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
|
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
|
||||||
import { DefaultTokenBuilder } from 'langium';
|
|
||||||
|
|
||||||
import type { TokenType } from '../chevrotainWrapper.js';
|
import type { TokenType } from '../chevrotainWrapper.js';
|
||||||
|
|
||||||
export class InfoTokenBuilder extends DefaultTokenBuilder {
|
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(
|
protected override buildKeywordTokens(
|
||||||
rules: Stream<GrammarAST.AbstractRule>,
|
rules: Stream<GrammarAST.AbstractRule>,
|
||||||
terminalTokens: TokenType[],
|
terminalTokens: TokenType[],
|
||||||
@ -12,10 +19,7 @@ export class InfoTokenBuilder extends DefaultTokenBuilder {
|
|||||||
const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options);
|
const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options);
|
||||||
// to restrict users, they mustn't have any non-whitespace characters after the keyword.
|
// to restrict users, they mustn't have any non-whitespace characters after the keyword.
|
||||||
tokenTypes.forEach((tokenType: TokenType): void => {
|
tokenTypes.forEach((tokenType: TokenType): void => {
|
||||||
if (
|
if (this.keywords.has(tokenType.name) && tokenType.PATTERN !== undefined) {
|
||||||
(tokenType.name === 'info' || tokenType.name === 'showInfo') &&
|
|
||||||
tokenType.PATTERN !== undefined
|
|
||||||
) {
|
|
||||||
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)');
|
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)');
|
||||||
}
|
}
|
||||||
});
|
});
|
82
packages/parser/src/language/common/valueConverter.ts
Normal file
82
packages/parser/src/language/common/valueConverter.ts
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
|
import type { CstNode, GrammarAST, ValueType } from 'langium';
|
||||||
|
import { DefaultValueConverter } from 'langium';
|
||||||
|
|
||||||
|
import { accessibilityDescrRegex, accessibilityTitleRegex, titleRegex } from './matcher.js';
|
||||||
|
|
||||||
|
const rulesRegexes: Record<string, RegExp> = {
|
||||||
|
ACC_DESCR: accessibilityDescrRegex,
|
||||||
|
ACC_TITLE: accessibilityTitleRegex,
|
||||||
|
TITLE: titleRegex,
|
||||||
|
};
|
||||||
|
|
||||||
|
export abstract class MermaidValueConverter extends DefaultValueConverter {
|
||||||
|
/**
|
||||||
|
* A method contains convert logic to be used by class.
|
||||||
|
*
|
||||||
|
* @param rule - Parsed rule.
|
||||||
|
* @param input - Matched string.
|
||||||
|
* @param cstNode - Node in the Concrete Syntax Tree (CST).
|
||||||
|
* @returns converted the value if it's available or `undefined` if it's not.
|
||||||
|
*/
|
||||||
|
protected abstract runCustomConverter(
|
||||||
|
rule: GrammarAST.AbstractRule,
|
||||||
|
input: string,
|
||||||
|
cstNode: CstNode
|
||||||
|
): ValueType | undefined;
|
||||||
|
|
||||||
|
protected override runConverter(
|
||||||
|
rule: GrammarAST.AbstractRule,
|
||||||
|
input: string,
|
||||||
|
cstNode: CstNode
|
||||||
|
): ValueType {
|
||||||
|
let value: ValueType | undefined = this.runCommonConverter(rule, input, cstNode);
|
||||||
|
|
||||||
|
if (value === undefined) {
|
||||||
|
value = this.runCustomConverter(rule, input, cstNode);
|
||||||
|
}
|
||||||
|
if (value === undefined) {
|
||||||
|
return super.runConverter(rule, input, cstNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private runCommonConverter(
|
||||||
|
rule: GrammarAST.AbstractRule,
|
||||||
|
input: string,
|
||||||
|
_cstNode: CstNode
|
||||||
|
): ValueType | undefined {
|
||||||
|
const regex: RegExp | undefined = rulesRegexes[rule.name];
|
||||||
|
if (regex === undefined) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const match = regex.exec(input);
|
||||||
|
if (match === null) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
// single line title, accTitle, accDescr
|
||||||
|
if (match[1] !== undefined) {
|
||||||
|
return match[1].trim().replace(/[\t ]{2,}/gm, ' ');
|
||||||
|
}
|
||||||
|
// multi line accDescr
|
||||||
|
if (match[2] !== undefined) {
|
||||||
|
return match[2]
|
||||||
|
.replace(/^\s*/gm, '')
|
||||||
|
.replace(/\s+$/gm, '')
|
||||||
|
.replace(/[\t ]{2,}/gm, ' ')
|
||||||
|
.replace(/[\n\r]{2,}/gm, '\n');
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CommonValueConverter extends MermaidValueConverter {
|
||||||
|
protected runCustomConverter(
|
||||||
|
_rule: GrammarAST.AbstractRule,
|
||||||
|
_input: string,
|
||||||
|
_cstNode: CstNode
|
||||||
|
): ValueType | undefined {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
export * from './infoModule.js';
|
export * from './module.js';
|
||||||
|
@ -7,10 +7,10 @@ import type {
|
|||||||
} from 'langium';
|
} from 'langium';
|
||||||
import { EmptyFileSystem, createDefaultModule, createDefaultSharedModule, inject } from 'langium';
|
import { EmptyFileSystem, createDefaultModule, createDefaultSharedModule, inject } from 'langium';
|
||||||
|
|
||||||
import { MermaidGeneratedSharedModule, InfoGeneratedModule } from '../generated/module.js';
|
import { CommonLexer } from '../common/lexer.js';
|
||||||
import { CommonLexer } from '../common/commonLexer.js';
|
import { CommonValueConverter } from '../common/valueConverter.js';
|
||||||
import { CommonValueConverter } from '../common/commonValueConverters.js';
|
import { InfoGeneratedModule, MermaidGeneratedSharedModule } from '../generated/module.js';
|
||||||
import { InfoTokenBuilder } from './infoTokenBuilder.js';
|
import { InfoTokenBuilder } from './tokenBuilder.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Declaration of `Info` services.
|
* Declaration of `Info` services.
|
||||||
@ -34,7 +34,7 @@ export type InfoServices = LangiumServices & InfoAddedServices;
|
|||||||
*/
|
*/
|
||||||
export const InfoModule: Module<InfoServices, PartialLangiumServices & InfoAddedServices> = {
|
export const InfoModule: Module<InfoServices, PartialLangiumServices & InfoAddedServices> = {
|
||||||
parser: {
|
parser: {
|
||||||
Lexer: (services) => new CommonLexer(services),
|
Lexer: (services: InfoServices) => new CommonLexer(services),
|
||||||
TokenBuilder: () => new InfoTokenBuilder(),
|
TokenBuilder: () => new InfoTokenBuilder(),
|
||||||
ValueConverter: () => new CommonValueConverter(),
|
ValueConverter: () => new CommonValueConverter(),
|
||||||
},
|
},
|
7
packages/parser/src/language/info/tokenBuilder.ts
Normal file
7
packages/parser/src/language/info/tokenBuilder.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { MermaidTokenBuilder } from '../common/index.js';
|
||||||
|
|
||||||
|
export class InfoTokenBuilder extends MermaidTokenBuilder {
|
||||||
|
public constructor() {
|
||||||
|
super(['info', 'showInfo']);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user